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*fbbe005aSdanielk1977 ** $Id: select.c,v 1.318 2006/06/21 07:02:33 danielk1977 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; 75b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 76b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 77b9bb7c18Sdrh pNew->addrOpenEphm[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 */ 1909c41938fSdrh Expr *sqlite3CreateIdExpr(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 2149c41938fSdrh pE1a = sqlite3CreateIdExpr(zCol); 2159c41938fSdrh pE2a = sqlite3CreateIdExpr(zCol); 216030530deSdrh if( zAlias1==0 ){ 217030530deSdrh zAlias1 = pTab1->zName; 218030530deSdrh } 2199c41938fSdrh pE1b = sqlite3CreateIdExpr(zAlias1); 220030530deSdrh if( zAlias2==0 ){ 221030530deSdrh zAlias2 = pTab2->zName; 222030530deSdrh } 2239c41938fSdrh pE2b = sqlite3CreateIdExpr(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: 525b9bb7c18Sdrh case SRT_EphemTab: { 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: 708b9bb7c18Sdrh case SRT_EphemTab: { 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 ){ 80830bcf5dbSdrh 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 if( pS ){ 845955de52cSdanielk1977 /* The "table" is actually a sub-select or a view in the FROM clause 846955de52cSdanielk1977 ** of the SELECT statement. Return the declaration type and origin 847955de52cSdanielk1977 ** data for the result-set column of the sub-select. 848955de52cSdanielk1977 */ 849955de52cSdanielk1977 if( iCol>=0 && iCol<pS->pEList->nExpr ){ 850955de52cSdanielk1977 /* If iCol is less than zero, then the expression requests the 851955de52cSdanielk1977 ** rowid of the sub-select or view. This expression is legal (see 852955de52cSdanielk1977 ** test case misc2.2.2) - it always evaluates to NULL. 853955de52cSdanielk1977 */ 854955de52cSdanielk1977 NameContext sNC; 855955de52cSdanielk1977 Expr *p = pS->pEList->a[iCol].pExpr; 856955de52cSdanielk1977 sNC.pSrcList = pS->pSrc; 857955de52cSdanielk1977 sNC.pNext = 0; 858955de52cSdanielk1977 sNC.pParse = pNC->pParse; 859955de52cSdanielk1977 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 860955de52cSdanielk1977 } 8614b2688abSdanielk1977 }else if( pTab->pSchema ){ 862955de52cSdanielk1977 /* A real table */ 863955de52cSdanielk1977 assert( !pS ); 864fcb78a49Sdrh if( iCol<0 ) iCol = pTab->iPKey; 865fcb78a49Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 866fcb78a49Sdrh if( iCol<0 ){ 867fcb78a49Sdrh zType = "INTEGER"; 868955de52cSdanielk1977 zOriginCol = "rowid"; 869fcb78a49Sdrh }else{ 870fcb78a49Sdrh zType = pTab->aCol[iCol].zType; 871955de52cSdanielk1977 zOriginCol = pTab->aCol[iCol].zName; 872955de52cSdanielk1977 } 873955de52cSdanielk1977 zOriginTab = pTab->zName; 874955de52cSdanielk1977 if( pNC->pParse ){ 875955de52cSdanielk1977 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); 876955de52cSdanielk1977 zOriginDb = pNC->pParse->db->aDb[iDb].zName; 877955de52cSdanielk1977 } 878fcb78a49Sdrh } 87900e279d9Sdanielk1977 break; 880736c22b8Sdrh } 88193758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 88200e279d9Sdanielk1977 case TK_SELECT: { 883955de52cSdanielk1977 /* The expression is a sub-select. Return the declaration type and 884955de52cSdanielk1977 ** origin info for the single column in the result set of the SELECT 885955de52cSdanielk1977 ** statement. 886955de52cSdanielk1977 */ 887b3bce662Sdanielk1977 NameContext sNC; 88800e279d9Sdanielk1977 Select *pS = pExpr->pSelect; 889955de52cSdanielk1977 Expr *p = pS->pEList->a[0].pExpr; 890955de52cSdanielk1977 sNC.pSrcList = pS->pSrc; 891b3bce662Sdanielk1977 sNC.pNext = pNC; 892955de52cSdanielk1977 sNC.pParse = pNC->pParse; 893955de52cSdanielk1977 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); 89400e279d9Sdanielk1977 break; 895fcb78a49Sdrh } 89693758c8dSdanielk1977 #endif 89700e279d9Sdanielk1977 } 89800e279d9Sdanielk1977 899955de52cSdanielk1977 if( pzOriginDb ){ 900955de52cSdanielk1977 assert( pzOriginTab && pzOriginCol ); 901955de52cSdanielk1977 *pzOriginDb = zOriginDb; 902955de52cSdanielk1977 *pzOriginTab = zOriginTab; 903955de52cSdanielk1977 *pzOriginCol = zOriginCol; 904955de52cSdanielk1977 } 905517eb646Sdanielk1977 return zType; 906517eb646Sdanielk1977 } 907517eb646Sdanielk1977 908517eb646Sdanielk1977 /* 909517eb646Sdanielk1977 ** Generate code that will tell the VDBE the declaration types of columns 910517eb646Sdanielk1977 ** in the result set. 911517eb646Sdanielk1977 */ 912517eb646Sdanielk1977 static void generateColumnTypes( 913517eb646Sdanielk1977 Parse *pParse, /* Parser context */ 914517eb646Sdanielk1977 SrcList *pTabList, /* List of tables */ 915517eb646Sdanielk1977 ExprList *pEList /* Expressions defining the result set */ 916517eb646Sdanielk1977 ){ 917517eb646Sdanielk1977 Vdbe *v = pParse->pVdbe; 918517eb646Sdanielk1977 int i; 919b3bce662Sdanielk1977 NameContext sNC; 920b3bce662Sdanielk1977 sNC.pSrcList = pTabList; 921955de52cSdanielk1977 sNC.pParse = pParse; 922517eb646Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 923517eb646Sdanielk1977 Expr *p = pEList->a[i].pExpr; 924955de52cSdanielk1977 const char *zOrigDb = 0; 925955de52cSdanielk1977 const char *zOrigTab = 0; 926955de52cSdanielk1977 const char *zOrigCol = 0; 927955de52cSdanielk1977 const char *zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); 928955de52cSdanielk1977 9294b1ae99dSdanielk1977 /* The vdbe must make it's own copy of the column-type and other 9304b1ae99dSdanielk1977 ** column specific strings, in case the schema is reset before this 9314b1ae99dSdanielk1977 ** virtual machine is deleted. 932fbcd585fSdanielk1977 */ 9334b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, P3_TRANSIENT); 9344b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, P3_TRANSIENT); 9354b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, P3_TRANSIENT); 9364b1ae99dSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, P3_TRANSIENT); 937fcb78a49Sdrh } 938fcb78a49Sdrh } 939fcb78a49Sdrh 940fcb78a49Sdrh /* 941fcb78a49Sdrh ** Generate code that will tell the VDBE the names of columns 942fcb78a49Sdrh ** in the result set. This information is used to provide the 943fcabd464Sdrh ** azCol[] values in the callback. 94482c3d636Sdrh */ 945832508b7Sdrh static void generateColumnNames( 946832508b7Sdrh Parse *pParse, /* Parser context */ 947ad3cab52Sdrh SrcList *pTabList, /* List of tables */ 948832508b7Sdrh ExprList *pEList /* Expressions defining the result set */ 949832508b7Sdrh ){ 950d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 9516a3ea0e6Sdrh int i, j; 9529bb575fdSdrh sqlite3 *db = pParse->db; 953fcabd464Sdrh int fullNames, shortNames; 954fcabd464Sdrh 955fe2093d7Sdrh #ifndef SQLITE_OMIT_EXPLAIN 9563cf86063Sdanielk1977 /* If this is an EXPLAIN, skip this step */ 9573cf86063Sdanielk1977 if( pParse->explain ){ 95861de0d1bSdanielk1977 return; 9593cf86063Sdanielk1977 } 9605338a5f7Sdanielk1977 #endif 9613cf86063Sdanielk1977 962d6502758Sdrh assert( v!=0 ); 9639e12800dSdanielk1977 if( pParse->colNamesSet || v==0 || sqlite3MallocFailed() ) return; 964d8bc7086Sdrh pParse->colNamesSet = 1; 965fcabd464Sdrh fullNames = (db->flags & SQLITE_FullColNames)!=0; 966fcabd464Sdrh shortNames = (db->flags & SQLITE_ShortColNames)!=0; 96722322fd4Sdanielk1977 sqlite3VdbeSetNumCols(v, pEList->nExpr); 96882c3d636Sdrh for(i=0; i<pEList->nExpr; i++){ 96982c3d636Sdrh Expr *p; 9705a38705eSdrh p = pEList->a[i].pExpr; 9715a38705eSdrh if( p==0 ) continue; 97282c3d636Sdrh if( pEList->a[i].zName ){ 97382c3d636Sdrh char *zName = pEList->a[i].zName; 974955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, strlen(zName)); 97582c3d636Sdrh continue; 97682c3d636Sdrh } 977fa173a76Sdrh if( p->op==TK_COLUMN && pTabList ){ 9786a3ea0e6Sdrh Table *pTab; 97997665873Sdrh char *zCol; 9808aff1015Sdrh int iCol = p->iColumn; 9816a3ea0e6Sdrh for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} 9826a3ea0e6Sdrh assert( j<pTabList->nSrc ); 9836a3ea0e6Sdrh pTab = pTabList->a[j].pTab; 9848aff1015Sdrh if( iCol<0 ) iCol = pTab->iPKey; 98597665873Sdrh assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); 986b1363206Sdrh if( iCol<0 ){ 98747a6db2bSdrh zCol = "rowid"; 988b1363206Sdrh }else{ 989b1363206Sdrh zCol = pTab->aCol[iCol].zName; 990b1363206Sdrh } 991fcabd464Sdrh if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ 992955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n); 993fcabd464Sdrh }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ 99482c3d636Sdrh char *zName = 0; 99582c3d636Sdrh char *zTab; 99682c3d636Sdrh 9976a3ea0e6Sdrh zTab = pTabList->a[j].zAlias; 998fcabd464Sdrh if( fullNames || zTab==0 ) zTab = pTab->zName; 999f93339deSdrh sqlite3SetString(&zName, zTab, ".", zCol, (char*)0); 1000955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, P3_DYNAMIC); 100182c3d636Sdrh }else{ 1002955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, strlen(zCol)); 100382c3d636Sdrh } 10046977fea8Sdrh }else if( p->span.z && p->span.z[0] ){ 1005955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, (char*)p->span.z, p->span.n); 10063cf86063Sdanielk1977 /* sqlite3VdbeCompressSpace(v, addr); */ 10071bee3d7bSdrh }else{ 10081bee3d7bSdrh char zName[30]; 10091bee3d7bSdrh assert( p->op!=TK_COLUMN || pTabList==0 ); 10101bee3d7bSdrh sprintf(zName, "column%d", i+1); 1011955de52cSdanielk1977 sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, 0); 101282c3d636Sdrh } 101382c3d636Sdrh } 101476d505baSdanielk1977 generateColumnTypes(pParse, pTabList, pEList); 10155080aaa7Sdrh } 101682c3d636Sdrh 101793758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 101882c3d636Sdrh /* 1019d8bc7086Sdrh ** Name of the connection operator, used for error messages. 1020d8bc7086Sdrh */ 1021d8bc7086Sdrh static const char *selectOpName(int id){ 1022d8bc7086Sdrh char *z; 1023d8bc7086Sdrh switch( id ){ 1024d8bc7086Sdrh case TK_ALL: z = "UNION ALL"; break; 1025d8bc7086Sdrh case TK_INTERSECT: z = "INTERSECT"; break; 1026d8bc7086Sdrh case TK_EXCEPT: z = "EXCEPT"; break; 1027d8bc7086Sdrh default: z = "UNION"; break; 1028d8bc7086Sdrh } 1029d8bc7086Sdrh return z; 1030d8bc7086Sdrh } 103193758c8dSdanielk1977 #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1032d8bc7086Sdrh 1033d8bc7086Sdrh /* 1034315555caSdrh ** Forward declaration 1035315555caSdrh */ 10369b3187e1Sdrh static int prepSelectStmt(Parse*, Select*); 1037315555caSdrh 1038315555caSdrh /* 103922f70c32Sdrh ** Given a SELECT statement, generate a Table structure that describes 104022f70c32Sdrh ** the result set of that SELECT. 104122f70c32Sdrh */ 10424adee20fSdanielk1977 Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ 104322f70c32Sdrh Table *pTab; 1044b733d037Sdrh int i, j; 104522f70c32Sdrh ExprList *pEList; 1046290c1948Sdrh Column *aCol, *pCol; 104722f70c32Sdrh 104892378253Sdrh while( pSelect->pPrior ) pSelect = pSelect->pPrior; 10499b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 105022f70c32Sdrh return 0; 105122f70c32Sdrh } 1052142bdf40Sdanielk1977 if( sqlite3SelectResolve(pParse, pSelect, 0) ){ 1053142bdf40Sdanielk1977 return 0; 1054142bdf40Sdanielk1977 } 105522f70c32Sdrh pTab = sqliteMalloc( sizeof(Table) ); 105622f70c32Sdrh if( pTab==0 ){ 105722f70c32Sdrh return 0; 105822f70c32Sdrh } 1059ed8a3bb1Sdrh pTab->nRef = 1; 106022f70c32Sdrh pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; 106122f70c32Sdrh pEList = pSelect->pEList; 106222f70c32Sdrh pTab->nCol = pEList->nExpr; 1063417be79cSdrh assert( pTab->nCol>0 ); 1064b733d037Sdrh pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); 1065290c1948Sdrh for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){ 106679d5f63fSdrh Expr *p, *pR; 1067517eb646Sdanielk1977 char *zType; 106891bb0eedSdrh char *zName; 106979d5f63fSdrh char *zBasename; 1070b3bf556eSdanielk1977 CollSeq *pColl; 107179d5f63fSdrh int cnt; 1072b3bce662Sdanielk1977 NameContext sNC; 107379d5f63fSdrh 107479d5f63fSdrh /* Get an appropriate name for the column 107579d5f63fSdrh */ 107679d5f63fSdrh p = pEList->a[i].pExpr; 1077290c1948Sdrh assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 ); 107891bb0eedSdrh if( (zName = pEList->a[i].zName)!=0 ){ 107979d5f63fSdrh /* If the column contains an "AS <name>" phrase, use <name> as the name */ 108091bb0eedSdrh zName = sqliteStrDup(zName); 1081517eb646Sdanielk1977 }else if( p->op==TK_DOT 1082b733d037Sdrh && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ 108379d5f63fSdrh /* For columns of the from A.B use B as the name */ 108491bb0eedSdrh zName = sqlite3MPrintf("%T", &pR->token); 1085b733d037Sdrh }else if( p->span.z && p->span.z[0] ){ 108679d5f63fSdrh /* Use the original text of the column expression as its name */ 108791bb0eedSdrh zName = sqlite3MPrintf("%T", &p->span); 108822f70c32Sdrh }else{ 108979d5f63fSdrh /* If all else fails, make up a name */ 109091bb0eedSdrh zName = sqlite3MPrintf("column%d", i+1); 109122f70c32Sdrh } 109291bb0eedSdrh sqlite3Dequote(zName); 10939e12800dSdanielk1977 if( sqlite3MallocFailed() ){ 1094dd5b2fa5Sdrh sqliteFree(zName); 1095dd5b2fa5Sdrh sqlite3DeleteTable(0, pTab); 1096dd5b2fa5Sdrh return 0; 1097dd5b2fa5Sdrh } 109879d5f63fSdrh 109979d5f63fSdrh /* Make sure the column name is unique. If the name is not unique, 110079d5f63fSdrh ** append a integer to the name so that it becomes unique. 110179d5f63fSdrh */ 110279d5f63fSdrh zBasename = zName; 110379d5f63fSdrh for(j=cnt=0; j<i; j++){ 110479d5f63fSdrh if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ 110579d5f63fSdrh zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt); 110679d5f63fSdrh j = -1; 1107dd5b2fa5Sdrh if( zName==0 ) break; 110879d5f63fSdrh } 110979d5f63fSdrh } 111079d5f63fSdrh if( zBasename!=zName ){ 111179d5f63fSdrh sqliteFree(zBasename); 111279d5f63fSdrh } 111391bb0eedSdrh pCol->zName = zName; 1114e014a838Sdanielk1977 111579d5f63fSdrh /* Get the typename, type affinity, and collating sequence for the 111679d5f63fSdrh ** column. 111779d5f63fSdrh */ 1118c43e8be8Sdrh memset(&sNC, 0, sizeof(sNC)); 1119b3bce662Sdanielk1977 sNC.pSrcList = pSelect->pSrc; 1120955de52cSdanielk1977 zType = sqliteStrDup(columnType(&sNC, p, 0, 0, 0)); 1121290c1948Sdrh pCol->zType = zType; 1122c60e9b82Sdanielk1977 pCol->affinity = sqlite3ExprAffinity(p); 1123b3bf556eSdanielk1977 pColl = sqlite3ExprCollSeq(pParse, p); 1124b3bf556eSdanielk1977 if( pColl ){ 1125e7259296Sdanielk1977 pCol->zColl = sqliteStrDup(pColl->zName); 11260202b29eSdanielk1977 } 112722f70c32Sdrh } 112822f70c32Sdrh pTab->iPKey = -1; 112922f70c32Sdrh return pTab; 113022f70c32Sdrh } 113122f70c32Sdrh 113222f70c32Sdrh /* 11339b3187e1Sdrh ** Prepare a SELECT statement for processing by doing the following 11349b3187e1Sdrh ** things: 1135d8bc7086Sdrh ** 11369b3187e1Sdrh ** (1) Make sure VDBE cursor numbers have been assigned to every 11379b3187e1Sdrh ** element of the FROM clause. 11389b3187e1Sdrh ** 11399b3187e1Sdrh ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that 11409b3187e1Sdrh ** defines FROM clause. When views appear in the FROM clause, 114163eb5f29Sdrh ** fill pTabList->a[].pSelect with a copy of the SELECT statement 114263eb5f29Sdrh ** that implements the view. A copy is made of the view's SELECT 114363eb5f29Sdrh ** statement so that we can freely modify or delete that statement 114463eb5f29Sdrh ** without worrying about messing up the presistent representation 114563eb5f29Sdrh ** of the view. 1146d8bc7086Sdrh ** 11479b3187e1Sdrh ** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword 1148ad2d8307Sdrh ** on joins and the ON and USING clause of joins. 1149ad2d8307Sdrh ** 11509b3187e1Sdrh ** (4) Scan the list of columns in the result set (pEList) looking 115154473229Sdrh ** for instances of the "*" operator or the TABLE.* operator. 115254473229Sdrh ** If found, expand each "*" to be every column in every table 115354473229Sdrh ** and TABLE.* to be every column in TABLE. 1154d8bc7086Sdrh ** 1155d8bc7086Sdrh ** Return 0 on success. If there are problems, leave an error message 1156d8bc7086Sdrh ** in pParse and return non-zero. 1157d8bc7086Sdrh */ 11589b3187e1Sdrh static int prepSelectStmt(Parse *pParse, Select *p){ 115954473229Sdrh int i, j, k, rc; 1160ad3cab52Sdrh SrcList *pTabList; 1161daffd0e5Sdrh ExprList *pEList; 1162290c1948Sdrh struct SrcList_item *pFrom; 1163daffd0e5Sdrh 11649e12800dSdanielk1977 if( p==0 || p->pSrc==0 || sqlite3MallocFailed() ){ 11656f7adc8aSdrh return 1; 11666f7adc8aSdrh } 1167daffd0e5Sdrh pTabList = p->pSrc; 1168daffd0e5Sdrh pEList = p->pEList; 1169d8bc7086Sdrh 11709b3187e1Sdrh /* Make sure cursor numbers have been assigned to all entries in 11719b3187e1Sdrh ** the FROM clause of the SELECT statement. 11729b3187e1Sdrh */ 11739b3187e1Sdrh sqlite3SrcListAssignCursors(pParse, p->pSrc); 11749b3187e1Sdrh 11759b3187e1Sdrh /* Look up every table named in the FROM clause of the select. If 11769b3187e1Sdrh ** an entry of the FROM clause is a subquery instead of a table or view, 11779b3187e1Sdrh ** then create a transient table structure to describe the subquery. 1178d8bc7086Sdrh */ 1179290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1180f0113000Sdanielk1977 Table *pTab; 11819b3187e1Sdrh if( pFrom->pTab!=0 ){ 11829b3187e1Sdrh /* This statement has already been prepared. There is no need 11839b3187e1Sdrh ** to go further. */ 11849b3187e1Sdrh assert( i==0 ); 1185d8bc7086Sdrh return 0; 1186d8bc7086Sdrh } 1187290c1948Sdrh if( pFrom->zName==0 ){ 118893758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 118922f70c32Sdrh /* A sub-query in the FROM clause of a SELECT */ 1190290c1948Sdrh assert( pFrom->pSelect!=0 ); 1191290c1948Sdrh if( pFrom->zAlias==0 ){ 119291bb0eedSdrh pFrom->zAlias = 119391bb0eedSdrh sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect); 1194ad2d8307Sdrh } 1195ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1196290c1948Sdrh pFrom->pTab = pTab = 1197290c1948Sdrh sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect); 119822f70c32Sdrh if( pTab==0 ){ 1199daffd0e5Sdrh return 1; 1200daffd0e5Sdrh } 1201b9bb7c18Sdrh /* The isEphem flag indicates that the Table structure has been 12025cf590c1Sdrh ** dynamically allocated and may be freed at any time. In other words, 12035cf590c1Sdrh ** pTab is not pointing to a persistent table structure that defines 12045cf590c1Sdrh ** part of the schema. */ 1205b9bb7c18Sdrh pTab->isEphem = 1; 120693758c8dSdanielk1977 #endif 120722f70c32Sdrh }else{ 1208a76b5dfcSdrh /* An ordinary table or view name in the FROM clause */ 1209ed8a3bb1Sdrh assert( pFrom->pTab==0 ); 1210290c1948Sdrh pFrom->pTab = pTab = 1211290c1948Sdrh sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); 1212a76b5dfcSdrh if( pTab==0 ){ 1213d8bc7086Sdrh return 1; 1214d8bc7086Sdrh } 1215ed8a3bb1Sdrh pTab->nRef++; 121693626f48Sdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) 121793626f48Sdanielk1977 if( pTab->pSelect || IsVirtual(pTab) ){ 121863eb5f29Sdrh /* We reach here if the named table is a really a view */ 12194adee20fSdanielk1977 if( sqlite3ViewGetColumnNames(pParse, pTab) ){ 1220417be79cSdrh return 1; 1221417be79cSdrh } 1222290c1948Sdrh /* If pFrom->pSelect!=0 it means we are dealing with a 122363eb5f29Sdrh ** view within a view. The SELECT structure has already been 122463eb5f29Sdrh ** copied by the outer view so we can skip the copy step here 122563eb5f29Sdrh ** in the inner view. 122663eb5f29Sdrh */ 1227290c1948Sdrh if( pFrom->pSelect==0 ){ 1228290c1948Sdrh pFrom->pSelect = sqlite3SelectDup(pTab->pSelect); 1229a76b5dfcSdrh } 1230d8bc7086Sdrh } 123193758c8dSdanielk1977 #endif 123222f70c32Sdrh } 123363eb5f29Sdrh } 1234d8bc7086Sdrh 1235ad2d8307Sdrh /* Process NATURAL keywords, and ON and USING clauses of joins. 1236ad2d8307Sdrh */ 1237ad2d8307Sdrh if( sqliteProcessJoin(pParse, p) ) return 1; 1238ad2d8307Sdrh 12397c917d19Sdrh /* For every "*" that occurs in the column list, insert the names of 124054473229Sdrh ** all columns in all tables. And for every TABLE.* insert the names 124154473229Sdrh ** of all columns in TABLE. The parser inserted a special expression 12427c917d19Sdrh ** with the TK_ALL operator for each "*" that it found in the column list. 12437c917d19Sdrh ** The following code just has to locate the TK_ALL expressions and expand 12447c917d19Sdrh ** each one to the list of all columns in all tables. 124554473229Sdrh ** 124654473229Sdrh ** The first loop just checks to see if there are any "*" operators 124754473229Sdrh ** that need expanding. 1248d8bc7086Sdrh */ 12497c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 125054473229Sdrh Expr *pE = pEList->a[k].pExpr; 125154473229Sdrh if( pE->op==TK_ALL ) break; 125254473229Sdrh if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL 125354473229Sdrh && pE->pLeft && pE->pLeft->op==TK_ID ) break; 12547c917d19Sdrh } 125554473229Sdrh rc = 0; 12567c917d19Sdrh if( k<pEList->nExpr ){ 125754473229Sdrh /* 125854473229Sdrh ** If we get here it means the result set contains one or more "*" 125954473229Sdrh ** operators that need to be expanded. Loop through each expression 126054473229Sdrh ** in the result set and expand them one by one. 126154473229Sdrh */ 12627c917d19Sdrh struct ExprList_item *a = pEList->a; 12637c917d19Sdrh ExprList *pNew = 0; 1264d70dc52dSdrh int flags = pParse->db->flags; 1265d70dc52dSdrh int longNames = (flags & SQLITE_FullColNames)!=0 && 1266d70dc52dSdrh (flags & SQLITE_ShortColNames)==0; 1267d70dc52dSdrh 12687c917d19Sdrh for(k=0; k<pEList->nExpr; k++){ 126954473229Sdrh Expr *pE = a[k].pExpr; 127054473229Sdrh if( pE->op!=TK_ALL && 127154473229Sdrh (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ 127254473229Sdrh /* This particular expression does not need to be expanded. 127354473229Sdrh */ 12744adee20fSdanielk1977 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); 1275261919ccSdanielk1977 if( pNew ){ 12767c917d19Sdrh pNew->a[pNew->nExpr-1].zName = a[k].zName; 1277261919ccSdanielk1977 }else{ 1278261919ccSdanielk1977 rc = 1; 1279261919ccSdanielk1977 } 12807c917d19Sdrh a[k].pExpr = 0; 12817c917d19Sdrh a[k].zName = 0; 12827c917d19Sdrh }else{ 128354473229Sdrh /* This expression is a "*" or a "TABLE.*" and needs to be 128454473229Sdrh ** expanded. */ 128554473229Sdrh int tableSeen = 0; /* Set to 1 when TABLE matches */ 1286cf55b7aeSdrh char *zTName; /* text of name of TABLE */ 128754473229Sdrh if( pE->op==TK_DOT && pE->pLeft ){ 1288cf55b7aeSdrh zTName = sqlite3NameFromToken(&pE->pLeft->token); 128954473229Sdrh }else{ 1290cf55b7aeSdrh zTName = 0; 129154473229Sdrh } 1292290c1948Sdrh for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ 1293290c1948Sdrh Table *pTab = pFrom->pTab; 1294290c1948Sdrh char *zTabName = pFrom->zAlias; 129554473229Sdrh if( zTabName==0 || zTabName[0]==0 ){ 129654473229Sdrh zTabName = pTab->zName; 129754473229Sdrh } 1298cf55b7aeSdrh if( zTName && (zTabName==0 || zTabName[0]==0 || 1299cf55b7aeSdrh sqlite3StrICmp(zTName, zTabName)!=0) ){ 130054473229Sdrh continue; 130154473229Sdrh } 130254473229Sdrh tableSeen = 1; 1303d8bc7086Sdrh for(j=0; j<pTab->nCol; j++){ 1304f0113000Sdanielk1977 Expr *pExpr, *pRight; 1305ad2d8307Sdrh char *zName = pTab->aCol[j].zName; 1306ad2d8307Sdrh 130791bb0eedSdrh if( i>0 ){ 130891bb0eedSdrh struct SrcList_item *pLeft = &pTabList->a[i-1]; 130991bb0eedSdrh if( (pLeft->jointype & JT_NATURAL)!=0 && 131091bb0eedSdrh columnIndex(pLeft->pTab, zName)>=0 ){ 1311ad2d8307Sdrh /* In a NATURAL join, omit the join columns from the 1312ad2d8307Sdrh ** table on the right */ 1313ad2d8307Sdrh continue; 1314ad2d8307Sdrh } 131591bb0eedSdrh if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ 1316ad2d8307Sdrh /* In a join with a USING clause, omit columns in the 1317ad2d8307Sdrh ** using clause from the table on the right. */ 1318ad2d8307Sdrh continue; 1319ad2d8307Sdrh } 132091bb0eedSdrh } 13214adee20fSdanielk1977 pRight = sqlite3Expr(TK_ID, 0, 0, 0); 132222f70c32Sdrh if( pRight==0 ) break; 132391bb0eedSdrh setToken(&pRight->token, zName); 1324d70dc52dSdrh if( zTabName && (longNames || pTabList->nSrc>1) ){ 1325f0113000Sdanielk1977 Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, 0); 13264adee20fSdanielk1977 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); 132722f70c32Sdrh if( pExpr==0 ) break; 132891bb0eedSdrh setToken(&pLeft->token, zTabName); 132991bb0eedSdrh setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); 13306977fea8Sdrh pExpr->span.dyn = 1; 13316977fea8Sdrh pExpr->token.z = 0; 13326977fea8Sdrh pExpr->token.n = 0; 13336977fea8Sdrh pExpr->token.dyn = 0; 133422f70c32Sdrh }else{ 133522f70c32Sdrh pExpr = pRight; 13366977fea8Sdrh pExpr->span = pExpr->token; 133722f70c32Sdrh } 1338d70dc52dSdrh if( longNames ){ 1339d70dc52dSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); 1340d70dc52dSdrh }else{ 134179d5f63fSdrh pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); 1342d8bc7086Sdrh } 1343d8bc7086Sdrh } 1344d70dc52dSdrh } 134554473229Sdrh if( !tableSeen ){ 1346cf55b7aeSdrh if( zTName ){ 1347cf55b7aeSdrh sqlite3ErrorMsg(pParse, "no such table: %s", zTName); 1348f5db2d3eSdrh }else{ 13494adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "no tables specified"); 1350f5db2d3eSdrh } 135154473229Sdrh rc = 1; 135254473229Sdrh } 1353cf55b7aeSdrh sqliteFree(zTName); 13547c917d19Sdrh } 13557c917d19Sdrh } 13564adee20fSdanielk1977 sqlite3ExprListDelete(pEList); 13577c917d19Sdrh p->pEList = pNew; 1358d8bc7086Sdrh } 135954473229Sdrh return rc; 1360d8bc7086Sdrh } 1361d8bc7086Sdrh 136293758c8dSdanielk1977 #ifndef SQLITE_OMIT_COMPOUND_SELECT 1363ff78bd2fSdrh /* 1364d8bc7086Sdrh ** This routine associates entries in an ORDER BY expression list with 1365d8bc7086Sdrh ** columns in a result. For each ORDER BY expression, the opcode of 1366967e8b73Sdrh ** the top-level node is changed to TK_COLUMN and the iColumn value of 1367d8bc7086Sdrh ** the top-level node is filled in with column number and the iTable 1368d8bc7086Sdrh ** value of the top-level node is filled with iTable parameter. 1369d8bc7086Sdrh ** 1370d8bc7086Sdrh ** If there are prior SELECT clauses, they are processed first. A match 1371d8bc7086Sdrh ** in an earlier SELECT takes precedence over a later SELECT. 1372d8bc7086Sdrh ** 1373d8bc7086Sdrh ** Any entry that does not match is flagged as an error. The number 1374d8bc7086Sdrh ** of errors is returned. 1375d8bc7086Sdrh */ 1376d8bc7086Sdrh static int matchOrderbyToColumn( 1377d8bc7086Sdrh Parse *pParse, /* A place to leave error messages */ 1378d8bc7086Sdrh Select *pSelect, /* Match to result columns of this SELECT */ 1379d8bc7086Sdrh ExprList *pOrderBy, /* The ORDER BY values to match against columns */ 1380e4de1febSdrh int iTable, /* Insert this value in iTable */ 1381d8bc7086Sdrh int mustComplete /* If TRUE all ORDER BYs must match */ 1382d8bc7086Sdrh ){ 1383d8bc7086Sdrh int nErr = 0; 1384d8bc7086Sdrh int i, j; 1385d8bc7086Sdrh ExprList *pEList; 1386d8bc7086Sdrh 1387daffd0e5Sdrh if( pSelect==0 || pOrderBy==0 ) return 1; 1388d8bc7086Sdrh if( mustComplete ){ 1389d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } 1390d8bc7086Sdrh } 13919b3187e1Sdrh if( prepSelectStmt(pParse, pSelect) ){ 1392d8bc7086Sdrh return 1; 1393d8bc7086Sdrh } 1394d8bc7086Sdrh if( pSelect->pPrior ){ 139592cd52f5Sdrh if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ 139692cd52f5Sdrh return 1; 139792cd52f5Sdrh } 1398d8bc7086Sdrh } 1399d8bc7086Sdrh pEList = pSelect->pEList; 1400d8bc7086Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 1401d8bc7086Sdrh Expr *pE = pOrderBy->a[i].pExpr; 1402e4de1febSdrh int iCol = -1; 1403d8bc7086Sdrh if( pOrderBy->a[i].done ) continue; 14044adee20fSdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 1405e4de1febSdrh if( iCol<=0 || iCol>pEList->nExpr ){ 14064adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1407da93d238Sdrh "ORDER BY position %d should be between 1 and %d", 1408e4de1febSdrh iCol, pEList->nExpr); 1409e4de1febSdrh nErr++; 1410e4de1febSdrh break; 1411e4de1febSdrh } 1412fcb78a49Sdrh if( !mustComplete ) continue; 1413e4de1febSdrh iCol--; 1414e4de1febSdrh } 1415e4de1febSdrh for(j=0; iCol<0 && j<pEList->nExpr; j++){ 14164cfa7934Sdrh if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ 1417a76b5dfcSdrh char *zName, *zLabel; 1418a76b5dfcSdrh zName = pEList->a[j].zName; 1419a99db3b6Sdrh zLabel = sqlite3NameFromToken(&pE->token); 1420a99db3b6Sdrh assert( zLabel!=0 ); 14214adee20fSdanielk1977 if( sqlite3StrICmp(zName, zLabel)==0 ){ 1422e4de1febSdrh iCol = j; 1423d8bc7086Sdrh } 14246e142f54Sdrh sqliteFree(zLabel); 1425d8bc7086Sdrh } 14264adee20fSdanielk1977 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ 1427e4de1febSdrh iCol = j; 1428d8bc7086Sdrh } 1429e4de1febSdrh } 1430e4de1febSdrh if( iCol>=0 ){ 1431967e8b73Sdrh pE->op = TK_COLUMN; 1432e4de1febSdrh pE->iColumn = iCol; 1433d8bc7086Sdrh pE->iTable = iTable; 1434a58fdfb1Sdanielk1977 pE->iAgg = -1; 1435d8bc7086Sdrh pOrderBy->a[i].done = 1; 1436d8bc7086Sdrh } 1437e4de1febSdrh if( iCol<0 && mustComplete ){ 14384adee20fSdanielk1977 sqlite3ErrorMsg(pParse, 1439da93d238Sdrh "ORDER BY term number %d does not match any result column", i+1); 1440d8bc7086Sdrh nErr++; 1441d8bc7086Sdrh break; 1442d8bc7086Sdrh } 1443d8bc7086Sdrh } 1444d8bc7086Sdrh return nErr; 1445d8bc7086Sdrh } 144693758c8dSdanielk1977 #endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT */ 1447d8bc7086Sdrh 1448d8bc7086Sdrh /* 1449d8bc7086Sdrh ** Get a VDBE for the given parser context. Create a new one if necessary. 1450d8bc7086Sdrh ** If an error occurs, return NULL and leave a message in pParse. 1451d8bc7086Sdrh */ 14524adee20fSdanielk1977 Vdbe *sqlite3GetVdbe(Parse *pParse){ 1453d8bc7086Sdrh Vdbe *v = pParse->pVdbe; 1454d8bc7086Sdrh if( v==0 ){ 14554adee20fSdanielk1977 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); 1456d8bc7086Sdrh } 1457d8bc7086Sdrh return v; 1458d8bc7086Sdrh } 1459d8bc7086Sdrh 146015007a99Sdrh 1461d8bc7086Sdrh /* 14627b58daeaSdrh ** Compute the iLimit and iOffset fields of the SELECT based on the 1463ec7429aeSdrh ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions 14647b58daeaSdrh ** that appear in the original SQL statement after the LIMIT and OFFSET 1465a2dc3b1aSdanielk1977 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset 1466a2dc3b1aSdanielk1977 ** are the integer memory register numbers for counters used to compute 1467a2dc3b1aSdanielk1977 ** the limit and offset. If there is no limit and/or offset, then 1468a2dc3b1aSdanielk1977 ** iLimit and iOffset are negative. 14697b58daeaSdrh ** 1470d59ba6ceSdrh ** This routine changes the values of iLimit and iOffset only if 1471ec7429aeSdrh ** a limit or offset is defined by pLimit and pOffset. iLimit and 14727b58daeaSdrh ** iOffset should have been preset to appropriate default values 14737b58daeaSdrh ** (usually but not always -1) prior to calling this routine. 1474ec7429aeSdrh ** Only if pLimit!=0 or pOffset!=0 do the limit registers get 14757b58daeaSdrh ** redefined. The UNION ALL operator uses this property to force 14767b58daeaSdrh ** the reuse of the same limit and offset registers across multiple 14777b58daeaSdrh ** SELECT statements. 14787b58daeaSdrh */ 1479ec7429aeSdrh static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ 148002afc861Sdrh Vdbe *v = 0; 148102afc861Sdrh int iLimit = 0; 148215007a99Sdrh int iOffset; 148315007a99Sdrh int addr1, addr2; 148415007a99Sdrh 14857b58daeaSdrh /* 14867b58daeaSdrh ** "LIMIT -1" always shows all rows. There is some 14877b58daeaSdrh ** contraversy about what the correct behavior should be. 14887b58daeaSdrh ** The current implementation interprets "LIMIT 0" to mean 14897b58daeaSdrh ** no rows. 14907b58daeaSdrh */ 1491a2dc3b1aSdanielk1977 if( p->pLimit ){ 149215007a99Sdrh p->iLimit = iLimit = pParse->nMem; 1493d59ba6ceSdrh pParse->nMem += 2; 149415007a99Sdrh v = sqlite3GetVdbe(pParse); 14957b58daeaSdrh if( v==0 ) return; 1496a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pLimit); 1497a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 149815007a99Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iLimit, 0); 1499ad6d9460Sdrh VdbeComment((v, "# LIMIT counter")); 150015007a99Sdrh sqlite3VdbeAddOp(v, OP_IfMemZero, iLimit, iBreak); 15017b58daeaSdrh } 1502a2dc3b1aSdanielk1977 if( p->pOffset ){ 150315007a99Sdrh p->iOffset = iOffset = pParse->nMem++; 150415007a99Sdrh v = sqlite3GetVdbe(pParse); 15057b58daeaSdrh if( v==0 ) return; 1506a2dc3b1aSdanielk1977 sqlite3ExprCode(pParse, p->pOffset); 1507a2dc3b1aSdanielk1977 sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0); 150815007a99Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iOffset, p->pLimit==0); 1509ad6d9460Sdrh VdbeComment((v, "# OFFSET counter")); 151015007a99Sdrh addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iOffset, 0); 151115007a99Sdrh sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 151215007a99Sdrh sqlite3VdbeAddOp(v, OP_Integer, 0, 0); 151315007a99Sdrh sqlite3VdbeJumpHere(v, addr1); 1514d59ba6ceSdrh if( p->pLimit ){ 1515d59ba6ceSdrh sqlite3VdbeAddOp(v, OP_Add, 0, 0); 1516d59ba6ceSdrh } 15177b58daeaSdrh } 1518d59ba6ceSdrh if( p->pLimit ){ 151915007a99Sdrh addr1 = sqlite3VdbeAddOp(v, OP_IfMemPos, iLimit, 0); 152015007a99Sdrh sqlite3VdbeAddOp(v, OP_Pop, 1, 0); 152115007a99Sdrh sqlite3VdbeAddOp(v, OP_MemInt, -1, iLimit+1); 152215007a99Sdrh addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); 152315007a99Sdrh sqlite3VdbeJumpHere(v, addr1); 152415007a99Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iLimit+1, 1); 1525d59ba6ceSdrh VdbeComment((v, "# LIMIT+OFFSET")); 152615007a99Sdrh sqlite3VdbeJumpHere(v, addr2); 1527d59ba6ceSdrh } 15287b58daeaSdrh } 15297b58daeaSdrh 15307b58daeaSdrh /* 15310342b1f5Sdrh ** Allocate a virtual index to use for sorting. 1532d3d39e93Sdrh */ 15334db38a70Sdrh static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){ 15340342b1f5Sdrh if( pOrderBy ){ 1535dc1bdc4fSdanielk1977 int addr; 15369d2985c7Sdrh assert( pOrderBy->iECursor==0 ); 15379d2985c7Sdrh pOrderBy->iECursor = pParse->nTab++; 1538b9bb7c18Sdrh addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenEphemeral, 15399d2985c7Sdrh pOrderBy->iECursor, pOrderBy->nExpr+1); 1540b9bb7c18Sdrh assert( p->addrOpenEphm[2] == -1 ); 1541b9bb7c18Sdrh p->addrOpenEphm[2] = addr; 1542736c22b8Sdrh } 1543dc1bdc4fSdanielk1977 } 1544dc1bdc4fSdanielk1977 1545b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1546fbc4ee7bSdrh /* 1547fbc4ee7bSdrh ** Return the appropriate collating sequence for the iCol-th column of 1548fbc4ee7bSdrh ** the result set for the compound-select statement "p". Return NULL if 1549fbc4ee7bSdrh ** the column has no default collating sequence. 1550fbc4ee7bSdrh ** 1551fbc4ee7bSdrh ** The collating sequence for the compound select is taken from the 1552fbc4ee7bSdrh ** left-most term of the select that has a collating sequence. 1553fbc4ee7bSdrh */ 1554dc1bdc4fSdanielk1977 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ 1555fbc4ee7bSdrh CollSeq *pRet; 1556dc1bdc4fSdanielk1977 if( p->pPrior ){ 1557dc1bdc4fSdanielk1977 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); 1558fbc4ee7bSdrh }else{ 1559fbc4ee7bSdrh pRet = 0; 1560dc1bdc4fSdanielk1977 } 1561fbc4ee7bSdrh if( pRet==0 ){ 1562dc1bdc4fSdanielk1977 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); 1563dc1bdc4fSdanielk1977 } 1564dc1bdc4fSdanielk1977 return pRet; 1565d3d39e93Sdrh } 1566b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 1567d3d39e93Sdrh 1568b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 1569d3d39e93Sdrh /* 157082c3d636Sdrh ** This routine is called to process a query that is really the union 157182c3d636Sdrh ** or intersection of two or more separate queries. 1572c926afbcSdrh ** 1573e78e8284Sdrh ** "p" points to the right-most of the two queries. the query on the 1574e78e8284Sdrh ** left is p->pPrior. The left query could also be a compound query 1575e78e8284Sdrh ** in which case this routine will be called recursively. 1576e78e8284Sdrh ** 1577e78e8284Sdrh ** The results of the total query are to be written into a destination 1578e78e8284Sdrh ** of type eDest with parameter iParm. 1579e78e8284Sdrh ** 1580e78e8284Sdrh ** Example 1: Consider a three-way compound SQL statement. 1581e78e8284Sdrh ** 1582e78e8284Sdrh ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 1583e78e8284Sdrh ** 1584e78e8284Sdrh ** This statement is parsed up as follows: 1585e78e8284Sdrh ** 1586e78e8284Sdrh ** SELECT c FROM t3 1587e78e8284Sdrh ** | 1588e78e8284Sdrh ** `-----> SELECT b FROM t2 1589e78e8284Sdrh ** | 15904b11c6d3Sjplyon ** `------> SELECT a FROM t1 1591e78e8284Sdrh ** 1592e78e8284Sdrh ** The arrows in the diagram above represent the Select.pPrior pointer. 1593e78e8284Sdrh ** So if this routine is called with p equal to the t3 query, then 1594e78e8284Sdrh ** pPrior will be the t2 query. p->op will be TK_UNION in this case. 1595e78e8284Sdrh ** 1596e78e8284Sdrh ** Notice that because of the way SQLite parses compound SELECTs, the 1597e78e8284Sdrh ** individual selects always group from left to right. 159882c3d636Sdrh */ 159984ac9d02Sdanielk1977 static int multiSelect( 1600fbc4ee7bSdrh Parse *pParse, /* Parsing context */ 1601fbc4ee7bSdrh Select *p, /* The right-most of SELECTs to be coded */ 1602fbc4ee7bSdrh int eDest, /* \___ Store query results as specified */ 1603fbc4ee7bSdrh int iParm, /* / by these two parameters. */ 160484ac9d02Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 160584ac9d02Sdanielk1977 ){ 160684ac9d02Sdanielk1977 int rc = SQLITE_OK; /* Success code from a subroutine */ 160710e5e3cfSdrh Select *pPrior; /* Another SELECT immediately to our left */ 160810e5e3cfSdrh Vdbe *v; /* Generate code to this VDBE */ 16098cdbf836Sdrh int nCol; /* Number of columns in the result set */ 16100342b1f5Sdrh ExprList *pOrderBy; /* The ORDER BY clause on p */ 16110342b1f5Sdrh int aSetP2[2]; /* Set P2 value of these op to number of columns */ 16120342b1f5Sdrh int nSetP2 = 0; /* Number of slots in aSetP2[] used */ 161382c3d636Sdrh 16147b58daeaSdrh /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only 1615fbc4ee7bSdrh ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. 161682c3d636Sdrh */ 161784ac9d02Sdanielk1977 if( p==0 || p->pPrior==0 ){ 161884ac9d02Sdanielk1977 rc = 1; 161984ac9d02Sdanielk1977 goto multi_select_end; 162084ac9d02Sdanielk1977 } 1621d8bc7086Sdrh pPrior = p->pPrior; 16220342b1f5Sdrh assert( pPrior->pRightmost!=pPrior ); 16230342b1f5Sdrh assert( pPrior->pRightmost==p->pRightmost ); 1624d8bc7086Sdrh if( pPrior->pOrderBy ){ 16254adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", 1626da93d238Sdrh selectOpName(p->op)); 162784ac9d02Sdanielk1977 rc = 1; 162884ac9d02Sdanielk1977 goto multi_select_end; 162982c3d636Sdrh } 1630a2dc3b1aSdanielk1977 if( pPrior->pLimit ){ 16314adee20fSdanielk1977 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", 16327b58daeaSdrh selectOpName(p->op)); 163384ac9d02Sdanielk1977 rc = 1; 163484ac9d02Sdanielk1977 goto multi_select_end; 16357b58daeaSdrh } 163682c3d636Sdrh 1637d8bc7086Sdrh /* Make sure we have a valid query engine. If not, create a new one. 1638d8bc7086Sdrh */ 16394adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 164084ac9d02Sdanielk1977 if( v==0 ){ 164184ac9d02Sdanielk1977 rc = 1; 164284ac9d02Sdanielk1977 goto multi_select_end; 164384ac9d02Sdanielk1977 } 1644d8bc7086Sdrh 16451cc3d75fSdrh /* Create the destination temporary table if necessary 16461cc3d75fSdrh */ 1647b9bb7c18Sdrh if( eDest==SRT_EphemTab ){ 1648b4964b72Sdanielk1977 assert( p->pEList ); 16490342b1f5Sdrh assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); 1650b9bb7c18Sdrh aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 0); 16511cc3d75fSdrh eDest = SRT_Table; 16521cc3d75fSdrh } 16531cc3d75fSdrh 1654f46f905aSdrh /* Generate code for the left and right SELECT statements. 1655d8bc7086Sdrh */ 16560342b1f5Sdrh pOrderBy = p->pOrderBy; 165782c3d636Sdrh switch( p->op ){ 1658f46f905aSdrh case TK_ALL: { 16590342b1f5Sdrh if( pOrderBy==0 ){ 1660ec7429aeSdrh int addr = 0; 1661a2dc3b1aSdanielk1977 assert( !pPrior->pLimit ); 1662a2dc3b1aSdanielk1977 pPrior->pLimit = p->pLimit; 1663a2dc3b1aSdanielk1977 pPrior->pOffset = p->pOffset; 1664b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); 1665ad68cb6bSdanielk1977 p->pLimit = 0; 1666ad68cb6bSdanielk1977 p->pOffset = 0; 166784ac9d02Sdanielk1977 if( rc ){ 166884ac9d02Sdanielk1977 goto multi_select_end; 166984ac9d02Sdanielk1977 } 1670f46f905aSdrh p->pPrior = 0; 16717b58daeaSdrh p->iLimit = pPrior->iLimit; 16727b58daeaSdrh p->iOffset = pPrior->iOffset; 1673ec7429aeSdrh if( p->iLimit>=0 ){ 1674ec7429aeSdrh addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0); 1675ec7429aeSdrh VdbeComment((v, "# Jump ahead if LIMIT reached")); 1676ec7429aeSdrh } 1677b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); 1678f46f905aSdrh p->pPrior = pPrior; 167984ac9d02Sdanielk1977 if( rc ){ 168084ac9d02Sdanielk1977 goto multi_select_end; 168184ac9d02Sdanielk1977 } 1682ec7429aeSdrh if( addr ){ 1683ec7429aeSdrh sqlite3VdbeJumpHere(v, addr); 1684ec7429aeSdrh } 1685f46f905aSdrh break; 1686f46f905aSdrh } 1687f46f905aSdrh /* For UNION ALL ... ORDER BY fall through to the next case */ 1688f46f905aSdrh } 168982c3d636Sdrh case TK_EXCEPT: 169082c3d636Sdrh case TK_UNION: { 1691d8bc7086Sdrh int unionTab; /* Cursor number of the temporary table holding result */ 1692742f947bSdanielk1977 int op = 0; /* One of the SRT_ operations to apply to self */ 1693d8bc7086Sdrh int priorOp; /* The SRT_ operation to apply to prior selects */ 1694a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ 1695dc1bdc4fSdanielk1977 int addr; 169682c3d636Sdrh 1697d8bc7086Sdrh priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; 16980342b1f5Sdrh if( eDest==priorOp && pOrderBy==0 && !p->pLimit && !p->pOffset ){ 1699d8bc7086Sdrh /* We can reuse a temporary table generated by a SELECT to our 1700c926afbcSdrh ** right. 1701d8bc7086Sdrh */ 170282c3d636Sdrh unionTab = iParm; 170382c3d636Sdrh }else{ 1704d8bc7086Sdrh /* We will need to create our own temporary table to hold the 1705d8bc7086Sdrh ** intermediate results. 1706d8bc7086Sdrh */ 170782c3d636Sdrh unionTab = pParse->nTab++; 17080342b1f5Sdrh if( pOrderBy && matchOrderbyToColumn(pParse, p, pOrderBy, unionTab,1) ){ 170984ac9d02Sdanielk1977 rc = 1; 171084ac9d02Sdanielk1977 goto multi_select_end; 1711d8bc7086Sdrh } 1712b9bb7c18Sdrh addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, unionTab, 0); 17130342b1f5Sdrh if( priorOp==SRT_Table ){ 17140342b1f5Sdrh assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); 17150342b1f5Sdrh aSetP2[nSetP2++] = addr; 17160342b1f5Sdrh }else{ 1717b9bb7c18Sdrh assert( p->addrOpenEphm[0] == -1 ); 1718b9bb7c18Sdrh p->addrOpenEphm[0] = addr; 1719b9bb7c18Sdrh p->pRightmost->usesEphm = 1; 1720dc1bdc4fSdanielk1977 } 17210342b1f5Sdrh createSortingIndex(pParse, p, pOrderBy); 172284ac9d02Sdanielk1977 assert( p->pEList ); 1723d8bc7086Sdrh } 1724d8bc7086Sdrh 1725d8bc7086Sdrh /* Code the SELECT statements to our left 1726d8bc7086Sdrh */ 1727b3bce662Sdanielk1977 assert( !pPrior->pOrderBy ); 1728b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); 172984ac9d02Sdanielk1977 if( rc ){ 173084ac9d02Sdanielk1977 goto multi_select_end; 173184ac9d02Sdanielk1977 } 1732d8bc7086Sdrh 1733d8bc7086Sdrh /* Code the current SELECT statement 1734d8bc7086Sdrh */ 1735d8bc7086Sdrh switch( p->op ){ 1736d8bc7086Sdrh case TK_EXCEPT: op = SRT_Except; break; 1737d8bc7086Sdrh case TK_UNION: op = SRT_Union; break; 1738d8bc7086Sdrh case TK_ALL: op = SRT_Table; break; 1739d8bc7086Sdrh } 174082c3d636Sdrh p->pPrior = 0; 1741c926afbcSdrh p->pOrderBy = 0; 17424b14b4d7Sdrh p->disallowOrderBy = pOrderBy!=0; 1743a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1744a2dc3b1aSdanielk1977 p->pLimit = 0; 1745a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1746a2dc3b1aSdanielk1977 p->pOffset = 0; 1747b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); 174882c3d636Sdrh p->pPrior = pPrior; 1749c926afbcSdrh p->pOrderBy = pOrderBy; 1750a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1751a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1752a2dc3b1aSdanielk1977 p->pOffset = pOffset; 1753be5fd490Sdrh p->iLimit = -1; 1754be5fd490Sdrh p->iOffset = -1; 175584ac9d02Sdanielk1977 if( rc ){ 175684ac9d02Sdanielk1977 goto multi_select_end; 175784ac9d02Sdanielk1977 } 175884ac9d02Sdanielk1977 1759d8bc7086Sdrh 1760d8bc7086Sdrh /* Convert the data in the temporary table into whatever form 1761d8bc7086Sdrh ** it is that we currently need. 1762d8bc7086Sdrh */ 1763c926afbcSdrh if( eDest!=priorOp || unionTab!=iParm ){ 17646b56344dSdrh int iCont, iBreak, iStart; 176582c3d636Sdrh assert( p->pEList ); 176641202ccaSdrh if( eDest==SRT_Callback ){ 176792378253Sdrh Select *pFirst = p; 176892378253Sdrh while( pFirst->pPrior ) pFirst = pFirst->pPrior; 176992378253Sdrh generateColumnNames(pParse, 0, pFirst->pEList); 177041202ccaSdrh } 17714adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 17724adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 1773ec7429aeSdrh computeLimitRegisters(pParse, p, iBreak); 17744adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); 17754adee20fSdanielk1977 iStart = sqlite3VdbeCurrentAddr(v); 177638640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, 17770342b1f5Sdrh pOrderBy, -1, eDest, iParm, 177884ac9d02Sdanielk1977 iCont, iBreak, 0); 177984ac9d02Sdanielk1977 if( rc ){ 178084ac9d02Sdanielk1977 rc = 1; 178184ac9d02Sdanielk1977 goto multi_select_end; 178284ac9d02Sdanielk1977 } 17834adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 17844adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); 17854adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 17864adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); 178782c3d636Sdrh } 178882c3d636Sdrh break; 178982c3d636Sdrh } 179082c3d636Sdrh case TK_INTERSECT: { 179182c3d636Sdrh int tab1, tab2; 17926b56344dSdrh int iCont, iBreak, iStart; 1793a2dc3b1aSdanielk1977 Expr *pLimit, *pOffset; 1794dc1bdc4fSdanielk1977 int addr; 179582c3d636Sdrh 1796d8bc7086Sdrh /* INTERSECT is different from the others since it requires 17976206d50aSdrh ** two temporary tables. Hence it has its own case. Begin 1798d8bc7086Sdrh ** by allocating the tables we will need. 1799d8bc7086Sdrh */ 180082c3d636Sdrh tab1 = pParse->nTab++; 180182c3d636Sdrh tab2 = pParse->nTab++; 18020342b1f5Sdrh if( pOrderBy && matchOrderbyToColumn(pParse,p,pOrderBy,tab1,1) ){ 180384ac9d02Sdanielk1977 rc = 1; 180484ac9d02Sdanielk1977 goto multi_select_end; 1805d8bc7086Sdrh } 18060342b1f5Sdrh createSortingIndex(pParse, p, pOrderBy); 1807dc1bdc4fSdanielk1977 1808b9bb7c18Sdrh addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab1, 0); 1809b9bb7c18Sdrh assert( p->addrOpenEphm[0] == -1 ); 1810b9bb7c18Sdrh p->addrOpenEphm[0] = addr; 1811b9bb7c18Sdrh p->pRightmost->usesEphm = 1; 181284ac9d02Sdanielk1977 assert( p->pEList ); 1813d8bc7086Sdrh 1814d8bc7086Sdrh /* Code the SELECTs to our left into temporary table "tab1". 1815d8bc7086Sdrh */ 1816b3bce662Sdanielk1977 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); 181784ac9d02Sdanielk1977 if( rc ){ 181884ac9d02Sdanielk1977 goto multi_select_end; 181984ac9d02Sdanielk1977 } 1820d8bc7086Sdrh 1821d8bc7086Sdrh /* Code the current SELECT into temporary table "tab2" 1822d8bc7086Sdrh */ 1823b9bb7c18Sdrh addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab2, 0); 1824b9bb7c18Sdrh assert( p->addrOpenEphm[1] == -1 ); 1825b9bb7c18Sdrh p->addrOpenEphm[1] = addr; 182682c3d636Sdrh p->pPrior = 0; 1827a2dc3b1aSdanielk1977 pLimit = p->pLimit; 1828a2dc3b1aSdanielk1977 p->pLimit = 0; 1829a2dc3b1aSdanielk1977 pOffset = p->pOffset; 1830a2dc3b1aSdanielk1977 p->pOffset = 0; 1831b3bce662Sdanielk1977 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); 183282c3d636Sdrh p->pPrior = pPrior; 1833a2dc3b1aSdanielk1977 sqlite3ExprDelete(p->pLimit); 1834a2dc3b1aSdanielk1977 p->pLimit = pLimit; 1835a2dc3b1aSdanielk1977 p->pOffset = pOffset; 183684ac9d02Sdanielk1977 if( rc ){ 183784ac9d02Sdanielk1977 goto multi_select_end; 183884ac9d02Sdanielk1977 } 1839d8bc7086Sdrh 1840d8bc7086Sdrh /* Generate code to take the intersection of the two temporary 1841d8bc7086Sdrh ** tables. 1842d8bc7086Sdrh */ 184382c3d636Sdrh assert( p->pEList ); 184441202ccaSdrh if( eDest==SRT_Callback ){ 184592378253Sdrh Select *pFirst = p; 184692378253Sdrh while( pFirst->pPrior ) pFirst = pFirst->pPrior; 184792378253Sdrh generateColumnNames(pParse, 0, pFirst->pEList); 184841202ccaSdrh } 18494adee20fSdanielk1977 iBreak = sqlite3VdbeMakeLabel(v); 18504adee20fSdanielk1977 iCont = sqlite3VdbeMakeLabel(v); 1851ec7429aeSdrh computeLimitRegisters(pParse, p, iBreak); 18524adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); 18534a9f241cSdrh iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0); 18544adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); 185538640e15Sdrh rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, 18560342b1f5Sdrh pOrderBy, -1, eDest, iParm, 185784ac9d02Sdanielk1977 iCont, iBreak, 0); 185884ac9d02Sdanielk1977 if( rc ){ 185984ac9d02Sdanielk1977 rc = 1; 186084ac9d02Sdanielk1977 goto multi_select_end; 186184ac9d02Sdanielk1977 } 18624adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iCont); 18634adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); 18644adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, iBreak); 18654adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab2, 0); 18664adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, tab1, 0); 186782c3d636Sdrh break; 186882c3d636Sdrh } 186982c3d636Sdrh } 18708cdbf836Sdrh 18718cdbf836Sdrh /* Make sure all SELECTs in the statement have the same number of elements 18728cdbf836Sdrh ** in their result sets. 18738cdbf836Sdrh */ 187482c3d636Sdrh assert( p->pEList && pPrior->pEList ); 187582c3d636Sdrh if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ 18764adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" 1877da93d238Sdrh " do not have the same number of result columns", selectOpName(p->op)); 187884ac9d02Sdanielk1977 rc = 1; 187984ac9d02Sdanielk1977 goto multi_select_end; 18802282792aSdrh } 188184ac9d02Sdanielk1977 18828cdbf836Sdrh /* Set the number of columns in temporary tables 18838cdbf836Sdrh */ 18848cdbf836Sdrh nCol = p->pEList->nExpr; 18850342b1f5Sdrh while( nSetP2 ){ 18860342b1f5Sdrh sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol); 18878cdbf836Sdrh } 18888cdbf836Sdrh 1889fbc4ee7bSdrh /* Compute collating sequences used by either the ORDER BY clause or 1890fbc4ee7bSdrh ** by any temporary tables needed to implement the compound select. 1891fbc4ee7bSdrh ** Attach the KeyInfo structure to all temporary tables. Invoke the 1892fbc4ee7bSdrh ** ORDER BY processing if there is an ORDER BY clause. 18938cdbf836Sdrh ** 18948cdbf836Sdrh ** This section is run by the right-most SELECT statement only. 18958cdbf836Sdrh ** SELECT statements to the left always skip this part. The right-most 18968cdbf836Sdrh ** SELECT might also skip this part if it has no ORDER BY clause and 18978cdbf836Sdrh ** no temp tables are required. 1898fbc4ee7bSdrh */ 1899b9bb7c18Sdrh if( pOrderBy || p->usesEphm ){ 1900fbc4ee7bSdrh int i; /* Loop counter */ 1901fbc4ee7bSdrh KeyInfo *pKeyInfo; /* Collating sequence for the result set */ 19020342b1f5Sdrh Select *pLoop; /* For looping through SELECT statements */ 19030342b1f5Sdrh CollSeq **apColl; 19040342b1f5Sdrh CollSeq **aCopy; 1905fbc4ee7bSdrh 19060342b1f5Sdrh assert( p->pRightmost==p ); 19074db38a70Sdrh pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*2*sizeof(CollSeq*) + nCol); 1908dc1bdc4fSdanielk1977 if( !pKeyInfo ){ 1909dc1bdc4fSdanielk1977 rc = SQLITE_NOMEM; 1910dc1bdc4fSdanielk1977 goto multi_select_end; 1911dc1bdc4fSdanielk1977 } 1912dc1bdc4fSdanielk1977 191314db2665Sdanielk1977 pKeyInfo->enc = ENC(pParse->db); 1914dc1bdc4fSdanielk1977 pKeyInfo->nField = nCol; 1915dc1bdc4fSdanielk1977 19160342b1f5Sdrh for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){ 19170342b1f5Sdrh *apColl = multiSelectCollSeq(pParse, p, i); 19180342b1f5Sdrh if( 0==*apColl ){ 19190342b1f5Sdrh *apColl = pParse->db->pDfltColl; 1920dc1bdc4fSdanielk1977 } 1921dc1bdc4fSdanielk1977 } 1922dc1bdc4fSdanielk1977 19230342b1f5Sdrh for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ 19240342b1f5Sdrh for(i=0; i<2; i++){ 1925b9bb7c18Sdrh int addr = pLoop->addrOpenEphm[i]; 19260342b1f5Sdrh if( addr<0 ){ 19270342b1f5Sdrh /* If [0] is unused then [1] is also unused. So we can 19280342b1f5Sdrh ** always safely abort as soon as the first unused slot is found */ 1929b9bb7c18Sdrh assert( pLoop->addrOpenEphm[1]<0 ); 19300342b1f5Sdrh break; 19310342b1f5Sdrh } 19320342b1f5Sdrh sqlite3VdbeChangeP2(v, addr, nCol); 19330342b1f5Sdrh sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO); 19340342b1f5Sdrh } 1935dc1bdc4fSdanielk1977 } 1936dc1bdc4fSdanielk1977 19370342b1f5Sdrh if( pOrderBy ){ 19380342b1f5Sdrh struct ExprList_item *pOTerm = pOrderBy->a; 19394efc083fSdrh int nOrderByExpr = pOrderBy->nExpr; 19400342b1f5Sdrh int addr; 19414db38a70Sdrh u8 *pSortOrder; 19420342b1f5Sdrh 1943a86a5b6cSdrh aCopy = &pKeyInfo->aColl[nCol]; 19444efc083fSdrh pSortOrder = pKeyInfo->aSortOrder = (u8*)&aCopy[nCol]; 19450342b1f5Sdrh memcpy(aCopy, pKeyInfo->aColl, nCol*sizeof(CollSeq*)); 19460342b1f5Sdrh apColl = pKeyInfo->aColl; 19474efc083fSdrh for(i=0; i<nOrderByExpr; i++, pOTerm++, apColl++, pSortOrder++){ 19480342b1f5Sdrh Expr *pExpr = pOTerm->pExpr; 19490342b1f5Sdrh char *zName = pOTerm->zName; 1950dc1bdc4fSdanielk1977 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); 1951dc1bdc4fSdanielk1977 if( zName ){ 19520342b1f5Sdrh *apColl = sqlite3LocateCollSeq(pParse, zName, -1); 195384ac9d02Sdanielk1977 }else{ 19540342b1f5Sdrh *apColl = aCopy[pExpr->iColumn]; 195584ac9d02Sdanielk1977 } 19564db38a70Sdrh *pSortOrder = pOTerm->sortOrder; 195784ac9d02Sdanielk1977 } 19580342b1f5Sdrh assert( p->pRightmost==p ); 1959b9bb7c18Sdrh assert( p->addrOpenEphm[2]>=0 ); 1960b9bb7c18Sdrh addr = p->addrOpenEphm[2]; 19614db38a70Sdrh sqlite3VdbeChangeP2(v, addr, p->pEList->nExpr+2); 19624efc083fSdrh pKeyInfo->nField = nOrderByExpr; 19634db38a70Sdrh sqlite3VdbeChangeP3(v, addr, (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 19644db38a70Sdrh pKeyInfo = 0; 1965cdd536f0Sdrh generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); 1966dc1bdc4fSdanielk1977 } 1967dc1bdc4fSdanielk1977 1968dc1bdc4fSdanielk1977 sqliteFree(pKeyInfo); 1969dc1bdc4fSdanielk1977 } 1970dc1bdc4fSdanielk1977 1971dc1bdc4fSdanielk1977 multi_select_end: 197284ac9d02Sdanielk1977 return rc; 19732282792aSdrh } 1974b7f9164eSdrh #endif /* SQLITE_OMIT_COMPOUND_SELECT */ 19752282792aSdrh 1976b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 19772282792aSdrh /* 1978832508b7Sdrh ** Scan through the expression pExpr. Replace every reference to 19796a3ea0e6Sdrh ** a column in table number iTable with a copy of the iColumn-th 198084e59207Sdrh ** entry in pEList. (But leave references to the ROWID column 19816a3ea0e6Sdrh ** unchanged.) 1982832508b7Sdrh ** 1983832508b7Sdrh ** This routine is part of the flattening procedure. A subquery 1984832508b7Sdrh ** whose result set is defined by pEList appears as entry in the 1985832508b7Sdrh ** FROM clause of a SELECT such that the VDBE cursor assigned to that 1986832508b7Sdrh ** FORM clause entry is iTable. This routine make the necessary 1987832508b7Sdrh ** changes to pExpr so that it refers directly to the source table 1988832508b7Sdrh ** of the subquery rather the result set of the subquery. 1989832508b7Sdrh */ 19906a3ea0e6Sdrh static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ 1991b3bce662Sdanielk1977 static void substSelect(Select *, int, ExprList *); /* Forward Decl */ 19926a3ea0e6Sdrh static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ 1993832508b7Sdrh if( pExpr==0 ) return; 199450350a15Sdrh if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ 199550350a15Sdrh if( pExpr->iColumn<0 ){ 199650350a15Sdrh pExpr->op = TK_NULL; 199750350a15Sdrh }else{ 1998832508b7Sdrh Expr *pNew; 199984e59207Sdrh assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); 2000832508b7Sdrh assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); 2001832508b7Sdrh pNew = pEList->a[pExpr->iColumn].pExpr; 2002832508b7Sdrh assert( pNew!=0 ); 2003832508b7Sdrh pExpr->op = pNew->op; 2004d94a6698Sdrh assert( pExpr->pLeft==0 ); 20054adee20fSdanielk1977 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); 2006d94a6698Sdrh assert( pExpr->pRight==0 ); 20074adee20fSdanielk1977 pExpr->pRight = sqlite3ExprDup(pNew->pRight); 2008d94a6698Sdrh assert( pExpr->pList==0 ); 20094adee20fSdanielk1977 pExpr->pList = sqlite3ExprListDup(pNew->pList); 2010832508b7Sdrh pExpr->iTable = pNew->iTable; 2011*fbbe005aSdanielk1977 pExpr->pTab = pNew->pTab; 2012832508b7Sdrh pExpr->iColumn = pNew->iColumn; 2013832508b7Sdrh pExpr->iAgg = pNew->iAgg; 20144adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->token, &pNew->token); 20154adee20fSdanielk1977 sqlite3TokenCopy(&pExpr->span, &pNew->span); 2016a1cb183dSdanielk1977 pExpr->pSelect = sqlite3SelectDup(pNew->pSelect); 2017a1cb183dSdanielk1977 pExpr->flags = pNew->flags; 201850350a15Sdrh } 2019832508b7Sdrh }else{ 20206a3ea0e6Sdrh substExpr(pExpr->pLeft, iTable, pEList); 20216a3ea0e6Sdrh substExpr(pExpr->pRight, iTable, pEList); 2022b3bce662Sdanielk1977 substSelect(pExpr->pSelect, iTable, pEList); 20236a3ea0e6Sdrh substExprList(pExpr->pList, iTable, pEList); 2024832508b7Sdrh } 2025832508b7Sdrh } 2026b3bce662Sdanielk1977 static void substExprList(ExprList *pList, int iTable, ExprList *pEList){ 2027832508b7Sdrh int i; 2028832508b7Sdrh if( pList==0 ) return; 2029832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 20306a3ea0e6Sdrh substExpr(pList->a[i].pExpr, iTable, pEList); 2031832508b7Sdrh } 2032832508b7Sdrh } 2033b3bce662Sdanielk1977 static void substSelect(Select *p, int iTable, ExprList *pEList){ 2034b3bce662Sdanielk1977 if( !p ) return; 2035b3bce662Sdanielk1977 substExprList(p->pEList, iTable, pEList); 2036b3bce662Sdanielk1977 substExprList(p->pGroupBy, iTable, pEList); 2037b3bce662Sdanielk1977 substExprList(p->pOrderBy, iTable, pEList); 2038b3bce662Sdanielk1977 substExpr(p->pHaving, iTable, pEList); 2039b3bce662Sdanielk1977 substExpr(p->pWhere, iTable, pEList); 2040b3bce662Sdanielk1977 } 2041b7f9164eSdrh #endif /* !defined(SQLITE_OMIT_VIEW) */ 2042832508b7Sdrh 2043b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 2044832508b7Sdrh /* 20451350b030Sdrh ** This routine attempts to flatten subqueries in order to speed 20461350b030Sdrh ** execution. It returns 1 if it makes changes and 0 if no flattening 20471350b030Sdrh ** occurs. 20481350b030Sdrh ** 20491350b030Sdrh ** To understand the concept of flattening, consider the following 20501350b030Sdrh ** query: 20511350b030Sdrh ** 20521350b030Sdrh ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 20531350b030Sdrh ** 20541350b030Sdrh ** The default way of implementing this query is to execute the 20551350b030Sdrh ** subquery first and store the results in a temporary table, then 20561350b030Sdrh ** run the outer query on that temporary table. This requires two 20571350b030Sdrh ** passes over the data. Furthermore, because the temporary table 20581350b030Sdrh ** has no indices, the WHERE clause on the outer query cannot be 2059832508b7Sdrh ** optimized. 20601350b030Sdrh ** 2061832508b7Sdrh ** This routine attempts to rewrite queries such as the above into 20621350b030Sdrh ** a single flat select, like this: 20631350b030Sdrh ** 20641350b030Sdrh ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 20651350b030Sdrh ** 20661350b030Sdrh ** The code generated for this simpification gives the same result 2067832508b7Sdrh ** but only has to scan the data once. And because indices might 2068832508b7Sdrh ** exist on the table t1, a complete scan of the data might be 2069832508b7Sdrh ** avoided. 20701350b030Sdrh ** 2071832508b7Sdrh ** Flattening is only attempted if all of the following are true: 20721350b030Sdrh ** 2073832508b7Sdrh ** (1) The subquery and the outer query do not both use aggregates. 20741350b030Sdrh ** 2075832508b7Sdrh ** (2) The subquery is not an aggregate or the outer query is not a join. 2076832508b7Sdrh ** 20778af4d3acSdrh ** (3) The subquery is not the right operand of a left outer join, or 20788af4d3acSdrh ** the subquery is not itself a join. (Ticket #306) 2079832508b7Sdrh ** 2080832508b7Sdrh ** (4) The subquery is not DISTINCT or the outer query is not a join. 2081832508b7Sdrh ** 2082832508b7Sdrh ** (5) The subquery is not DISTINCT or the outer query does not use 2083832508b7Sdrh ** aggregates. 2084832508b7Sdrh ** 2085832508b7Sdrh ** (6) The subquery does not use aggregates or the outer query is not 2086832508b7Sdrh ** DISTINCT. 2087832508b7Sdrh ** 208808192d5fSdrh ** (7) The subquery has a FROM clause. 208908192d5fSdrh ** 2090df199a25Sdrh ** (8) The subquery does not use LIMIT or the outer query is not a join. 2091df199a25Sdrh ** 2092df199a25Sdrh ** (9) The subquery does not use LIMIT or the outer query does not use 2093df199a25Sdrh ** aggregates. 2094df199a25Sdrh ** 2095df199a25Sdrh ** (10) The subquery does not use aggregates or the outer query does not 2096df199a25Sdrh ** use LIMIT. 2097df199a25Sdrh ** 2098174b6195Sdrh ** (11) The subquery and the outer query do not both have ORDER BY clauses. 2099174b6195Sdrh ** 21003fc673e6Sdrh ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the 21013fc673e6Sdrh ** subquery has no WHERE clause. (added by ticket #350) 21023fc673e6Sdrh ** 2103ac83963aSdrh ** (13) The subquery and outer query do not both use LIMIT 2104ac83963aSdrh ** 2105ac83963aSdrh ** (14) The subquery does not use OFFSET 2106ac83963aSdrh ** 2107832508b7Sdrh ** In this routine, the "p" parameter is a pointer to the outer query. 2108832508b7Sdrh ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query 2109832508b7Sdrh ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. 2110832508b7Sdrh ** 2111665de47aSdrh ** If flattening is not attempted, this routine is a no-op and returns 0. 2112832508b7Sdrh ** If flattening is attempted this routine returns 1. 2113832508b7Sdrh ** 2114832508b7Sdrh ** All of the expression analysis must occur on both the outer query and 2115832508b7Sdrh ** the subquery before this routine runs. 21161350b030Sdrh */ 21178c74a8caSdrh static int flattenSubquery( 21188c74a8caSdrh Select *p, /* The parent or outer SELECT statement */ 21198c74a8caSdrh int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ 21208c74a8caSdrh int isAgg, /* True if outer SELECT uses aggregate functions */ 21218c74a8caSdrh int subqueryIsAgg /* True if the subquery uses aggregate functions */ 21228c74a8caSdrh ){ 21230bb28106Sdrh Select *pSub; /* The inner query or "subquery" */ 2124ad3cab52Sdrh SrcList *pSrc; /* The FROM clause of the outer query */ 2125ad3cab52Sdrh SrcList *pSubSrc; /* The FROM clause of the subquery */ 21260bb28106Sdrh ExprList *pList; /* The result set of the outer query */ 21276a3ea0e6Sdrh int iParent; /* VDBE cursor number of the pSub result set temp table */ 212891bb0eedSdrh int i; /* Loop counter */ 212991bb0eedSdrh Expr *pWhere; /* The WHERE clause */ 213091bb0eedSdrh struct SrcList_item *pSubitem; /* The subquery */ 21311350b030Sdrh 2132832508b7Sdrh /* Check to see if flattening is permitted. Return 0 if not. 2133832508b7Sdrh */ 2134832508b7Sdrh if( p==0 ) return 0; 2135832508b7Sdrh pSrc = p->pSrc; 2136ad3cab52Sdrh assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); 213791bb0eedSdrh pSubitem = &pSrc->a[iFrom]; 213891bb0eedSdrh pSub = pSubitem->pSelect; 2139832508b7Sdrh assert( pSub!=0 ); 2140ac83963aSdrh if( isAgg && subqueryIsAgg ) return 0; /* Restriction (1) */ 2141ac83963aSdrh if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; /* Restriction (2) */ 2142832508b7Sdrh pSubSrc = pSub->pSrc; 2143832508b7Sdrh assert( pSubSrc ); 2144ac83963aSdrh /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, 2145ac83963aSdrh ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET 2146ac83963aSdrh ** because they could be computed at compile-time. But when LIMIT and OFFSET 2147ac83963aSdrh ** became arbitrary expressions, we were forced to add restrictions (13) 2148ac83963aSdrh ** and (14). */ 2149ac83963aSdrh if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ 2150ac83963aSdrh if( pSub->pOffset ) return 0; /* Restriction (14) */ 2151ac83963aSdrh if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ 2152ac83963aSdrh if( (pSub->isDistinct || pSub->pLimit) 2153ac83963aSdrh && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */ 2154df199a25Sdrh return 0; 2155df199a25Sdrh } 2156ac83963aSdrh if( p->isDistinct && subqueryIsAgg ) return 0; /* Restriction (6) */ 2157ac83963aSdrh if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){ 2158ac83963aSdrh return 0; /* Restriction (11) */ 2159ac83963aSdrh } 2160832508b7Sdrh 21618af4d3acSdrh /* Restriction 3: If the subquery is a join, make sure the subquery is 21628af4d3acSdrh ** not used as the right operand of an outer join. Examples of why this 21638af4d3acSdrh ** is not allowed: 21648af4d3acSdrh ** 21658af4d3acSdrh ** t1 LEFT OUTER JOIN (t2 JOIN t3) 21668af4d3acSdrh ** 21678af4d3acSdrh ** If we flatten the above, we would get 21688af4d3acSdrh ** 21698af4d3acSdrh ** (t1 LEFT OUTER JOIN t2) JOIN t3 21708af4d3acSdrh ** 21718af4d3acSdrh ** which is not at all the same thing. 21728af4d3acSdrh */ 21738af4d3acSdrh if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ 21748af4d3acSdrh return 0; 21758af4d3acSdrh } 21768af4d3acSdrh 21773fc673e6Sdrh /* Restriction 12: If the subquery is the right operand of a left outer 21783fc673e6Sdrh ** join, make sure the subquery has no WHERE clause. 21793fc673e6Sdrh ** An examples of why this is not allowed: 21803fc673e6Sdrh ** 21813fc673e6Sdrh ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) 21823fc673e6Sdrh ** 21833fc673e6Sdrh ** If we flatten the above, we would get 21843fc673e6Sdrh ** 21853fc673e6Sdrh ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 21863fc673e6Sdrh ** 21873fc673e6Sdrh ** But the t2.x>0 test will always fail on a NULL row of t2, which 21883fc673e6Sdrh ** effectively converts the OUTER JOIN into an INNER JOIN. 21893fc673e6Sdrh */ 21903fc673e6Sdrh if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 21913fc673e6Sdrh && pSub->pWhere!=0 ){ 21923fc673e6Sdrh return 0; 21933fc673e6Sdrh } 21943fc673e6Sdrh 21950bb28106Sdrh /* If we reach this point, it means flattening is permitted for the 219663eb5f29Sdrh ** iFrom-th entry of the FROM clause in the outer query. 2197832508b7Sdrh */ 2198c31c2eb8Sdrh 2199c31c2eb8Sdrh /* Move all of the FROM elements of the subquery into the 2200c31c2eb8Sdrh ** the FROM clause of the outer query. Before doing this, remember 2201c31c2eb8Sdrh ** the cursor number for the original outer query FROM element in 2202c31c2eb8Sdrh ** iParent. The iParent cursor will never be used. Subsequent code 2203c31c2eb8Sdrh ** will scan expressions looking for iParent references and replace 2204c31c2eb8Sdrh ** those references with expressions that resolve to the subquery FROM 2205c31c2eb8Sdrh ** elements we are now copying in. 2206c31c2eb8Sdrh */ 220791bb0eedSdrh iParent = pSubitem->iCursor; 2208c31c2eb8Sdrh { 2209c31c2eb8Sdrh int nSubSrc = pSubSrc->nSrc; 221091bb0eedSdrh int jointype = pSubitem->jointype; 2211c31c2eb8Sdrh 221291bb0eedSdrh sqlite3DeleteTable(0, pSubitem->pTab); 221391bb0eedSdrh sqliteFree(pSubitem->zDatabase); 221491bb0eedSdrh sqliteFree(pSubitem->zName); 221591bb0eedSdrh sqliteFree(pSubitem->zAlias); 2216c31c2eb8Sdrh if( nSubSrc>1 ){ 2217c31c2eb8Sdrh int extra = nSubSrc - 1; 2218c31c2eb8Sdrh for(i=1; i<nSubSrc; i++){ 22194adee20fSdanielk1977 pSrc = sqlite3SrcListAppend(pSrc, 0, 0); 2220c31c2eb8Sdrh } 2221c31c2eb8Sdrh p->pSrc = pSrc; 2222c31c2eb8Sdrh for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ 2223c31c2eb8Sdrh pSrc->a[i] = pSrc->a[i-extra]; 2224c31c2eb8Sdrh } 2225c31c2eb8Sdrh } 2226c31c2eb8Sdrh for(i=0; i<nSubSrc; i++){ 2227c31c2eb8Sdrh pSrc->a[i+iFrom] = pSubSrc->a[i]; 2228c31c2eb8Sdrh memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); 2229c31c2eb8Sdrh } 22308af4d3acSdrh pSrc->a[iFrom+nSubSrc-1].jointype = jointype; 2231c31c2eb8Sdrh } 2232c31c2eb8Sdrh 2233c31c2eb8Sdrh /* Now begin substituting subquery result set expressions for 2234c31c2eb8Sdrh ** references to the iParent in the outer query. 2235c31c2eb8Sdrh ** 2236c31c2eb8Sdrh ** Example: 2237c31c2eb8Sdrh ** 2238c31c2eb8Sdrh ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; 2239c31c2eb8Sdrh ** \ \_____________ subquery __________/ / 2240c31c2eb8Sdrh ** \_____________________ outer query ______________________________/ 2241c31c2eb8Sdrh ** 2242c31c2eb8Sdrh ** We look at every expression in the outer query and every place we see 2243c31c2eb8Sdrh ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". 2244c31c2eb8Sdrh */ 2245832508b7Sdrh pList = p->pEList; 2246832508b7Sdrh for(i=0; i<pList->nExpr; i++){ 22476977fea8Sdrh Expr *pExpr; 22486977fea8Sdrh if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ 22492646da7eSdrh pList->a[i].zName = sqliteStrNDup((char*)pExpr->span.z, pExpr->span.n); 2250832508b7Sdrh } 2251832508b7Sdrh } 2252643054c1Sdrh substExprList(p->pEList, iParent, pSub->pEList); 22531b2e0329Sdrh if( isAgg ){ 22546a3ea0e6Sdrh substExprList(p->pGroupBy, iParent, pSub->pEList); 22556a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 22561b2e0329Sdrh } 2257174b6195Sdrh if( pSub->pOrderBy ){ 2258174b6195Sdrh assert( p->pOrderBy==0 ); 2259174b6195Sdrh p->pOrderBy = pSub->pOrderBy; 2260174b6195Sdrh pSub->pOrderBy = 0; 2261174b6195Sdrh }else if( p->pOrderBy ){ 22626a3ea0e6Sdrh substExprList(p->pOrderBy, iParent, pSub->pEList); 2263174b6195Sdrh } 2264832508b7Sdrh if( pSub->pWhere ){ 22654adee20fSdanielk1977 pWhere = sqlite3ExprDup(pSub->pWhere); 2266832508b7Sdrh }else{ 2267832508b7Sdrh pWhere = 0; 2268832508b7Sdrh } 2269832508b7Sdrh if( subqueryIsAgg ){ 2270832508b7Sdrh assert( p->pHaving==0 ); 22711b2e0329Sdrh p->pHaving = p->pWhere; 22721b2e0329Sdrh p->pWhere = pWhere; 22736a3ea0e6Sdrh substExpr(p->pHaving, iParent, pSub->pEList); 227491bb0eedSdrh p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving)); 22751b2e0329Sdrh assert( p->pGroupBy==0 ); 22764adee20fSdanielk1977 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); 2277832508b7Sdrh }else{ 22786a3ea0e6Sdrh substExpr(p->pWhere, iParent, pSub->pEList); 227991bb0eedSdrh p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere); 2280832508b7Sdrh } 2281c31c2eb8Sdrh 2282c31c2eb8Sdrh /* The flattened query is distinct if either the inner or the 2283c31c2eb8Sdrh ** outer query is distinct. 2284c31c2eb8Sdrh */ 2285832508b7Sdrh p->isDistinct = p->isDistinct || pSub->isDistinct; 22868c74a8caSdrh 2287a58fdfb1Sdanielk1977 /* 2288a58fdfb1Sdanielk1977 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; 2289ac83963aSdrh ** 2290ac83963aSdrh ** One is tempted to try to add a and b to combine the limits. But this 2291ac83963aSdrh ** does not work if either limit is negative. 2292a58fdfb1Sdanielk1977 */ 2293a2dc3b1aSdanielk1977 if( pSub->pLimit ){ 2294a2dc3b1aSdanielk1977 p->pLimit = pSub->pLimit; 2295a2dc3b1aSdanielk1977 pSub->pLimit = 0; 2296df199a25Sdrh } 22978c74a8caSdrh 2298c31c2eb8Sdrh /* Finially, delete what is left of the subquery and return 2299c31c2eb8Sdrh ** success. 2300c31c2eb8Sdrh */ 23014adee20fSdanielk1977 sqlite3SelectDelete(pSub); 2302832508b7Sdrh return 1; 23031350b030Sdrh } 2304b7f9164eSdrh #endif /* SQLITE_OMIT_VIEW */ 23051350b030Sdrh 23061350b030Sdrh /* 23079562b551Sdrh ** Analyze the SELECT statement passed in as an argument to see if it 23089562b551Sdrh ** is a simple min() or max() query. If it is and this query can be 23099562b551Sdrh ** satisfied using a single seek to the beginning or end of an index, 2310e78e8284Sdrh ** then generate the code for this SELECT and return 1. If this is not a 23119562b551Sdrh ** simple min() or max() query, then return 0; 23129562b551Sdrh ** 23139562b551Sdrh ** A simply min() or max() query looks like this: 23149562b551Sdrh ** 23159562b551Sdrh ** SELECT min(a) FROM table; 23169562b551Sdrh ** SELECT max(a) FROM table; 23179562b551Sdrh ** 23189562b551Sdrh ** The query may have only a single table in its FROM argument. There 23199562b551Sdrh ** can be no GROUP BY or HAVING or WHERE clauses. The result set must 23209562b551Sdrh ** be the min() or max() of a single column of the table. The column 23219562b551Sdrh ** in the min() or max() function must be indexed. 23229562b551Sdrh ** 23234adee20fSdanielk1977 ** The parameters to this routine are the same as for sqlite3Select(). 23249562b551Sdrh ** See the header comment on that routine for additional information. 23259562b551Sdrh */ 23269562b551Sdrh static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ 23279562b551Sdrh Expr *pExpr; 23289562b551Sdrh int iCol; 23299562b551Sdrh Table *pTab; 23309562b551Sdrh Index *pIdx; 23319562b551Sdrh int base; 23329562b551Sdrh Vdbe *v; 23339562b551Sdrh int seekOp; 23346e17529eSdrh ExprList *pEList, *pList, eList; 23359562b551Sdrh struct ExprList_item eListItem; 23366e17529eSdrh SrcList *pSrc; 2337ec7429aeSdrh int brk; 2338da184236Sdanielk1977 int iDb; 23396e17529eSdrh 23409562b551Sdrh /* Check to see if this query is a simple min() or max() query. Return 23419562b551Sdrh ** zero if it is not. 23429562b551Sdrh */ 23439562b551Sdrh if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; 23446e17529eSdrh pSrc = p->pSrc; 23456e17529eSdrh if( pSrc->nSrc!=1 ) return 0; 23466e17529eSdrh pEList = p->pEList; 23476e17529eSdrh if( pEList->nExpr!=1 ) return 0; 23486e17529eSdrh pExpr = pEList->a[0].pExpr; 23499562b551Sdrh if( pExpr->op!=TK_AGG_FUNCTION ) return 0; 23506e17529eSdrh pList = pExpr->pList; 23516e17529eSdrh if( pList==0 || pList->nExpr!=1 ) return 0; 23526977fea8Sdrh if( pExpr->token.n!=3 ) return 0; 23532646da7eSdrh if( sqlite3StrNICmp((char*)pExpr->token.z,"min",3)==0 ){ 23540bce8354Sdrh seekOp = OP_Rewind; 23552646da7eSdrh }else if( sqlite3StrNICmp((char*)pExpr->token.z,"max",3)==0 ){ 23560bce8354Sdrh seekOp = OP_Last; 23570bce8354Sdrh }else{ 23580bce8354Sdrh return 0; 23590bce8354Sdrh } 23606e17529eSdrh pExpr = pList->a[0].pExpr; 23619562b551Sdrh if( pExpr->op!=TK_COLUMN ) return 0; 23629562b551Sdrh iCol = pExpr->iColumn; 23636e17529eSdrh pTab = pSrc->a[0].pTab; 23649562b551Sdrh 2365c00da105Sdanielk1977 23669562b551Sdrh /* If we get to here, it means the query is of the correct form. 236717f71934Sdrh ** Check to make sure we have an index and make pIdx point to the 236817f71934Sdrh ** appropriate index. If the min() or max() is on an INTEGER PRIMARY 236917f71934Sdrh ** key column, no index is necessary so set pIdx to NULL. If no 237017f71934Sdrh ** usable index is found, return 0. 23719562b551Sdrh */ 23729562b551Sdrh if( iCol<0 ){ 23739562b551Sdrh pIdx = 0; 23749562b551Sdrh }else{ 2375dc1bdc4fSdanielk1977 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 23769562b551Sdrh for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ 23779562b551Sdrh assert( pIdx->nColumn>=1 ); 2378b3bf556eSdanielk1977 if( pIdx->aiColumn[0]==iCol && 2379b3bf556eSdanielk1977 0==sqlite3StrICmp(pIdx->azColl[0], pColl->zName) ){ 2380b3bf556eSdanielk1977 break; 2381b3bf556eSdanielk1977 } 23829562b551Sdrh } 23839562b551Sdrh if( pIdx==0 ) return 0; 23849562b551Sdrh } 23859562b551Sdrh 2386e5f50722Sdrh /* Identify column types if we will be using the callback. This 23879562b551Sdrh ** step is skipped if the output is going to a table or a memory cell. 2388e5f50722Sdrh ** The column names have already been generated in the calling function. 23899562b551Sdrh */ 23904adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 23919562b551Sdrh if( v==0 ) return 0; 23929562b551Sdrh 23930c37e630Sdrh /* If the output is destined for a temporary table, open that table. 23940c37e630Sdrh */ 2395b9bb7c18Sdrh if( eDest==SRT_EphemTab ){ 2396b9bb7c18Sdrh sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 1); 23970c37e630Sdrh } 23980c37e630Sdrh 239917f71934Sdrh /* Generating code to find the min or the max. Basically all we have 240017f71934Sdrh ** to do is find the first or the last entry in the chosen index. If 240117f71934Sdrh ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first 240217f71934Sdrh ** or last entry in the main table. 24039562b551Sdrh */ 2404da184236Sdanielk1977 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); 2405b9bb7c18Sdrh assert( iDb>=0 || pTab->isEphem ); 2406da184236Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 2407c00da105Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 24086e17529eSdrh base = pSrc->a[0].iCursor; 2409ec7429aeSdrh brk = sqlite3VdbeMakeLabel(v); 2410ec7429aeSdrh computeLimitRegisters(pParse, p, brk); 24116e17529eSdrh if( pSrc->a[0].pSelect==0 ){ 2412c00da105Sdanielk1977 sqlite3OpenTable(pParse, base, iDb, pTab, OP_OpenRead); 24136e17529eSdrh } 24149562b551Sdrh if( pIdx==0 ){ 24154adee20fSdanielk1977 sqlite3VdbeAddOp(v, seekOp, base, 0); 24169562b551Sdrh }else{ 24173719d7f9Sdanielk1977 /* Even though the cursor used to open the index here is closed 24183719d7f9Sdanielk1977 ** as soon as a single value has been read from it, allocate it 24193719d7f9Sdanielk1977 ** using (pParse->nTab++) to prevent the cursor id from being 24203719d7f9Sdanielk1977 ** reused. This is important for statements of the form 24213719d7f9Sdanielk1977 ** "INSERT INTO x SELECT max() FROM x". 24223719d7f9Sdanielk1977 */ 24233719d7f9Sdanielk1977 int iIdx; 2424b3bf556eSdanielk1977 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); 24253719d7f9Sdanielk1977 iIdx = pParse->nTab++; 2426da184236Sdanielk1977 assert( pIdx->pSchema==pTab->pSchema ); 2427da184236Sdanielk1977 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0); 24283719d7f9Sdanielk1977 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum, 2429b3bf556eSdanielk1977 (char*)pKey, P3_KEYINFO_HANDOFF); 24309eb516c0Sdrh if( seekOp==OP_Rewind ){ 2431f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_Null, 0, 0); 24321af3fdb4Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); 24331af3fdb4Sdrh seekOp = OP_MoveGt; 24349eb516c0Sdrh } 24353719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, seekOp, iIdx, 0); 2436f0863fe5Sdrh sqlite3VdbeAddOp(v, OP_IdxRowid, iIdx, 0); 24373719d7f9Sdanielk1977 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0); 24387cf6e4deSdrh sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); 24399562b551Sdrh } 24405cf8e8c7Sdrh eList.nExpr = 1; 24415cf8e8c7Sdrh memset(&eListItem, 0, sizeof(eListItem)); 24425cf8e8c7Sdrh eList.a = &eListItem; 24435cf8e8c7Sdrh eList.a[0].pExpr = pExpr; 2444ec7429aeSdrh selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, brk, brk, 0); 2445ec7429aeSdrh sqlite3VdbeResolveLabel(v, brk); 24464adee20fSdanielk1977 sqlite3VdbeAddOp(v, OP_Close, base, 0); 24476e17529eSdrh 24489562b551Sdrh return 1; 24499562b551Sdrh } 24509562b551Sdrh 24519562b551Sdrh /* 2452290c1948Sdrh ** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return 2453290c1948Sdrh ** the number of errors seen. 2454290c1948Sdrh ** 2455290c1948Sdrh ** An ORDER BY or GROUP BY is a list of expressions. If any expression 2456290c1948Sdrh ** is an integer constant, then that expression is replaced by the 2457290c1948Sdrh ** corresponding entry in the result set. 2458290c1948Sdrh */ 2459290c1948Sdrh static int processOrderGroupBy( 2460b3bce662Sdanielk1977 NameContext *pNC, /* Name context of the SELECT statement. */ 2461290c1948Sdrh ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ 2462290c1948Sdrh const char *zType /* Either "ORDER" or "GROUP", as appropriate */ 2463290c1948Sdrh ){ 2464290c1948Sdrh int i; 2465b3bce662Sdanielk1977 ExprList *pEList = pNC->pEList; /* The result set of the SELECT */ 2466b3bce662Sdanielk1977 Parse *pParse = pNC->pParse; /* The result set of the SELECT */ 2467b3bce662Sdanielk1977 assert( pEList ); 2468b3bce662Sdanielk1977 2469290c1948Sdrh if( pOrderBy==0 ) return 0; 2470290c1948Sdrh for(i=0; i<pOrderBy->nExpr; i++){ 2471290c1948Sdrh int iCol; 2472290c1948Sdrh Expr *pE = pOrderBy->a[i].pExpr; 2473b3bce662Sdanielk1977 if( sqlite3ExprIsInteger(pE, &iCol) ){ 2474b3bce662Sdanielk1977 if( iCol>0 && iCol<=pEList->nExpr ){ 2475290c1948Sdrh sqlite3ExprDelete(pE); 2476290c1948Sdrh pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); 2477b3bce662Sdanielk1977 }else{ 2478290c1948Sdrh sqlite3ErrorMsg(pParse, 2479290c1948Sdrh "%s BY column number %d out of range - should be " 2480290c1948Sdrh "between 1 and %d", zType, iCol, pEList->nExpr); 2481290c1948Sdrh return 1; 2482290c1948Sdrh } 2483290c1948Sdrh } 2484b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(pNC, pE) ){ 2485b3bce662Sdanielk1977 return 1; 2486b3bce662Sdanielk1977 } 2487290c1948Sdrh } 2488290c1948Sdrh return 0; 2489290c1948Sdrh } 2490290c1948Sdrh 2491290c1948Sdrh /* 2492b3bce662Sdanielk1977 ** This routine resolves any names used in the result set of the 2493b3bce662Sdanielk1977 ** supplied SELECT statement. If the SELECT statement being resolved 2494b3bce662Sdanielk1977 ** is a sub-select, then pOuterNC is a pointer to the NameContext 2495b3bce662Sdanielk1977 ** of the parent SELECT. 2496b3bce662Sdanielk1977 */ 2497b3bce662Sdanielk1977 int sqlite3SelectResolve( 2498b3bce662Sdanielk1977 Parse *pParse, /* The parser context */ 2499b3bce662Sdanielk1977 Select *p, /* The SELECT statement being coded. */ 2500b3bce662Sdanielk1977 NameContext *pOuterNC /* The outer name context. May be NULL. */ 2501b3bce662Sdanielk1977 ){ 2502b3bce662Sdanielk1977 ExprList *pEList; /* Result set. */ 2503b3bce662Sdanielk1977 int i; /* For-loop variable used in multiple places */ 2504b3bce662Sdanielk1977 NameContext sNC; /* Local name-context */ 250513449892Sdrh ExprList *pGroupBy; /* The group by clause */ 2506b3bce662Sdanielk1977 2507b3bce662Sdanielk1977 /* If this routine has run before, return immediately. */ 2508b3bce662Sdanielk1977 if( p->isResolved ){ 2509b3bce662Sdanielk1977 assert( !pOuterNC ); 2510b3bce662Sdanielk1977 return SQLITE_OK; 2511b3bce662Sdanielk1977 } 2512b3bce662Sdanielk1977 p->isResolved = 1; 2513b3bce662Sdanielk1977 2514b3bce662Sdanielk1977 /* If there have already been errors, do nothing. */ 2515b3bce662Sdanielk1977 if( pParse->nErr>0 ){ 2516b3bce662Sdanielk1977 return SQLITE_ERROR; 2517b3bce662Sdanielk1977 } 2518b3bce662Sdanielk1977 2519b3bce662Sdanielk1977 /* Prepare the select statement. This call will allocate all cursors 2520b3bce662Sdanielk1977 ** required to handle the tables and subqueries in the FROM clause. 2521b3bce662Sdanielk1977 */ 2522b3bce662Sdanielk1977 if( prepSelectStmt(pParse, p) ){ 2523b3bce662Sdanielk1977 return SQLITE_ERROR; 2524b3bce662Sdanielk1977 } 2525b3bce662Sdanielk1977 2526a2dc3b1aSdanielk1977 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 2527a2dc3b1aSdanielk1977 ** are not allowed to refer to any names, so pass an empty NameContext. 2528a2dc3b1aSdanielk1977 */ 2529ffe07b2dSdrh memset(&sNC, 0, sizeof(sNC)); 2530b3bce662Sdanielk1977 sNC.pParse = pParse; 2531a2dc3b1aSdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pLimit) || 2532a2dc3b1aSdanielk1977 sqlite3ExprResolveNames(&sNC, p->pOffset) ){ 2533a2dc3b1aSdanielk1977 return SQLITE_ERROR; 2534a2dc3b1aSdanielk1977 } 2535a2dc3b1aSdanielk1977 2536a2dc3b1aSdanielk1977 /* Set up the local name-context to pass to ExprResolveNames() to 2537a2dc3b1aSdanielk1977 ** resolve the expression-list. 2538a2dc3b1aSdanielk1977 */ 2539a2dc3b1aSdanielk1977 sNC.allowAgg = 1; 2540a2dc3b1aSdanielk1977 sNC.pSrcList = p->pSrc; 2541a2dc3b1aSdanielk1977 sNC.pNext = pOuterNC; 2542b3bce662Sdanielk1977 2543b3bce662Sdanielk1977 /* Resolve names in the result set. */ 2544b3bce662Sdanielk1977 pEList = p->pEList; 2545b3bce662Sdanielk1977 if( !pEList ) return SQLITE_ERROR; 2546b3bce662Sdanielk1977 for(i=0; i<pEList->nExpr; i++){ 2547b3bce662Sdanielk1977 Expr *pX = pEList->a[i].pExpr; 2548b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, pX) ){ 2549b3bce662Sdanielk1977 return SQLITE_ERROR; 2550b3bce662Sdanielk1977 } 2551b3bce662Sdanielk1977 } 2552b3bce662Sdanielk1977 2553b3bce662Sdanielk1977 /* If there are no aggregate functions in the result-set, and no GROUP BY 2554b3bce662Sdanielk1977 ** expression, do not allow aggregates in any of the other expressions. 2555b3bce662Sdanielk1977 */ 2556b3bce662Sdanielk1977 assert( !p->isAgg ); 255713449892Sdrh pGroupBy = p->pGroupBy; 255813449892Sdrh if( pGroupBy || sNC.hasAgg ){ 2559b3bce662Sdanielk1977 p->isAgg = 1; 2560b3bce662Sdanielk1977 }else{ 2561b3bce662Sdanielk1977 sNC.allowAgg = 0; 2562b3bce662Sdanielk1977 } 2563b3bce662Sdanielk1977 2564b3bce662Sdanielk1977 /* If a HAVING clause is present, then there must be a GROUP BY clause. 2565b3bce662Sdanielk1977 */ 256613449892Sdrh if( p->pHaving && !pGroupBy ){ 2567b3bce662Sdanielk1977 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); 2568b3bce662Sdanielk1977 return SQLITE_ERROR; 2569b3bce662Sdanielk1977 } 2570b3bce662Sdanielk1977 2571b3bce662Sdanielk1977 /* Add the expression list to the name-context before parsing the 2572b3bce662Sdanielk1977 ** other expressions in the SELECT statement. This is so that 2573b3bce662Sdanielk1977 ** expressions in the WHERE clause (etc.) can refer to expressions by 2574b3bce662Sdanielk1977 ** aliases in the result set. 2575b3bce662Sdanielk1977 ** 2576b3bce662Sdanielk1977 ** Minor point: If this is the case, then the expression will be 2577b3bce662Sdanielk1977 ** re-evaluated for each reference to it. 2578b3bce662Sdanielk1977 */ 2579b3bce662Sdanielk1977 sNC.pEList = p->pEList; 2580b3bce662Sdanielk1977 if( sqlite3ExprResolveNames(&sNC, p->pWhere) || 2581b3bce662Sdanielk1977 sqlite3ExprResolveNames(&sNC, p->pHaving) || 2582b3bce662Sdanielk1977 processOrderGroupBy(&sNC, p->pOrderBy, "ORDER") || 258313449892Sdrh processOrderGroupBy(&sNC, pGroupBy, "GROUP") 2584b3bce662Sdanielk1977 ){ 2585b3bce662Sdanielk1977 return SQLITE_ERROR; 2586b3bce662Sdanielk1977 } 2587b3bce662Sdanielk1977 258813449892Sdrh /* Make sure the GROUP BY clause does not contain aggregate functions. 258913449892Sdrh */ 259013449892Sdrh if( pGroupBy ){ 259113449892Sdrh struct ExprList_item *pItem; 259213449892Sdrh 259313449892Sdrh for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ 259413449892Sdrh if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ 259513449892Sdrh sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " 259613449892Sdrh "the GROUP BY clause"); 259713449892Sdrh return SQLITE_ERROR; 259813449892Sdrh } 259913449892Sdrh } 260013449892Sdrh } 260113449892Sdrh 2602b3bce662Sdanielk1977 return SQLITE_OK; 2603b3bce662Sdanielk1977 } 2604b3bce662Sdanielk1977 2605b3bce662Sdanielk1977 /* 260613449892Sdrh ** Reset the aggregate accumulator. 260713449892Sdrh ** 260813449892Sdrh ** The aggregate accumulator is a set of memory cells that hold 260913449892Sdrh ** intermediate results while calculating an aggregate. This 261013449892Sdrh ** routine simply stores NULLs in all of those memory cells. 2611b3bce662Sdanielk1977 */ 261213449892Sdrh static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ 261313449892Sdrh Vdbe *v = pParse->pVdbe; 261413449892Sdrh int i; 2615c99130fdSdrh struct AggInfo_func *pFunc; 261613449892Sdrh if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){ 261713449892Sdrh return; 261813449892Sdrh } 261913449892Sdrh for(i=0; i<pAggInfo->nColumn; i++){ 2620d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemNull, pAggInfo->aCol[i].iMem, 0); 262113449892Sdrh } 2622c99130fdSdrh for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){ 2623d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemNull, pFunc->iMem, 0); 2624c99130fdSdrh if( pFunc->iDistinct>=0 ){ 2625c99130fdSdrh Expr *pE = pFunc->pExpr; 2626c99130fdSdrh if( pE->pList==0 || pE->pList->nExpr!=1 ){ 2627c99130fdSdrh sqlite3ErrorMsg(pParse, "DISTINCT in aggregate must be followed " 2628c99130fdSdrh "by an expression"); 2629c99130fdSdrh pFunc->iDistinct = -1; 2630c99130fdSdrh }else{ 2631c99130fdSdrh KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->pList); 2632b9bb7c18Sdrh sqlite3VdbeOp3(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 2633c99130fdSdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 2634c99130fdSdrh } 2635c99130fdSdrh } 263613449892Sdrh } 2637b3bce662Sdanielk1977 } 2638b3bce662Sdanielk1977 2639b3bce662Sdanielk1977 /* 264013449892Sdrh ** Invoke the OP_AggFinalize opcode for every aggregate function 264113449892Sdrh ** in the AggInfo structure. 2642b3bce662Sdanielk1977 */ 264313449892Sdrh static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ 264413449892Sdrh Vdbe *v = pParse->pVdbe; 264513449892Sdrh int i; 264613449892Sdrh struct AggInfo_func *pF; 264713449892Sdrh for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ 2648a10a34b8Sdrh ExprList *pList = pF->pExpr->pList; 2649a10a34b8Sdrh sqlite3VdbeOp3(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 2650a10a34b8Sdrh (void*)pF->pFunc, P3_FUNCDEF); 2651b3bce662Sdanielk1977 } 265213449892Sdrh } 265313449892Sdrh 265413449892Sdrh /* 265513449892Sdrh ** Update the accumulator memory cells for an aggregate based on 265613449892Sdrh ** the current cursor position. 265713449892Sdrh */ 265813449892Sdrh static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ 265913449892Sdrh Vdbe *v = pParse->pVdbe; 266013449892Sdrh int i; 266113449892Sdrh struct AggInfo_func *pF; 266213449892Sdrh struct AggInfo_col *pC; 266313449892Sdrh 266413449892Sdrh pAggInfo->directMode = 1; 266513449892Sdrh for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ 266613449892Sdrh int nArg; 2667c99130fdSdrh int addrNext = 0; 266813449892Sdrh ExprList *pList = pF->pExpr->pList; 266913449892Sdrh if( pList ){ 267013449892Sdrh nArg = pList->nExpr; 267113449892Sdrh sqlite3ExprCodeExprList(pParse, pList); 267213449892Sdrh }else{ 267313449892Sdrh nArg = 0; 267413449892Sdrh } 2675c99130fdSdrh if( pF->iDistinct>=0 ){ 2676c99130fdSdrh addrNext = sqlite3VdbeMakeLabel(v); 2677c99130fdSdrh assert( nArg==1 ); 2678f8875400Sdrh codeDistinct(v, pF->iDistinct, addrNext, 1); 2679c99130fdSdrh } 268013449892Sdrh if( pF->pFunc->needCollSeq ){ 268113449892Sdrh CollSeq *pColl = 0; 268213449892Sdrh struct ExprList_item *pItem; 268313449892Sdrh int j; 268443617e9aSdrh assert( pList!=0 ); /* pList!=0 if pF->pFunc->needCollSeq is true */ 268543617e9aSdrh for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){ 268613449892Sdrh pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); 268713449892Sdrh } 268813449892Sdrh if( !pColl ){ 268913449892Sdrh pColl = pParse->db->pDfltColl; 269013449892Sdrh } 269113449892Sdrh sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); 269213449892Sdrh } 269313449892Sdrh sqlite3VdbeOp3(v, OP_AggStep, pF->iMem, nArg, (void*)pF->pFunc, P3_FUNCDEF); 2694c99130fdSdrh if( addrNext ){ 2695c99130fdSdrh sqlite3VdbeResolveLabel(v, addrNext); 2696c99130fdSdrh } 269713449892Sdrh } 269813449892Sdrh for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){ 26995774b806Sdrh sqlite3ExprCode(pParse, pC->pExpr); 270013449892Sdrh sqlite3VdbeAddOp(v, OP_MemStore, pC->iMem, 1); 270113449892Sdrh } 270213449892Sdrh pAggInfo->directMode = 0; 270313449892Sdrh } 270413449892Sdrh 2705b3bce662Sdanielk1977 2706b3bce662Sdanielk1977 /* 27079bb61fe7Sdrh ** Generate code for the given SELECT statement. 27089bb61fe7Sdrh ** 2709fef5208cSdrh ** The results are distributed in various ways depending on the 2710fef5208cSdrh ** value of eDest and iParm. 2711fef5208cSdrh ** 2712fef5208cSdrh ** eDest Value Result 2713fef5208cSdrh ** ------------ ------------------------------------------- 2714fef5208cSdrh ** SRT_Callback Invoke the callback for each row of the result. 2715fef5208cSdrh ** 2716fef5208cSdrh ** SRT_Mem Store first result in memory cell iParm 2717fef5208cSdrh ** 2718e014a838Sdanielk1977 ** SRT_Set Store results as keys of table iParm. 2719fef5208cSdrh ** 272082c3d636Sdrh ** SRT_Union Store results as a key in a temporary table iParm 272182c3d636Sdrh ** 27224b11c6d3Sjplyon ** SRT_Except Remove results from the temporary table iParm. 2723c4a3c779Sdrh ** 2724c4a3c779Sdrh ** SRT_Table Store results in temporary table iParm 27259bb61fe7Sdrh ** 2726e78e8284Sdrh ** The table above is incomplete. Additional eDist value have be added 2727e78e8284Sdrh ** since this comment was written. See the selectInnerLoop() function for 2728e78e8284Sdrh ** a complete listing of the allowed values of eDest and their meanings. 2729e78e8284Sdrh ** 27309bb61fe7Sdrh ** This routine returns the number of errors. If any errors are 27319bb61fe7Sdrh ** encountered, then an appropriate error message is left in 27329bb61fe7Sdrh ** pParse->zErrMsg. 27339bb61fe7Sdrh ** 27349bb61fe7Sdrh ** This routine does NOT free the Select structure passed in. The 27359bb61fe7Sdrh ** calling function needs to do that. 27361b2e0329Sdrh ** 27371b2e0329Sdrh ** The pParent, parentTab, and *pParentAgg fields are filled in if this 27381b2e0329Sdrh ** SELECT is a subquery. This routine may try to combine this SELECT 27391b2e0329Sdrh ** with its parent to form a single flat query. In so doing, it might 27401b2e0329Sdrh ** change the parent query from a non-aggregate to an aggregate query. 27411b2e0329Sdrh ** For that reason, the pParentAgg flag is passed as a pointer, so it 27421b2e0329Sdrh ** can be changed. 2743e78e8284Sdrh ** 2744e78e8284Sdrh ** Example 1: The meaning of the pParent parameter. 2745e78e8284Sdrh ** 2746e78e8284Sdrh ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; 2747e78e8284Sdrh ** \ \_______ subquery _______/ / 2748e78e8284Sdrh ** \ / 2749e78e8284Sdrh ** \____________________ outer query ___________________/ 2750e78e8284Sdrh ** 2751e78e8284Sdrh ** This routine is called for the outer query first. For that call, 2752e78e8284Sdrh ** pParent will be NULL. During the processing of the outer query, this 2753e78e8284Sdrh ** routine is called recursively to handle the subquery. For the recursive 2754e78e8284Sdrh ** call, pParent will point to the outer query. Because the subquery is 2755e78e8284Sdrh ** the second element in a three-way join, the parentTab parameter will 2756e78e8284Sdrh ** be 1 (the 2nd value of a 0-indexed array.) 27579bb61fe7Sdrh */ 27584adee20fSdanielk1977 int sqlite3Select( 2759cce7d176Sdrh Parse *pParse, /* The parser context */ 27609bb61fe7Sdrh Select *p, /* The SELECT statement being coded. */ 2761e78e8284Sdrh int eDest, /* How to dispose of the results */ 2762e78e8284Sdrh int iParm, /* A parameter used by the eDest disposal method */ 2763832508b7Sdrh Select *pParent, /* Another SELECT for which this is a sub-query */ 2764832508b7Sdrh int parentTab, /* Index in pParent->pSrc of this query */ 276584ac9d02Sdanielk1977 int *pParentAgg, /* True if pParent uses aggregate functions */ 2766b3bce662Sdanielk1977 char *aff /* If eDest is SRT_Union, the affinity string */ 2767cce7d176Sdrh ){ 276813449892Sdrh int i, j; /* Loop counters */ 276913449892Sdrh WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */ 277013449892Sdrh Vdbe *v; /* The virtual machine under construction */ 2771b3bce662Sdanielk1977 int isAgg; /* True for select lists like "count(*)" */ 2772a2e00042Sdrh ExprList *pEList; /* List of columns to extract. */ 2773ad3cab52Sdrh SrcList *pTabList; /* List of tables to select from */ 27749bb61fe7Sdrh Expr *pWhere; /* The WHERE clause. May be NULL */ 27759bb61fe7Sdrh ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ 27762282792aSdrh ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ 27772282792aSdrh Expr *pHaving; /* The HAVING clause. May be NULL */ 277819a775c2Sdrh int isDistinct; /* True if the DISTINCT keyword is present */ 277919a775c2Sdrh int distinct; /* Table to use for the distinct set */ 27801d83f052Sdrh int rc = 1; /* Value to return from this function */ 2781b9bb7c18Sdrh int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */ 278213449892Sdrh AggInfo sAggInfo; /* Information used by aggregate queries */ 2783ec7429aeSdrh int iEnd; /* Address of the end of the query */ 27849bb61fe7Sdrh 27859e12800dSdanielk1977 if( p==0 || sqlite3MallocFailed() || pParse->nErr ){ 27866f7adc8aSdrh return 1; 27876f7adc8aSdrh } 27884adee20fSdanielk1977 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; 278913449892Sdrh memset(&sAggInfo, 0, sizeof(sAggInfo)); 2790daffd0e5Sdrh 2791b7f9164eSdrh #ifndef SQLITE_OMIT_COMPOUND_SELECT 279282c3d636Sdrh /* If there is are a sequence of queries, do the earlier ones first. 279382c3d636Sdrh */ 279482c3d636Sdrh if( p->pPrior ){ 27950342b1f5Sdrh if( p->pRightmost==0 ){ 27960342b1f5Sdrh Select *pLoop; 27970342b1f5Sdrh for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ 27980342b1f5Sdrh pLoop->pRightmost = p; 27990342b1f5Sdrh } 28000342b1f5Sdrh } 280184ac9d02Sdanielk1977 return multiSelect(pParse, p, eDest, iParm, aff); 280282c3d636Sdrh } 2803b7f9164eSdrh #endif 280482c3d636Sdrh 2805b3bce662Sdanielk1977 pOrderBy = p->pOrderBy; 280613449892Sdrh if( IgnorableOrderby(eDest) ){ 2807b3bce662Sdanielk1977 p->pOrderBy = 0; 2808b3bce662Sdanielk1977 } 2809b3bce662Sdanielk1977 if( sqlite3SelectResolve(pParse, p, 0) ){ 2810b3bce662Sdanielk1977 goto select_end; 2811b3bce662Sdanielk1977 } 2812b3bce662Sdanielk1977 p->pOrderBy = pOrderBy; 2813b3bce662Sdanielk1977 281482c3d636Sdrh /* Make local copies of the parameters for this query. 281582c3d636Sdrh */ 28169bb61fe7Sdrh pTabList = p->pSrc; 28179bb61fe7Sdrh pWhere = p->pWhere; 28182282792aSdrh pGroupBy = p->pGroupBy; 28192282792aSdrh pHaving = p->pHaving; 2820b3bce662Sdanielk1977 isAgg = p->isAgg; 282119a775c2Sdrh isDistinct = p->isDistinct; 2822b3bce662Sdanielk1977 pEList = p->pEList; 2823b3bce662Sdanielk1977 if( pEList==0 ) goto select_end; 28249bb61fe7Sdrh 28259bb61fe7Sdrh /* 28269bb61fe7Sdrh ** Do not even attempt to generate any code if we have already seen 28279bb61fe7Sdrh ** errors before this routine starts. 28289bb61fe7Sdrh */ 28291d83f052Sdrh if( pParse->nErr>0 ) goto select_end; 2830cce7d176Sdrh 28312282792aSdrh /* If writing to memory or generating a set 28322282792aSdrh ** only a single column may be output. 283319a775c2Sdrh */ 283493758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 2835fef5208cSdrh if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ 28364adee20fSdanielk1977 sqlite3ErrorMsg(pParse, "only a single result allowed for " 2837da93d238Sdrh "a SELECT that is part of an expression"); 28381d83f052Sdrh goto select_end; 283919a775c2Sdrh } 284093758c8dSdanielk1977 #endif 284119a775c2Sdrh 2842c926afbcSdrh /* ORDER BY is ignored for some destinations. 28432282792aSdrh */ 284413449892Sdrh if( IgnorableOrderby(eDest) ){ 2845acd4c695Sdrh pOrderBy = 0; 28462282792aSdrh } 28472282792aSdrh 2848d820cb1bSdrh /* Begin generating code. 2849d820cb1bSdrh */ 28504adee20fSdanielk1977 v = sqlite3GetVdbe(pParse); 2851d820cb1bSdrh if( v==0 ) goto select_end; 2852d820cb1bSdrh 2853d820cb1bSdrh /* Generate code for all sub-queries in the FROM clause 2854d820cb1bSdrh */ 285551522cd3Sdrh #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) 2856ad3cab52Sdrh for(i=0; i<pTabList->nSrc; i++){ 2857742f947bSdanielk1977 const char *zSavedAuthContext = 0; 2858c31c2eb8Sdrh int needRestoreContext; 285913449892Sdrh struct SrcList_item *pItem = &pTabList->a[i]; 2860c31c2eb8Sdrh 28611787ccabSdanielk1977 if( pItem->pSelect==0 || pItem->isPopulated ) continue; 286213449892Sdrh if( pItem->zName!=0 ){ 28635cf590c1Sdrh zSavedAuthContext = pParse->zAuthContext; 286413449892Sdrh pParse->zAuthContext = pItem->zName; 2865c31c2eb8Sdrh needRestoreContext = 1; 2866c31c2eb8Sdrh }else{ 2867c31c2eb8Sdrh needRestoreContext = 0; 28685cf590c1Sdrh } 2869b9bb7c18Sdrh sqlite3Select(pParse, pItem->pSelect, SRT_EphemTab, 287013449892Sdrh pItem->iCursor, p, i, &isAgg, 0); 2871c31c2eb8Sdrh if( needRestoreContext ){ 28725cf590c1Sdrh pParse->zAuthContext = zSavedAuthContext; 28735cf590c1Sdrh } 2874832508b7Sdrh pTabList = p->pSrc; 2875832508b7Sdrh pWhere = p->pWhere; 287613449892Sdrh if( !IgnorableOrderby(eDest) ){ 2877832508b7Sdrh pOrderBy = p->pOrderBy; 2878acd4c695Sdrh } 2879832508b7Sdrh pGroupBy = p->pGroupBy; 2880832508b7Sdrh pHaving = p->pHaving; 2881832508b7Sdrh isDistinct = p->isDistinct; 28821b2e0329Sdrh } 288351522cd3Sdrh #endif 28841b2e0329Sdrh 28856e17529eSdrh /* Check for the special case of a min() or max() function by itself 28866e17529eSdrh ** in the result set. 28876e17529eSdrh */ 28886e17529eSdrh if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ 28896e17529eSdrh rc = 0; 28906e17529eSdrh goto select_end; 28916e17529eSdrh } 28926e17529eSdrh 28931b2e0329Sdrh /* Check to see if this is a subquery that can be "flattened" into its parent. 28941b2e0329Sdrh ** If flattening is a possiblity, do so and return immediately. 28951b2e0329Sdrh */ 2896b7f9164eSdrh #ifndef SQLITE_OMIT_VIEW 28971b2e0329Sdrh if( pParent && pParentAgg && 289874161705Sdrh flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){ 28991b2e0329Sdrh if( isAgg ) *pParentAgg = 1; 2900b3bce662Sdanielk1977 goto select_end; 29011b2e0329Sdrh } 2902b7f9164eSdrh #endif 2903832508b7Sdrh 29047cedc8d4Sdanielk1977 /* If there is an ORDER BY clause, resolve any collation sequences 29059d2985c7Sdrh ** names that have been explicitly specified and create a sorting index. 29069d2985c7Sdrh ** 29079d2985c7Sdrh ** This sorting index might end up being unused if the data can be 29089d2985c7Sdrh ** extracted in pre-sorted order. If that is the case, then the 2909b9bb7c18Sdrh ** OP_OpenEphemeral instruction will be changed to an OP_Noop once 29109d2985c7Sdrh ** we figure out that the sorting index is not needed. The addrSortIndex 29119d2985c7Sdrh ** variable is used to facilitate that change. 29127cedc8d4Sdanielk1977 */ 29137cedc8d4Sdanielk1977 if( pOrderBy ){ 29145d9a4af9Sdrh struct ExprList_item *pTerm; 29150342b1f5Sdrh KeyInfo *pKeyInfo; 29165d9a4af9Sdrh for(i=0, pTerm=pOrderBy->a; i<pOrderBy->nExpr; i++, pTerm++){ 29175d9a4af9Sdrh if( pTerm->zName ){ 29185d9a4af9Sdrh pTerm->pExpr->pColl = sqlite3LocateCollSeq(pParse, pTerm->zName, -1); 29197cedc8d4Sdanielk1977 } 29207cedc8d4Sdanielk1977 } 29217cedc8d4Sdanielk1977 if( pParse->nErr ){ 29227cedc8d4Sdanielk1977 goto select_end; 29237cedc8d4Sdanielk1977 } 29240342b1f5Sdrh pKeyInfo = keyInfoFromExprList(pParse, pOrderBy); 29259d2985c7Sdrh pOrderBy->iECursor = pParse->nTab++; 2926b9bb7c18Sdrh p->addrOpenEphm[2] = addrSortIndex = 2927b9bb7c18Sdrh sqlite3VdbeOp3(v, OP_OpenEphemeral, pOrderBy->iECursor, pOrderBy->nExpr+2, 29280342b1f5Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 29299d2985c7Sdrh }else{ 29309d2985c7Sdrh addrSortIndex = -1; 29317cedc8d4Sdanielk1977 } 29327cedc8d4Sdanielk1977 29332d0794e3Sdrh /* If the output is destined for a temporary table, open that table. 29342d0794e3Sdrh */ 2935b9bb7c18Sdrh if( eDest==SRT_EphemTab ){ 2936b9bb7c18Sdrh sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, pEList->nExpr); 29372d0794e3Sdrh } 29382d0794e3Sdrh 2939f42bacc2Sdrh /* Set the limiter. 2940f42bacc2Sdrh */ 2941f42bacc2Sdrh iEnd = sqlite3VdbeMakeLabel(v); 2942f42bacc2Sdrh computeLimitRegisters(pParse, p, iEnd); 2943f42bacc2Sdrh 2944dece1a84Sdrh /* Open a virtual index to use for the distinct set. 2945cce7d176Sdrh */ 294619a775c2Sdrh if( isDistinct ){ 29470342b1f5Sdrh KeyInfo *pKeyInfo; 2948832508b7Sdrh distinct = pParse->nTab++; 29490342b1f5Sdrh pKeyInfo = keyInfoFromExprList(pParse, p->pEList); 2950b9bb7c18Sdrh sqlite3VdbeOp3(v, OP_OpenEphemeral, distinct, 0, 29510342b1f5Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 2952832508b7Sdrh }else{ 2953832508b7Sdrh distinct = -1; 2954efb7251dSdrh } 2955832508b7Sdrh 295613449892Sdrh /* Aggregate and non-aggregate queries are handled differently */ 295713449892Sdrh if( !isAgg && pGroupBy==0 ){ 295813449892Sdrh /* This case is for non-aggregate queries 295913449892Sdrh ** Begin the database scan 2960832508b7Sdrh */ 296113449892Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy); 29621d83f052Sdrh if( pWInfo==0 ) goto select_end; 2963cce7d176Sdrh 2964b9bb7c18Sdrh /* If sorting index that was created by a prior OP_OpenEphemeral 2965b9bb7c18Sdrh ** instruction ended up not being needed, then change the OP_OpenEphemeral 29669d2985c7Sdrh ** into an OP_Noop. 29679d2985c7Sdrh */ 29689d2985c7Sdrh if( addrSortIndex>=0 && pOrderBy==0 ){ 2969f8875400Sdrh sqlite3VdbeChangeToNoop(v, addrSortIndex, 1); 2970b9bb7c18Sdrh p->addrOpenEphm[2] = -1; 29719d2985c7Sdrh } 29729d2985c7Sdrh 297313449892Sdrh /* Use the standard inner loop 2974cce7d176Sdrh */ 2975df199a25Sdrh if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, 297684ac9d02Sdanielk1977 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ 29771d83f052Sdrh goto select_end; 2978cce7d176Sdrh } 29792282792aSdrh 2980cce7d176Sdrh /* End the database scan loop. 2981cce7d176Sdrh */ 29824adee20fSdanielk1977 sqlite3WhereEnd(pWInfo); 298313449892Sdrh }else{ 298413449892Sdrh /* This is the processing for aggregate queries */ 298513449892Sdrh NameContext sNC; /* Name context for processing aggregate information */ 298613449892Sdrh int iAMem; /* First Mem address for storing current GROUP BY */ 298713449892Sdrh int iBMem; /* First Mem address for previous GROUP BY */ 298813449892Sdrh int iUseFlag; /* Mem address holding flag indicating that at least 298913449892Sdrh ** one row of the input to the aggregator has been 299013449892Sdrh ** processed */ 299113449892Sdrh int iAbortFlag; /* Mem address which causes query abort if positive */ 299213449892Sdrh int groupBySort; /* Rows come from source in GROUP BY order */ 2993cce7d176Sdrh 299413449892Sdrh 299513449892Sdrh /* The following variables hold addresses or labels for parts of the 299613449892Sdrh ** virtual machine program we are putting together */ 299713449892Sdrh int addrOutputRow; /* Start of subroutine that outputs a result row */ 299813449892Sdrh int addrSetAbort; /* Set the abort flag and return */ 299913449892Sdrh int addrInitializeLoop; /* Start of code that initializes the input loop */ 300013449892Sdrh int addrTopOfLoop; /* Top of the input loop */ 300113449892Sdrh int addrGroupByChange; /* Code that runs when any GROUP BY term changes */ 300213449892Sdrh int addrProcessRow; /* Code to process a single input row */ 300313449892Sdrh int addrEnd; /* End of all processing */ 3004b9bb7c18Sdrh int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */ 3005e313382eSdrh int addrReset; /* Subroutine for resetting the accumulator */ 300613449892Sdrh 300713449892Sdrh addrEnd = sqlite3VdbeMakeLabel(v); 300813449892Sdrh 300913449892Sdrh /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in 301013449892Sdrh ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the 301113449892Sdrh ** SELECT statement. 30122282792aSdrh */ 301313449892Sdrh memset(&sNC, 0, sizeof(sNC)); 301413449892Sdrh sNC.pParse = pParse; 301513449892Sdrh sNC.pSrcList = pTabList; 301613449892Sdrh sNC.pAggInfo = &sAggInfo; 301713449892Sdrh sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0; 30189d2985c7Sdrh sAggInfo.pGroupBy = pGroupBy; 301913449892Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pEList) ){ 30201d83f052Sdrh goto select_end; 30212282792aSdrh } 302213449892Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, pOrderBy) ){ 302313449892Sdrh goto select_end; 30242282792aSdrh } 302513449892Sdrh if( pHaving && sqlite3ExprAnalyzeAggregates(&sNC, pHaving) ){ 302613449892Sdrh goto select_end; 302713449892Sdrh } 302813449892Sdrh sAggInfo.nAccumulator = sAggInfo.nColumn; 302913449892Sdrh for(i=0; i<sAggInfo.nFunc; i++){ 303013449892Sdrh if( sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->pList) ){ 303113449892Sdrh goto select_end; 303213449892Sdrh } 303313449892Sdrh } 30349e12800dSdanielk1977 if( sqlite3MallocFailed() ) goto select_end; 303513449892Sdrh 303613449892Sdrh /* Processing for aggregates with GROUP BY is very different and 303713449892Sdrh ** much more complex tha aggregates without a GROUP BY. 303813449892Sdrh */ 303913449892Sdrh if( pGroupBy ){ 304013449892Sdrh KeyInfo *pKeyInfo; /* Keying information for the group by clause */ 304113449892Sdrh 304213449892Sdrh /* Create labels that we will be needing 304313449892Sdrh */ 304413449892Sdrh 304513449892Sdrh addrInitializeLoop = sqlite3VdbeMakeLabel(v); 304613449892Sdrh addrGroupByChange = sqlite3VdbeMakeLabel(v); 304713449892Sdrh addrProcessRow = sqlite3VdbeMakeLabel(v); 304813449892Sdrh 304913449892Sdrh /* If there is a GROUP BY clause we might need a sorting index to 305013449892Sdrh ** implement it. Allocate that sorting index now. If it turns out 3051b9bb7c18Sdrh ** that we do not need it after all, the OpenEphemeral instruction 305213449892Sdrh ** will be converted into a Noop. 305313449892Sdrh */ 305413449892Sdrh sAggInfo.sortingIdx = pParse->nTab++; 305513449892Sdrh pKeyInfo = keyInfoFromExprList(pParse, pGroupBy); 305613449892Sdrh addrSortingIdx = 3057b9bb7c18Sdrh sqlite3VdbeOp3(v, OP_OpenEphemeral, sAggInfo.sortingIdx, 305813449892Sdrh sAggInfo.nSortingColumn, 305913449892Sdrh (char*)pKeyInfo, P3_KEYINFO_HANDOFF); 306013449892Sdrh 306113449892Sdrh /* Initialize memory locations used by GROUP BY aggregate processing 306213449892Sdrh */ 306313449892Sdrh iUseFlag = pParse->nMem++; 306413449892Sdrh iAbortFlag = pParse->nMem++; 306513449892Sdrh iAMem = pParse->nMem; 306613449892Sdrh pParse->nMem += pGroupBy->nExpr; 306713449892Sdrh iBMem = pParse->nMem; 306813449892Sdrh pParse->nMem += pGroupBy->nExpr; 3069d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iAbortFlag); 3070de29e3e9Sdrh VdbeComment((v, "# clear abort flag")); 3071d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 0, iUseFlag); 3072de29e3e9Sdrh VdbeComment((v, "# indicate accumulator empty")); 307313449892Sdrh sqlite3VdbeAddOp(v, OP_Goto, 0, addrInitializeLoop); 307413449892Sdrh 307513449892Sdrh /* Generate a subroutine that outputs a single row of the result 307613449892Sdrh ** set. This subroutine first looks at the iUseFlag. If iUseFlag 307713449892Sdrh ** is less than or equal to zero, the subroutine is a no-op. If 307813449892Sdrh ** the processing calls for the query to abort, this subroutine 307913449892Sdrh ** increments the iAbortFlag memory location before returning in 308013449892Sdrh ** order to signal the caller to abort. 308113449892Sdrh */ 308213449892Sdrh addrSetAbort = sqlite3VdbeCurrentAddr(v); 3083de29e3e9Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, iAbortFlag); 3084de29e3e9Sdrh VdbeComment((v, "# set abort flag")); 308513449892Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 308613449892Sdrh addrOutputRow = sqlite3VdbeCurrentAddr(v); 308713449892Sdrh sqlite3VdbeAddOp(v, OP_IfMemPos, iUseFlag, addrOutputRow+2); 3088de29e3e9Sdrh VdbeComment((v, "# Groupby result generator entry point")); 308913449892Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 309013449892Sdrh finalizeAggFunctions(pParse, &sAggInfo); 309113449892Sdrh if( pHaving ){ 309213449892Sdrh sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, 1); 309313449892Sdrh } 309413449892Sdrh rc = selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy, 309513449892Sdrh distinct, eDest, iParm, 309613449892Sdrh addrOutputRow+1, addrSetAbort, aff); 309713449892Sdrh if( rc ){ 309813449892Sdrh goto select_end; 309913449892Sdrh } 310013449892Sdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 3101de29e3e9Sdrh VdbeComment((v, "# end groupby result generator")); 310213449892Sdrh 3103e313382eSdrh /* Generate a subroutine that will reset the group-by accumulator 3104e313382eSdrh */ 3105e313382eSdrh addrReset = sqlite3VdbeCurrentAddr(v); 3106e313382eSdrh resetAccumulator(pParse, &sAggInfo); 3107e313382eSdrh sqlite3VdbeAddOp(v, OP_Return, 0, 0); 3108e313382eSdrh 310913449892Sdrh /* Begin a loop that will extract all source rows in GROUP BY order. 311013449892Sdrh ** This might involve two separate loops with an OP_Sort in between, or 311113449892Sdrh ** it might be a single loop that uses an index to extract information 311213449892Sdrh ** in the right order to begin with. 311313449892Sdrh */ 311413449892Sdrh sqlite3VdbeResolveLabel(v, addrInitializeLoop); 3115e313382eSdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset); 311613449892Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy); 31175360ad34Sdrh if( pWInfo==0 ) goto select_end; 311813449892Sdrh if( pGroupBy==0 ){ 311913449892Sdrh /* The optimizer is able to deliver rows in group by order so 3120b9bb7c18Sdrh ** we do not have to sort. The OP_OpenEphemeral table will be 312113449892Sdrh ** cancelled later because we still need to use the pKeyInfo 312213449892Sdrh */ 312313449892Sdrh pGroupBy = p->pGroupBy; 312413449892Sdrh groupBySort = 0; 312513449892Sdrh }else{ 312613449892Sdrh /* Rows are coming out in undetermined order. We have to push 312713449892Sdrh ** each row into a sorting index, terminate the first loop, 312813449892Sdrh ** then loop over the sorting index in order to get the output 312913449892Sdrh ** in sorted order 313013449892Sdrh */ 313113449892Sdrh groupBySort = 1; 313213449892Sdrh sqlite3ExprCodeExprList(pParse, pGroupBy); 313313449892Sdrh sqlite3VdbeAddOp(v, OP_Sequence, sAggInfo.sortingIdx, 0); 313413449892Sdrh j = pGroupBy->nExpr+1; 313513449892Sdrh for(i=0; i<sAggInfo.nColumn; i++){ 313613449892Sdrh struct AggInfo_col *pCol = &sAggInfo.aCol[i]; 313713449892Sdrh if( pCol->iSorterColumn<j ) continue; 313813449892Sdrh if( pCol->iColumn<0 ){ 313913449892Sdrh sqlite3VdbeAddOp(v, OP_Rowid, pCol->iTable, 0); 314013449892Sdrh }else{ 314113449892Sdrh sqlite3VdbeAddOp(v, OP_Column, pCol->iTable, pCol->iColumn); 314213449892Sdrh } 314313449892Sdrh j++; 314413449892Sdrh } 314513449892Sdrh sqlite3VdbeAddOp(v, OP_MakeRecord, j, 0); 314613449892Sdrh sqlite3VdbeAddOp(v, OP_IdxInsert, sAggInfo.sortingIdx, 0); 314713449892Sdrh sqlite3WhereEnd(pWInfo); 314897571957Sdrh sqlite3VdbeAddOp(v, OP_Sort, sAggInfo.sortingIdx, addrEnd); 3149de29e3e9Sdrh VdbeComment((v, "# GROUP BY sort")); 315013449892Sdrh sAggInfo.useSortingIdx = 1; 315113449892Sdrh } 315213449892Sdrh 315313449892Sdrh /* Evaluate the current GROUP BY terms and store in b0, b1, b2... 315413449892Sdrh ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth) 315513449892Sdrh ** Then compare the current GROUP BY terms against the GROUP BY terms 315613449892Sdrh ** from the previous row currently stored in a0, a1, a2... 315713449892Sdrh */ 315813449892Sdrh addrTopOfLoop = sqlite3VdbeCurrentAddr(v); 315913449892Sdrh for(j=0; j<pGroupBy->nExpr; j++){ 316013449892Sdrh if( groupBySort ){ 316113449892Sdrh sqlite3VdbeAddOp(v, OP_Column, sAggInfo.sortingIdx, j); 316213449892Sdrh }else{ 316313449892Sdrh sAggInfo.directMode = 1; 316413449892Sdrh sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr); 316513449892Sdrh } 316613449892Sdrh sqlite3VdbeAddOp(v, OP_MemStore, iBMem+j, j<pGroupBy->nExpr-1); 316713449892Sdrh } 316813449892Sdrh for(j=pGroupBy->nExpr-1; j>=0; j--){ 316913449892Sdrh if( j<pGroupBy->nExpr-1 ){ 317013449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, iBMem+j, 0); 317113449892Sdrh } 317213449892Sdrh sqlite3VdbeAddOp(v, OP_MemLoad, iAMem+j, 0); 317313449892Sdrh if( j==0 ){ 3174e313382eSdrh sqlite3VdbeAddOp(v, OP_Eq, 0x200, addrProcessRow); 317513449892Sdrh }else{ 31764f686238Sdrh sqlite3VdbeAddOp(v, OP_Ne, 0x200, addrGroupByChange); 317713449892Sdrh } 317813449892Sdrh sqlite3VdbeChangeP3(v, -1, (void*)pKeyInfo->aColl[j], P3_COLLSEQ); 317913449892Sdrh } 318013449892Sdrh 318113449892Sdrh /* Generate code that runs whenever the GROUP BY changes. 318213449892Sdrh ** Change in the GROUP BY are detected by the previous code 318313449892Sdrh ** block. If there were no changes, this block is skipped. 318413449892Sdrh ** 318513449892Sdrh ** This code copies current group by terms in b0,b1,b2,... 318613449892Sdrh ** over to a0,a1,a2. It then calls the output subroutine 318713449892Sdrh ** and resets the aggregate accumulator registers in preparation 318813449892Sdrh ** for the next GROUP BY batch. 318913449892Sdrh */ 319013449892Sdrh sqlite3VdbeResolveLabel(v, addrGroupByChange); 319113449892Sdrh for(j=0; j<pGroupBy->nExpr; j++){ 3192d654be80Sdrh sqlite3VdbeAddOp(v, OP_MemMove, iAMem+j, iBMem+j); 319313449892Sdrh } 319413449892Sdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow); 3195de29e3e9Sdrh VdbeComment((v, "# output one row")); 319613449892Sdrh sqlite3VdbeAddOp(v, OP_IfMemPos, iAbortFlag, addrEnd); 3197de29e3e9Sdrh VdbeComment((v, "# check abort flag")); 3198e313382eSdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrReset); 3199de29e3e9Sdrh VdbeComment((v, "# reset accumulator")); 320013449892Sdrh 320113449892Sdrh /* Update the aggregate accumulators based on the content of 320213449892Sdrh ** the current row 320313449892Sdrh */ 320413449892Sdrh sqlite3VdbeResolveLabel(v, addrProcessRow); 320513449892Sdrh updateAccumulator(pParse, &sAggInfo); 3206de29e3e9Sdrh sqlite3VdbeAddOp(v, OP_MemInt, 1, iUseFlag); 3207de29e3e9Sdrh VdbeComment((v, "# indicate data in accumulator")); 320813449892Sdrh 320913449892Sdrh /* End of the loop 321013449892Sdrh */ 321113449892Sdrh if( groupBySort ){ 321213449892Sdrh sqlite3VdbeAddOp(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop); 321313449892Sdrh }else{ 321413449892Sdrh sqlite3WhereEnd(pWInfo); 3215f8875400Sdrh sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1); 321613449892Sdrh } 321713449892Sdrh 321813449892Sdrh /* Output the final row of result 321913449892Sdrh */ 322013449892Sdrh sqlite3VdbeAddOp(v, OP_Gosub, 0, addrOutputRow); 3221de29e3e9Sdrh VdbeComment((v, "# output final row")); 322213449892Sdrh 322313449892Sdrh } /* endif pGroupBy */ 322413449892Sdrh else { 322513449892Sdrh /* This case runs if the aggregate has no GROUP BY clause. The 322613449892Sdrh ** processing is much simpler since there is only a single row 322713449892Sdrh ** of output. 322813449892Sdrh */ 322913449892Sdrh resetAccumulator(pParse, &sAggInfo); 323013449892Sdrh pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0); 32315360ad34Sdrh if( pWInfo==0 ) goto select_end; 323213449892Sdrh updateAccumulator(pParse, &sAggInfo); 323313449892Sdrh sqlite3WhereEnd(pWInfo); 323413449892Sdrh finalizeAggFunctions(pParse, &sAggInfo); 323513449892Sdrh pOrderBy = 0; 32365774b806Sdrh if( pHaving ){ 32375774b806Sdrh sqlite3ExprIfFalse(pParse, pHaving, addrEnd, 1); 32385774b806Sdrh } 323913449892Sdrh selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 324013449892Sdrh eDest, iParm, addrEnd, addrEnd, aff); 324113449892Sdrh } 324213449892Sdrh sqlite3VdbeResolveLabel(v, addrEnd); 324313449892Sdrh 324413449892Sdrh } /* endif aggregate query */ 32452282792aSdrh 3246cce7d176Sdrh /* If there is an ORDER BY clause, then we need to sort the results 3247cce7d176Sdrh ** and send them to the callback one by one. 3248cce7d176Sdrh */ 3249cce7d176Sdrh if( pOrderBy ){ 3250cdd536f0Sdrh generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); 3251cce7d176Sdrh } 32526a535340Sdrh 325393758c8dSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 3254f620b4e2Sdrh /* If this was a subquery, we have now converted the subquery into a 32551787ccabSdanielk1977 ** temporary table. So set the SrcList_item.isPopulated flag to prevent 32561787ccabSdanielk1977 ** this subquery from being evaluated again and to force the use of 32571787ccabSdanielk1977 ** the temporary table. 3258f620b4e2Sdrh */ 3259f620b4e2Sdrh if( pParent ){ 3260f620b4e2Sdrh assert( pParent->pSrc->nSrc>parentTab ); 3261f620b4e2Sdrh assert( pParent->pSrc->a[parentTab].pSelect==p ); 32621787ccabSdanielk1977 pParent->pSrc->a[parentTab].isPopulated = 1; 3263f620b4e2Sdrh } 326493758c8dSdanielk1977 #endif 3265f620b4e2Sdrh 3266ec7429aeSdrh /* Jump here to skip this query 3267ec7429aeSdrh */ 3268ec7429aeSdrh sqlite3VdbeResolveLabel(v, iEnd); 3269ec7429aeSdrh 32701d83f052Sdrh /* The SELECT was successfully coded. Set the return code to 0 32711d83f052Sdrh ** to indicate no errors. 32721d83f052Sdrh */ 32731d83f052Sdrh rc = 0; 32741d83f052Sdrh 32751d83f052Sdrh /* Control jumps to here if an error is encountered above, or upon 32761d83f052Sdrh ** successful coding of the SELECT. 32771d83f052Sdrh */ 32781d83f052Sdrh select_end: 3279955de52cSdanielk1977 3280955de52cSdanielk1977 /* Identify column names if we will be using them in a callback. This 3281955de52cSdanielk1977 ** step is skipped if the output is going to some other destination. 3282955de52cSdanielk1977 */ 3283955de52cSdanielk1977 if( rc==SQLITE_OK && eDest==SRT_Callback ){ 3284955de52cSdanielk1977 generateColumnNames(pParse, pTabList, pEList); 3285955de52cSdanielk1977 } 3286955de52cSdanielk1977 328713449892Sdrh sqliteFree(sAggInfo.aCol); 328813449892Sdrh sqliteFree(sAggInfo.aFunc); 32891d83f052Sdrh return rc; 3290cce7d176Sdrh } 3291