00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "sqliteInt.h"
00018
00019
00020
00021
00022
00023
00024 Select *sqliteSelectNew(
00025 ExprList *pEList,
00026 SrcList *pSrc,
00027 Expr *pWhere,
00028 ExprList *pGroupBy,
00029 Expr *pHaving,
00030 ExprList *pOrderBy,
00031 int isDistinct,
00032 int nLimit,
00033 int nOffset
00034 ){
00035 Select *pNew;
00036 pNew = sqliteMalloc( sizeof(*pNew) );
00037 if( pNew==0 ){
00038 sqliteExprListDelete(pEList);
00039 sqliteSrcListDelete(pSrc);
00040 sqliteExprDelete(pWhere);
00041 sqliteExprListDelete(pGroupBy);
00042 sqliteExprDelete(pHaving);
00043 sqliteExprListDelete(pOrderBy);
00044 }else{
00045 if( pEList==0 ){
00046 pEList = sqliteExprListAppend(0, sqliteExpr(TK_ALL,0,0,0), 0);
00047 }
00048 pNew->pEList = pEList;
00049 pNew->pSrc = pSrc;
00050 pNew->pWhere = pWhere;
00051 pNew->pGroupBy = pGroupBy;
00052 pNew->pHaving = pHaving;
00053 pNew->pOrderBy = pOrderBy;
00054 pNew->isDistinct = isDistinct;
00055 pNew->op = TK_SELECT;
00056 pNew->nLimit = nLimit;
00057 pNew->nOffset = nOffset;
00058 pNew->iLimit = -1;
00059 pNew->iOffset = -1;
00060 }
00061 return pNew;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
00081 int jointype = 0;
00082 Token *apAll[3];
00083 Token *p;
00084 static struct {
00085 const char *zKeyword;
00086 int nChar;
00087 int code;
00088 } keywords[] = {
00089 { "natural", 7, JT_NATURAL },
00090 { "left", 4, JT_LEFT|JT_OUTER },
00091 { "right", 5, JT_RIGHT|JT_OUTER },
00092 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
00093 { "outer", 5, JT_OUTER },
00094 { "inner", 5, JT_INNER },
00095 { "cross", 5, JT_INNER },
00096 };
00097 int i, j;
00098 apAll[0] = pA;
00099 apAll[1] = pB;
00100 apAll[2] = pC;
00101 for(i=0; i<3 && apAll[i]; i++){
00102 p = apAll[i];
00103 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
00104 if( p->n==keywords[j].nChar
00105 && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
00106 jointype |= keywords[j].code;
00107 break;
00108 }
00109 }
00110 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
00111 jointype |= JT_ERROR;
00112 break;
00113 }
00114 }
00115 if(
00116 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
00117 (jointype & JT_ERROR)!=0
00118 ){
00119 static Token dummy = { 0, 0 };
00120 char *zSp1 = " ", *zSp2 = " ";
00121 if( pB==0 ){ pB = &dummy; zSp1 = 0; }
00122 if( pC==0 ){ pC = &dummy; zSp2 = 0; }
00123 sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
00124 pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
00125 pParse->nErr++;
00126 jointype = JT_INNER;
00127 }else if( jointype & JT_RIGHT ){
00128 sqliteErrorMsg(pParse,
00129 "RIGHT and FULL OUTER JOINs are not currently supported");
00130 jointype = JT_INNER;
00131 }
00132 return jointype;
00133 }
00134
00135
00136
00137
00138
00139 static int columnIndex(Table *pTab, const char *zCol){
00140 int i;
00141 for(i=0; i<pTab->nCol; i++){
00142 if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
00143 }
00144 return -1;
00145 }
00146
00147
00148
00149
00150
00151 static void addWhereTerm(
00152 const char *zCol,
00153 const Table *pTab1,
00154 const Table *pTab2,
00155 Expr **ppExpr
00156 ){
00157 Token dummy;
00158 Expr *pE1a, *pE1b, *pE1c;
00159 Expr *pE2a, *pE2b, *pE2c;
00160 Expr *pE;
00161
00162 dummy.z = zCol;
00163 dummy.n = strlen(zCol);
00164 dummy.dyn = 0;
00165 pE1a = sqliteExpr(TK_ID, 0, 0, &dummy);
00166 pE2a = sqliteExpr(TK_ID, 0, 0, &dummy);
00167 dummy.z = pTab1->zName;
00168 dummy.n = strlen(dummy.z);
00169 pE1b = sqliteExpr(TK_ID, 0, 0, &dummy);
00170 dummy.z = pTab2->zName;
00171 dummy.n = strlen(dummy.z);
00172 pE2b = sqliteExpr(TK_ID, 0, 0, &dummy);
00173 pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0);
00174 pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0);
00175 pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0);
00176 ExprSetProperty(pE, EP_FromJoin);
00177 if( *ppExpr ){
00178 *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0);
00179 }else{
00180 *ppExpr = pE;
00181 }
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 static void setJoinExpr(Expr *p){
00195 while( p ){
00196 ExprSetProperty(p, EP_FromJoin);
00197 setJoinExpr(p->pLeft);
00198 p = p->pRight;
00199 }
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209 static int sqliteProcessJoin(Parse *pParse, Select *p){
00210 SrcList *pSrc;
00211 int i, j;
00212 pSrc = p->pSrc;
00213 for(i=0; i<pSrc->nSrc-1; i++){
00214 struct SrcList_item *pTerm = &pSrc->a[i];
00215 struct SrcList_item *pOther = &pSrc->a[i+1];
00216
00217 if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
00218
00219
00220
00221
00222 if( pTerm->jointype & JT_NATURAL ){
00223 Table *pTab;
00224 if( pTerm->pOn || pTerm->pUsing ){
00225 sqliteErrorMsg(pParse, "a NATURAL join may not have "
00226 "an ON or USING clause", 0);
00227 return 1;
00228 }
00229 pTab = pTerm->pTab;
00230 for(j=0; j<pTab->nCol; j++){
00231 if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
00232 addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
00233 }
00234 }
00235 }
00236
00237
00238
00239 if( pTerm->pOn && pTerm->pUsing ){
00240 sqliteErrorMsg(pParse, "cannot have both ON and USING "
00241 "clauses in the same join");
00242 return 1;
00243 }
00244
00245
00246
00247
00248 if( pTerm->pOn ){
00249 setJoinExpr(pTerm->pOn);
00250 if( p->pWhere==0 ){
00251 p->pWhere = pTerm->pOn;
00252 }else{
00253 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
00254 }
00255 pTerm->pOn = 0;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265 if( pTerm->pUsing ){
00266 IdList *pList;
00267 int j;
00268 assert( i<pSrc->nSrc-1 );
00269 pList = pTerm->pUsing;
00270 for(j=0; j<pList->nId; j++){
00271 if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 ||
00272 columnIndex(pOther->pTab, pList->a[j].zName)<0 ){
00273 sqliteErrorMsg(pParse, "cannot join using column %s - column "
00274 "not present in both tables", pList->a[j].zName);
00275 return 1;
00276 }
00277 addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
00278 }
00279 }
00280 }
00281 return 0;
00282 }
00283
00284
00285
00286
00287 void sqliteSelectDelete(Select *p){
00288 if( p==0 ) return;
00289 sqliteExprListDelete(p->pEList);
00290 sqliteSrcListDelete(p->pSrc);
00291 sqliteExprDelete(p->pWhere);
00292 sqliteExprListDelete(p->pGroupBy);
00293 sqliteExprDelete(p->pHaving);
00294 sqliteExprListDelete(p->pOrderBy);
00295 sqliteSelectDelete(p->pPrior);
00296 sqliteFree(p->zSelect);
00297 sqliteFree(p);
00298 }
00299
00300
00301
00302
00303 static void sqliteAggregateInfoReset(Parse *pParse){
00304 sqliteFree(pParse->aAgg);
00305 pParse->aAgg = 0;
00306 pParse->nAgg = 0;
00307 pParse->useAgg = 0;
00308 }
00309
00310
00311
00312
00313
00314 static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
00315 char *zSortOrder;
00316 int i;
00317 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
00318 if( zSortOrder==0 ) return;
00319 for(i=0; i<pOrderBy->nExpr; i++){
00320 int order = pOrderBy->a[i].sortOrder;
00321 int type;
00322 int c;
00323 if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
00324 type = SQLITE_SO_TEXT;
00325 }else if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_NUM ){
00326 type = SQLITE_SO_NUM;
00327 }else if( pParse->db->file_format>=4 ){
00328 type = sqliteExprType(pOrderBy->a[i].pExpr);
00329 }else{
00330 type = SQLITE_SO_NUM;
00331 }
00332 if( (order & SQLITE_SO_DIRMASK)==SQLITE_SO_ASC ){
00333 c = type==SQLITE_SO_TEXT ? 'A' : '+';
00334 }else{
00335 c = type==SQLITE_SO_TEXT ? 'D' : '-';
00336 }
00337 zSortOrder[i] = c;
00338 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
00339 }
00340 zSortOrder[pOrderBy->nExpr] = 0;
00341 sqliteVdbeOp3(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, P3_DYNAMIC);
00342 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355 void sqliteAddKeyType(Vdbe *v, ExprList *pEList){
00356 int nColumn = pEList->nExpr;
00357 char *zType = sqliteMalloc( nColumn+1 );
00358 int i;
00359 if( zType==0 ) return;
00360 for(i=0; i<nColumn; i++){
00361 zType[i] = sqliteExprType(pEList->a[i].pExpr)==SQLITE_SO_NUM ? 'n' : 't';
00362 }
00363 zType[i] = 0;
00364 sqliteVdbeChangeP3(v, -1, zType, P3_DYNAMIC);
00365 }
00366
00367
00368
00369
00370 static void codeLimiter(
00371 Vdbe *v,
00372 Select *p,
00373 int iContinue,
00374 int iBreak,
00375 int nPop
00376 ){
00377 if( p->iOffset>=0 ){
00378 int addr = sqliteVdbeCurrentAddr(v) + 2;
00379 if( nPop>0 ) addr++;
00380 sqliteVdbeAddOp(v, OP_MemIncr, p->iOffset, addr);
00381 if( nPop>0 ){
00382 sqliteVdbeAddOp(v, OP_Pop, nPop, 0);
00383 }
00384 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
00385 }
00386 if( p->iLimit>=0 ){
00387 sqliteVdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak);
00388 }
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 static int selectInnerLoop(
00401 Parse *pParse,
00402 Select *p,
00403 ExprList *pEList,
00404 int srcTab,
00405 int nColumn,
00406 ExprList *pOrderBy,
00407 int distinct,
00408 int eDest,
00409 int iParm,
00410 int iContinue,
00411 int iBreak
00412 ){
00413 Vdbe *v = pParse->pVdbe;
00414 int i;
00415 int hasDistinct;
00416
00417 if( v==0 ) return 0;
00418 assert( pEList!=0 );
00419
00420
00421
00422
00423 hasDistinct = distinct>=0 && pEList && pEList->nExpr>0;
00424 if( pOrderBy==0 && !hasDistinct ){
00425 codeLimiter(v, p, iContinue, iBreak, 0);
00426 }
00427
00428
00429
00430 if( nColumn>0 ){
00431 for(i=0; i<nColumn; i++){
00432 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
00433 }
00434 }else{
00435 nColumn = pEList->nExpr;
00436 for(i=0; i<pEList->nExpr; i++){
00437 sqliteExprCode(pParse, pEList->a[i].pExpr);
00438 }
00439 }
00440
00441
00442
00443
00444
00445 if( hasDistinct ){
00446 #if NULL_ALWAYS_DISTINCT
00447 sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7);
00448 #endif
00449 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
00450 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pEList);
00451 sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3);
00452 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
00453 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
00454 sqliteVdbeAddOp(v, OP_String, 0, 0);
00455 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
00456 if( pOrderBy==0 ){
00457 codeLimiter(v, p, iContinue, iBreak, nColumn);
00458 }
00459 }
00460
00461 switch( eDest ){
00462
00463
00464
00465 case SRT_Union: {
00466 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
00467 sqliteVdbeAddOp(v, OP_String, 0, 0);
00468 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
00469 break;
00470 }
00471
00472
00473
00474 case SRT_Table:
00475 case SRT_TempTable: {
00476 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
00477 if( pOrderBy ){
00478 pushOntoSorter(pParse, v, pOrderBy);
00479 }else{
00480 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
00481 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
00482 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
00483 }
00484 break;
00485 }
00486
00487
00488
00489
00490
00491 case SRT_Except: {
00492 int addr;
00493 addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
00494 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
00495 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
00496 break;
00497 }
00498
00499
00500
00501
00502
00503 case SRT_Set: {
00504 int addr1 = sqliteVdbeCurrentAddr(v);
00505 int addr2;
00506 assert( nColumn==1 );
00507 sqliteVdbeAddOp(v, OP_NotNull, -1, addr1+3);
00508 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
00509 addr2 = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
00510 if( pOrderBy ){
00511 pushOntoSorter(pParse, v, pOrderBy);
00512 }else{
00513 sqliteVdbeAddOp(v, OP_String, 0, 0);
00514 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
00515 }
00516 sqliteVdbeChangeP2(v, addr2, sqliteVdbeCurrentAddr(v));
00517 break;
00518 }
00519
00520
00521
00522
00523
00524 case SRT_Mem: {
00525 assert( nColumn==1 );
00526 if( pOrderBy ){
00527 pushOntoSorter(pParse, v, pOrderBy);
00528 }else{
00529 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
00530 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
00531 }
00532 break;
00533 }
00534
00535
00536
00537 case SRT_Callback:
00538 case SRT_Sorter: {
00539 if( pOrderBy ){
00540 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
00541 pushOntoSorter(pParse, v, pOrderBy);
00542 }else{
00543 assert( eDest==SRT_Callback );
00544 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0);
00545 }
00546 break;
00547 }
00548
00549
00550
00551
00552 case SRT_Subroutine: {
00553 if( pOrderBy ){
00554 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
00555 pushOntoSorter(pParse, v, pOrderBy);
00556 }else{
00557 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
00558 }
00559 break;
00560 }
00561
00562
00563
00564
00565
00566
00567 default: {
00568 assert( eDest==SRT_Discard );
00569 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
00570 break;
00571 }
00572 }
00573 return 0;
00574 }
00575
00576
00577
00578
00579
00580
00581
00582 static void generateSortTail(
00583 Select *p,
00584 Vdbe *v,
00585 int nColumn,
00586 int eDest,
00587 int iParm
00588 ){
00589 int end1 = sqliteVdbeMakeLabel(v);
00590 int end2 = sqliteVdbeMakeLabel(v);
00591 int addr;
00592 if( eDest==SRT_Sorter ) return;
00593 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
00594 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end1);
00595 codeLimiter(v, p, addr, end2, 1);
00596 switch( eDest ){
00597 case SRT_Callback: {
00598 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
00599 break;
00600 }
00601 case SRT_Table:
00602 case SRT_TempTable: {
00603 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
00604 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
00605 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
00606 break;
00607 }
00608 case SRT_Set: {
00609 assert( nColumn==1 );
00610 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
00611 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
00612 sqliteVdbeAddOp(v, OP_Goto, 0, sqliteVdbeCurrentAddr(v)+3);
00613 sqliteVdbeAddOp(v, OP_String, 0, 0);
00614 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
00615 break;
00616 }
00617 case SRT_Mem: {
00618 assert( nColumn==1 );
00619 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
00620 sqliteVdbeAddOp(v, OP_Goto, 0, end1);
00621 break;
00622 }
00623 case SRT_Subroutine: {
00624 int i;
00625 for(i=0; i<nColumn; i++){
00626 sqliteVdbeAddOp(v, OP_Column, -1-i, i);
00627 }
00628 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
00629 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
00630 break;
00631 }
00632 default: {
00633
00634 break;
00635 }
00636 }
00637 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
00638 sqliteVdbeResolveLabel(v, end2);
00639 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
00640 sqliteVdbeResolveLabel(v, end1);
00641 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
00642 }
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659 static void generateColumnTypes(
00660 Parse *pParse,
00661 SrcList *pTabList,
00662 ExprList *pEList
00663 ){
00664 Vdbe *v = pParse->pVdbe;
00665 int i, j;
00666 for(i=0; i<pEList->nExpr; i++){
00667 Expr *p = pEList->a[i].pExpr;
00668 char *zType = 0;
00669 if( p==0 ) continue;
00670 if( p->op==TK_COLUMN && pTabList ){
00671 Table *pTab;
00672 int iCol = p->iColumn;
00673 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
00674 assert( j<pTabList->nSrc );
00675 pTab = pTabList->a[j].pTab;
00676 if( iCol<0 ) iCol = pTab->iPKey;
00677 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
00678 if( iCol<0 ){
00679 zType = "INTEGER";
00680 }else{
00681 zType = pTab->aCol[iCol].zType;
00682 }
00683 }else{
00684 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
00685 zType = "TEXT";
00686 }else{
00687 zType = "NUMERIC";
00688 }
00689 }
00690 sqliteVdbeOp3(v, OP_ColumnName, i + pEList->nExpr, 0, zType, 0);
00691 }
00692 }
00693
00694
00695
00696
00697
00698
00699 static void generateColumnNames(
00700 Parse *pParse,
00701 SrcList *pTabList,
00702 ExprList *pEList
00703 ){
00704 Vdbe *v = pParse->pVdbe;
00705 int i, j;
00706 sqlite *db = pParse->db;
00707 int fullNames, shortNames;
00708
00709 assert( v!=0 );
00710 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
00711 pParse->colNamesSet = 1;
00712 fullNames = (db->flags & SQLITE_FullColNames)!=0;
00713 shortNames = (db->flags & SQLITE_ShortColNames)!=0;
00714 for(i=0; i<pEList->nExpr; i++){
00715 Expr *p;
00716 int p2 = i==pEList->nExpr-1;
00717 p = pEList->a[i].pExpr;
00718 if( p==0 ) continue;
00719 if( pEList->a[i].zName ){
00720 char *zName = pEList->a[i].zName;
00721 sqliteVdbeOp3(v, OP_ColumnName, i, p2, zName, 0);
00722 continue;
00723 }
00724 if( p->op==TK_COLUMN && pTabList ){
00725 Table *pTab;
00726 char *zCol;
00727 int iCol = p->iColumn;
00728 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
00729 assert( j<pTabList->nSrc );
00730 pTab = pTabList->a[j].pTab;
00731 if( iCol<0 ) iCol = pTab->iPKey;
00732 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
00733 if( iCol<0 ){
00734 zCol = "_ROWID_";
00735 }else{
00736 zCol = pTab->aCol[iCol].zName;
00737 }
00738 if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){
00739 int addr = sqliteVdbeOp3(v,OP_ColumnName, i, p2, p->span.z, p->span.n);
00740 sqliteVdbeCompressSpace(v, addr);
00741 }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){
00742 char *zName = 0;
00743 char *zTab;
00744
00745 zTab = pTabList->a[j].zAlias;
00746 if( fullNames || zTab==0 ) zTab = pTab->zName;
00747 sqliteSetString(&zName, zTab, ".", zCol, 0);
00748 sqliteVdbeOp3(v, OP_ColumnName, i, p2, zName, P3_DYNAMIC);
00749 }else{
00750 sqliteVdbeOp3(v, OP_ColumnName, i, p2, zCol, 0);
00751 }
00752 }else if( p->span.z && p->span.z[0] ){
00753 int addr = sqliteVdbeOp3(v,OP_ColumnName, i, p2, p->span.z, p->span.n);
00754 sqliteVdbeCompressSpace(v, addr);
00755 }else{
00756 char zName[30];
00757 assert( p->op!=TK_COLUMN || pTabList==0 );
00758 sprintf(zName, "column%d", i+1);
00759 sqliteVdbeOp3(v, OP_ColumnName, i, p2, zName, 0);
00760 }
00761 }
00762 }
00763
00764
00765
00766
00767 static const char *selectOpName(int id){
00768 char *z;
00769 switch( id ){
00770 case TK_ALL: z = "UNION ALL"; break;
00771 case TK_INTERSECT: z = "INTERSECT"; break;
00772 case TK_EXCEPT: z = "EXCEPT"; break;
00773 default: z = "UNION"; break;
00774 }
00775 return z;
00776 }
00777
00778
00779
00780
00781 static int fillInColumnList(Parse*, Select*);
00782
00783
00784
00785
00786
00787 Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
00788 Table *pTab;
00789 int i, j;
00790 ExprList *pEList;
00791 Column *aCol;
00792
00793 if( fillInColumnList(pParse, pSelect) ){
00794 return 0;
00795 }
00796 pTab = sqliteMalloc( sizeof(Table) );
00797 if( pTab==0 ){
00798 return 0;
00799 }
00800 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
00801 pEList = pSelect->pEList;
00802 pTab->nCol = pEList->nExpr;
00803 assert( pTab->nCol>0 );
00804 pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
00805 for(i=0; i<pTab->nCol; i++){
00806 Expr *p, *pR;
00807 if( pEList->a[i].zName ){
00808 aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
00809 }else if( (p=pEList->a[i].pExpr)->op==TK_DOT
00810 && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
00811 int cnt;
00812 sqliteSetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0);
00813 for(j=cnt=0; j<i; j++){
00814 if( sqliteStrICmp(aCol[j].zName, aCol[i].zName)==0 ){
00815 int n;
00816 char zBuf[30];
00817 sprintf(zBuf,"_%d",++cnt);
00818 n = strlen(zBuf);
00819 sqliteSetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf, n,0);
00820 j = -1;
00821 }
00822 }
00823 }else if( p->span.z && p->span.z[0] ){
00824 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
00825 }else{
00826 char zBuf[30];
00827 sprintf(zBuf, "column%d", i+1);
00828 aCol[i].zName = sqliteStrDup(zBuf);
00829 }
00830 sqliteDequote(aCol[i].zName);
00831 }
00832 pTab->iPKey = -1;
00833 return pTab;
00834 }
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858 static int fillInColumnList(Parse *pParse, Select *p){
00859 int i, j, k, rc;
00860 SrcList *pTabList;
00861 ExprList *pEList;
00862 Table *pTab;
00863
00864 if( p==0 || p->pSrc==0 ) return 1;
00865 pTabList = p->pSrc;
00866 pEList = p->pEList;
00867
00868
00869
00870 for(i=0; i<pTabList->nSrc; i++){
00871 if( pTabList->a[i].pTab ){
00872
00873 return 0;
00874 }
00875 if( pTabList->a[i].zName==0 ){
00876
00877 assert( pTabList->a[i].pSelect!=0 );
00878 if( pTabList->a[i].zAlias==0 ){
00879 char zFakeName[60];
00880 sprintf(zFakeName, "sqlite_subquery_%p_",
00881 (void*)pTabList->a[i].pSelect);
00882 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
00883 }
00884 pTabList->a[i].pTab = pTab =
00885 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
00886 pTabList->a[i].pSelect);
00887 if( pTab==0 ){
00888 return 1;
00889 }
00890
00891
00892
00893
00894 pTab->isTransient = 1;
00895 }else{
00896
00897 pTabList->a[i].pTab = pTab =
00898 sqliteLocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase);
00899 if( pTab==0 ){
00900 return 1;
00901 }
00902 if( pTab->pSelect ){
00903
00904 if( sqliteViewGetColumnNames(pParse, pTab) ){
00905 return 1;
00906 }
00907
00908
00909
00910
00911
00912 if( pTabList->a[i].pSelect==0 ){
00913 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
00914 }
00915 }
00916 }
00917 }
00918
00919
00920
00921 if( sqliteProcessJoin(pParse, p) ) return 1;
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933 for(k=0; k<pEList->nExpr; k++){
00934 Expr *pE = pEList->a[k].pExpr;
00935 if( pE->op==TK_ALL ) break;
00936 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
00937 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
00938 }
00939 rc = 0;
00940 if( k<pEList->nExpr ){
00941
00942
00943
00944
00945
00946 struct ExprList_item *a = pEList->a;
00947 ExprList *pNew = 0;
00948 for(k=0; k<pEList->nExpr; k++){
00949 Expr *pE = a[k].pExpr;
00950 if( pE->op!=TK_ALL &&
00951 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
00952
00953
00954 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
00955 pNew->a[pNew->nExpr-1].zName = a[k].zName;
00956 a[k].pExpr = 0;
00957 a[k].zName = 0;
00958 }else{
00959
00960
00961 int tableSeen = 0;
00962 char *zTName;
00963 if( pE->op==TK_DOT && pE->pLeft ){
00964 zTName = sqliteTableNameFromToken(&pE->pLeft->token);
00965 }else{
00966 zTName = 0;
00967 }
00968 for(i=0; i<pTabList->nSrc; i++){
00969 Table *pTab = pTabList->a[i].pTab;
00970 char *zTabName = pTabList->a[i].zAlias;
00971 if( zTabName==0 || zTabName[0]==0 ){
00972 zTabName = pTab->zName;
00973 }
00974 if( zTName && (zTabName==0 || zTabName[0]==0 ||
00975 sqliteStrICmp(zTName, zTabName)!=0) ){
00976 continue;
00977 }
00978 tableSeen = 1;
00979 for(j=0; j<pTab->nCol; j++){
00980 Expr *pExpr, *pLeft, *pRight;
00981 char *zName = pTab->aCol[j].zName;
00982
00983 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
00984 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
00985
00986
00987 continue;
00988 }
00989 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
00990
00991
00992 continue;
00993 }
00994 pRight = sqliteExpr(TK_ID, 0, 0, 0);
00995 if( pRight==0 ) break;
00996 pRight->token.z = zName;
00997 pRight->token.n = strlen(zName);
00998 pRight->token.dyn = 0;
00999 if( zTabName && pTabList->nSrc>1 ){
01000 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
01001 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
01002 if( pExpr==0 ) break;
01003 pLeft->token.z = zTabName;
01004 pLeft->token.n = strlen(zTabName);
01005 pLeft->token.dyn = 0;
01006 sqliteSetString((char**)&pExpr->span.z, zTabName, ".", zName, 0);
01007 pExpr->span.n = strlen(pExpr->span.z);
01008 pExpr->span.dyn = 1;
01009 pExpr->token.z = 0;
01010 pExpr->token.n = 0;
01011 pExpr->token.dyn = 0;
01012 }else{
01013 pExpr = pRight;
01014 pExpr->span = pExpr->token;
01015 }
01016 pNew = sqliteExprListAppend(pNew, pExpr, 0);
01017 }
01018 }
01019 if( !tableSeen ){
01020 if( zTName ){
01021 sqliteErrorMsg(pParse, "no such table: %s", zTName);
01022 }else{
01023 sqliteErrorMsg(pParse, "no tables specified");
01024 }
01025 rc = 1;
01026 }
01027 sqliteFree(zTName);
01028 }
01029 }
01030 sqliteExprListDelete(pEList);
01031 p->pEList = pNew;
01032 }
01033 return rc;
01034 }
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049 void sqliteSelectUnbind(Select *p){
01050 int i;
01051 SrcList *pSrc = p->pSrc;
01052 Table *pTab;
01053 if( p==0 ) return;
01054 for(i=0; i<pSrc->nSrc; i++){
01055 if( (pTab = pSrc->a[i].pTab)!=0 ){
01056 if( pTab->isTransient ){
01057 sqliteDeleteTable(0, pTab);
01058 }
01059 pSrc->a[i].pTab = 0;
01060 if( pSrc->a[i].pSelect ){
01061 sqliteSelectUnbind(pSrc->a[i].pSelect);
01062 }
01063 }
01064 }
01065 }
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087 static int matchOrderbyToColumn(
01088 Parse *pParse,
01089 Select *pSelect,
01090 ExprList *pOrderBy,
01091 int iTable,
01092 int mustComplete
01093 ){
01094 int nErr = 0;
01095 int i, j;
01096 ExprList *pEList;
01097
01098 if( pSelect==0 || pOrderBy==0 ) return 1;
01099 if( mustComplete ){
01100 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
01101 }
01102 if( fillInColumnList(pParse, pSelect) ){
01103 return 1;
01104 }
01105 if( pSelect->pPrior ){
01106 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
01107 return 1;
01108 }
01109 }
01110 pEList = pSelect->pEList;
01111 for(i=0; i<pOrderBy->nExpr; i++){
01112 Expr *pE = pOrderBy->a[i].pExpr;
01113 int iCol = -1;
01114 if( pOrderBy->a[i].done ) continue;
01115 if( sqliteExprIsInteger(pE, &iCol) ){
01116 if( iCol<=0 || iCol>pEList->nExpr ){
01117 sqliteErrorMsg(pParse,
01118 "ORDER BY position %d should be between 1 and %d",
01119 iCol, pEList->nExpr);
01120 nErr++;
01121 break;
01122 }
01123 if( !mustComplete ) continue;
01124 iCol--;
01125 }
01126 for(j=0; iCol<0 && j<pEList->nExpr; j++){
01127 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
01128 char *zName, *zLabel;
01129 zName = pEList->a[j].zName;
01130 assert( pE->token.z );
01131 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
01132 sqliteDequote(zLabel);
01133 if( sqliteStrICmp(zName, zLabel)==0 ){
01134 iCol = j;
01135 }
01136 sqliteFree(zLabel);
01137 }
01138 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
01139 iCol = j;
01140 }
01141 }
01142 if( iCol>=0 ){
01143 pE->op = TK_COLUMN;
01144 pE->iColumn = iCol;
01145 pE->iTable = iTable;
01146 pOrderBy->a[i].done = 1;
01147 }
01148 if( iCol<0 && mustComplete ){
01149 sqliteErrorMsg(pParse,
01150 "ORDER BY term number %d does not match any result column", i+1);
01151 nErr++;
01152 break;
01153 }
01154 }
01155 return nErr;
01156 }
01157
01158
01159
01160
01161
01162 Vdbe *sqliteGetVdbe(Parse *pParse){
01163 Vdbe *v = pParse->pVdbe;
01164 if( v==0 ){
01165 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
01166 }
01167 return v;
01168 }
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197 static void multiSelectSortOrder(Select *p, ExprList *pOrderBy){
01198 int i;
01199 ExprList *pEList;
01200 if( pOrderBy==0 ) return;
01201 if( p==0 ){
01202 for(i=0; i<pOrderBy->nExpr; i++){
01203 pOrderBy->a[i].pExpr->dataType = SQLITE_SO_TEXT;
01204 }
01205 return;
01206 }
01207 multiSelectSortOrder(p->pPrior, pOrderBy);
01208 pEList = p->pEList;
01209 for(i=0; i<pOrderBy->nExpr; i++){
01210 Expr *pE = pOrderBy->a[i].pExpr;
01211 if( pE->dataType==SQLITE_SO_NUM ) continue;
01212 assert( pE->iColumn>=0 );
01213 if( pEList->nExpr>pE->iColumn ){
01214 pE->dataType = sqliteExprType(pEList->a[pE->iColumn].pExpr);
01215 }
01216 }
01217 }
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237 static void computeLimitRegisters(Parse *pParse, Select *p){
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247 if( p->nLimit>=0 ){
01248 int iMem = pParse->nMem++;
01249 Vdbe *v = sqliteGetVdbe(pParse);
01250 if( v==0 ) return;
01251 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
01252 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
01253 p->iLimit = iMem;
01254 }
01255 if( p->nOffset>0 ){
01256 int iMem = pParse->nMem++;
01257 Vdbe *v = sqliteGetVdbe(pParse);
01258 if( v==0 ) return;
01259 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
01260 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
01261 p->iOffset = iMem;
01262 }
01263 }
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295 static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
01296 int rc;
01297 Select *pPrior;
01298 Vdbe *v;
01299
01300
01301
01302
01303 if( p==0 || p->pPrior==0 ) return 1;
01304 pPrior = p->pPrior;
01305 if( pPrior->pOrderBy ){
01306 sqliteErrorMsg(pParse,"ORDER BY clause should come after %s not before",
01307 selectOpName(p->op));
01308 return 1;
01309 }
01310 if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){
01311 sqliteErrorMsg(pParse,"LIMIT clause should come after %s not before",
01312 selectOpName(p->op));
01313 return 1;
01314 }
01315
01316
01317
01318 v = sqliteGetVdbe(pParse);
01319 if( v==0 ) return 1;
01320
01321
01322
01323 if( eDest==SRT_TempTable ){
01324 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
01325 eDest = SRT_Table;
01326 }
01327
01328
01329
01330 switch( p->op ){
01331 case TK_ALL: {
01332 if( p->pOrderBy==0 ){
01333 pPrior->nLimit = p->nLimit;
01334 pPrior->nOffset = p->nOffset;
01335 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
01336 if( rc ) return rc;
01337 p->pPrior = 0;
01338 p->iLimit = pPrior->iLimit;
01339 p->iOffset = pPrior->iOffset;
01340 p->nLimit = -1;
01341 p->nOffset = 0;
01342 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
01343 p->pPrior = pPrior;
01344 if( rc ) return rc;
01345 break;
01346 }
01347
01348 }
01349 case TK_EXCEPT:
01350 case TK_UNION: {
01351 int unionTab;
01352 int op;
01353 int priorOp;
01354 int nLimit, nOffset;
01355 ExprList *pOrderBy;
01356
01357 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
01358 if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){
01359
01360
01361
01362 unionTab = iParm;
01363 }else{
01364
01365
01366
01367 unionTab = pParse->nTab++;
01368 if( p->pOrderBy
01369 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
01370 return 1;
01371 }
01372 if( p->op!=TK_ALL ){
01373 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
01374 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
01375 }else{
01376 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
01377 }
01378 }
01379
01380
01381
01382 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
01383 if( rc ) return rc;
01384
01385
01386
01387 switch( p->op ){
01388 case TK_EXCEPT: op = SRT_Except; break;
01389 case TK_UNION: op = SRT_Union; break;
01390 case TK_ALL: op = SRT_Table; break;
01391 }
01392 p->pPrior = 0;
01393 pOrderBy = p->pOrderBy;
01394 p->pOrderBy = 0;
01395 nLimit = p->nLimit;
01396 p->nLimit = -1;
01397 nOffset = p->nOffset;
01398 p->nOffset = 0;
01399 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
01400 p->pPrior = pPrior;
01401 p->pOrderBy = pOrderBy;
01402 p->nLimit = nLimit;
01403 p->nOffset = nOffset;
01404 if( rc ) return rc;
01405
01406
01407
01408
01409 if( eDest!=priorOp || unionTab!=iParm ){
01410 int iCont, iBreak, iStart;
01411 assert( p->pEList );
01412 if( eDest==SRT_Callback ){
01413 generateColumnNames(pParse, 0, p->pEList);
01414 generateColumnTypes(pParse, p->pSrc, p->pEList);
01415 }
01416 iBreak = sqliteVdbeMakeLabel(v);
01417 iCont = sqliteVdbeMakeLabel(v);
01418 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
01419 computeLimitRegisters(pParse, p);
01420 iStart = sqliteVdbeCurrentAddr(v);
01421 multiSelectSortOrder(p, p->pOrderBy);
01422 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
01423 p->pOrderBy, -1, eDest, iParm,
01424 iCont, iBreak);
01425 if( rc ) return 1;
01426 sqliteVdbeResolveLabel(v, iCont);
01427 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
01428 sqliteVdbeResolveLabel(v, iBreak);
01429 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
01430 if( p->pOrderBy ){
01431 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
01432 }
01433 }
01434 break;
01435 }
01436 case TK_INTERSECT: {
01437 int tab1, tab2;
01438 int iCont, iBreak, iStart;
01439 int nLimit, nOffset;
01440
01441
01442
01443
01444
01445 tab1 = pParse->nTab++;
01446 tab2 = pParse->nTab++;
01447 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
01448 return 1;
01449 }
01450 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
01451 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
01452
01453
01454
01455 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
01456 if( rc ) return rc;
01457
01458
01459
01460 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
01461 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
01462 p->pPrior = 0;
01463 nLimit = p->nLimit;
01464 p->nLimit = -1;
01465 nOffset = p->nOffset;
01466 p->nOffset = 0;
01467 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
01468 p->pPrior = pPrior;
01469 p->nLimit = nLimit;
01470 p->nOffset = nOffset;
01471 if( rc ) return rc;
01472
01473
01474
01475
01476 assert( p->pEList );
01477 if( eDest==SRT_Callback ){
01478 generateColumnNames(pParse, 0, p->pEList);
01479 generateColumnTypes(pParse, p->pSrc, p->pEList);
01480 }
01481 iBreak = sqliteVdbeMakeLabel(v);
01482 iCont = sqliteVdbeMakeLabel(v);
01483 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
01484 computeLimitRegisters(pParse, p);
01485 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
01486 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
01487 multiSelectSortOrder(p, p->pOrderBy);
01488 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
01489 p->pOrderBy, -1, eDest, iParm,
01490 iCont, iBreak);
01491 if( rc ) return 1;
01492 sqliteVdbeResolveLabel(v, iCont);
01493 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
01494 sqliteVdbeResolveLabel(v, iBreak);
01495 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
01496 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
01497 if( p->pOrderBy ){
01498 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
01499 }
01500 break;
01501 }
01502 }
01503 assert( p->pEList && pPrior->pEList );
01504 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
01505 sqliteErrorMsg(pParse, "SELECTs to the left and right of %s"
01506 " do not have the same number of result columns", selectOpName(p->op));
01507 return 1;
01508 }
01509 return 0;
01510 }
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525 static void substExprList(ExprList*,int,ExprList*);
01526 static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){
01527 if( pExpr==0 ) return;
01528 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
01529 if( pExpr->iColumn<0 ){
01530 pExpr->op = TK_NULL;
01531 }else{
01532 Expr *pNew;
01533 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
01534 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
01535 pNew = pEList->a[pExpr->iColumn].pExpr;
01536 assert( pNew!=0 );
01537 pExpr->op = pNew->op;
01538 pExpr->dataType = pNew->dataType;
01539 assert( pExpr->pLeft==0 );
01540 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
01541 assert( pExpr->pRight==0 );
01542 pExpr->pRight = sqliteExprDup(pNew->pRight);
01543 assert( pExpr->pList==0 );
01544 pExpr->pList = sqliteExprListDup(pNew->pList);
01545 pExpr->iTable = pNew->iTable;
01546 pExpr->iColumn = pNew->iColumn;
01547 pExpr->iAgg = pNew->iAgg;
01548 sqliteTokenCopy(&pExpr->token, &pNew->token);
01549 sqliteTokenCopy(&pExpr->span, &pNew->span);
01550 }
01551 }else{
01552 substExpr(pExpr->pLeft, iTable, pEList);
01553 substExpr(pExpr->pRight, iTable, pEList);
01554 substExprList(pExpr->pList, iTable, pEList);
01555 }
01556 }
01557 static void
01558 substExprList(ExprList *pList, int iTable, ExprList *pEList){
01559 int i;
01560 if( pList==0 ) return;
01561 for(i=0; i<pList->nExpr; i++){
01562 substExpr(pList->a[i].pExpr, iTable, pEList);
01563 }
01564 }
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635 static int flattenSubquery(
01636 Parse *pParse,
01637 Select *p,
01638 int iFrom,
01639 int isAgg,
01640 int subqueryIsAgg
01641 ){
01642 Select *pSub;
01643 SrcList *pSrc;
01644 SrcList *pSubSrc;
01645 ExprList *pList;
01646 int iParent;
01647 int i;
01648 Expr *pWhere;
01649
01650
01651
01652 if( p==0 ) return 0;
01653 pSrc = p->pSrc;
01654 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
01655 pSub = pSrc->a[iFrom].pSelect;
01656 assert( pSub!=0 );
01657 if( isAgg && subqueryIsAgg ) return 0;
01658 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
01659 pSubSrc = pSub->pSrc;
01660 assert( pSubSrc );
01661 if( pSubSrc->nSrc==0 ) return 0;
01662 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
01663 return 0;
01664 }
01665 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
01666 if( p->pOrderBy && pSub->pOrderBy ) return 0;
01667
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680 if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){
01681 return 0;
01682 }
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695
01696
01697 if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0
01698 && pSub->pWhere!=0 ){
01699 return 0;
01700 }
01701
01702
01703
01704
01705
01706
01707
01708
01709
01710
01711
01712
01713
01714 iParent = pSrc->a[iFrom].iCursor;
01715 {
01716 int nSubSrc = pSubSrc->nSrc;
01717 int jointype = pSrc->a[iFrom].jointype;
01718
01719 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
01720 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
01721 }
01722 sqliteFree(pSrc->a[iFrom].zDatabase);
01723 sqliteFree(pSrc->a[iFrom].zName);
01724 sqliteFree(pSrc->a[iFrom].zAlias);
01725 if( nSubSrc>1 ){
01726 int extra = nSubSrc - 1;
01727 for(i=1; i<nSubSrc; i++){
01728 pSrc = sqliteSrcListAppend(pSrc, 0, 0);
01729 }
01730 p->pSrc = pSrc;
01731 for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
01732 pSrc->a[i] = pSrc->a[i-extra];
01733 }
01734 }
01735 for(i=0; i<nSubSrc; i++){
01736 pSrc->a[i+iFrom] = pSubSrc->a[i];
01737 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
01738 }
01739 pSrc->a[iFrom+nSubSrc-1].jointype = jointype;
01740 }
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754 substExprList(p->pEList, iParent, pSub->pEList);
01755 pList = p->pEList;
01756 for(i=0; i<pList->nExpr; i++){
01757 Expr *pExpr;
01758 if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
01759 pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
01760 }
01761 }
01762 if( isAgg ){
01763 substExprList(p->pGroupBy, iParent, pSub->pEList);
01764 substExpr(p->pHaving, iParent, pSub->pEList);
01765 }
01766 if( pSub->pOrderBy ){
01767 assert( p->pOrderBy==0 );
01768 p->pOrderBy = pSub->pOrderBy;
01769 pSub->pOrderBy = 0;
01770 }else if( p->pOrderBy ){
01771 substExprList(p->pOrderBy, iParent, pSub->pEList);
01772 }
01773 if( pSub->pWhere ){
01774 pWhere = sqliteExprDup(pSub->pWhere);
01775 }else{
01776 pWhere = 0;
01777 }
01778 if( subqueryIsAgg ){
01779 assert( p->pHaving==0 );
01780 p->pHaving = p->pWhere;
01781 p->pWhere = pWhere;
01782 substExpr(p->pHaving, iParent, pSub->pEList);
01783 if( pSub->pHaving ){
01784 Expr *pHaving = sqliteExprDup(pSub->pHaving);
01785 if( p->pHaving ){
01786 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
01787 }else{
01788 p->pHaving = pHaving;
01789 }
01790 }
01791 assert( p->pGroupBy==0 );
01792 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
01793 }else if( p->pWhere==0 ){
01794 p->pWhere = pWhere;
01795 }else{
01796 substExpr(p->pWhere, iParent, pSub->pEList);
01797 if( pWhere ){
01798 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
01799 }
01800 }
01801
01802
01803
01804
01805 p->isDistinct = p->isDistinct || pSub->isDistinct;
01806
01807
01808
01809
01810 if( pSub->nLimit>=0 ){
01811 if( p->nLimit<0 ){
01812 p->nLimit = pSub->nLimit;
01813 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
01814 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
01815 }
01816 }
01817 p->nOffset += pSub->nOffset;
01818
01819
01820
01821
01822 sqliteSelectDelete(pSub);
01823 return 1;
01824 }
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846 static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
01847 Expr *pExpr;
01848 int iCol;
01849 Table *pTab;
01850 Index *pIdx;
01851 int base;
01852 Vdbe *v;
01853 int seekOp;
01854 int cont;
01855 ExprList *pEList, *pList, eList;
01856 struct ExprList_item eListItem;
01857 SrcList *pSrc;
01858
01859
01860
01861
01862
01863 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
01864 pSrc = p->pSrc;
01865 if( pSrc->nSrc!=1 ) return 0;
01866 pEList = p->pEList;
01867 if( pEList->nExpr!=1 ) return 0;
01868 pExpr = pEList->a[0].pExpr;
01869 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
01870 pList = pExpr->pList;
01871 if( pList==0 || pList->nExpr!=1 ) return 0;
01872 if( pExpr->token.n!=3 ) return 0;
01873 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
01874 seekOp = OP_Rewind;
01875 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
01876 seekOp = OP_Last;
01877 }else{
01878 return 0;
01879 }
01880 pExpr = pList->a[0].pExpr;
01881 if( pExpr->op!=TK_COLUMN ) return 0;
01882 iCol = pExpr->iColumn;
01883 pTab = pSrc->a[0].pTab;
01884
01885
01886
01887
01888
01889
01890
01891 if( iCol<0 ){
01892 pIdx = 0;
01893 }else{
01894 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
01895 assert( pIdx->nColumn>=1 );
01896 if( pIdx->aiColumn[0]==iCol ) break;
01897 }
01898 if( pIdx==0 ) return 0;
01899 }
01900
01901
01902
01903
01904
01905 v = sqliteGetVdbe(pParse);
01906 if( v==0 ) return 0;
01907 if( eDest==SRT_Callback ){
01908 generateColumnTypes(pParse, p->pSrc, p->pEList);
01909 }
01910
01911
01912
01913 if( eDest==SRT_TempTable ){
01914 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
01915 }
01916
01917
01918
01919
01920
01921
01922 sqliteCodeVerifySchema(pParse, pTab->iDb);
01923 base = pSrc->a[0].iCursor;
01924 computeLimitRegisters(pParse, p);
01925 if( pSrc->a[0].pSelect==0 ){
01926 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
01927 sqliteVdbeOp3(v, OP_OpenRead, base, pTab->tnum, pTab->zName, 0);
01928 }
01929 cont = sqliteVdbeMakeLabel(v);
01930 if( pIdx==0 ){
01931 sqliteVdbeAddOp(v, seekOp, base, 0);
01932 }else{
01933 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
01934 sqliteVdbeOp3(v, OP_OpenRead, base+1, pIdx->tnum, pIdx->zName, P3_STATIC);
01935 if( seekOp==OP_Rewind ){
01936 sqliteVdbeAddOp(v, OP_String, 0, 0);
01937 sqliteVdbeAddOp(v, OP_MakeKey, 1, 0);
01938 sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
01939 seekOp = OP_MoveTo;
01940 }
01941 sqliteVdbeAddOp(v, seekOp, base+1, 0);
01942 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
01943 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
01944 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
01945 }
01946 eList.nExpr = 1;
01947 memset(&eListItem, 0, sizeof(eListItem));
01948 eList.a = &eListItem;
01949 eList.a[0].pExpr = pExpr;
01950 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
01951 sqliteVdbeResolveLabel(v, cont);
01952 sqliteVdbeAddOp(v, OP_Close, base, 0);
01953
01954 return 1;
01955 }
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987
01988
01989
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005
02006
02007
02008
02009 int sqliteSelect(
02010 Parse *pParse,
02011 Select *p,
02012 int eDest,
02013 int iParm,
02014 Select *pParent,
02015 int parentTab,
02016 int *pParentAgg
02017 ){
02018 int i;
02019 WhereInfo *pWInfo;
02020 Vdbe *v;
02021 int isAgg = 0;
02022 ExprList *pEList;
02023 SrcList *pTabList;
02024 Expr *pWhere;
02025 ExprList *pOrderBy;
02026 ExprList *pGroupBy;
02027 Expr *pHaving;
02028 int isDistinct;
02029 int distinct;
02030 int rc = 1;
02031
02032 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
02033 if( sqliteAuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
02034
02035
02036
02037 if( p->pPrior ){
02038 return multiSelect(pParse, p, eDest, iParm);
02039 }
02040
02041
02042
02043 pTabList = p->pSrc;
02044 pWhere = p->pWhere;
02045 pOrderBy = p->pOrderBy;
02046 pGroupBy = p->pGroupBy;
02047 pHaving = p->pHaving;
02048 isDistinct = p->isDistinct;
02049
02050
02051
02052 sqliteSrcListAssignCursors(pParse, pTabList);
02053
02054
02055
02056
02057
02058 if( pParse->nErr>0 ) goto select_end;
02059
02060
02061
02062
02063
02064 if( fillInColumnList(pParse, p) ){
02065 goto select_end;
02066 }
02067 pWhere = p->pWhere;
02068 pEList = p->pEList;
02069 if( pEList==0 ) goto select_end;
02070
02071
02072
02073
02074 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
02075 sqliteErrorMsg(pParse, "only a single result allowed for "
02076 "a SELECT that is part of an expression");
02077 goto select_end;
02078 }
02079
02080
02081
02082 switch( eDest ){
02083 case SRT_Union:
02084 case SRT_Except:
02085 case SRT_Discard:
02086 pOrderBy = 0;
02087 break;
02088 default:
02089 break;
02090 }
02091
02092
02093
02094
02095
02096
02097 for(i=0; i<pEList->nExpr; i++){
02098 if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){
02099 goto select_end;
02100 }
02101 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
02102 goto select_end;
02103 }
02104 }
02105 if( pWhere ){
02106 if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){
02107 goto select_end;
02108 }
02109 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
02110 goto select_end;
02111 }
02112 }
02113 if( pHaving ){
02114 if( pGroupBy==0 ){
02115 sqliteErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
02116 goto select_end;
02117 }
02118 if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){
02119 goto select_end;
02120 }
02121 if( sqliteExprCheck(pParse, pHaving, 1, &isAgg) ){
02122 goto select_end;
02123 }
02124 }
02125 if( pOrderBy ){
02126 for(i=0; i<pOrderBy->nExpr; i++){
02127 int iCol;
02128 Expr *pE = pOrderBy->a[i].pExpr;
02129 if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
02130 sqliteExprDelete(pE);
02131 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
02132 }
02133 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
02134 goto select_end;
02135 }
02136 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
02137 goto select_end;
02138 }
02139 if( sqliteExprIsConstant(pE) ){
02140 if( sqliteExprIsInteger(pE, &iCol)==0 ){
02141 sqliteErrorMsg(pParse,
02142 "ORDER BY terms must not be non-integer constants");
02143 goto select_end;
02144 }else if( iCol<=0 || iCol>pEList->nExpr ){
02145 sqliteErrorMsg(pParse,
02146 "ORDER BY column number %d out of range - should be "
02147 "between 1 and %d", iCol, pEList->nExpr);
02148 goto select_end;
02149 }
02150 }
02151 }
02152 }
02153 if( pGroupBy ){
02154 for(i=0; i<pGroupBy->nExpr; i++){
02155 int iCol;
02156 Expr *pE = pGroupBy->a[i].pExpr;
02157 if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
02158 sqliteExprDelete(pE);
02159 pE = pGroupBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
02160 }
02161 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
02162 goto select_end;
02163 }
02164 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
02165 goto select_end;
02166 }
02167 if( sqliteExprIsConstant(pE) ){
02168 if( sqliteExprIsInteger(pE, &iCol)==0 ){
02169 sqliteErrorMsg(pParse,
02170 "GROUP BY terms must not be non-integer constants");
02171 goto select_end;
02172 }else if( iCol<=0 || iCol>pEList->nExpr ){
02173 sqliteErrorMsg(pParse,
02174 "GROUP BY column number %d out of range - should be "
02175 "between 1 and %d", iCol, pEList->nExpr);
02176 goto select_end;
02177 }
02178 }
02179 }
02180 }
02181
02182
02183
02184 v = sqliteGetVdbe(pParse);
02185 if( v==0 ) goto select_end;
02186
02187
02188
02189
02190 if( eDest==SRT_Callback ){
02191 generateColumnNames(pParse, pTabList, pEList);
02192 }
02193
02194
02195
02196 for(i=0; i<pTabList->nSrc; i++){
02197 const char *zSavedAuthContext;
02198 int needRestoreContext;
02199
02200 if( pTabList->a[i].pSelect==0 ) continue;
02201 if( pTabList->a[i].zName!=0 ){
02202 zSavedAuthContext = pParse->zAuthContext;
02203 pParse->zAuthContext = pTabList->a[i].zName;
02204 needRestoreContext = 1;
02205 }else{
02206 needRestoreContext = 0;
02207 }
02208 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable,
02209 pTabList->a[i].iCursor, p, i, &isAgg);
02210 if( needRestoreContext ){
02211 pParse->zAuthContext = zSavedAuthContext;
02212 }
02213 pTabList = p->pSrc;
02214 pWhere = p->pWhere;
02215 if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){
02216 pOrderBy = p->pOrderBy;
02217 }
02218 pGroupBy = p->pGroupBy;
02219 pHaving = p->pHaving;
02220 isDistinct = p->isDistinct;
02221 }
02222
02223
02224
02225
02226 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
02227 rc = 0;
02228 goto select_end;
02229 }
02230
02231
02232
02233
02234 if( pParent && pParentAgg &&
02235 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
02236 if( isAgg ) *pParentAgg = 1;
02237 return rc;
02238 }
02239
02240
02241
02242 computeLimitRegisters(pParse, p);
02243
02244
02245
02246
02247
02248
02249
02250
02251
02252
02253 if( eDest==SRT_Callback ){
02254 generateColumnTypes(pParse, pTabList, pEList);
02255 }
02256
02257
02258
02259 if( eDest==SRT_TempTable ){
02260 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
02261 }
02262
02263
02264
02265 sqliteAggregateInfoReset(pParse);
02266 if( isAgg || pGroupBy ){
02267 assert( pParse->nAgg==0 );
02268 isAgg = 1;
02269 for(i=0; i<pEList->nExpr; i++){
02270 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
02271 goto select_end;
02272 }
02273 }
02274 if( pGroupBy ){
02275 for(i=0; i<pGroupBy->nExpr; i++){
02276 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
02277 goto select_end;
02278 }
02279 }
02280 }
02281 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
02282 goto select_end;
02283 }
02284 if( pOrderBy ){
02285 for(i=0; i<pOrderBy->nExpr; i++){
02286 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
02287 goto select_end;
02288 }
02289 }
02290 }
02291 }
02292
02293
02294
02295 if( isAgg ){
02296 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
02297 for(i=0; i<pParse->nAgg; i++){
02298 FuncDef *pFunc;
02299 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
02300 sqliteVdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_POINTER);
02301 }
02302 }
02303 if( pGroupBy==0 ){
02304 sqliteVdbeAddOp(v, OP_String, 0, 0);
02305 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
02306 }
02307 }
02308
02309
02310
02311 if( eDest==SRT_Mem ){
02312 sqliteVdbeAddOp(v, OP_String, 0, 0);
02313 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
02314 }
02315
02316
02317
02318 if( isDistinct ){
02319 distinct = pParse->nTab++;
02320 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
02321 }else{
02322 distinct = -1;
02323 }
02324
02325
02326
02327 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0,
02328 pGroupBy ? 0 : &pOrderBy);
02329 if( pWInfo==0 ) goto select_end;
02330
02331
02332
02333
02334 if( !isAgg ){
02335 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
02336 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
02337 goto select_end;
02338 }
02339 }
02340
02341
02342
02343
02344 else{
02345 AggExpr *pAgg;
02346 if( pGroupBy ){
02347 int lbl1;
02348 for(i=0; i<pGroupBy->nExpr; i++){
02349 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
02350 }
02351 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
02352 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
02353 lbl1 = sqliteVdbeMakeLabel(v);
02354 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
02355 for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){
02356 if( pAgg->isAgg ) continue;
02357 sqliteExprCode(pParse, pAgg->pExpr);
02358 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
02359 }
02360 sqliteVdbeResolveLabel(v, lbl1);
02361 }
02362 for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){
02363 Expr *pE;
02364 int nExpr;
02365 FuncDef *pDef;
02366 if( !pAgg->isAgg ) continue;
02367 assert( pAgg->pFunc!=0 );
02368 assert( pAgg->pFunc->xStep!=0 );
02369 pDef = pAgg->pFunc;
02370 pE = pAgg->pExpr;
02371 assert( pE!=0 );
02372 assert( pE->op==TK_AGG_FUNCTION );
02373 nExpr = sqliteExprCodeExprList(pParse, pE->pList, pDef->includeTypes);
02374 sqliteVdbeAddOp(v, OP_Integer, i, 0);
02375 sqliteVdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER);
02376 }
02377 }
02378
02379
02380
02381 sqliteWhereEnd(pWInfo);
02382
02383
02384
02385
02386 if( isAgg ){
02387 int endagg = sqliteVdbeMakeLabel(v);
02388 int startagg;
02389 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
02390 pParse->useAgg = 1;
02391 if( pHaving ){
02392 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
02393 }
02394 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
02395 iParm, startagg, endagg) ){
02396 goto select_end;
02397 }
02398 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
02399 sqliteVdbeResolveLabel(v, endagg);
02400 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
02401 pParse->useAgg = 0;
02402 }
02403
02404
02405
02406
02407 if( pOrderBy ){
02408 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
02409 }
02410
02411
02412
02413
02414
02415
02416 if( pParent ){
02417 assert( pParent->pSrc->nSrc>parentTab );
02418 assert( pParent->pSrc->a[parentTab].pSelect==p );
02419 sqliteSelectDelete(p);
02420 pParent->pSrc->a[parentTab].pSelect = 0;
02421 }
02422
02423
02424
02425
02426 rc = 0;
02427
02428
02429
02430
02431 select_end:
02432 sqliteAggregateInfoReset(pParse);
02433 return rc;
02434 }