| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2001 September 15 | 2 ** 2001 September 15 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 typedef struct SortCtx SortCtx; | 49 typedef struct SortCtx SortCtx; |
| 50 struct SortCtx { | 50 struct SortCtx { |
| 51 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */ | 51 ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */ |
| 52 int nOBSat; /* Number of ORDER BY terms satisfied by indices */ | 52 int nOBSat; /* Number of ORDER BY terms satisfied by indices */ |
| 53 int iECursor; /* Cursor number for the sorter */ | 53 int iECursor; /* Cursor number for the sorter */ |
| 54 int regReturn; /* Register holding block-output return address */ | 54 int regReturn; /* Register holding block-output return address */ |
| 55 int labelBkOut; /* Start label for the block-output subroutine */ | 55 int labelBkOut; /* Start label for the block-output subroutine */ |
| 56 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */ | 56 int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */ |
| 57 int labelDone; /* Jump here when done, ex: LIMIT reached */ | 57 int labelDone; /* Jump here when done, ex: LIMIT reached */ |
| 58 u8 sortFlags; /* Zero or more SORTFLAG_* bits */ | 58 u8 sortFlags; /* Zero or more SORTFLAG_* bits */ |
| 59 u8 bOrderedInnerLoop; /* ORDER BY correctly sorts the inner loop */ |
| 59 }; | 60 }; |
| 60 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */ | 61 #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */ |
| 61 | 62 |
| 62 /* | 63 /* |
| 63 ** Delete all the content of a Select structure. Deallocate the structure | 64 ** Delete all the content of a Select structure. Deallocate the structure |
| 64 ** itself only if bFree is true. | 65 ** itself only if bFree is true. |
| 65 */ | 66 */ |
| 66 static void clearSelect(sqlite3 *db, Select *p, int bFree){ | 67 static void clearSelect(sqlite3 *db, Select *p, int bFree){ |
| 67 while( p ){ | 68 while( p ){ |
| 68 Select *pPrior = p->pPrior; | 69 Select *pPrior = p->pPrior; |
| 69 sqlite3ExprListDelete(db, p->pEList); | 70 sqlite3ExprListDelete(db, p->pEList); |
| 70 sqlite3SrcListDelete(db, p->pSrc); | 71 sqlite3SrcListDelete(db, p->pSrc); |
| 71 sqlite3ExprDelete(db, p->pWhere); | 72 sqlite3ExprDelete(db, p->pWhere); |
| 72 sqlite3ExprListDelete(db, p->pGroupBy); | 73 sqlite3ExprListDelete(db, p->pGroupBy); |
| 73 sqlite3ExprDelete(db, p->pHaving); | 74 sqlite3ExprDelete(db, p->pHaving); |
| 74 sqlite3ExprListDelete(db, p->pOrderBy); | 75 sqlite3ExprListDelete(db, p->pOrderBy); |
| 75 sqlite3ExprDelete(db, p->pLimit); | 76 sqlite3ExprDelete(db, p->pLimit); |
| 76 sqlite3ExprDelete(db, p->pOffset); | 77 sqlite3ExprDelete(db, p->pOffset); |
| 77 sqlite3WithDelete(db, p->pWith); | 78 if( p->pWith ) sqlite3WithDelete(db, p->pWith); |
| 78 if( bFree ) sqlite3DbFree(db, p); | 79 if( bFree ) sqlite3DbFree(db, p); |
| 79 p = pPrior; | 80 p = pPrior; |
| 80 bFree = 1; | 81 bFree = 1; |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 | 84 |
| 84 /* | 85 /* |
| 85 ** Initialize a SelectDest structure. | 86 ** Initialize a SelectDest structure. |
| 86 */ | 87 */ |
| 87 void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ | 88 void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ |
| 88 pDest->eDest = (u8)eDest; | 89 pDest->eDest = (u8)eDest; |
| 89 pDest->iSDParm = iParm; | 90 pDest->iSDParm = iParm; |
| 90 pDest->affSdst = 0; | 91 pDest->zAffSdst = 0; |
| 91 pDest->iSdst = 0; | 92 pDest->iSdst = 0; |
| 92 pDest->nSdst = 0; | 93 pDest->nSdst = 0; |
| 93 } | 94 } |
| 94 | 95 |
| 95 | 96 |
| 96 /* | 97 /* |
| 97 ** Allocate a new Select structure and return a pointer to that | 98 ** Allocate a new Select structure and return a pointer to that |
| 98 ** structure. | 99 ** structure. |
| 99 */ | 100 */ |
| 100 Select *sqlite3SelectNew( | 101 Select *sqlite3SelectNew( |
| 101 Parse *pParse, /* Parsing context */ | 102 Parse *pParse, /* Parsing context */ |
| 102 ExprList *pEList, /* which columns to include in the result */ | 103 ExprList *pEList, /* which columns to include in the result */ |
| 103 SrcList *pSrc, /* the FROM clause -- which tables to scan */ | 104 SrcList *pSrc, /* the FROM clause -- which tables to scan */ |
| 104 Expr *pWhere, /* the WHERE clause */ | 105 Expr *pWhere, /* the WHERE clause */ |
| 105 ExprList *pGroupBy, /* the GROUP BY clause */ | 106 ExprList *pGroupBy, /* the GROUP BY clause */ |
| 106 Expr *pHaving, /* the HAVING clause */ | 107 Expr *pHaving, /* the HAVING clause */ |
| 107 ExprList *pOrderBy, /* the ORDER BY clause */ | 108 ExprList *pOrderBy, /* the ORDER BY clause */ |
| 108 u16 selFlags, /* Flag parameters, such as SF_Distinct */ | 109 u32 selFlags, /* Flag parameters, such as SF_Distinct */ |
| 109 Expr *pLimit, /* LIMIT value. NULL means not used */ | 110 Expr *pLimit, /* LIMIT value. NULL means not used */ |
| 110 Expr *pOffset /* OFFSET value. NULL means no offset */ | 111 Expr *pOffset /* OFFSET value. NULL means no offset */ |
| 111 ){ | 112 ){ |
| 112 Select *pNew; | 113 Select *pNew; |
| 113 Select standin; | 114 Select standin; |
| 114 sqlite3 *db = pParse->db; | 115 sqlite3 *db = pParse->db; |
| 115 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); | 116 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); |
| 116 if( pNew==0 ){ | 117 if( pNew==0 ){ |
| 117 assert( db->mallocFailed ); | 118 assert( db->mallocFailed ); |
| 118 pNew = &standin; | 119 pNew = &standin; |
| 119 memset(pNew, 0, sizeof(*pNew)); | |
| 120 } | 120 } |
| 121 if( pEList==0 ){ | 121 if( pEList==0 ){ |
| 122 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0)); | 122 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ASTERISK,0)); |
| 123 } | 123 } |
| 124 pNew->pEList = pEList; | 124 pNew->pEList = pEList; |
| 125 pNew->op = TK_SELECT; |
| 126 pNew->selFlags = selFlags; |
| 127 pNew->iLimit = 0; |
| 128 pNew->iOffset = 0; |
| 129 #if SELECTTRACE_ENABLED |
| 130 pNew->zSelName[0] = 0; |
| 131 #endif |
| 132 pNew->addrOpenEphm[0] = -1; |
| 133 pNew->addrOpenEphm[1] = -1; |
| 134 pNew->nSelectRow = 0; |
| 125 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc)); | 135 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc)); |
| 126 pNew->pSrc = pSrc; | 136 pNew->pSrc = pSrc; |
| 127 pNew->pWhere = pWhere; | 137 pNew->pWhere = pWhere; |
| 128 pNew->pGroupBy = pGroupBy; | 138 pNew->pGroupBy = pGroupBy; |
| 129 pNew->pHaving = pHaving; | 139 pNew->pHaving = pHaving; |
| 130 pNew->pOrderBy = pOrderBy; | 140 pNew->pOrderBy = pOrderBy; |
| 131 pNew->selFlags = selFlags; | 141 pNew->pPrior = 0; |
| 132 pNew->op = TK_SELECT; | 142 pNew->pNext = 0; |
| 133 pNew->pLimit = pLimit; | 143 pNew->pLimit = pLimit; |
| 134 pNew->pOffset = pOffset; | 144 pNew->pOffset = pOffset; |
| 145 pNew->pWith = 0; |
| 135 assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 ); | 146 assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 ); |
| 136 pNew->addrOpenEphm[0] = -1; | |
| 137 pNew->addrOpenEphm[1] = -1; | |
| 138 if( db->mallocFailed ) { | 147 if( db->mallocFailed ) { |
| 139 clearSelect(db, pNew, pNew!=&standin); | 148 clearSelect(db, pNew, pNew!=&standin); |
| 140 pNew = 0; | 149 pNew = 0; |
| 141 }else{ | 150 }else{ |
| 142 assert( pNew->pSrc!=0 || pParse->nErr>0 ); | 151 assert( pNew->pSrc!=0 || pParse->nErr>0 ); |
| 143 } | 152 } |
| 144 assert( pNew!=&standin ); | 153 assert( pNew!=&standin ); |
| 145 return pNew; | 154 return pNew; |
| 146 } | 155 } |
| 147 | 156 |
| 148 #if SELECTTRACE_ENABLED | 157 #if SELECTTRACE_ENABLED |
| 149 /* | 158 /* |
| 150 ** Set the name of a Select object | 159 ** Set the name of a Select object |
| 151 */ | 160 */ |
| 152 void sqlite3SelectSetName(Select *p, const char *zName){ | 161 void sqlite3SelectSetName(Select *p, const char *zName){ |
| 153 if( p && zName ){ | 162 if( p && zName ){ |
| 154 sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName); | 163 sqlite3_snprintf(sizeof(p->zSelName), p->zSelName, "%s", zName); |
| 155 } | 164 } |
| 156 } | 165 } |
| 157 #endif | 166 #endif |
| 158 | 167 |
| 159 | 168 |
| 160 /* | 169 /* |
| 161 ** Delete the given Select structure and all of its substructures. | 170 ** Delete the given Select structure and all of its substructures. |
| 162 */ | 171 */ |
| 163 void sqlite3SelectDelete(sqlite3 *db, Select *p){ | 172 void sqlite3SelectDelete(sqlite3 *db, Select *p){ |
| 164 clearSelect(db, p, 1); | 173 if( p ) clearSelect(db, p, 1); |
| 165 } | 174 } |
| 166 | 175 |
| 167 /* | 176 /* |
| 168 ** Return a pointer to the right-most SELECT statement in a compound. | 177 ** Return a pointer to the right-most SELECT statement in a compound. |
| 169 */ | 178 */ |
| 170 static Select *findRightmost(Select *p){ | 179 static Select *findRightmost(Select *p){ |
| 171 while( p->pNext ) p = p->pNext; | 180 while( p->pNext ) p = p->pNext; |
| 172 return p; | 181 return p; |
| 173 } | 182 } |
| 174 | 183 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 Expr *pEq; | 327 Expr *pEq; |
| 319 | 328 |
| 320 assert( iLeft<iRight ); | 329 assert( iLeft<iRight ); |
| 321 assert( pSrc->nSrc>iRight ); | 330 assert( pSrc->nSrc>iRight ); |
| 322 assert( pSrc->a[iLeft].pTab ); | 331 assert( pSrc->a[iLeft].pTab ); |
| 323 assert( pSrc->a[iRight].pTab ); | 332 assert( pSrc->a[iRight].pTab ); |
| 324 | 333 |
| 325 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft); | 334 pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft); |
| 326 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight); | 335 pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight); |
| 327 | 336 |
| 328 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0); | 337 pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2); |
| 329 if( pEq && isOuterJoin ){ | 338 if( pEq && isOuterJoin ){ |
| 330 ExprSetProperty(pEq, EP_FromJoin); | 339 ExprSetProperty(pEq, EP_FromJoin); |
| 331 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); | 340 assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); |
| 332 ExprSetVVAProperty(pEq, EP_NoReduce); | 341 ExprSetVVAProperty(pEq, EP_NoReduce); |
| 333 pEq->iRightJoinTable = (i16)pE2->iTable; | 342 pEq->iRightJoinTable = (i16)pE2->iTable; |
| 334 } | 343 } |
| 335 *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq); | 344 *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq); |
| 336 } | 345 } |
| 337 | 346 |
| 338 /* | 347 /* |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0); | 514 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0); |
| 506 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */ | 515 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */ |
| 507 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */ | 516 int nBase = nExpr + bSeq + nData; /* Fields in sorter record */ |
| 508 int regBase; /* Regs for sorter record */ | 517 int regBase; /* Regs for sorter record */ |
| 509 int regRecord = ++pParse->nMem; /* Assembled sorter record */ | 518 int regRecord = ++pParse->nMem; /* Assembled sorter record */ |
| 510 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ | 519 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ |
| 511 int op; /* Opcode to add sorter record to sorter */ | 520 int op; /* Opcode to add sorter record to sorter */ |
| 512 int iLimit; /* LIMIT counter */ | 521 int iLimit; /* LIMIT counter */ |
| 513 | 522 |
| 514 assert( bSeq==0 || bSeq==1 ); | 523 assert( bSeq==0 || bSeq==1 ); |
| 515 assert( nData==1 || regData==regOrigData ); | 524 assert( nData==1 || regData==regOrigData || regOrigData==0 ); |
| 516 if( nPrefixReg ){ | 525 if( nPrefixReg ){ |
| 517 assert( nPrefixReg==nExpr+bSeq ); | 526 assert( nPrefixReg==nExpr+bSeq ); |
| 518 regBase = regData - nExpr - bSeq; | 527 regBase = regData - nExpr - bSeq; |
| 519 }else{ | 528 }else{ |
| 520 regBase = pParse->nMem + 1; | 529 regBase = pParse->nMem + 1; |
| 521 pParse->nMem += nBase; | 530 pParse->nMem += nBase; |
| 522 } | 531 } |
| 523 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); | 532 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); |
| 524 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; | 533 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; |
| 525 pSort->labelDone = sqlite3VdbeMakeLabel(v); | 534 pSort->labelDone = sqlite3VdbeMakeLabel(v); |
| 526 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, | 535 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, |
| 527 SQLITE_ECEL_DUP|SQLITE_ECEL_REF); | 536 SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0)); |
| 528 if( bSeq ){ | 537 if( bSeq ){ |
| 529 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); | 538 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); |
| 530 } | 539 } |
| 531 if( nPrefixReg==0 ){ | 540 if( nPrefixReg==0 && nData>0 ){ |
| 532 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); | 541 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); |
| 533 } | 542 } |
| 534 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord); | 543 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regRecord); |
| 535 if( nOBSat>0 ){ | 544 if( nOBSat>0 ){ |
| 536 int regPrevKey; /* The first nOBSat columns of the previous row */ | 545 int regPrevKey; /* The first nOBSat columns of the previous row */ |
| 537 int addrFirst; /* Address of the OP_IfNot opcode */ | 546 int addrFirst; /* Address of the OP_IfNot opcode */ |
| 538 int addrJmp; /* Address of the OP_Jump opcode */ | 547 int addrJmp; /* Address of the OP_Jump opcode */ |
| 539 VdbeOp *pOp; /* Opcode that opens the sorter */ | 548 VdbeOp *pOp; /* Opcode that opens the sorter */ |
| 540 int nKey; /* Number of sorting key columns, including OP_Sequence */ | 549 int nKey; /* Number of sorting key columns, including OP_Sequence */ |
| 541 KeyInfo *pKI; /* Original KeyInfo on the sorter table */ | 550 KeyInfo *pKI; /* Original KeyInfo on the sorter table */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 571 } | 580 } |
| 572 sqlite3VdbeJumpHere(v, addrFirst); | 581 sqlite3VdbeJumpHere(v, addrFirst); |
| 573 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat); | 582 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat); |
| 574 sqlite3VdbeJumpHere(v, addrJmp); | 583 sqlite3VdbeJumpHere(v, addrJmp); |
| 575 } | 584 } |
| 576 if( pSort->sortFlags & SORTFLAG_UseSorter ){ | 585 if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
| 577 op = OP_SorterInsert; | 586 op = OP_SorterInsert; |
| 578 }else{ | 587 }else{ |
| 579 op = OP_IdxInsert; | 588 op = OP_IdxInsert; |
| 580 } | 589 } |
| 581 sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord); | 590 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord, |
| 591 regBase+nOBSat, nBase-nOBSat); |
| 582 if( iLimit ){ | 592 if( iLimit ){ |
| 583 int addr; | 593 int addr; |
| 584 addr = sqlite3VdbeAddOp3(v, OP_IfNotZero, iLimit, 0, 1); VdbeCoverage(v); | 594 int r1 = 0; |
| 595 /* Fill the sorter until it contains LIMIT+OFFSET entries. (The iLimit |
| 596 ** register is initialized with value of LIMIT+OFFSET.) After the sorter |
| 597 ** fills up, delete the least entry in the sorter after each insert. |
| 598 ** Thus we never hold more than the LIMIT+OFFSET rows in memory at once */ |
| 599 addr = sqlite3VdbeAddOp1(v, OP_IfNotZero, iLimit); VdbeCoverage(v); |
| 585 sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor); | 600 sqlite3VdbeAddOp1(v, OP_Last, pSort->iECursor); |
| 601 if( pSort->bOrderedInnerLoop ){ |
| 602 r1 = ++pParse->nMem; |
| 603 sqlite3VdbeAddOp3(v, OP_Column, pSort->iECursor, nExpr, r1); |
| 604 VdbeComment((v, "seq")); |
| 605 } |
| 586 sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor); | 606 sqlite3VdbeAddOp1(v, OP_Delete, pSort->iECursor); |
| 607 if( pSort->bOrderedInnerLoop ){ |
| 608 /* If the inner loop is driven by an index such that values from |
| 609 ** the same iteration of the inner loop are in sorted order, then |
| 610 ** immediately jump to the next iteration of an inner loop if the |
| 611 ** entry from the current iteration does not fit into the top |
| 612 ** LIMIT+OFFSET entries of the sorter. */ |
| 613 int iBrk = sqlite3VdbeCurrentAddr(v) + 2; |
| 614 sqlite3VdbeAddOp3(v, OP_Eq, regBase+nExpr, iBrk, r1); |
| 615 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 616 VdbeCoverage(v); |
| 617 } |
| 587 sqlite3VdbeJumpHere(v, addr); | 618 sqlite3VdbeJumpHere(v, addr); |
| 588 } | 619 } |
| 589 } | 620 } |
| 590 | 621 |
| 591 /* | 622 /* |
| 592 ** Add code to implement the OFFSET | 623 ** Add code to implement the OFFSET |
| 593 */ | 624 */ |
| 594 static void codeOffset( | 625 static void codeOffset( |
| 595 Vdbe *v, /* Generate code into this VM */ | 626 Vdbe *v, /* Generate code into this VM */ |
| 596 int iOffset, /* Register holding the offset counter */ | 627 int iOffset, /* Register holding the offset counter */ |
| (...skipping 21 matching lines...) Expand all Loading... |
| 618 int N, /* Number of elements */ | 649 int N, /* Number of elements */ |
| 619 int iMem /* First element */ | 650 int iMem /* First element */ |
| 620 ){ | 651 ){ |
| 621 Vdbe *v; | 652 Vdbe *v; |
| 622 int r1; | 653 int r1; |
| 623 | 654 |
| 624 v = pParse->pVdbe; | 655 v = pParse->pVdbe; |
| 625 r1 = sqlite3GetTempReg(pParse); | 656 r1 = sqlite3GetTempReg(pParse); |
| 626 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v); | 657 sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v); |
| 627 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); | 658 sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); |
| 628 sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); | 659 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, iMem, N); |
| 660 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 629 sqlite3ReleaseTempReg(pParse, r1); | 661 sqlite3ReleaseTempReg(pParse, r1); |
| 630 } | 662 } |
| 631 | 663 |
| 632 #ifndef SQLITE_OMIT_SUBQUERY | |
| 633 /* | |
| 634 ** Generate an error message when a SELECT is used within a subexpression | |
| 635 ** (example: "a IN (SELECT * FROM table)") but it has more than 1 result | |
| 636 ** column. We do this in a subroutine because the error used to occur | |
| 637 ** in multiple places. (The error only occurs in one place now, but we | |
| 638 ** retain the subroutine to minimize code disruption.) | |
| 639 */ | |
| 640 static int checkForMultiColumnSelectError( | |
| 641 Parse *pParse, /* Parse context. */ | |
| 642 SelectDest *pDest, /* Destination of SELECT results */ | |
| 643 int nExpr /* Number of result columns returned by SELECT */ | |
| 644 ){ | |
| 645 int eDest = pDest->eDest; | |
| 646 if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){ | |
| 647 sqlite3ErrorMsg(pParse, "only a single result allowed for " | |
| 648 "a SELECT that is part of an expression"); | |
| 649 return 1; | |
| 650 }else{ | |
| 651 return 0; | |
| 652 } | |
| 653 } | |
| 654 #endif | |
| 655 | |
| 656 /* | 664 /* |
| 657 ** This routine generates the code for the inside of the inner loop | 665 ** This routine generates the code for the inside of the inner loop |
| 658 ** of a SELECT. | 666 ** of a SELECT. |
| 659 ** | 667 ** |
| 660 ** If srcTab is negative, then the pEList expressions | 668 ** If srcTab is negative, then the pEList expressions |
| 661 ** are evaluated in order to get the data for this row. If srcTab is | 669 ** are evaluated in order to get the data for this row. If srcTab is |
| 662 ** zero or more, then data is pulled from srcTab and pEList is used only | 670 ** zero or more, then data is pulled from srcTab and pEList is used only |
| 663 ** to get number columns and the datatype for each column. | 671 ** to get the number of columns and the collation sequence for each column. |
| 664 */ | 672 */ |
| 665 static void selectInnerLoop( | 673 static void selectInnerLoop( |
| 666 Parse *pParse, /* The parser context */ | 674 Parse *pParse, /* The parser context */ |
| 667 Select *p, /* The complete select statement being coded */ | 675 Select *p, /* The complete select statement being coded */ |
| 668 ExprList *pEList, /* List of values being extracted */ | 676 ExprList *pEList, /* List of values being extracted */ |
| 669 int srcTab, /* Pull data from this table */ | 677 int srcTab, /* Pull data from this table */ |
| 670 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */ | 678 SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */ |
| 671 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */ | 679 DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */ |
| 672 SelectDest *pDest, /* How to dispose of the results */ | 680 SelectDest *pDest, /* How to dispose of the results */ |
| 673 int iContinue, /* Jump here to continue with next row */ | 681 int iContinue, /* Jump here to continue with next row */ |
| 674 int iBreak /* Jump here to break out of the inner loop */ | 682 int iBreak /* Jump here to break out of the inner loop */ |
| 675 ){ | 683 ){ |
| 676 Vdbe *v = pParse->pVdbe; | 684 Vdbe *v = pParse->pVdbe; |
| 677 int i; | 685 int i; |
| 678 int hasDistinct; /* True if the DISTINCT keyword is present */ | 686 int hasDistinct; /* True if the DISTINCT keyword is present */ |
| 679 int regResult; /* Start of memory holding result set */ | |
| 680 int eDest = pDest->eDest; /* How to dispose of results */ | 687 int eDest = pDest->eDest; /* How to dispose of results */ |
| 681 int iParm = pDest->iSDParm; /* First argument to disposal method */ | 688 int iParm = pDest->iSDParm; /* First argument to disposal method */ |
| 682 int nResultCol; /* Number of result columns */ | 689 int nResultCol; /* Number of result columns */ |
| 683 int nPrefixReg = 0; /* Number of extra registers before regResult */ | 690 int nPrefixReg = 0; /* Number of extra registers before regResult */ |
| 684 | 691 |
| 692 /* Usually, regResult is the first cell in an array of memory cells |
| 693 ** containing the current result row. In this case regOrig is set to the |
| 694 ** same value. However, if the results are being sent to the sorter, the |
| 695 ** values for any expressions that are also part of the sort-key are omitted |
| 696 ** from this array. In this case regOrig is set to zero. */ |
| 697 int regResult; /* Start of memory holding current results */ |
| 698 int regOrig; /* Start of memory holding full result (or 0) */ |
| 699 |
| 685 assert( v ); | 700 assert( v ); |
| 686 assert( pEList!=0 ); | 701 assert( pEList!=0 ); |
| 687 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP; | 702 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP; |
| 688 if( pSort && pSort->pOrderBy==0 ) pSort = 0; | 703 if( pSort && pSort->pOrderBy==0 ) pSort = 0; |
| 689 if( pSort==0 && !hasDistinct ){ | 704 if( pSort==0 && !hasDistinct ){ |
| 690 assert( iContinue!=0 ); | 705 assert( iContinue!=0 ); |
| 691 codeOffset(v, p->iOffset, iContinue); | 706 codeOffset(v, p->iOffset, iContinue); |
| 692 } | 707 } |
| 693 | 708 |
| 694 /* Pull the requested columns. | 709 /* Pull the requested columns. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 705 pParse->nMem += nResultCol; | 720 pParse->nMem += nResultCol; |
| 706 }else if( pDest->iSdst+nResultCol > pParse->nMem ){ | 721 }else if( pDest->iSdst+nResultCol > pParse->nMem ){ |
| 707 /* This is an error condition that can result, for example, when a SELECT | 722 /* This is an error condition that can result, for example, when a SELECT |
| 708 ** on the right-hand side of an INSERT contains more result columns than | 723 ** on the right-hand side of an INSERT contains more result columns than |
| 709 ** there are columns in the table on the left. The error will be caught | 724 ** there are columns in the table on the left. The error will be caught |
| 710 ** and reported later. But we need to make sure enough memory is allocated | 725 ** and reported later. But we need to make sure enough memory is allocated |
| 711 ** to avoid other spurious errors in the meantime. */ | 726 ** to avoid other spurious errors in the meantime. */ |
| 712 pParse->nMem += nResultCol; | 727 pParse->nMem += nResultCol; |
| 713 } | 728 } |
| 714 pDest->nSdst = nResultCol; | 729 pDest->nSdst = nResultCol; |
| 715 regResult = pDest->iSdst; | 730 regOrig = regResult = pDest->iSdst; |
| 716 if( srcTab>=0 ){ | 731 if( srcTab>=0 ){ |
| 717 for(i=0; i<nResultCol; i++){ | 732 for(i=0; i<nResultCol; i++){ |
| 718 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i); | 733 sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i); |
| 719 VdbeComment((v, "%s", pEList->a[i].zName)); | 734 VdbeComment((v, "%s", pEList->a[i].zName)); |
| 720 } | 735 } |
| 721 }else if( eDest!=SRT_Exists ){ | 736 }else if( eDest!=SRT_Exists ){ |
| 722 /* If the destination is an EXISTS(...) expression, the actual | 737 /* If the destination is an EXISTS(...) expression, the actual |
| 723 ** values returned by the SELECT are not required. | 738 ** values returned by the SELECT are not required. |
| 724 */ | 739 */ |
| 725 u8 ecelFlags; | 740 u8 ecelFlags; |
| 726 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ | 741 if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ |
| 727 ecelFlags = SQLITE_ECEL_DUP; | 742 ecelFlags = SQLITE_ECEL_DUP; |
| 728 }else{ | 743 }else{ |
| 729 ecelFlags = 0; | 744 ecelFlags = 0; |
| 730 } | 745 } |
| 731 sqlite3ExprCodeExprList(pParse, pEList, regResult, 0, ecelFlags); | 746 if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){ |
| 747 /* For each expression in pEList that is a copy of an expression in |
| 748 ** the ORDER BY clause (pSort->pOrderBy), set the associated |
| 749 ** iOrderByCol value to one more than the index of the ORDER BY |
| 750 ** expression within the sort-key that pushOntoSorter() will generate. |
| 751 ** This allows the pEList field to be omitted from the sorted record, |
| 752 ** saving space and CPU cycles. */ |
| 753 ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF); |
| 754 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){ |
| 755 int j; |
| 756 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){ |
| 757 pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat; |
| 758 } |
| 759 } |
| 760 regOrig = 0; |
| 761 assert( eDest==SRT_Set || eDest==SRT_Mem |
| 762 || eDest==SRT_Coroutine || eDest==SRT_Output ); |
| 763 } |
| 764 nResultCol = sqlite3ExprCodeExprList(pParse,pEList,regResult,0,ecelFlags); |
| 732 } | 765 } |
| 733 | 766 |
| 734 /* If the DISTINCT keyword was present on the SELECT statement | 767 /* If the DISTINCT keyword was present on the SELECT statement |
| 735 ** and this row has been seen before, then do not make this row | 768 ** and this row has been seen before, then do not make this row |
| 736 ** part of the result. | 769 ** part of the result. |
| 737 */ | 770 */ |
| 738 if( hasDistinct ){ | 771 if( hasDistinct ){ |
| 739 switch( pDistinct->eTnctType ){ | 772 switch( pDistinct->eTnctType ){ |
| 740 case WHERE_DISTINCT_ORDERED: { | 773 case WHERE_DISTINCT_ORDERED: { |
| 741 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */ | 774 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */ |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 795 | 828 |
| 796 switch( eDest ){ | 829 switch( eDest ){ |
| 797 /* In this mode, write each query result to the key of the temporary | 830 /* In this mode, write each query result to the key of the temporary |
| 798 ** table iParm. | 831 ** table iParm. |
| 799 */ | 832 */ |
| 800 #ifndef SQLITE_OMIT_COMPOUND_SELECT | 833 #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 801 case SRT_Union: { | 834 case SRT_Union: { |
| 802 int r1; | 835 int r1; |
| 803 r1 = sqlite3GetTempReg(pParse); | 836 r1 = sqlite3GetTempReg(pParse); |
| 804 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1); | 837 sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1); |
| 805 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); | 838 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
| 806 sqlite3ReleaseTempReg(pParse, r1); | 839 sqlite3ReleaseTempReg(pParse, r1); |
| 807 break; | 840 break; |
| 808 } | 841 } |
| 809 | 842 |
| 810 /* Construct a record from the query result, but instead of | 843 /* Construct a record from the query result, but instead of |
| 811 ** saving that record, use it as a key to delete elements from | 844 ** saving that record, use it as a key to delete elements from |
| 812 ** the temporary table iParm. | 845 ** the temporary table iParm. |
| 813 */ | 846 */ |
| 814 case SRT_Except: { | 847 case SRT_Except: { |
| 815 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol); | 848 sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 832 #ifndef SQLITE_OMIT_CTE | 865 #ifndef SQLITE_OMIT_CTE |
| 833 if( eDest==SRT_DistFifo ){ | 866 if( eDest==SRT_DistFifo ){ |
| 834 /* If the destination is DistFifo, then cursor (iParm+1) is open | 867 /* If the destination is DistFifo, then cursor (iParm+1) is open |
| 835 ** on an ephemeral index. If the current row is already present | 868 ** on an ephemeral index. If the current row is already present |
| 836 ** in the index, do not write it to the output. If not, add the | 869 ** in the index, do not write it to the output. If not, add the |
| 837 ** current row to the index and proceed with writing it to the | 870 ** current row to the index and proceed with writing it to the |
| 838 ** output table as well. */ | 871 ** output table as well. */ |
| 839 int addr = sqlite3VdbeCurrentAddr(v) + 4; | 872 int addr = sqlite3VdbeCurrentAddr(v) + 4; |
| 840 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); | 873 sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); |
| 841 VdbeCoverage(v); | 874 VdbeCoverage(v); |
| 842 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1); | 875 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol); |
| 843 assert( pSort==0 ); | 876 assert( pSort==0 ); |
| 844 } | 877 } |
| 845 #endif | 878 #endif |
| 846 if( pSort ){ | 879 if( pSort ){ |
| 847 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg); | 880 pushOntoSorter(pParse, pSort, p, r1+nPrefixReg,regResult,1,nPrefixReg); |
| 848 }else{ | 881 }else{ |
| 849 int r2 = sqlite3GetTempReg(pParse); | 882 int r2 = sqlite3GetTempReg(pParse); |
| 850 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); | 883 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); |
| 851 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); | 884 sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); |
| 852 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); | 885 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 853 sqlite3ReleaseTempReg(pParse, r2); | 886 sqlite3ReleaseTempReg(pParse, r2); |
| 854 } | 887 } |
| 855 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1); | 888 sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1); |
| 856 break; | 889 break; |
| 857 } | 890 } |
| 858 | 891 |
| 859 #ifndef SQLITE_OMIT_SUBQUERY | 892 #ifndef SQLITE_OMIT_SUBQUERY |
| 860 /* If we are creating a set for an "expr IN (SELECT ...)" construct, | 893 /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 861 ** then there should be a single item on the stack. Write this | 894 ** then there should be a single item on the stack. Write this |
| 862 ** item into the set table with bogus data. | 895 ** item into the set table with bogus data. |
| 863 */ | 896 */ |
| 864 case SRT_Set: { | 897 case SRT_Set: { |
| 865 assert( nResultCol==1 ); | |
| 866 pDest->affSdst = | |
| 867 sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst); | |
| 868 if( pSort ){ | 898 if( pSort ){ |
| 869 /* At first glance you would think we could optimize out the | 899 /* At first glance you would think we could optimize out the |
| 870 ** ORDER BY in this case since the order of entries in the set | 900 ** ORDER BY in this case since the order of entries in the set |
| 871 ** does not matter. But there might be a LIMIT clause, in which | 901 ** does not matter. But there might be a LIMIT clause, in which |
| 872 ** case the order does matter */ | 902 ** case the order does matter */ |
| 873 pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg); | 903 pushOntoSorter( |
| 904 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
| 874 }else{ | 905 }else{ |
| 875 int r1 = sqlite3GetTempReg(pParse); | 906 int r1 = sqlite3GetTempReg(pParse); |
| 876 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1); | 907 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol ); |
| 877 sqlite3ExprCacheAffinityChange(pParse, regResult, 1); | 908 sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, |
| 878 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); | 909 r1, pDest->zAffSdst, nResultCol); |
| 910 sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol); |
| 911 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
| 879 sqlite3ReleaseTempReg(pParse, r1); | 912 sqlite3ReleaseTempReg(pParse, r1); |
| 880 } | 913 } |
| 881 break; | 914 break; |
| 882 } | 915 } |
| 883 | 916 |
| 884 /* If any row exist in the result set, record that fact and abort. | 917 /* If any row exist in the result set, record that fact and abort. |
| 885 */ | 918 */ |
| 886 case SRT_Exists: { | 919 case SRT_Exists: { |
| 887 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm); | 920 sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm); |
| 888 /* The LIMIT clause will terminate the loop for us */ | 921 /* The LIMIT clause will terminate the loop for us */ |
| 889 break; | 922 break; |
| 890 } | 923 } |
| 891 | 924 |
| 892 /* If this is a scalar select that is part of an expression, then | 925 /* If this is a scalar select that is part of an expression, then |
| 893 ** store the results in the appropriate memory cell and break out | 926 ** store the results in the appropriate memory cell or array of |
| 894 ** of the scan loop. | 927 ** memory cells and break out of the scan loop. |
| 895 */ | 928 */ |
| 896 case SRT_Mem: { | 929 case SRT_Mem: { |
| 897 assert( nResultCol==1 ); | |
| 898 if( pSort ){ | 930 if( pSort ){ |
| 899 pushOntoSorter(pParse, pSort, p, regResult, regResult, 1, nPrefixReg); | 931 assert( nResultCol<=pDest->nSdst ); |
| 932 pushOntoSorter( |
| 933 pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
| 900 }else{ | 934 }else{ |
| 935 assert( nResultCol==pDest->nSdst ); |
| 901 assert( regResult==iParm ); | 936 assert( regResult==iParm ); |
| 902 /* The LIMIT clause will jump out of the loop for us */ | 937 /* The LIMIT clause will jump out of the loop for us */ |
| 903 } | 938 } |
| 904 break; | 939 break; |
| 905 } | 940 } |
| 906 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ | 941 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ |
| 907 | 942 |
| 908 case SRT_Coroutine: /* Send data to a co-routine */ | 943 case SRT_Coroutine: /* Send data to a co-routine */ |
| 909 case SRT_Output: { /* Return the results */ | 944 case SRT_Output: { /* Return the results */ |
| 910 testcase( eDest==SRT_Coroutine ); | 945 testcase( eDest==SRT_Coroutine ); |
| 911 testcase( eDest==SRT_Output ); | 946 testcase( eDest==SRT_Output ); |
| 912 if( pSort ){ | 947 if( pSort ){ |
| 913 pushOntoSorter(pParse, pSort, p, regResult, regResult, nResultCol, | 948 pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol, |
| 914 nPrefixReg); | 949 nPrefixReg); |
| 915 }else if( eDest==SRT_Coroutine ){ | 950 }else if( eDest==SRT_Coroutine ){ |
| 916 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); | 951 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
| 917 }else{ | 952 }else{ |
| 918 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol); | 953 sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol); |
| 919 sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol); | 954 sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol); |
| 920 } | 955 } |
| 921 break; | 956 break; |
| 922 } | 957 } |
| 923 | 958 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 953 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3); | 988 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3); |
| 954 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); | 989 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 955 } | 990 } |
| 956 for(i=0; i<nKey; i++){ | 991 for(i=0; i<nKey; i++){ |
| 957 sqlite3VdbeAddOp2(v, OP_SCopy, | 992 sqlite3VdbeAddOp2(v, OP_SCopy, |
| 958 regResult + pSO->a[i].u.x.iOrderByCol - 1, | 993 regResult + pSO->a[i].u.x.iOrderByCol - 1, |
| 959 r2+i); | 994 r2+i); |
| 960 } | 995 } |
| 961 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey); | 996 sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey); |
| 962 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1); | 997 sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1); |
| 963 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); | 998 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2); |
| 964 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest); | 999 if( addrTest ) sqlite3VdbeJumpHere(v, addrTest); |
| 965 sqlite3ReleaseTempReg(pParse, r1); | 1000 sqlite3ReleaseTempReg(pParse, r1); |
| 966 sqlite3ReleaseTempRange(pParse, r2, nKey+2); | 1001 sqlite3ReleaseTempRange(pParse, r2, nKey+2); |
| 967 break; | 1002 break; |
| 968 } | 1003 } |
| 969 #endif /* SQLITE_OMIT_CTE */ | 1004 #endif /* SQLITE_OMIT_CTE */ |
| 970 | 1005 |
| 971 | 1006 |
| 972 | 1007 |
| 973 #if !defined(SQLITE_OMIT_TRIGGER) | 1008 #if !defined(SQLITE_OMIT_TRIGGER) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 990 if( pSort==0 && p->iLimit ){ | 1025 if( pSort==0 && p->iLimit ){ |
| 991 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); | 1026 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); |
| 992 } | 1027 } |
| 993 } | 1028 } |
| 994 | 1029 |
| 995 /* | 1030 /* |
| 996 ** Allocate a KeyInfo object sufficient for an index of N key columns and | 1031 ** Allocate a KeyInfo object sufficient for an index of N key columns and |
| 997 ** X extra columns. | 1032 ** X extra columns. |
| 998 */ | 1033 */ |
| 999 KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){ | 1034 KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){ |
| 1000 KeyInfo *p = sqlite3DbMallocZero(0, | 1035 int nExtra = (N+X)*(sizeof(CollSeq*)+1); |
| 1001 sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1)); | 1036 KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra); |
| 1002 if( p ){ | 1037 if( p ){ |
| 1003 p->aSortOrder = (u8*)&p->aColl[N+X]; | 1038 p->aSortOrder = (u8*)&p->aColl[N+X]; |
| 1004 p->nField = (u16)N; | 1039 p->nField = (u16)N; |
| 1005 p->nXField = (u16)X; | 1040 p->nXField = (u16)X; |
| 1006 p->enc = ENC(db); | 1041 p->enc = ENC(db); |
| 1007 p->db = db; | 1042 p->db = db; |
| 1008 p->nRef = 1; | 1043 p->nRef = 1; |
| 1044 memset(&p[1], 0, nExtra); |
| 1009 }else{ | 1045 }else{ |
| 1010 db->mallocFailed = 1; | 1046 sqlite3OomFault(db); |
| 1011 } | 1047 } |
| 1012 return p; | 1048 return p; |
| 1013 } | 1049 } |
| 1014 | 1050 |
| 1015 /* | 1051 /* |
| 1016 ** Deallocate a KeyInfo object | 1052 ** Deallocate a KeyInfo object |
| 1017 */ | 1053 */ |
| 1018 void sqlite3KeyInfoUnref(KeyInfo *p){ | 1054 void sqlite3KeyInfoUnref(KeyInfo *p){ |
| 1019 if( p ){ | 1055 if( p ){ |
| 1020 assert( p->nRef>0 ); | 1056 assert( p->nRef>0 ); |
| 1021 p->nRef--; | 1057 p->nRef--; |
| 1022 if( p->nRef==0 ) sqlite3DbFree(0, p); | 1058 if( p->nRef==0 ) sqlite3DbFree(p->db, p); |
| 1023 } | 1059 } |
| 1024 } | 1060 } |
| 1025 | 1061 |
| 1026 /* | 1062 /* |
| 1027 ** Make a new pointer to a KeyInfo object | 1063 ** Make a new pointer to a KeyInfo object |
| 1028 */ | 1064 */ |
| 1029 KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){ | 1065 KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){ |
| 1030 if( p ){ | 1066 if( p ){ |
| 1031 assert( p->nRef>0 ); | 1067 assert( p->nRef>0 ); |
| 1032 p->nRef++; | 1068 p->nRef++; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1187 int addrBreak = pSort->labelDone; /* Jump here to exit loop */ | 1223 int addrBreak = pSort->labelDone; /* Jump here to exit loop */ |
| 1188 int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */ | 1224 int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */ |
| 1189 int addr; | 1225 int addr; |
| 1190 int addrOnce = 0; | 1226 int addrOnce = 0; |
| 1191 int iTab; | 1227 int iTab; |
| 1192 ExprList *pOrderBy = pSort->pOrderBy; | 1228 ExprList *pOrderBy = pSort->pOrderBy; |
| 1193 int eDest = pDest->eDest; | 1229 int eDest = pDest->eDest; |
| 1194 int iParm = pDest->iSDParm; | 1230 int iParm = pDest->iSDParm; |
| 1195 int regRow; | 1231 int regRow; |
| 1196 int regRowid; | 1232 int regRowid; |
| 1233 int iCol; |
| 1197 int nKey; | 1234 int nKey; |
| 1198 int iSortTab; /* Sorter cursor to read from */ | 1235 int iSortTab; /* Sorter cursor to read from */ |
| 1199 int nSortData; /* Trailing values to read from sorter */ | 1236 int nSortData; /* Trailing values to read from sorter */ |
| 1200 int i; | 1237 int i; |
| 1201 int bSeq; /* True if sorter record includes seq. no. */ | 1238 int bSeq; /* True if sorter record includes seq. no. */ |
| 1202 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
| 1203 struct ExprList_item *aOutEx = p->pEList->a; | 1239 struct ExprList_item *aOutEx = p->pEList->a; |
| 1204 #endif | |
| 1205 | 1240 |
| 1206 assert( addrBreak<0 ); | 1241 assert( addrBreak<0 ); |
| 1207 if( pSort->labelBkOut ){ | 1242 if( pSort->labelBkOut ){ |
| 1208 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); | 1243 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); |
| 1209 sqlite3VdbeGoto(v, addrBreak); | 1244 sqlite3VdbeGoto(v, addrBreak); |
| 1210 sqlite3VdbeResolveLabel(v, pSort->labelBkOut); | 1245 sqlite3VdbeResolveLabel(v, pSort->labelBkOut); |
| 1211 } | 1246 } |
| 1212 iTab = pSort->iECursor; | 1247 iTab = pSort->iECursor; |
| 1213 if( eDest==SRT_Output || eDest==SRT_Coroutine ){ | 1248 if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){ |
| 1214 regRowid = 0; | 1249 regRowid = 0; |
| 1215 regRow = pDest->iSdst; | 1250 regRow = pDest->iSdst; |
| 1216 nSortData = nColumn; | 1251 nSortData = nColumn; |
| 1217 }else{ | 1252 }else{ |
| 1218 regRowid = sqlite3GetTempReg(pParse); | 1253 regRowid = sqlite3GetTempReg(pParse); |
| 1219 regRow = sqlite3GetTempReg(pParse); | 1254 regRow = sqlite3GetTempRange(pParse, nColumn); |
| 1220 nSortData = 1; | 1255 nSortData = nColumn; |
| 1221 } | 1256 } |
| 1222 nKey = pOrderBy->nExpr - pSort->nOBSat; | 1257 nKey = pOrderBy->nExpr - pSort->nOBSat; |
| 1223 if( pSort->sortFlags & SORTFLAG_UseSorter ){ | 1258 if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
| 1224 int regSortOut = ++pParse->nMem; | 1259 int regSortOut = ++pParse->nMem; |
| 1225 iSortTab = pParse->nTab++; | 1260 iSortTab = pParse->nTab++; |
| 1226 if( pSort->labelBkOut ){ | 1261 if( pSort->labelBkOut ){ |
| 1227 addrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v); | 1262 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 1228 } | 1263 } |
| 1229 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData); | 1264 sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData); |
| 1230 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); | 1265 if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); |
| 1231 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); | 1266 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); |
| 1232 VdbeCoverage(v); | 1267 VdbeCoverage(v); |
| 1233 codeOffset(v, p->iOffset, addrContinue); | 1268 codeOffset(v, p->iOffset, addrContinue); |
| 1234 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); | 1269 sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); |
| 1235 bSeq = 0; | 1270 bSeq = 0; |
| 1236 }else{ | 1271 }else{ |
| 1237 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); | 1272 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); |
| 1238 codeOffset(v, p->iOffset, addrContinue); | 1273 codeOffset(v, p->iOffset, addrContinue); |
| 1239 iSortTab = iTab; | 1274 iSortTab = iTab; |
| 1240 bSeq = 1; | 1275 bSeq = 1; |
| 1241 } | 1276 } |
| 1242 for(i=0; i<nSortData; i++){ | 1277 for(i=0, iCol=nKey+bSeq; i<nSortData; i++){ |
| 1243 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i); | 1278 int iRead; |
| 1279 if( aOutEx[i].u.x.iOrderByCol ){ |
| 1280 iRead = aOutEx[i].u.x.iOrderByCol-1; |
| 1281 }else{ |
| 1282 iRead = iCol++; |
| 1283 } |
| 1284 sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i); |
| 1244 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); | 1285 VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); |
| 1245 } | 1286 } |
| 1246 switch( eDest ){ | 1287 switch( eDest ){ |
| 1288 case SRT_Table: |
| 1247 case SRT_EphemTab: { | 1289 case SRT_EphemTab: { |
| 1248 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); | 1290 sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); |
| 1249 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); | 1291 sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); |
| 1250 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); | 1292 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 1251 break; | 1293 break; |
| 1252 } | 1294 } |
| 1253 #ifndef SQLITE_OMIT_SUBQUERY | 1295 #ifndef SQLITE_OMIT_SUBQUERY |
| 1254 case SRT_Set: { | 1296 case SRT_Set: { |
| 1255 assert( nColumn==1 ); | 1297 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) ); |
| 1256 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, | 1298 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid, |
| 1257 &pDest->affSdst, 1); | 1299 pDest->zAffSdst, nColumn); |
| 1258 sqlite3ExprCacheAffinityChange(pParse, regRow, 1); | 1300 sqlite3ExprCacheAffinityChange(pParse, regRow, nColumn); |
| 1259 sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid); | 1301 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn); |
| 1260 break; | 1302 break; |
| 1261 } | 1303 } |
| 1262 case SRT_Mem: { | 1304 case SRT_Mem: { |
| 1263 assert( nColumn==1 ); | |
| 1264 sqlite3ExprCodeMove(pParse, regRow, iParm, 1); | |
| 1265 /* The LIMIT clause will terminate the loop for us */ | 1305 /* The LIMIT clause will terminate the loop for us */ |
| 1266 break; | 1306 break; |
| 1267 } | 1307 } |
| 1268 #endif | 1308 #endif |
| 1269 default: { | 1309 default: { |
| 1270 assert( eDest==SRT_Output || eDest==SRT_Coroutine ); | 1310 assert( eDest==SRT_Output || eDest==SRT_Coroutine ); |
| 1271 testcase( eDest==SRT_Output ); | 1311 testcase( eDest==SRT_Output ); |
| 1272 testcase( eDest==SRT_Coroutine ); | 1312 testcase( eDest==SRT_Coroutine ); |
| 1273 if( eDest==SRT_Output ){ | 1313 if( eDest==SRT_Output ){ |
| 1274 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn); | 1314 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn); |
| 1275 sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn); | 1315 sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn); |
| 1276 }else{ | 1316 }else{ |
| 1277 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); | 1317 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
| 1278 } | 1318 } |
| 1279 break; | 1319 break; |
| 1280 } | 1320 } |
| 1281 } | 1321 } |
| 1282 if( regRowid ){ | 1322 if( regRowid ){ |
| 1283 sqlite3ReleaseTempReg(pParse, regRow); | 1323 if( eDest==SRT_Set ){ |
| 1324 sqlite3ReleaseTempRange(pParse, regRow, nColumn); |
| 1325 }else{ |
| 1326 sqlite3ReleaseTempReg(pParse, regRow); |
| 1327 } |
| 1284 sqlite3ReleaseTempReg(pParse, regRowid); | 1328 sqlite3ReleaseTempReg(pParse, regRowid); |
| 1285 } | 1329 } |
| 1286 /* The bottom of the loop | 1330 /* The bottom of the loop |
| 1287 */ | 1331 */ |
| 1288 sqlite3VdbeResolveLabel(v, addrContinue); | 1332 sqlite3VdbeResolveLabel(v, addrContinue); |
| 1289 if( pSort->sortFlags & SORTFLAG_UseSorter ){ | 1333 if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
| 1290 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v); | 1334 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v); |
| 1291 }else{ | 1335 }else{ |
| 1292 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v); | 1336 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v); |
| 1293 } | 1337 } |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1413 }else if( pTab->pSchema ){ | 1457 }else if( pTab->pSchema ){ |
| 1414 /* A real table */ | 1458 /* A real table */ |
| 1415 assert( !pS ); | 1459 assert( !pS ); |
| 1416 if( iCol<0 ) iCol = pTab->iPKey; | 1460 if( iCol<0 ) iCol = pTab->iPKey; |
| 1417 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); | 1461 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
| 1418 #ifdef SQLITE_ENABLE_COLUMN_METADATA | 1462 #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1419 if( iCol<0 ){ | 1463 if( iCol<0 ){ |
| 1420 zType = "INTEGER"; | 1464 zType = "INTEGER"; |
| 1421 zOrigCol = "rowid"; | 1465 zOrigCol = "rowid"; |
| 1422 }else{ | 1466 }else{ |
| 1423 zType = pTab->aCol[iCol].zType; | |
| 1424 zOrigCol = pTab->aCol[iCol].zName; | 1467 zOrigCol = pTab->aCol[iCol].zName; |
| 1468 zType = sqlite3ColumnType(&pTab->aCol[iCol],0); |
| 1425 estWidth = pTab->aCol[iCol].szEst; | 1469 estWidth = pTab->aCol[iCol].szEst; |
| 1426 } | 1470 } |
| 1427 zOrigTab = pTab->zName; | 1471 zOrigTab = pTab->zName; |
| 1428 if( pNC->pParse ){ | 1472 if( pNC->pParse ){ |
| 1429 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); | 1473 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); |
| 1430 zOrigDb = pNC->pParse->db->aDb[iDb].zName; | 1474 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName; |
| 1431 } | 1475 } |
| 1432 #else | 1476 #else |
| 1433 if( iCol<0 ){ | 1477 if( iCol<0 ){ |
| 1434 zType = "INTEGER"; | 1478 zType = "INTEGER"; |
| 1435 }else{ | 1479 }else{ |
| 1436 zType = pTab->aCol[iCol].zType; | 1480 zType = sqlite3ColumnType(&pTab->aCol[iCol],0); |
| 1437 estWidth = pTab->aCol[iCol].szEst; | 1481 estWidth = pTab->aCol[iCol].szEst; |
| 1438 } | 1482 } |
| 1439 #endif | 1483 #endif |
| 1440 } | 1484 } |
| 1441 break; | 1485 break; |
| 1442 } | 1486 } |
| 1443 #ifndef SQLITE_OMIT_SUBQUERY | 1487 #ifndef SQLITE_OMIT_SUBQUERY |
| 1444 case TK_SELECT: { | 1488 case TK_SELECT: { |
| 1445 /* The expression is a sub-select. Return the declaration type and | 1489 /* The expression is a sub-select. Return the declaration type and |
| 1446 ** origin info for the single column in the result set of the SELECT | 1490 ** origin info for the single column in the result set of the SELECT |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1661 if( nName>0 ){ | 1705 if( nName>0 ){ |
| 1662 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){} | 1706 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){} |
| 1663 if( zName[j]==':' ) nName = j; | 1707 if( zName[j]==':' ) nName = j; |
| 1664 } | 1708 } |
| 1665 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt); | 1709 zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt); |
| 1666 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); | 1710 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); |
| 1667 } | 1711 } |
| 1668 pCol->zName = zName; | 1712 pCol->zName = zName; |
| 1669 sqlite3ColumnPropertiesFromName(0, pCol); | 1713 sqlite3ColumnPropertiesFromName(0, pCol); |
| 1670 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){ | 1714 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){ |
| 1671 db->mallocFailed = 1; | 1715 sqlite3OomFault(db); |
| 1672 } | 1716 } |
| 1673 } | 1717 } |
| 1674 sqlite3HashClear(&ht); | 1718 sqlite3HashClear(&ht); |
| 1675 if( db->mallocFailed ){ | 1719 if( db->mallocFailed ){ |
| 1676 for(j=0; j<i; j++){ | 1720 for(j=0; j<i; j++){ |
| 1677 sqlite3DbFree(db, aCol[j].zName); | 1721 sqlite3DbFree(db, aCol[j].zName); |
| 1678 } | 1722 } |
| 1679 sqlite3DbFree(db, aCol); | 1723 sqlite3DbFree(db, aCol); |
| 1680 *paCol = 0; | 1724 *paCol = 0; |
| 1681 *pnCol = 0; | 1725 *pnCol = 0; |
| 1682 return SQLITE_NOMEM; | 1726 return SQLITE_NOMEM_BKPT; |
| 1683 } | 1727 } |
| 1684 return SQLITE_OK; | 1728 return SQLITE_OK; |
| 1685 } | 1729 } |
| 1686 | 1730 |
| 1687 /* | 1731 /* |
| 1688 ** Add type and collation information to a column list based on | 1732 ** Add type and collation information to a column list based on |
| 1689 ** a SELECT statement. | 1733 ** a SELECT statement. |
| 1690 ** | 1734 ** |
| 1691 ** The column list presumably came from selectColumnNamesFromExprList(). | 1735 ** The column list presumably came from selectColumnNamesFromExprList(). |
| 1692 ** The column list has only names, not types or collations. This | 1736 ** The column list has only names, not types or collations. This |
| 1693 ** routine goes through and adds the types and collations. | 1737 ** routine goes through and adds the types and collations. |
| 1694 ** | 1738 ** |
| 1695 ** This routine requires that all identifiers in the SELECT | 1739 ** This routine requires that all identifiers in the SELECT |
| 1696 ** statement be resolved. | 1740 ** statement be resolved. |
| 1697 */ | 1741 */ |
| 1698 static void selectAddColumnTypeAndCollation( | 1742 void sqlite3SelectAddColumnTypeAndCollation( |
| 1699 Parse *pParse, /* Parsing contexts */ | 1743 Parse *pParse, /* Parsing contexts */ |
| 1700 Table *pTab, /* Add column type information to this table */ | 1744 Table *pTab, /* Add column type information to this table */ |
| 1701 Select *pSelect /* SELECT used to determine types and collations */ | 1745 Select *pSelect /* SELECT used to determine types and collations */ |
| 1702 ){ | 1746 ){ |
| 1703 sqlite3 *db = pParse->db; | 1747 sqlite3 *db = pParse->db; |
| 1704 NameContext sNC; | 1748 NameContext sNC; |
| 1705 Column *pCol; | 1749 Column *pCol; |
| 1706 CollSeq *pColl; | 1750 CollSeq *pColl; |
| 1707 int i; | 1751 int i; |
| 1708 Expr *p; | 1752 Expr *p; |
| 1709 struct ExprList_item *a; | 1753 struct ExprList_item *a; |
| 1710 u64 szAll = 0; | 1754 u64 szAll = 0; |
| 1711 | 1755 |
| 1712 assert( pSelect!=0 ); | 1756 assert( pSelect!=0 ); |
| 1713 assert( (pSelect->selFlags & SF_Resolved)!=0 ); | 1757 assert( (pSelect->selFlags & SF_Resolved)!=0 ); |
| 1714 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed ); | 1758 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed ); |
| 1715 if( db->mallocFailed ) return; | 1759 if( db->mallocFailed ) return; |
| 1716 memset(&sNC, 0, sizeof(sNC)); | 1760 memset(&sNC, 0, sizeof(sNC)); |
| 1717 sNC.pSrcList = pSelect->pSrc; | 1761 sNC.pSrcList = pSelect->pSrc; |
| 1718 a = pSelect->pEList->a; | 1762 a = pSelect->pEList->a; |
| 1719 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ | 1763 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
| 1764 const char *zType; |
| 1765 int n, m; |
| 1720 p = a[i].pExpr; | 1766 p = a[i].pExpr; |
| 1721 if( pCol->zType==0 ){ | 1767 zType = columnType(&sNC, p, 0, 0, 0, &pCol->szEst); |
| 1722 pCol->zType = sqlite3DbStrDup(db, | |
| 1723 columnType(&sNC, p,0,0,0, &pCol->szEst)); | |
| 1724 } | |
| 1725 szAll += pCol->szEst; | 1768 szAll += pCol->szEst; |
| 1726 pCol->affinity = sqlite3ExprAffinity(p); | 1769 pCol->affinity = sqlite3ExprAffinity(p); |
| 1770 if( zType && (m = sqlite3Strlen30(zType))>0 ){ |
| 1771 n = sqlite3Strlen30(pCol->zName); |
| 1772 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2); |
| 1773 if( pCol->zName ){ |
| 1774 memcpy(&pCol->zName[n+1], zType, m+1); |
| 1775 pCol->colFlags |= COLFLAG_HASTYPE; |
| 1776 } |
| 1777 } |
| 1727 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB; | 1778 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB; |
| 1728 pColl = sqlite3ExprCollSeq(pParse, p); | 1779 pColl = sqlite3ExprCollSeq(pParse, p); |
| 1729 if( pColl && pCol->zColl==0 ){ | 1780 if( pColl && pCol->zColl==0 ){ |
| 1730 pCol->zColl = sqlite3DbStrDup(db, pColl->zName); | 1781 pCol->zColl = sqlite3DbStrDup(db, pColl->zName); |
| 1731 } | 1782 } |
| 1732 } | 1783 } |
| 1733 pTab->szTabRow = sqlite3LogEst(szAll*4); | 1784 pTab->szTabRow = sqlite3LogEst(szAll*4); |
| 1734 } | 1785 } |
| 1735 | 1786 |
| 1736 /* | 1787 /* |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1748 sqlite3SelectPrep(pParse, pSelect, 0); | 1799 sqlite3SelectPrep(pParse, pSelect, 0); |
| 1749 if( pParse->nErr ) return 0; | 1800 if( pParse->nErr ) return 0; |
| 1750 while( pSelect->pPrior ) pSelect = pSelect->pPrior; | 1801 while( pSelect->pPrior ) pSelect = pSelect->pPrior; |
| 1751 db->flags = savedFlags; | 1802 db->flags = savedFlags; |
| 1752 pTab = sqlite3DbMallocZero(db, sizeof(Table) ); | 1803 pTab = sqlite3DbMallocZero(db, sizeof(Table) ); |
| 1753 if( pTab==0 ){ | 1804 if( pTab==0 ){ |
| 1754 return 0; | 1805 return 0; |
| 1755 } | 1806 } |
| 1756 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside | 1807 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside |
| 1757 ** is disabled */ | 1808 ** is disabled */ |
| 1758 assert( db->lookaside.bEnabled==0 ); | 1809 assert( db->lookaside.bDisable ); |
| 1759 pTab->nRef = 1; | 1810 pTab->nTabRef = 1; |
| 1760 pTab->zName = 0; | 1811 pTab->zName = 0; |
| 1761 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); | 1812 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 1762 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); | 1813 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); |
| 1763 selectAddColumnTypeAndCollation(pParse, pTab, pSelect); | 1814 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect); |
| 1764 pTab->iPKey = -1; | 1815 pTab->iPKey = -1; |
| 1765 if( db->mallocFailed ){ | 1816 if( db->mallocFailed ){ |
| 1766 sqlite3DeleteTable(db, pTab); | 1817 sqlite3DeleteTable(db, pTab); |
| 1767 return 0; | 1818 return 0; |
| 1768 } | 1819 } |
| 1769 return pTab; | 1820 return pTab; |
| 1770 } | 1821 } |
| 1771 | 1822 |
| 1772 /* | 1823 /* |
| 1773 ** Get a VDBE for the given parser context. Create a new one if necessary. | 1824 ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 1774 ** If an error occurs, return NULL and leave a message in pParse. | 1825 ** If an error occurs, return NULL and leave a message in pParse. |
| 1775 */ | 1826 */ |
| 1827 static SQLITE_NOINLINE Vdbe *allocVdbe(Parse *pParse){ |
| 1828 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(pParse); |
| 1829 if( v ) sqlite3VdbeAddOp2(v, OP_Init, 0, 1); |
| 1830 if( pParse->pToplevel==0 |
| 1831 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) |
| 1832 ){ |
| 1833 pParse->okConstFactor = 1; |
| 1834 } |
| 1835 return v; |
| 1836 } |
| 1776 Vdbe *sqlite3GetVdbe(Parse *pParse){ | 1837 Vdbe *sqlite3GetVdbe(Parse *pParse){ |
| 1777 Vdbe *v = pParse->pVdbe; | 1838 Vdbe *v = pParse->pVdbe; |
| 1778 if( v==0 ){ | 1839 return v ? v : allocVdbe(pParse); |
| 1779 v = pParse->pVdbe = sqlite3VdbeCreate(pParse); | |
| 1780 if( v ) sqlite3VdbeAddOp0(v, OP_Init); | |
| 1781 if( pParse->pToplevel==0 | |
| 1782 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) | |
| 1783 ){ | |
| 1784 pParse->okConstFactor = 1; | |
| 1785 } | |
| 1786 | |
| 1787 } | |
| 1788 return v; | |
| 1789 } | 1840 } |
| 1790 | 1841 |
| 1791 | 1842 |
| 1792 /* | 1843 /* |
| 1793 ** Compute the iLimit and iOffset fields of the SELECT based on the | 1844 ** Compute the iLimit and iOffset fields of the SELECT based on the |
| 1794 ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions | 1845 ** pLimit and pOffset expressions. pLimit and pOffset hold the expressions |
| 1795 ** that appear in the original SQL statement after the LIMIT and OFFSET | 1846 ** that appear in the original SQL statement after the LIMIT and OFFSET |
| 1796 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset | 1847 ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset |
| 1797 ** are the integer memory register numbers for counters used to compute | 1848 ** are the integer memory register numbers for counters used to compute |
| 1798 ** the limit and offset. If there is no limit and/or offset, then | 1849 ** the limit and offset. If there is no limit and/or offset, then |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1829 assert( p->pOffset==0 || p->pLimit!=0 ); | 1880 assert( p->pOffset==0 || p->pLimit!=0 ); |
| 1830 if( p->pLimit ){ | 1881 if( p->pLimit ){ |
| 1831 p->iLimit = iLimit = ++pParse->nMem; | 1882 p->iLimit = iLimit = ++pParse->nMem; |
| 1832 v = sqlite3GetVdbe(pParse); | 1883 v = sqlite3GetVdbe(pParse); |
| 1833 assert( v!=0 ); | 1884 assert( v!=0 ); |
| 1834 if( sqlite3ExprIsInteger(p->pLimit, &n) ){ | 1885 if( sqlite3ExprIsInteger(p->pLimit, &n) ){ |
| 1835 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); | 1886 sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); |
| 1836 VdbeComment((v, "LIMIT counter")); | 1887 VdbeComment((v, "LIMIT counter")); |
| 1837 if( n==0 ){ | 1888 if( n==0 ){ |
| 1838 sqlite3VdbeGoto(v, iBreak); | 1889 sqlite3VdbeGoto(v, iBreak); |
| 1839 }else if( n>=0 && p->nSelectRow>(u64)n ){ | 1890 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){ |
| 1840 p->nSelectRow = n; | 1891 p->nSelectRow = sqlite3LogEst((u64)n); |
| 1892 p->selFlags |= SF_FixedLimit; |
| 1841 } | 1893 } |
| 1842 }else{ | 1894 }else{ |
| 1843 sqlite3ExprCode(pParse, p->pLimit, iLimit); | 1895 sqlite3ExprCode(pParse, p->pLimit, iLimit); |
| 1844 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v); | 1896 sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v); |
| 1845 VdbeComment((v, "LIMIT counter")); | 1897 VdbeComment((v, "LIMIT counter")); |
| 1846 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v); | 1898 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v); |
| 1847 } | 1899 } |
| 1848 if( p->pOffset ){ | 1900 if( p->pOffset ){ |
| 1849 p->iOffset = iOffset = ++pParse->nMem; | 1901 p->iOffset = iOffset = ++pParse->nMem; |
| 1850 pParse->nMem++; /* Allocate an extra register for limit+offset */ | 1902 pParse->nMem++; /* Allocate an extra register for limit+offset */ |
| 1851 sqlite3ExprCode(pParse, p->pOffset, iOffset); | 1903 sqlite3ExprCode(pParse, p->pOffset, iOffset); |
| 1852 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v); | 1904 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v); |
| 1853 VdbeComment((v, "OFFSET counter")); | 1905 VdbeComment((v, "OFFSET counter")); |
| 1854 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iOffset, iOffset, 0); | 1906 sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset); |
| 1855 sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1); | |
| 1856 VdbeComment((v, "LIMIT+OFFSET")); | 1907 VdbeComment((v, "LIMIT+OFFSET")); |
| 1857 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iLimit, iOffset+1, -1); | |
| 1858 } | 1908 } |
| 1859 } | 1909 } |
| 1860 } | 1910 } |
| 1861 | 1911 |
| 1862 #ifndef SQLITE_OMIT_COMPOUND_SELECT | 1912 #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 1863 /* | 1913 /* |
| 1864 ** Return the appropriate collating sequence for the iCol-th column of | 1914 ** Return the appropriate collating sequence for the iCol-th column of |
| 1865 ** the result set for the compound-select statement "p". Return NULL if | 1915 ** the result set for the compound-select statement "p". Return NULL if |
| 1866 ** the column has no default collating sequence. | 1916 ** the column has no default collating sequence. |
| 1867 ** | 1917 ** |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1981 int rc; /* Result code */ | 2031 int rc; /* Result code */ |
| 1982 ExprList *pOrderBy; /* The ORDER BY clause */ | 2032 ExprList *pOrderBy; /* The ORDER BY clause */ |
| 1983 Expr *pLimit, *pOffset; /* Saved LIMIT and OFFSET */ | 2033 Expr *pLimit, *pOffset; /* Saved LIMIT and OFFSET */ |
| 1984 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */ | 2034 int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */ |
| 1985 | 2035 |
| 1986 /* Obtain authorization to do a recursive query */ | 2036 /* Obtain authorization to do a recursive query */ |
| 1987 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return; | 2037 if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return; |
| 1988 | 2038 |
| 1989 /* Process the LIMIT and OFFSET clauses, if they exist */ | 2039 /* Process the LIMIT and OFFSET clauses, if they exist */ |
| 1990 addrBreak = sqlite3VdbeMakeLabel(v); | 2040 addrBreak = sqlite3VdbeMakeLabel(v); |
| 2041 p->nSelectRow = 320; /* 4 billion rows */ |
| 1991 computeLimitRegisters(pParse, p, addrBreak); | 2042 computeLimitRegisters(pParse, p, addrBreak); |
| 1992 pLimit = p->pLimit; | 2043 pLimit = p->pLimit; |
| 1993 pOffset = p->pOffset; | 2044 pOffset = p->pOffset; |
| 1994 regLimit = p->iLimit; | 2045 regLimit = p->iLimit; |
| 1995 regOffset = p->iOffset; | 2046 regOffset = p->iOffset; |
| 1996 p->pLimit = p->pOffset = 0; | 2047 p->pLimit = p->pOffset = 0; |
| 1997 p->iLimit = p->iOffset = 0; | 2048 p->iLimit = p->iOffset = 0; |
| 1998 pOrderBy = p->pOrderBy; | 2049 pOrderBy = p->pOrderBy; |
| 1999 | 2050 |
| 2000 /* Locate the cursor number of the Current table */ | 2051 /* Locate the cursor number of the Current table */ |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2210 } | 2261 } |
| 2211 | 2262 |
| 2212 v = sqlite3GetVdbe(pParse); | 2263 v = sqlite3GetVdbe(pParse); |
| 2213 assert( v!=0 ); /* The VDBE already created by calling function */ | 2264 assert( v!=0 ); /* The VDBE already created by calling function */ |
| 2214 | 2265 |
| 2215 /* Create the destination temporary table if necessary | 2266 /* Create the destination temporary table if necessary |
| 2216 */ | 2267 */ |
| 2217 if( dest.eDest==SRT_EphemTab ){ | 2268 if( dest.eDest==SRT_EphemTab ){ |
| 2218 assert( p->pEList ); | 2269 assert( p->pEList ); |
| 2219 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr); | 2270 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr); |
| 2220 sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | |
| 2221 dest.eDest = SRT_Table; | 2271 dest.eDest = SRT_Table; |
| 2222 } | 2272 } |
| 2223 | 2273 |
| 2224 /* Special handling for a compound-select that originates as a VALUES clause. | 2274 /* Special handling for a compound-select that originates as a VALUES clause. |
| 2225 */ | 2275 */ |
| 2226 if( p->selFlags & SF_MultiValue ){ | 2276 if( p->selFlags & SF_MultiValue ){ |
| 2227 rc = multiSelectValues(pParse, p, &dest); | 2277 rc = multiSelectValues(pParse, p, &dest); |
| 2228 goto multi_select_end; | 2278 goto multi_select_end; |
| 2229 } | 2279 } |
| 2230 | 2280 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2264 if( rc ){ | 2314 if( rc ){ |
| 2265 goto multi_select_end; | 2315 goto multi_select_end; |
| 2266 } | 2316 } |
| 2267 p->pPrior = 0; | 2317 p->pPrior = 0; |
| 2268 p->iLimit = pPrior->iLimit; | 2318 p->iLimit = pPrior->iLimit; |
| 2269 p->iOffset = pPrior->iOffset; | 2319 p->iOffset = pPrior->iOffset; |
| 2270 if( p->iLimit ){ | 2320 if( p->iLimit ){ |
| 2271 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); | 2321 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); |
| 2272 VdbeComment((v, "Jump ahead if LIMIT reached")); | 2322 VdbeComment((v, "Jump ahead if LIMIT reached")); |
| 2273 if( p->iOffset ){ | 2323 if( p->iOffset ){ |
| 2274 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iOffset, p->iOffset, 0); | 2324 sqlite3VdbeAddOp3(v, OP_OffsetLimit, |
| 2275 sqlite3VdbeAddOp3(v, OP_Add, p->iLimit, p->iOffset, p->iOffset+1); | 2325 p->iLimit, p->iOffset+1, p->iOffset); |
| 2276 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iLimit, p->iOffset+1, -1); | |
| 2277 } | 2326 } |
| 2278 } | 2327 } |
| 2279 explainSetInteger(iSub2, pParse->iNextSelectId); | 2328 explainSetInteger(iSub2, pParse->iNextSelectId); |
| 2280 rc = sqlite3Select(pParse, p, &dest); | 2329 rc = sqlite3Select(pParse, p, &dest); |
| 2281 testcase( rc!=SQLITE_OK ); | 2330 testcase( rc!=SQLITE_OK ); |
| 2282 pDelete = p->pPrior; | 2331 pDelete = p->pPrior; |
| 2283 p->pPrior = pPrior; | 2332 p->pPrior = pPrior; |
| 2284 p->nSelectRow += pPrior->nSelectRow; | 2333 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 2285 if( pPrior->pLimit | 2334 if( pPrior->pLimit |
| 2286 && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) | 2335 && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) |
| 2287 && nLimit>0 && p->nSelectRow > (u64)nLimit | 2336 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) |
| 2288 ){ | 2337 ){ |
| 2289 p->nSelectRow = nLimit; | 2338 p->nSelectRow = sqlite3LogEst((u64)nLimit); |
| 2290 } | 2339 } |
| 2291 if( addr ){ | 2340 if( addr ){ |
| 2292 sqlite3VdbeJumpHere(v, addr); | 2341 sqlite3VdbeJumpHere(v, addr); |
| 2293 } | 2342 } |
| 2294 break; | 2343 break; |
| 2295 } | 2344 } |
| 2296 case TK_EXCEPT: | 2345 case TK_EXCEPT: |
| 2297 case TK_UNION: { | 2346 case TK_UNION: { |
| 2298 int unionTab; /* Cursor number of the temporary table holding result */ | 2347 int unionTab; /* Cursor number of the temporary table holding result */ |
| 2299 u8 op = 0; /* One of the SRT_ operations to apply to self */ | 2348 u8 op = 0; /* One of the SRT_ operations to apply to self */ |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2351 uniondest.eDest = op; | 2400 uniondest.eDest = op; |
| 2352 explainSetInteger(iSub2, pParse->iNextSelectId); | 2401 explainSetInteger(iSub2, pParse->iNextSelectId); |
| 2353 rc = sqlite3Select(pParse, p, &uniondest); | 2402 rc = sqlite3Select(pParse, p, &uniondest); |
| 2354 testcase( rc!=SQLITE_OK ); | 2403 testcase( rc!=SQLITE_OK ); |
| 2355 /* Query flattening in sqlite3Select() might refill p->pOrderBy. | 2404 /* Query flattening in sqlite3Select() might refill p->pOrderBy. |
| 2356 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ | 2405 ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ |
| 2357 sqlite3ExprListDelete(db, p->pOrderBy); | 2406 sqlite3ExprListDelete(db, p->pOrderBy); |
| 2358 pDelete = p->pPrior; | 2407 pDelete = p->pPrior; |
| 2359 p->pPrior = pPrior; | 2408 p->pPrior = pPrior; |
| 2360 p->pOrderBy = 0; | 2409 p->pOrderBy = 0; |
| 2361 if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow; | 2410 if( p->op==TK_UNION ){ |
| 2411 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 2412 } |
| 2362 sqlite3ExprDelete(db, p->pLimit); | 2413 sqlite3ExprDelete(db, p->pLimit); |
| 2363 p->pLimit = pLimit; | 2414 p->pLimit = pLimit; |
| 2364 p->pOffset = pOffset; | 2415 p->pOffset = pOffset; |
| 2365 p->iLimit = 0; | 2416 p->iLimit = 0; |
| 2366 p->iOffset = 0; | 2417 p->iOffset = 0; |
| 2367 | 2418 |
| 2368 /* Convert the data in the temporary table into whatever form | 2419 /* Convert the data in the temporary table into whatever form |
| 2369 ** it is that we currently need. | 2420 ** it is that we currently need. |
| 2370 */ | 2421 */ |
| 2371 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); | 2422 assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2450 if( dest.eDest==SRT_Output ){ | 2501 if( dest.eDest==SRT_Output ){ |
| 2451 Select *pFirst = p; | 2502 Select *pFirst = p; |
| 2452 while( pFirst->pPrior ) pFirst = pFirst->pPrior; | 2503 while( pFirst->pPrior ) pFirst = pFirst->pPrior; |
| 2453 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); | 2504 generateColumnNames(pParse, pFirst->pSrc, pFirst->pEList); |
| 2454 } | 2505 } |
| 2455 iBreak = sqlite3VdbeMakeLabel(v); | 2506 iBreak = sqlite3VdbeMakeLabel(v); |
| 2456 iCont = sqlite3VdbeMakeLabel(v); | 2507 iCont = sqlite3VdbeMakeLabel(v); |
| 2457 computeLimitRegisters(pParse, p, iBreak); | 2508 computeLimitRegisters(pParse, p, iBreak); |
| 2458 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); | 2509 sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); |
| 2459 r1 = sqlite3GetTempReg(pParse); | 2510 r1 = sqlite3GetTempReg(pParse); |
| 2460 iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1); | 2511 iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1); |
| 2461 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v); | 2512 sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v); |
| 2462 sqlite3ReleaseTempReg(pParse, r1); | 2513 sqlite3ReleaseTempReg(pParse, r1); |
| 2463 selectInnerLoop(pParse, p, p->pEList, tab1, | 2514 selectInnerLoop(pParse, p, p->pEList, tab1, |
| 2464 0, 0, &dest, iCont, iBreak); | 2515 0, 0, &dest, iCont, iBreak); |
| 2465 sqlite3VdbeResolveLabel(v, iCont); | 2516 sqlite3VdbeResolveLabel(v, iCont); |
| 2466 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); | 2517 sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); |
| 2467 sqlite3VdbeResolveLabel(v, iBreak); | 2518 sqlite3VdbeResolveLabel(v, iBreak); |
| 2468 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); | 2519 sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); |
| 2469 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); | 2520 sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); |
| 2470 break; | 2521 break; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 2486 int i; /* Loop counter */ | 2537 int i; /* Loop counter */ |
| 2487 KeyInfo *pKeyInfo; /* Collating sequence for the result set */ | 2538 KeyInfo *pKeyInfo; /* Collating sequence for the result set */ |
| 2488 Select *pLoop; /* For looping through SELECT statements */ | 2539 Select *pLoop; /* For looping through SELECT statements */ |
| 2489 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ | 2540 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ |
| 2490 int nCol; /* Number of columns in result set */ | 2541 int nCol; /* Number of columns in result set */ |
| 2491 | 2542 |
| 2492 assert( p->pNext==0 ); | 2543 assert( p->pNext==0 ); |
| 2493 nCol = p->pEList->nExpr; | 2544 nCol = p->pEList->nExpr; |
| 2494 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1); | 2545 pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1); |
| 2495 if( !pKeyInfo ){ | 2546 if( !pKeyInfo ){ |
| 2496 rc = SQLITE_NOMEM; | 2547 rc = SQLITE_NOMEM_BKPT; |
| 2497 goto multi_select_end; | 2548 goto multi_select_end; |
| 2498 } | 2549 } |
| 2499 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){ | 2550 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){ |
| 2500 *apColl = multiSelectCollSeq(pParse, p, i); | 2551 *apColl = multiSelectCollSeq(pParse, p, i); |
| 2501 if( 0==*apColl ){ | 2552 if( 0==*apColl ){ |
| 2502 *apColl = db->pDfltColl; | 2553 *apColl = db->pDfltColl; |
| 2503 } | 2554 } |
| 2504 } | 2555 } |
| 2505 | 2556 |
| 2506 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ | 2557 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2608 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1); | 2659 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1); |
| 2609 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2); | 2660 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2); |
| 2610 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2); | 2661 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2); |
| 2611 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); | 2662 sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 2612 sqlite3ReleaseTempReg(pParse, r2); | 2663 sqlite3ReleaseTempReg(pParse, r2); |
| 2613 sqlite3ReleaseTempReg(pParse, r1); | 2664 sqlite3ReleaseTempReg(pParse, r1); |
| 2614 break; | 2665 break; |
| 2615 } | 2666 } |
| 2616 | 2667 |
| 2617 #ifndef SQLITE_OMIT_SUBQUERY | 2668 #ifndef SQLITE_OMIT_SUBQUERY |
| 2618 /* If we are creating a set for an "expr IN (SELECT ...)" construct, | 2669 /* If we are creating a set for an "expr IN (SELECT ...)". |
| 2619 ** then there should be a single item on the stack. Write this | |
| 2620 ** item into the set table with bogus data. | |
| 2621 */ | 2670 */ |
| 2622 case SRT_Set: { | 2671 case SRT_Set: { |
| 2623 int r1; | 2672 int r1; |
| 2624 assert( pIn->nSdst==1 || pParse->nErr>0 ); | 2673 testcase( pIn->nSdst>1 ); |
| 2625 pDest->affSdst = | |
| 2626 sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst); | |
| 2627 r1 = sqlite3GetTempReg(pParse); | 2674 r1 = sqlite3GetTempReg(pParse); |
| 2628 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1); | 2675 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, |
| 2629 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1); | 2676 r1, pDest->zAffSdst, pIn->nSdst); |
| 2630 sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1); | 2677 sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst); |
| 2678 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1, |
| 2679 pIn->iSdst, pIn->nSdst); |
| 2631 sqlite3ReleaseTempReg(pParse, r1); | 2680 sqlite3ReleaseTempReg(pParse, r1); |
| 2632 break; | 2681 break; |
| 2633 } | 2682 } |
| 2634 | 2683 |
| 2635 /* If this is a scalar select that is part of an expression, then | 2684 /* If this is a scalar select that is part of an expression, then |
| 2636 ** store the results in the appropriate memory cell and break out | 2685 ** store the results in the appropriate memory cell and break out |
| 2637 ** of the scan loop. | 2686 ** of the scan loop. |
| 2638 */ | 2687 */ |
| 2639 case SRT_Mem: { | 2688 case SRT_Mem: { |
| 2640 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 ); | 2689 assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 ); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2841 */ | 2890 */ |
| 2842 if( op!=TK_ALL ){ | 2891 if( op!=TK_ALL ){ |
| 2843 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ | 2892 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ |
| 2844 struct ExprList_item *pItem; | 2893 struct ExprList_item *pItem; |
| 2845 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){ | 2894 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){ |
| 2846 assert( pItem->u.x.iOrderByCol>0 ); | 2895 assert( pItem->u.x.iOrderByCol>0 ); |
| 2847 if( pItem->u.x.iOrderByCol==i ) break; | 2896 if( pItem->u.x.iOrderByCol==i ) break; |
| 2848 } | 2897 } |
| 2849 if( j==nOrderBy ){ | 2898 if( j==nOrderBy ){ |
| 2850 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); | 2899 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
| 2851 if( pNew==0 ) return SQLITE_NOMEM; | 2900 if( pNew==0 ) return SQLITE_NOMEM_BKPT; |
| 2852 pNew->flags |= EP_IntValue; | 2901 pNew->flags |= EP_IntValue; |
| 2853 pNew->u.iValue = i; | 2902 pNew->u.iValue = i; |
| 2854 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); | 2903 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); |
| 2855 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; | 2904 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; |
| 2856 } | 2905 } |
| 2857 } | 2906 } |
| 2858 } | 2907 } |
| 2859 | 2908 |
| 2860 /* Compute the comparison permutation and keyinfo that is used with | 2909 /* Compute the comparison permutation and keyinfo that is used with |
| 2861 ** the permutation used to determine if the next | 2910 ** the permutation used to determine if the next |
| 2862 ** row of results comes from selectA or selectB. Also add explicit | 2911 ** row of results comes from selectA or selectB. Also add explicit |
| 2863 ** collations to the ORDER BY clause terms so that when the subqueries | 2912 ** collations to the ORDER BY clause terms so that when the subqueries |
| 2864 ** to the right and the left are evaluated, they use the correct | 2913 ** to the right and the left are evaluated, they use the correct |
| 2865 ** collation. | 2914 ** collation. |
| 2866 */ | 2915 */ |
| 2867 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy); | 2916 aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1)); |
| 2868 if( aPermute ){ | 2917 if( aPermute ){ |
| 2869 struct ExprList_item *pItem; | 2918 struct ExprList_item *pItem; |
| 2870 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){ | 2919 aPermute[0] = nOrderBy; |
| 2920 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){ |
| 2871 assert( pItem->u.x.iOrderByCol>0 ); | 2921 assert( pItem->u.x.iOrderByCol>0 ); |
| 2872 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); | 2922 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); |
| 2873 aPermute[i] = pItem->u.x.iOrderByCol - 1; | 2923 aPermute[i] = pItem->u.x.iOrderByCol - 1; |
| 2874 } | 2924 } |
| 2875 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); | 2925 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); |
| 2876 }else{ | 2926 }else{ |
| 2877 pKeyMerge = 0; | 2927 pKeyMerge = 0; |
| 2878 } | 2928 } |
| 2879 | 2929 |
| 2880 /* Reattach the ORDER BY clause to the query. | 2930 /* Reattach the ORDER BY clause to the query. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2938 | 2988 |
| 2939 /* Generate a coroutine to evaluate the SELECT statement to the | 2989 /* Generate a coroutine to evaluate the SELECT statement to the |
| 2940 ** left of the compound operator - the "A" select. | 2990 ** left of the compound operator - the "A" select. |
| 2941 */ | 2991 */ |
| 2942 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1; | 2992 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1; |
| 2943 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); | 2993 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); |
| 2944 VdbeComment((v, "left SELECT")); | 2994 VdbeComment((v, "left SELECT")); |
| 2945 pPrior->iLimit = regLimitA; | 2995 pPrior->iLimit = regLimitA; |
| 2946 explainSetInteger(iSub1, pParse->iNextSelectId); | 2996 explainSetInteger(iSub1, pParse->iNextSelectId); |
| 2947 sqlite3Select(pParse, pPrior, &destA); | 2997 sqlite3Select(pParse, pPrior, &destA); |
| 2948 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA); | 2998 sqlite3VdbeEndCoroutine(v, regAddrA); |
| 2949 sqlite3VdbeJumpHere(v, addr1); | 2999 sqlite3VdbeJumpHere(v, addr1); |
| 2950 | 3000 |
| 2951 /* Generate a coroutine to evaluate the SELECT statement on | 3001 /* Generate a coroutine to evaluate the SELECT statement on |
| 2952 ** the right - the "B" select | 3002 ** the right - the "B" select |
| 2953 */ | 3003 */ |
| 2954 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1; | 3004 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1; |
| 2955 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB); | 3005 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB); |
| 2956 VdbeComment((v, "right SELECT")); | 3006 VdbeComment((v, "right SELECT")); |
| 2957 savedLimit = p->iLimit; | 3007 savedLimit = p->iLimit; |
| 2958 savedOffset = p->iOffset; | 3008 savedOffset = p->iOffset; |
| 2959 p->iLimit = regLimitB; | 3009 p->iLimit = regLimitB; |
| 2960 p->iOffset = 0; | 3010 p->iOffset = 0; |
| 2961 explainSetInteger(iSub2, pParse->iNextSelectId); | 3011 explainSetInteger(iSub2, pParse->iNextSelectId); |
| 2962 sqlite3Select(pParse, p, &destB); | 3012 sqlite3Select(pParse, p, &destB); |
| 2963 p->iLimit = savedLimit; | 3013 p->iLimit = savedLimit; |
| 2964 p->iOffset = savedOffset; | 3014 p->iOffset = savedOffset; |
| 2965 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB); | 3015 sqlite3VdbeEndCoroutine(v, regAddrB); |
| 2966 | 3016 |
| 2967 /* Generate a subroutine that outputs the current row of the A | 3017 /* Generate a subroutine that outputs the current row of the A |
| 2968 ** select as the next output row of the compound select. | 3018 ** select as the next output row of the compound select. |
| 2969 */ | 3019 */ |
| 2970 VdbeNoopComment((v, "Output routine for A")); | 3020 VdbeNoopComment((v, "Output routine for A")); |
| 2971 addrOutA = generateOutputSubroutine(pParse, | 3021 addrOutA = generateOutputSubroutine(pParse, |
| 2972 p, &destA, pDest, regOutA, | 3022 p, &destA, pDest, regOutA, |
| 2973 regPrev, pKeyDup, labelEnd); | 3023 regPrev, pKeyDup, labelEnd); |
| 2974 | 3024 |
| 2975 /* Generate a subroutine that outputs the current row of the B | 3025 /* Generate a subroutine that outputs the current row of the B |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2987 ** are exhausted and only data in select B remains. | 3037 ** are exhausted and only data in select B remains. |
| 2988 */ | 3038 */ |
| 2989 if( op==TK_EXCEPT || op==TK_INTERSECT ){ | 3039 if( op==TK_EXCEPT || op==TK_INTERSECT ){ |
| 2990 addrEofA_noB = addrEofA = labelEnd; | 3040 addrEofA_noB = addrEofA = labelEnd; |
| 2991 }else{ | 3041 }else{ |
| 2992 VdbeNoopComment((v, "eof-A subroutine")); | 3042 VdbeNoopComment((v, "eof-A subroutine")); |
| 2993 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); | 3043 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); |
| 2994 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd); | 3044 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd); |
| 2995 VdbeCoverage(v); | 3045 VdbeCoverage(v); |
| 2996 sqlite3VdbeGoto(v, addrEofA); | 3046 sqlite3VdbeGoto(v, addrEofA); |
| 2997 p->nSelectRow += pPrior->nSelectRow; | 3047 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 2998 } | 3048 } |
| 2999 | 3049 |
| 3000 /* Generate a subroutine to run when the results from select B | 3050 /* Generate a subroutine to run when the results from select B |
| 3001 ** are exhausted and only data in select A remains. | 3051 ** are exhausted and only data in select A remains. |
| 3002 */ | 3052 */ |
| 3003 if( op==TK_INTERSECT ){ | 3053 if( op==TK_INTERSECT ){ |
| 3004 addrEofB = addrEofA; | 3054 addrEofB = addrEofA; |
| 3005 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; | 3055 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; |
| 3006 }else{ | 3056 }else{ |
| 3007 VdbeNoopComment((v, "eof-B subroutine")); | 3057 VdbeNoopComment((v, "eof-B subroutine")); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3078 | 3128 |
| 3079 /*** TBD: Insert subroutine calls to close cursors on incomplete | 3129 /*** TBD: Insert subroutine calls to close cursors on incomplete |
| 3080 **** subqueries ****/ | 3130 **** subqueries ****/ |
| 3081 explainComposite(pParse, p->op, iSub1, iSub2, 0); | 3131 explainComposite(pParse, p->op, iSub1, iSub2, 0); |
| 3082 return pParse->nErr!=0; | 3132 return pParse->nErr!=0; |
| 3083 } | 3133 } |
| 3084 #endif | 3134 #endif |
| 3085 | 3135 |
| 3086 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) | 3136 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 3087 /* Forward Declarations */ | 3137 /* Forward Declarations */ |
| 3088 static void substExprList(sqlite3*, ExprList*, int, ExprList*); | 3138 static void substExprList(Parse*, ExprList*, int, ExprList*); |
| 3089 static void substSelect(sqlite3*, Select *, int, ExprList*, int); | 3139 static void substSelect(Parse*, Select *, int, ExprList*, int); |
| 3090 | 3140 |
| 3091 /* | 3141 /* |
| 3092 ** Scan through the expression pExpr. Replace every reference to | 3142 ** Scan through the expression pExpr. Replace every reference to |
| 3093 ** a column in table number iTable with a copy of the iColumn-th | 3143 ** a column in table number iTable with a copy of the iColumn-th |
| 3094 ** entry in pEList. (But leave references to the ROWID column | 3144 ** entry in pEList. (But leave references to the ROWID column |
| 3095 ** unchanged.) | 3145 ** unchanged.) |
| 3096 ** | 3146 ** |
| 3097 ** This routine is part of the flattening procedure. A subquery | 3147 ** This routine is part of the flattening procedure. A subquery |
| 3098 ** whose result set is defined by pEList appears as entry in the | 3148 ** whose result set is defined by pEList appears as entry in the |
| 3099 ** FROM clause of a SELECT such that the VDBE cursor assigned to that | 3149 ** FROM clause of a SELECT such that the VDBE cursor assigned to that |
| 3100 ** FORM clause entry is iTable. This routine make the necessary | 3150 ** FORM clause entry is iTable. This routine make the necessary |
| 3101 ** changes to pExpr so that it refers directly to the source table | 3151 ** changes to pExpr so that it refers directly to the source table |
| 3102 ** of the subquery rather the result set of the subquery. | 3152 ** of the subquery rather the result set of the subquery. |
| 3103 */ | 3153 */ |
| 3104 static Expr *substExpr( | 3154 static Expr *substExpr( |
| 3105 sqlite3 *db, /* Report malloc errors to this connection */ | 3155 Parse *pParse, /* Report errors here */ |
| 3106 Expr *pExpr, /* Expr in which substitution occurs */ | 3156 Expr *pExpr, /* Expr in which substitution occurs */ |
| 3107 int iTable, /* Table to be substituted */ | 3157 int iTable, /* Table to be substituted */ |
| 3108 ExprList *pEList /* Substitute expressions */ | 3158 ExprList *pEList /* Substitute expressions */ |
| 3109 ){ | 3159 ){ |
| 3160 sqlite3 *db = pParse->db; |
| 3110 if( pExpr==0 ) return 0; | 3161 if( pExpr==0 ) return 0; |
| 3111 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ | 3162 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ |
| 3112 if( pExpr->iColumn<0 ){ | 3163 if( pExpr->iColumn<0 ){ |
| 3113 pExpr->op = TK_NULL; | 3164 pExpr->op = TK_NULL; |
| 3114 }else{ | 3165 }else{ |
| 3115 Expr *pNew; | 3166 Expr *pNew; |
| 3167 Expr *pCopy = pEList->a[pExpr->iColumn].pExpr; |
| 3116 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); | 3168 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); |
| 3117 assert( pExpr->pLeft==0 && pExpr->pRight==0 ); | 3169 assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
| 3118 pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0); | 3170 if( sqlite3ExprIsVector(pCopy) ){ |
| 3119 sqlite3ExprDelete(db, pExpr); | 3171 sqlite3VectorErrorMsg(pParse, pCopy); |
| 3120 pExpr = pNew; | 3172 }else{ |
| 3173 pNew = sqlite3ExprDup(db, pCopy, 0); |
| 3174 if( pNew && (pExpr->flags & EP_FromJoin) ){ |
| 3175 pNew->iRightJoinTable = pExpr->iRightJoinTable; |
| 3176 pNew->flags |= EP_FromJoin; |
| 3177 } |
| 3178 sqlite3ExprDelete(db, pExpr); |
| 3179 pExpr = pNew; |
| 3180 } |
| 3121 } | 3181 } |
| 3122 }else{ | 3182 }else{ |
| 3123 pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList); | 3183 pExpr->pLeft = substExpr(pParse, pExpr->pLeft, iTable, pEList); |
| 3124 pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList); | 3184 pExpr->pRight = substExpr(pParse, pExpr->pRight, iTable, pEList); |
| 3125 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ | 3185 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 3126 substSelect(db, pExpr->x.pSelect, iTable, pEList, 1); | 3186 substSelect(pParse, pExpr->x.pSelect, iTable, pEList, 1); |
| 3127 }else{ | 3187 }else{ |
| 3128 substExprList(db, pExpr->x.pList, iTable, pEList); | 3188 substExprList(pParse, pExpr->x.pList, iTable, pEList); |
| 3129 } | 3189 } |
| 3130 } | 3190 } |
| 3131 return pExpr; | 3191 return pExpr; |
| 3132 } | 3192 } |
| 3133 static void substExprList( | 3193 static void substExprList( |
| 3134 sqlite3 *db, /* Report malloc errors here */ | 3194 Parse *pParse, /* Report errors here */ |
| 3135 ExprList *pList, /* List to scan and in which to make substitutes */ | 3195 ExprList *pList, /* List to scan and in which to make substitutes */ |
| 3136 int iTable, /* Table to be substituted */ | 3196 int iTable, /* Table to be substituted */ |
| 3137 ExprList *pEList /* Substitute values */ | 3197 ExprList *pEList /* Substitute values */ |
| 3138 ){ | 3198 ){ |
| 3139 int i; | 3199 int i; |
| 3140 if( pList==0 ) return; | 3200 if( pList==0 ) return; |
| 3141 for(i=0; i<pList->nExpr; i++){ | 3201 for(i=0; i<pList->nExpr; i++){ |
| 3142 pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList); | 3202 pList->a[i].pExpr = substExpr(pParse, pList->a[i].pExpr, iTable, pEList); |
| 3143 } | 3203 } |
| 3144 } | 3204 } |
| 3145 static void substSelect( | 3205 static void substSelect( |
| 3146 sqlite3 *db, /* Report malloc errors here */ | 3206 Parse *pParse, /* Report errors here */ |
| 3147 Select *p, /* SELECT statement in which to make substitutions */ | 3207 Select *p, /* SELECT statement in which to make substitutions */ |
| 3148 int iTable, /* Table to be replaced */ | 3208 int iTable, /* Table to be replaced */ |
| 3149 ExprList *pEList, /* Substitute values */ | 3209 ExprList *pEList, /* Substitute values */ |
| 3150 int doPrior /* Do substitutes on p->pPrior too */ | 3210 int doPrior /* Do substitutes on p->pPrior too */ |
| 3151 ){ | 3211 ){ |
| 3152 SrcList *pSrc; | 3212 SrcList *pSrc; |
| 3153 struct SrcList_item *pItem; | 3213 struct SrcList_item *pItem; |
| 3154 int i; | 3214 int i; |
| 3155 if( !p ) return; | 3215 if( !p ) return; |
| 3156 do{ | 3216 do{ |
| 3157 substExprList(db, p->pEList, iTable, pEList); | 3217 substExprList(pParse, p->pEList, iTable, pEList); |
| 3158 substExprList(db, p->pGroupBy, iTable, pEList); | 3218 substExprList(pParse, p->pGroupBy, iTable, pEList); |
| 3159 substExprList(db, p->pOrderBy, iTable, pEList); | 3219 substExprList(pParse, p->pOrderBy, iTable, pEList); |
| 3160 p->pHaving = substExpr(db, p->pHaving, iTable, pEList); | 3220 p->pHaving = substExpr(pParse, p->pHaving, iTable, pEList); |
| 3161 p->pWhere = substExpr(db, p->pWhere, iTable, pEList); | 3221 p->pWhere = substExpr(pParse, p->pWhere, iTable, pEList); |
| 3162 pSrc = p->pSrc; | 3222 pSrc = p->pSrc; |
| 3163 assert( pSrc!=0 ); | 3223 assert( pSrc!=0 ); |
| 3164 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ | 3224 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ |
| 3165 substSelect(db, pItem->pSelect, iTable, pEList, 1); | 3225 substSelect(pParse, pItem->pSelect, iTable, pEList, 1); |
| 3166 if( pItem->fg.isTabFunc ){ | 3226 if( pItem->fg.isTabFunc ){ |
| 3167 substExprList(db, pItem->u1.pFuncArg, iTable, pEList); | 3227 substExprList(pParse, pItem->u1.pFuncArg, iTable, pEList); |
| 3168 } | 3228 } |
| 3169 } | 3229 } |
| 3170 }while( doPrior && (p = p->pPrior)!=0 ); | 3230 }while( doPrior && (p = p->pPrior)!=0 ); |
| 3171 } | 3231 } |
| 3172 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ | 3232 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 3173 | 3233 |
| 3174 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) | 3234 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 3175 /* | 3235 /* |
| 3176 ** This routine attempts to flatten subqueries as a performance optimization. | 3236 ** This routine attempts to flatten subqueries as a performance optimization. |
| 3177 ** This routine returns 1 if it makes changes and 0 if no flattening occurs. | 3237 ** This routine returns 1 if it makes changes and 0 if no flattening occurs. |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3554 | 3614 |
| 3555 /* Defer deleting the Table object associated with the | 3615 /* Defer deleting the Table object associated with the |
| 3556 ** subquery until code generation is | 3616 ** subquery until code generation is |
| 3557 ** complete, since there may still exist Expr.pTab entries that | 3617 ** complete, since there may still exist Expr.pTab entries that |
| 3558 ** refer to the subquery even after flattening. Ticket #3346. | 3618 ** refer to the subquery even after flattening. Ticket #3346. |
| 3559 ** | 3619 ** |
| 3560 ** pSubitem->pTab is always non-NULL by test restrictions and tests above. | 3620 ** pSubitem->pTab is always non-NULL by test restrictions and tests above. |
| 3561 */ | 3621 */ |
| 3562 if( ALWAYS(pSubitem->pTab!=0) ){ | 3622 if( ALWAYS(pSubitem->pTab!=0) ){ |
| 3563 Table *pTabToDel = pSubitem->pTab; | 3623 Table *pTabToDel = pSubitem->pTab; |
| 3564 if( pTabToDel->nRef==1 ){ | 3624 if( pTabToDel->nTabRef==1 ){ |
| 3565 Parse *pToplevel = sqlite3ParseToplevel(pParse); | 3625 Parse *pToplevel = sqlite3ParseToplevel(pParse); |
| 3566 pTabToDel->pNextZombie = pToplevel->pZombieTab; | 3626 pTabToDel->pNextZombie = pToplevel->pZombieTab; |
| 3567 pToplevel->pZombieTab = pTabToDel; | 3627 pToplevel->pZombieTab = pTabToDel; |
| 3568 }else{ | 3628 }else{ |
| 3569 pTabToDel->nRef--; | 3629 pTabToDel->nTabRef--; |
| 3570 } | 3630 } |
| 3571 pSubitem->pTab = 0; | 3631 pSubitem->pTab = 0; |
| 3572 } | 3632 } |
| 3573 | 3633 |
| 3574 /* The following loop runs once for each term in a compound-subquery | 3634 /* The following loop runs once for each term in a compound-subquery |
| 3575 ** flattening (as described above). If we are doing a different kind | 3635 ** flattening (as described above). If we are doing a different kind |
| 3576 ** of flattening - a flattening other than a compound-subquery flattening - | 3636 ** of flattening - a flattening other than a compound-subquery flattening - |
| 3577 ** then this loop only runs once. | 3637 ** then this loop only runs once. |
| 3578 ** | 3638 ** |
| 3579 ** This loop moves all of the FROM elements of the subquery into the | 3639 ** This loop moves all of the FROM elements of the subquery into the |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3674 assert( pParent->pOrderBy==0 ); | 3734 assert( pParent->pOrderBy==0 ); |
| 3675 assert( pSub->pPrior==0 ); | 3735 assert( pSub->pPrior==0 ); |
| 3676 pParent->pOrderBy = pOrderBy; | 3736 pParent->pOrderBy = pOrderBy; |
| 3677 pSub->pOrderBy = 0; | 3737 pSub->pOrderBy = 0; |
| 3678 } | 3738 } |
| 3679 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0); | 3739 pWhere = sqlite3ExprDup(db, pSub->pWhere, 0); |
| 3680 if( subqueryIsAgg ){ | 3740 if( subqueryIsAgg ){ |
| 3681 assert( pParent->pHaving==0 ); | 3741 assert( pParent->pHaving==0 ); |
| 3682 pParent->pHaving = pParent->pWhere; | 3742 pParent->pHaving = pParent->pWhere; |
| 3683 pParent->pWhere = pWhere; | 3743 pParent->pWhere = pWhere; |
| 3684 pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, | 3744 pParent->pHaving = sqlite3ExprAnd(db, |
| 3685 sqlite3ExprDup(db, pSub->pHaving, 0)); | 3745 sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving |
| 3746 ); |
| 3686 assert( pParent->pGroupBy==0 ); | 3747 assert( pParent->pGroupBy==0 ); |
| 3687 pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0); | 3748 pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0); |
| 3688 }else{ | 3749 }else{ |
| 3689 pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere); | 3750 pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere); |
| 3690 } | 3751 } |
| 3691 substSelect(db, pParent, iParent, pSub->pEList, 0); | 3752 substSelect(pParse, pParent, iParent, pSub->pEList, 0); |
| 3692 | 3753 |
| 3693 /* The flattened query is distinct if either the inner or the | 3754 /* The flattened query is distinct if either the inner or the |
| 3694 ** outer query is distinct. | 3755 ** outer query is distinct. |
| 3695 */ | 3756 */ |
| 3696 pParent->selFlags |= pSub->selFlags & SF_Distinct; | 3757 pParent->selFlags |= pSub->selFlags & SF_Distinct; |
| 3697 | 3758 |
| 3698 /* | 3759 /* |
| 3699 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; | 3760 ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; |
| 3700 ** | 3761 ** |
| 3701 ** One is tempted to try to add a and b to combine the limits. But this | 3762 ** One is tempted to try to add a and b to combine the limits. But this |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3755 ** enforces this restriction since this routine does not have enough | 3816 ** enforces this restriction since this routine does not have enough |
| 3756 ** information to know.) | 3817 ** information to know.) |
| 3757 ** | 3818 ** |
| 3758 ** (5) The WHERE clause expression originates in the ON or USING clause | 3819 ** (5) The WHERE clause expression originates in the ON or USING clause |
| 3759 ** of a LEFT JOIN. | 3820 ** of a LEFT JOIN. |
| 3760 ** | 3821 ** |
| 3761 ** Return 0 if no changes are made and non-zero if one or more WHERE clause | 3822 ** Return 0 if no changes are made and non-zero if one or more WHERE clause |
| 3762 ** terms are duplicated into the subquery. | 3823 ** terms are duplicated into the subquery. |
| 3763 */ | 3824 */ |
| 3764 static int pushDownWhereTerms( | 3825 static int pushDownWhereTerms( |
| 3765 sqlite3 *db, /* The database connection (for malloc()) */ | 3826 Parse *pParse, /* Parse context (for malloc() and error reporting) */ |
| 3766 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ | 3827 Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ |
| 3767 Expr *pWhere, /* The WHERE clause of the outer query */ | 3828 Expr *pWhere, /* The WHERE clause of the outer query */ |
| 3768 int iCursor /* Cursor number of the subquery */ | 3829 int iCursor /* Cursor number of the subquery */ |
| 3769 ){ | 3830 ){ |
| 3770 Expr *pNew; | 3831 Expr *pNew; |
| 3771 int nChng = 0; | 3832 int nChng = 0; |
| 3833 Select *pX; /* For looping over compound SELECTs in pSubq */ |
| 3772 if( pWhere==0 ) return 0; | 3834 if( pWhere==0 ) return 0; |
| 3773 if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){ | 3835 for(pX=pSubq; pX; pX=pX->pPrior){ |
| 3774 return 0; /* restrictions (1) and (2) */ | 3836 if( (pX->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){ |
| 3837 testcase( pX->selFlags & SF_Aggregate ); |
| 3838 testcase( pX->selFlags & SF_Recursive ); |
| 3839 testcase( pX!=pSubq ); |
| 3840 return 0; /* restrictions (1) and (2) */ |
| 3841 } |
| 3775 } | 3842 } |
| 3776 if( pSubq->pLimit!=0 ){ | 3843 if( pSubq->pLimit!=0 ){ |
| 3777 return 0; /* restriction (3) */ | 3844 return 0; /* restriction (3) */ |
| 3778 } | 3845 } |
| 3779 while( pWhere->op==TK_AND ){ | 3846 while( pWhere->op==TK_AND ){ |
| 3780 nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor); | 3847 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor); |
| 3781 pWhere = pWhere->pLeft; | 3848 pWhere = pWhere->pLeft; |
| 3782 } | 3849 } |
| 3783 if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */ | 3850 if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */ |
| 3784 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){ | 3851 if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){ |
| 3785 nChng++; | 3852 nChng++; |
| 3786 while( pSubq ){ | 3853 while( pSubq ){ |
| 3787 pNew = sqlite3ExprDup(db, pWhere, 0); | 3854 pNew = sqlite3ExprDup(pParse->db, pWhere, 0); |
| 3788 pNew = substExpr(db, pNew, iCursor, pSubq->pEList); | 3855 pNew = substExpr(pParse, pNew, iCursor, pSubq->pEList); |
| 3789 pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew); | 3856 pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew); |
| 3790 pSubq = pSubq->pPrior; | 3857 pSubq = pSubq->pPrior; |
| 3791 } | 3858 } |
| 3792 } | 3859 } |
| 3793 return nChng; | 3860 return nChng; |
| 3794 } | 3861 } |
| 3795 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ | 3862 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 3796 | 3863 |
| 3797 /* | 3864 /* |
| 3798 ** Based on the contents of the AggInfo structure indicated by the first | 3865 ** Based on the contents of the AggInfo structure indicated by the first |
| 3799 ** argument, this function checks if the following are true: | 3866 ** argument, this function checks if the following are true: |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4071 ** In this case, proceed. */ | 4138 ** In this case, proceed. */ |
| 4072 if( pCte->zCteErr ){ | 4139 if( pCte->zCteErr ){ |
| 4073 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName); | 4140 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName); |
| 4074 return SQLITE_ERROR; | 4141 return SQLITE_ERROR; |
| 4075 } | 4142 } |
| 4076 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR; | 4143 if( cannotBeFunction(pParse, pFrom) ) return SQLITE_ERROR; |
| 4077 | 4144 |
| 4078 assert( pFrom->pTab==0 ); | 4145 assert( pFrom->pTab==0 ); |
| 4079 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); | 4146 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); |
| 4080 if( pTab==0 ) return WRC_Abort; | 4147 if( pTab==0 ) return WRC_Abort; |
| 4081 pTab->nRef = 1; | 4148 pTab->nTabRef = 1; |
| 4082 pTab->zName = sqlite3DbStrDup(db, pCte->zName); | 4149 pTab->zName = sqlite3DbStrDup(db, pCte->zName); |
| 4083 pTab->iPKey = -1; | 4150 pTab->iPKey = -1; |
| 4084 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); | 4151 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 4085 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; | 4152 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; |
| 4086 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); | 4153 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); |
| 4087 if( db->mallocFailed ) return SQLITE_NOMEM; | 4154 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; |
| 4088 assert( pFrom->pSelect ); | 4155 assert( pFrom->pSelect ); |
| 4089 | 4156 |
| 4090 /* Check if this is a recursive CTE. */ | 4157 /* Check if this is a recursive CTE. */ |
| 4091 pSel = pFrom->pSelect; | 4158 pSel = pFrom->pSelect; |
| 4092 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION ); | 4159 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION ); |
| 4093 if( bMayRecursive ){ | 4160 if( bMayRecursive ){ |
| 4094 int i; | 4161 int i; |
| 4095 SrcList *pSrc = pFrom->pSelect->pSrc; | 4162 SrcList *pSrc = pFrom->pSelect->pSrc; |
| 4096 for(i=0; i<pSrc->nSrc; i++){ | 4163 for(i=0; i<pSrc->nSrc; i++){ |
| 4097 struct SrcList_item *pItem = &pSrc->a[i]; | 4164 struct SrcList_item *pItem = &pSrc->a[i]; |
| 4098 if( pItem->zDatabase==0 | 4165 if( pItem->zDatabase==0 |
| 4099 && pItem->zName!=0 | 4166 && pItem->zName!=0 |
| 4100 && 0==sqlite3StrICmp(pItem->zName, pCte->zName) | 4167 && 0==sqlite3StrICmp(pItem->zName, pCte->zName) |
| 4101 ){ | 4168 ){ |
| 4102 pItem->pTab = pTab; | 4169 pItem->pTab = pTab; |
| 4103 pItem->fg.isRecursive = 1; | 4170 pItem->fg.isRecursive = 1; |
| 4104 pTab->nRef++; | 4171 pTab->nTabRef++; |
| 4105 pSel->selFlags |= SF_Recursive; | 4172 pSel->selFlags |= SF_Recursive; |
| 4106 } | 4173 } |
| 4107 } | 4174 } |
| 4108 } | 4175 } |
| 4109 | 4176 |
| 4110 /* Only one recursive reference is permitted. */ | 4177 /* Only one recursive reference is permitted. */ |
| 4111 if( pTab->nRef>2 ){ | 4178 if( pTab->nTabRef>2 ){ |
| 4112 sqlite3ErrorMsg( | 4179 sqlite3ErrorMsg( |
| 4113 pParse, "multiple references to recursive table: %s", pCte->zName | 4180 pParse, "multiple references to recursive table: %s", pCte->zName |
| 4114 ); | 4181 ); |
| 4115 return SQLITE_ERROR; | 4182 return SQLITE_ERROR; |
| 4116 } | 4183 } |
| 4117 assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 )); | 4184 assert( pTab->nTabRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nTabRef=
=2 )); |
| 4118 | 4185 |
| 4119 pCte->zCteErr = "circular reference: %s"; | 4186 pCte->zCteErr = "circular reference: %s"; |
| 4120 pSavedWith = pParse->pWith; | 4187 pSavedWith = pParse->pWith; |
| 4121 pParse->pWith = pWith; | 4188 pParse->pWith = pWith; |
| 4122 sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel); | 4189 if( bMayRecursive ){ |
| 4190 Select *pPrior = pSel->pPrior; |
| 4191 assert( pPrior->pWith==0 ); |
| 4192 pPrior->pWith = pSel->pWith; |
| 4193 sqlite3WalkSelect(pWalker, pPrior); |
| 4194 pPrior->pWith = 0; |
| 4195 }else{ |
| 4196 sqlite3WalkSelect(pWalker, pSel); |
| 4197 } |
| 4123 pParse->pWith = pWith; | 4198 pParse->pWith = pWith; |
| 4124 | 4199 |
| 4125 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); | 4200 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); |
| 4126 pEList = pLeft->pEList; | 4201 pEList = pLeft->pEList; |
| 4127 if( pCte->pCols ){ | 4202 if( pCte->pCols ){ |
| 4128 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ | 4203 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ |
| 4129 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", | 4204 sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", |
| 4130 pCte->zName, pEList->nExpr, pCte->pCols->nExpr | 4205 pCte->zName, pEList->nExpr, pCte->pCols->nExpr |
| 4131 ); | 4206 ); |
| 4132 pParse->pWith = pSavedWith; | 4207 pParse->pWith = pSavedWith; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 4156 /* | 4231 /* |
| 4157 ** If the SELECT passed as the second argument has an associated WITH | 4232 ** If the SELECT passed as the second argument has an associated WITH |
| 4158 ** clause, pop it from the stack stored as part of the Parse object. | 4233 ** clause, pop it from the stack stored as part of the Parse object. |
| 4159 ** | 4234 ** |
| 4160 ** This function is used as the xSelectCallback2() callback by | 4235 ** This function is used as the xSelectCallback2() callback by |
| 4161 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table | 4236 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table |
| 4162 ** names and other FROM clause elements. | 4237 ** names and other FROM clause elements. |
| 4163 */ | 4238 */ |
| 4164 static void selectPopWith(Walker *pWalker, Select *p){ | 4239 static void selectPopWith(Walker *pWalker, Select *p){ |
| 4165 Parse *pParse = pWalker->pParse; | 4240 Parse *pParse = pWalker->pParse; |
| 4166 With *pWith = findRightmost(p)->pWith; | 4241 if( pParse->pWith && p->pPrior==0 ){ |
| 4167 if( pWith!=0 ){ | 4242 With *pWith = findRightmost(p)->pWith; |
| 4168 assert( pParse->pWith==pWith ); | 4243 if( pWith!=0 ){ |
| 4169 pParse->pWith = pWith->pOuter; | 4244 assert( pParse->pWith==pWith ); |
| 4245 pParse->pWith = pWith->pOuter; |
| 4246 } |
| 4170 } | 4247 } |
| 4171 } | 4248 } |
| 4172 #else | 4249 #else |
| 4173 #define selectPopWith 0 | 4250 #define selectPopWith 0 |
| 4174 #endif | 4251 #endif |
| 4175 | 4252 |
| 4176 /* | 4253 /* |
| 4177 ** This routine is a Walker callback for "expanding" a SELECT statement. | 4254 ** This routine is a Walker callback for "expanding" a SELECT statement. |
| 4178 ** "Expanding" means to do the following: | 4255 ** "Expanding" means to do the following: |
| 4179 ** | 4256 ** |
| (...skipping 29 matching lines...) Expand all Loading... |
| 4209 | 4286 |
| 4210 p->selFlags |= SF_Expanded; | 4287 p->selFlags |= SF_Expanded; |
| 4211 if( db->mallocFailed ){ | 4288 if( db->mallocFailed ){ |
| 4212 return WRC_Abort; | 4289 return WRC_Abort; |
| 4213 } | 4290 } |
| 4214 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){ | 4291 if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){ |
| 4215 return WRC_Prune; | 4292 return WRC_Prune; |
| 4216 } | 4293 } |
| 4217 pTabList = p->pSrc; | 4294 pTabList = p->pSrc; |
| 4218 pEList = p->pEList; | 4295 pEList = p->pEList; |
| 4219 if( pWalker->xSelectCallback2==selectPopWith ){ | 4296 if( p->pWith ){ |
| 4220 sqlite3WithPush(pParse, findRightmost(p)->pWith, 0); | 4297 sqlite3WithPush(pParse, p->pWith, 0); |
| 4221 } | 4298 } |
| 4222 | 4299 |
| 4223 /* Make sure cursor numbers have been assigned to all entries in | 4300 /* Make sure cursor numbers have been assigned to all entries in |
| 4224 ** the FROM clause of the SELECT statement. | 4301 ** the FROM clause of the SELECT statement. |
| 4225 */ | 4302 */ |
| 4226 sqlite3SrcListAssignCursors(pParse, pTabList); | 4303 sqlite3SrcListAssignCursors(pParse, pTabList); |
| 4227 | 4304 |
| 4228 /* Look up every table named in the FROM clause of the select. If | 4305 /* Look up every table named in the FROM clause of the select. If |
| 4229 ** an entry of the FROM clause is a subquery instead of a table or view, | 4306 ** an entry of the FROM clause is a subquery instead of a table or view, |
| 4230 ** then create a transient table structure to describe the subquery. | 4307 ** then create a transient table structure to describe the subquery. |
| 4231 */ | 4308 */ |
| 4232 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ | 4309 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ |
| 4233 Table *pTab; | 4310 Table *pTab; |
| 4234 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 ); | 4311 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 ); |
| 4235 if( pFrom->fg.isRecursive ) continue; | 4312 if( pFrom->fg.isRecursive ) continue; |
| 4236 assert( pFrom->pTab==0 ); | 4313 assert( pFrom->pTab==0 ); |
| 4237 #ifndef SQLITE_OMIT_CTE | 4314 #ifndef SQLITE_OMIT_CTE |
| 4238 if( withExpand(pWalker, pFrom) ) return WRC_Abort; | 4315 if( withExpand(pWalker, pFrom) ) return WRC_Abort; |
| 4239 if( pFrom->pTab ) {} else | 4316 if( pFrom->pTab ) {} else |
| 4240 #endif | 4317 #endif |
| 4241 if( pFrom->zName==0 ){ | 4318 if( pFrom->zName==0 ){ |
| 4242 #ifndef SQLITE_OMIT_SUBQUERY | 4319 #ifndef SQLITE_OMIT_SUBQUERY |
| 4243 Select *pSel = pFrom->pSelect; | 4320 Select *pSel = pFrom->pSelect; |
| 4244 /* A sub-query in the FROM clause of a SELECT */ | 4321 /* A sub-query in the FROM clause of a SELECT */ |
| 4245 assert( pSel!=0 ); | 4322 assert( pSel!=0 ); |
| 4246 assert( pFrom->pTab==0 ); | 4323 assert( pFrom->pTab==0 ); |
| 4247 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort; | 4324 if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort; |
| 4248 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); | 4325 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); |
| 4249 if( pTab==0 ) return WRC_Abort; | 4326 if( pTab==0 ) return WRC_Abort; |
| 4250 pTab->nRef = 1; | 4327 pTab->nTabRef = 1; |
| 4251 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab); | 4328 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab); |
| 4252 while( pSel->pPrior ){ pSel = pSel->pPrior; } | 4329 while( pSel->pPrior ){ pSel = pSel->pPrior; } |
| 4253 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); | 4330 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); |
| 4254 pTab->iPKey = -1; | 4331 pTab->iPKey = -1; |
| 4255 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); | 4332 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 4256 pTab->tabFlags |= TF_Ephemeral; | 4333 pTab->tabFlags |= TF_Ephemeral; |
| 4257 #endif | 4334 #endif |
| 4258 }else{ | 4335 }else{ |
| 4259 /* An ordinary table or view name in the FROM clause */ | 4336 /* An ordinary table or view name in the FROM clause */ |
| 4260 assert( pFrom->pTab==0 ); | 4337 assert( pFrom->pTab==0 ); |
| 4261 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); | 4338 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); |
| 4262 if( pTab==0 ) return WRC_Abort; | 4339 if( pTab==0 ) return WRC_Abort; |
| 4263 if( pTab->nRef==0xffff ){ | 4340 if( pTab->nTabRef>=0xffff ){ |
| 4264 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535", | 4341 sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535", |
| 4265 pTab->zName); | 4342 pTab->zName); |
| 4266 pFrom->pTab = 0; | 4343 pFrom->pTab = 0; |
| 4267 return WRC_Abort; | 4344 return WRC_Abort; |
| 4268 } | 4345 } |
| 4269 pTab->nRef++; | 4346 pTab->nTabRef++; |
| 4270 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){ | 4347 if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){ |
| 4271 return WRC_Abort; | 4348 return WRC_Abort; |
| 4272 } | 4349 } |
| 4273 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) | 4350 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) |
| 4274 if( IsVirtual(pTab) || pTab->pSelect ){ | 4351 if( IsVirtual(pTab) || pTab->pSelect ){ |
| 4275 i16 nCol; | 4352 i16 nCol; |
| 4276 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; | 4353 if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; |
| 4277 assert( pFrom->pSelect==0 ); | 4354 assert( pFrom->pSelect==0 ); |
| 4278 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); | 4355 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); |
| 4279 sqlite3SelectSetName(pFrom->pSelect, pTab->zName); | 4356 sqlite3SelectSetName(pFrom->pSelect, pTab->zName); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4363 if( zTabName==0 ){ | 4440 if( zTabName==0 ){ |
| 4364 zTabName = pTab->zName; | 4441 zTabName = pTab->zName; |
| 4365 } | 4442 } |
| 4366 if( db->mallocFailed ) break; | 4443 if( db->mallocFailed ) break; |
| 4367 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){ | 4444 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){ |
| 4368 pSub = 0; | 4445 pSub = 0; |
| 4369 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ | 4446 if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ |
| 4370 continue; | 4447 continue; |
| 4371 } | 4448 } |
| 4372 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | 4449 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 4373 zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*"; | 4450 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*"; |
| 4374 } | 4451 } |
| 4375 for(j=0; j<pTab->nCol; j++){ | 4452 for(j=0; j<pTab->nCol; j++){ |
| 4376 char *zName = pTab->aCol[j].zName; | 4453 char *zName = pTab->aCol[j].zName; |
| 4377 char *zColname; /* The computed column name */ | 4454 char *zColname; /* The computed column name */ |
| 4378 char *zToFree; /* Malloced string that needs to be freed */ | 4455 char *zToFree; /* Malloced string that needs to be freed */ |
| 4379 Token sColname; /* Computed column name as a token */ | 4456 Token sColname; /* Computed column name as a token */ |
| 4380 | 4457 |
| 4381 assert( zName ); | 4458 assert( zName ); |
| 4382 if( zTName && pSub | 4459 if( zTName && pSub |
| 4383 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0 | 4460 && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 4409 ** using clause from the table on the right. */ | 4486 ** using clause from the table on the right. */ |
| 4410 continue; | 4487 continue; |
| 4411 } | 4488 } |
| 4412 } | 4489 } |
| 4413 pRight = sqlite3Expr(db, TK_ID, zName); | 4490 pRight = sqlite3Expr(db, TK_ID, zName); |
| 4414 zColname = zName; | 4491 zColname = zName; |
| 4415 zToFree = 0; | 4492 zToFree = 0; |
| 4416 if( longNames || pTabList->nSrc>1 ){ | 4493 if( longNames || pTabList->nSrc>1 ){ |
| 4417 Expr *pLeft; | 4494 Expr *pLeft; |
| 4418 pLeft = sqlite3Expr(db, TK_ID, zTabName); | 4495 pLeft = sqlite3Expr(db, TK_ID, zTabName); |
| 4419 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); | 4496 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); |
| 4420 if( zSchemaName ){ | 4497 if( zSchemaName ){ |
| 4421 pLeft = sqlite3Expr(db, TK_ID, zSchemaName); | 4498 pLeft = sqlite3Expr(db, TK_ID, zSchemaName); |
| 4422 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0); | 4499 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr); |
| 4423 } | 4500 } |
| 4424 if( longNames ){ | 4501 if( longNames ){ |
| 4425 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName); | 4502 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName); |
| 4426 zToFree = zColname; | 4503 zToFree = zColname; |
| 4427 } | 4504 } |
| 4428 }else{ | 4505 }else{ |
| 4429 pExpr = pRight; | 4506 pExpr = pRight; |
| 4430 } | 4507 } |
| 4431 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); | 4508 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); |
| 4432 sColname.z = zColname; | 4509 sqlite3TokenInit(&sColname, zColname); |
| 4433 sColname.n = sqlite3Strlen30(zColname); | |
| 4434 sqlite3ExprListSetName(pParse, pNew, &sColname, 0); | 4510 sqlite3ExprListSetName(pParse, pNew, &sColname, 0); |
| 4435 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){ | 4511 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){ |
| 4436 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; | 4512 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; |
| 4437 if( pSub ){ | 4513 if( pSub ){ |
| 4438 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan); | 4514 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan); |
| 4439 testcase( pX->zSpan==0 ); | 4515 testcase( pX->zSpan==0 ); |
| 4440 }else{ | 4516 }else{ |
| 4441 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s", | 4517 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s", |
| 4442 zSchemaName, zTabName, zColname); | 4518 zSchemaName, zTabName, zColname); |
| 4443 testcase( pX->zSpan==0 ); | 4519 testcase( pX->zSpan==0 ); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4498 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ | 4574 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ |
| 4499 Walker w; | 4575 Walker w; |
| 4500 memset(&w, 0, sizeof(w)); | 4576 memset(&w, 0, sizeof(w)); |
| 4501 w.xExprCallback = sqlite3ExprWalkNoop; | 4577 w.xExprCallback = sqlite3ExprWalkNoop; |
| 4502 w.pParse = pParse; | 4578 w.pParse = pParse; |
| 4503 if( pParse->hasCompound ){ | 4579 if( pParse->hasCompound ){ |
| 4504 w.xSelectCallback = convertCompoundSelectToSubquery; | 4580 w.xSelectCallback = convertCompoundSelectToSubquery; |
| 4505 sqlite3WalkSelect(&w, pSelect); | 4581 sqlite3WalkSelect(&w, pSelect); |
| 4506 } | 4582 } |
| 4507 w.xSelectCallback = selectExpander; | 4583 w.xSelectCallback = selectExpander; |
| 4508 if( (pSelect->selFlags & SF_MultiValue)==0 ){ | 4584 w.xSelectCallback2 = selectPopWith; |
| 4509 w.xSelectCallback2 = selectPopWith; | |
| 4510 } | |
| 4511 sqlite3WalkSelect(&w, pSelect); | 4585 sqlite3WalkSelect(&w, pSelect); |
| 4512 } | 4586 } |
| 4513 | 4587 |
| 4514 | 4588 |
| 4515 #ifndef SQLITE_OMIT_SUBQUERY | 4589 #ifndef SQLITE_OMIT_SUBQUERY |
| 4516 /* | 4590 /* |
| 4517 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() | 4591 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() |
| 4518 ** interface. | 4592 ** interface. |
| 4519 ** | 4593 ** |
| 4520 ** For each FROM-clause subquery, add Column.zType and Column.zColl | 4594 ** For each FROM-clause subquery, add Column.zType and Column.zColl |
| (...skipping 17 matching lines...) Expand all Loading... |
| 4538 pParse = pWalker->pParse; | 4612 pParse = pWalker->pParse; |
| 4539 pTabList = p->pSrc; | 4613 pTabList = p->pSrc; |
| 4540 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ | 4614 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ |
| 4541 Table *pTab = pFrom->pTab; | 4615 Table *pTab = pFrom->pTab; |
| 4542 assert( pTab!=0 ); | 4616 assert( pTab!=0 ); |
| 4543 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){ | 4617 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){ |
| 4544 /* A sub-query in the FROM clause of a SELECT */ | 4618 /* A sub-query in the FROM clause of a SELECT */ |
| 4545 Select *pSel = pFrom->pSelect; | 4619 Select *pSel = pFrom->pSelect; |
| 4546 if( pSel ){ | 4620 if( pSel ){ |
| 4547 while( pSel->pPrior ) pSel = pSel->pPrior; | 4621 while( pSel->pPrior ) pSel = pSel->pPrior; |
| 4548 selectAddColumnTypeAndCollation(pParse, pTab, pSel); | 4622 sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel); |
| 4549 } | 4623 } |
| 4550 } | 4624 } |
| 4551 } | 4625 } |
| 4552 } | 4626 } |
| 4553 #endif | 4627 #endif |
| 4554 | 4628 |
| 4555 | 4629 |
| 4556 /* | 4630 /* |
| 4557 ** This routine adds datatype and collating sequence information to | 4631 ** This routine adds datatype and collating sequence information to |
| 4558 ** the Table structures of all FROM-clause subqueries in a | 4632 ** the Table structures of all FROM-clause subqueries in a |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4650 ** Invoke the OP_AggFinalize opcode for every aggregate function | 4724 ** Invoke the OP_AggFinalize opcode for every aggregate function |
| 4651 ** in the AggInfo structure. | 4725 ** in the AggInfo structure. |
| 4652 */ | 4726 */ |
| 4653 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ | 4727 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ |
| 4654 Vdbe *v = pParse->pVdbe; | 4728 Vdbe *v = pParse->pVdbe; |
| 4655 int i; | 4729 int i; |
| 4656 struct AggInfo_func *pF; | 4730 struct AggInfo_func *pF; |
| 4657 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ | 4731 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ |
| 4658 ExprList *pList = pF->pExpr->x.pList; | 4732 ExprList *pList = pF->pExpr->x.pList; |
| 4659 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); | 4733 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); |
| 4660 sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0, | 4734 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0); |
| 4661 (void*)pF->pFunc, P4_FUNCDEF); | 4735 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
| 4662 } | 4736 } |
| 4663 } | 4737 } |
| 4664 | 4738 |
| 4665 /* | 4739 /* |
| 4666 ** Update the accumulator memory cells for an aggregate based on | 4740 ** Update the accumulator memory cells for an aggregate based on |
| 4667 ** the current cursor position. | 4741 ** the current cursor position. |
| 4668 */ | 4742 */ |
| 4669 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ | 4743 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ |
| 4670 Vdbe *v = pParse->pVdbe; | 4744 Vdbe *v = pParse->pVdbe; |
| 4671 int i; | 4745 int i; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 4702 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ | 4776 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ |
| 4703 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){ | 4777 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){ |
| 4704 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); | 4778 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); |
| 4705 } | 4779 } |
| 4706 if( !pColl ){ | 4780 if( !pColl ){ |
| 4707 pColl = pParse->db->pDfltColl; | 4781 pColl = pParse->db->pDfltColl; |
| 4708 } | 4782 } |
| 4709 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; | 4783 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; |
| 4710 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); | 4784 sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); |
| 4711 } | 4785 } |
| 4712 sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem, | 4786 sqlite3VdbeAddOp3(v, OP_AggStep0, 0, regAgg, pF->iMem); |
| 4713 (void*)pF->pFunc, P4_FUNCDEF); | 4787 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
| 4714 sqlite3VdbeChangeP5(v, (u8)nArg); | 4788 sqlite3VdbeChangeP5(v, (u8)nArg); |
| 4715 sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg); | 4789 sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg); |
| 4716 sqlite3ReleaseTempRange(pParse, regAgg, nArg); | 4790 sqlite3ReleaseTempRange(pParse, regAgg, nArg); |
| 4717 if( addrNext ){ | 4791 if( addrNext ){ |
| 4718 sqlite3VdbeResolveLabel(v, addrNext); | 4792 sqlite3VdbeResolveLabel(v, addrNext); |
| 4719 sqlite3ExprCacheClear(pParse); | 4793 sqlite3ExprCacheClear(pParse); |
| 4720 } | 4794 } |
| 4721 } | 4795 } |
| 4722 | 4796 |
| 4723 /* Before populating the accumulator registers, clear the column cache. | 4797 /* Before populating the accumulator registers, clear the column cache. |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4847 } | 4921 } |
| 4848 assert( p->pEList!=0 ); | 4922 assert( p->pEList!=0 ); |
| 4849 isAgg = (p->selFlags & SF_Aggregate)!=0; | 4923 isAgg = (p->selFlags & SF_Aggregate)!=0; |
| 4850 #if SELECTTRACE_ENABLED | 4924 #if SELECTTRACE_ENABLED |
| 4851 if( sqlite3SelectTrace & 0x100 ){ | 4925 if( sqlite3SelectTrace & 0x100 ){ |
| 4852 SELECTTRACE(0x100,pParse,p, ("after name resolution:\n")); | 4926 SELECTTRACE(0x100,pParse,p, ("after name resolution:\n")); |
| 4853 sqlite3TreeViewSelect(0, p, 0); | 4927 sqlite3TreeViewSelect(0, p, 0); |
| 4854 } | 4928 } |
| 4855 #endif | 4929 #endif |
| 4856 | 4930 |
| 4857 | |
| 4858 /* If writing to memory or generating a set | |
| 4859 ** only a single column may be output. | |
| 4860 */ | |
| 4861 #ifndef SQLITE_OMIT_SUBQUERY | |
| 4862 if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){ | |
| 4863 goto select_end; | |
| 4864 } | |
| 4865 #endif | |
| 4866 | |
| 4867 /* Try to flatten subqueries in the FROM clause up into the main query | 4931 /* Try to flatten subqueries in the FROM clause up into the main query |
| 4868 */ | 4932 */ |
| 4869 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) | 4933 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 4870 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ | 4934 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
| 4871 struct SrcList_item *pItem = &pTabList->a[i]; | 4935 struct SrcList_item *pItem = &pTabList->a[i]; |
| 4872 Select *pSub = pItem->pSelect; | 4936 Select *pSub = pItem->pSelect; |
| 4873 int isAggSub; | 4937 int isAggSub; |
| 4874 Table *pTab = pItem->pTab; | 4938 Table *pTab = pItem->pTab; |
| 4875 if( pSub==0 ) continue; | 4939 if( pSub==0 ) continue; |
| 4876 | 4940 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4947 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit | 5011 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit |
| 4948 ** more conservative than necessary, but much easier than enforcing | 5012 ** more conservative than necessary, but much easier than enforcing |
| 4949 ** an exact limit. | 5013 ** an exact limit. |
| 4950 */ | 5014 */ |
| 4951 pParse->nHeight += sqlite3SelectExprHeight(p); | 5015 pParse->nHeight += sqlite3SelectExprHeight(p); |
| 4952 | 5016 |
| 4953 /* Make copies of constant WHERE-clause terms in the outer query down | 5017 /* Make copies of constant WHERE-clause terms in the outer query down |
| 4954 ** inside the subquery. This can help the subquery to run more efficiently. | 5018 ** inside the subquery. This can help the subquery to run more efficiently. |
| 4955 */ | 5019 */ |
| 4956 if( (pItem->fg.jointype & JT_OUTER)==0 | 5020 if( (pItem->fg.jointype & JT_OUTER)==0 |
| 4957 && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor) | 5021 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor) |
| 4958 ){ | 5022 ){ |
| 4959 #if SELECTTRACE_ENABLED | 5023 #if SELECTTRACE_ENABLED |
| 4960 if( sqlite3SelectTrace & 0x100 ){ | 5024 if( sqlite3SelectTrace & 0x100 ){ |
| 4961 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n")); | 5025 SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n")); |
| 4962 sqlite3TreeViewSelect(0, p, 0); | 5026 sqlite3TreeViewSelect(0, p, 0); |
| 4963 } | 5027 } |
| 4964 #endif | 5028 #endif |
| 4965 } | 5029 } |
| 4966 | 5030 |
| 4967 /* Generate code to implement the subquery | 5031 /* Generate code to implement the subquery |
| 5032 ** |
| 5033 ** The subquery is implemented as a co-routine if all of these are true: |
| 5034 ** (1) The subquery is guaranteed to be the outer loop (so that it |
| 5035 ** does not need to be computed more than once) |
| 5036 ** (2) The ALL keyword after SELECT is omitted. (Applications are |
| 5037 ** allowed to say "SELECT ALL" instead of just "SELECT" to disable |
| 5038 ** the use of co-routines.) |
| 5039 ** (3) Co-routines are not disabled using sqlite3_test_control() |
| 5040 ** with SQLITE_TESTCTRL_OPTIMIZATIONS. |
| 5041 ** |
| 5042 ** TODO: Are there other reasons beside (1) to use a co-routine |
| 5043 ** implementation? |
| 4968 */ | 5044 */ |
| 4969 if( pTabList->nSrc==1 | 5045 if( i==0 |
| 4970 && (p->selFlags & SF_All)==0 | 5046 && (pTabList->nSrc==1 |
| 4971 && OptimizationEnabled(db, SQLITE_SubqCoroutine) | 5047 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */ |
| 5048 && (p->selFlags & SF_All)==0 /* (2) */ |
| 5049 && OptimizationEnabled(db, SQLITE_SubqCoroutine) /* (3) */ |
| 4972 ){ | 5050 ){ |
| 4973 /* Implement a co-routine that will return a single row of the result | 5051 /* Implement a co-routine that will return a single row of the result |
| 4974 ** set on each invocation. | 5052 ** set on each invocation. |
| 4975 */ | 5053 */ |
| 4976 int addrTop = sqlite3VdbeCurrentAddr(v)+1; | 5054 int addrTop = sqlite3VdbeCurrentAddr(v)+1; |
| 4977 pItem->regReturn = ++pParse->nMem; | 5055 pItem->regReturn = ++pParse->nMem; |
| 4978 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop); | 5056 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop); |
| 4979 VdbeComment((v, "%s", pItem->pTab->zName)); | 5057 VdbeComment((v, "%s", pItem->pTab->zName)); |
| 4980 pItem->addrFillSub = addrTop; | 5058 pItem->addrFillSub = addrTop; |
| 4981 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn); | 5059 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn); |
| 4982 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); | 5060 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); |
| 4983 sqlite3Select(pParse, pSub, &dest); | 5061 sqlite3Select(pParse, pSub, &dest); |
| 4984 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow); | 5062 pItem->pTab->nRowLogEst = pSub->nSelectRow; |
| 4985 pItem->fg.viaCoroutine = 1; | 5063 pItem->fg.viaCoroutine = 1; |
| 4986 pItem->regResult = dest.iSdst; | 5064 pItem->regResult = dest.iSdst; |
| 4987 sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn); | 5065 sqlite3VdbeEndCoroutine(v, pItem->regReturn); |
| 4988 sqlite3VdbeJumpHere(v, addrTop-1); | 5066 sqlite3VdbeJumpHere(v, addrTop-1); |
| 4989 sqlite3ClearTempRegCache(pParse); | 5067 sqlite3ClearTempRegCache(pParse); |
| 4990 }else{ | 5068 }else{ |
| 4991 /* Generate a subroutine that will fill an ephemeral table with | 5069 /* Generate a subroutine that will fill an ephemeral table with |
| 4992 ** the content of this subquery. pItem->addrFillSub will point | 5070 ** the content of this subquery. pItem->addrFillSub will point |
| 4993 ** to the address of the generated subroutine. pItem->regReturn | 5071 ** to the address of the generated subroutine. pItem->regReturn |
| 4994 ** is a register allocated to hold the subroutine return address | 5072 ** is a register allocated to hold the subroutine return address |
| 4995 */ | 5073 */ |
| 4996 int topAddr; | 5074 int topAddr; |
| 4997 int onceAddr = 0; | 5075 int onceAddr = 0; |
| 4998 int retAddr; | 5076 int retAddr; |
| 4999 assert( pItem->addrFillSub==0 ); | 5077 assert( pItem->addrFillSub==0 ); |
| 5000 pItem->regReturn = ++pParse->nMem; | 5078 pItem->regReturn = ++pParse->nMem; |
| 5001 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn); | 5079 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn); |
| 5002 pItem->addrFillSub = topAddr+1; | 5080 pItem->addrFillSub = topAddr+1; |
| 5003 if( pItem->fg.isCorrelated==0 ){ | 5081 if( pItem->fg.isCorrelated==0 ){ |
| 5004 /* If the subquery is not correlated and if we are not inside of | 5082 /* If the subquery is not correlated and if we are not inside of |
| 5005 ** a trigger, then we only need to compute the value of the subquery | 5083 ** a trigger, then we only need to compute the value of the subquery |
| 5006 ** once. */ | 5084 ** once. */ |
| 5007 onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); | 5085 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 5008 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName)); | 5086 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName)); |
| 5009 }else{ | 5087 }else{ |
| 5010 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName)); | 5088 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName)); |
| 5011 } | 5089 } |
| 5012 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); | 5090 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); |
| 5013 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); | 5091 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); |
| 5014 sqlite3Select(pParse, pSub, &dest); | 5092 sqlite3Select(pParse, pSub, &dest); |
| 5015 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow); | 5093 pItem->pTab->nRowLogEst = pSub->nSelectRow; |
| 5016 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); | 5094 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); |
| 5017 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); | 5095 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); |
| 5018 VdbeComment((v, "end %s", pItem->pTab->zName)); | 5096 VdbeComment((v, "end %s", pItem->pTab->zName)); |
| 5019 sqlite3VdbeChangeP1(v, topAddr, retAddr); | 5097 sqlite3VdbeChangeP1(v, topAddr, retAddr); |
| 5020 sqlite3ClearTempRegCache(pParse); | 5098 sqlite3ClearTempRegCache(pParse); |
| 5021 } | 5099 } |
| 5022 if( db->mallocFailed ) goto select_end; | 5100 if( db->mallocFailed ) goto select_end; |
| 5023 pParse->nHeight -= sqlite3SelectExprHeight(p); | 5101 pParse->nHeight -= sqlite3SelectExprHeight(p); |
| 5024 } | 5102 } |
| 5025 #endif | 5103 #endif |
| (...skipping 30 matching lines...) Expand all Loading... |
| 5056 */ | 5134 */ |
| 5057 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct | 5135 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct |
| 5058 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 | 5136 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 |
| 5059 ){ | 5137 ){ |
| 5060 p->selFlags &= ~SF_Distinct; | 5138 p->selFlags &= ~SF_Distinct; |
| 5061 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0); | 5139 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0); |
| 5062 /* Notice that even thought SF_Distinct has been cleared from p->selFlags, | 5140 /* Notice that even thought SF_Distinct has been cleared from p->selFlags, |
| 5063 ** the sDistinct.isTnct is still set. Hence, isTnct represents the | 5141 ** the sDistinct.isTnct is still set. Hence, isTnct represents the |
| 5064 ** original setting of the SF_Distinct flag, not the current setting */ | 5142 ** original setting of the SF_Distinct flag, not the current setting */ |
| 5065 assert( sDistinct.isTnct ); | 5143 assert( sDistinct.isTnct ); |
| 5144 |
| 5145 #if SELECTTRACE_ENABLED |
| 5146 if( sqlite3SelectTrace & 0x400 ){ |
| 5147 SELECTTRACE(0x400,pParse,p,("Transform DISTINCT into GROUP BY:\n")); |
| 5148 sqlite3TreeViewSelect(0, p, 0); |
| 5149 } |
| 5150 #endif |
| 5066 } | 5151 } |
| 5067 | 5152 |
| 5068 /* If there is an ORDER BY clause, then create an ephemeral index to | 5153 /* If there is an ORDER BY clause, then create an ephemeral index to |
| 5069 ** do the sorting. But this sorting ephemeral index might end up | 5154 ** do the sorting. But this sorting ephemeral index might end up |
| 5070 ** being unused if the data can be extracted in pre-sorted order. | 5155 ** being unused if the data can be extracted in pre-sorted order. |
| 5071 ** If that is the case, then the OP_OpenEphemeral instruction will be | 5156 ** If that is the case, then the OP_OpenEphemeral instruction will be |
| 5072 ** changed to an OP_Noop once we figure out that the sorting index is | 5157 ** changed to an OP_Noop once we figure out that the sorting index is |
| 5073 ** not needed. The sSort.addrSortIndex variable is used to facilitate | 5158 ** not needed. The sSort.addrSortIndex variable is used to facilitate |
| 5074 ** that change. | 5159 ** that change. |
| 5075 */ | 5160 */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 5088 | 5173 |
| 5089 /* If the output is destined for a temporary table, open that table. | 5174 /* If the output is destined for a temporary table, open that table. |
| 5090 */ | 5175 */ |
| 5091 if( pDest->eDest==SRT_EphemTab ){ | 5176 if( pDest->eDest==SRT_EphemTab ){ |
| 5092 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr); | 5177 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr); |
| 5093 } | 5178 } |
| 5094 | 5179 |
| 5095 /* Set the limiter. | 5180 /* Set the limiter. |
| 5096 */ | 5181 */ |
| 5097 iEnd = sqlite3VdbeMakeLabel(v); | 5182 iEnd = sqlite3VdbeMakeLabel(v); |
| 5098 p->nSelectRow = LARGEST_INT64; | 5183 if( (p->selFlags & SF_FixedLimit)==0 ){ |
| 5184 p->nSelectRow = 320; /* 4 billion rows */ |
| 5185 } |
| 5099 computeLimitRegisters(pParse, p, iEnd); | 5186 computeLimitRegisters(pParse, p, iEnd); |
| 5100 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ | 5187 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ |
| 5101 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen); | 5188 sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen); |
| 5102 sSort.sortFlags |= SORTFLAG_UseSorter; | 5189 sSort.sortFlags |= SORTFLAG_UseSorter; |
| 5103 } | 5190 } |
| 5104 | 5191 |
| 5105 /* Open an ephemeral index to use for the distinct set. | 5192 /* Open an ephemeral index to use for the distinct set. |
| 5106 */ | 5193 */ |
| 5107 if( p->selFlags & SF_Distinct ){ | 5194 if( p->selFlags & SF_Distinct ){ |
| 5108 sDistinct.tabTnct = pParse->nTab++; | 5195 sDistinct.tabTnct = pParse->nTab++; |
| 5109 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, | 5196 sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
| 5110 sDistinct.tabTnct, 0, 0, | 5197 sDistinct.tabTnct, 0, 0, |
| 5111 (char*)keyInfoFromExprList(pParse, p->pEList,0,0), | 5198 (char*)keyInfoFromExprList(pParse, p->pEList,0,0), |
| 5112 P4_KEYINFO); | 5199 P4_KEYINFO); |
| 5113 sqlite3VdbeChangeP5(v, BTREE_UNORDERED); | 5200 sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
| 5114 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED; | 5201 sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED; |
| 5115 }else{ | 5202 }else{ |
| 5116 sDistinct.eTnctType = WHERE_DISTINCT_NOOP; | 5203 sDistinct.eTnctType = WHERE_DISTINCT_NOOP; |
| 5117 } | 5204 } |
| 5118 | 5205 |
| 5119 if( !isAgg && pGroupBy==0 ){ | 5206 if( !isAgg && pGroupBy==0 ){ |
| 5120 /* No aggregate functions and no GROUP BY clause */ | 5207 /* No aggregate functions and no GROUP BY clause */ |
| 5121 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0); | 5208 u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0); |
| 5209 assert( WHERE_USE_LIMIT==SF_FixedLimit ); |
| 5210 wctrlFlags |= p->selFlags & SF_FixedLimit; |
| 5122 | 5211 |
| 5123 /* Begin the database scan. */ | 5212 /* Begin the database scan. */ |
| 5124 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, | 5213 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, |
| 5125 p->pEList, wctrlFlags, 0); | 5214 p->pEList, wctrlFlags, p->nSelectRow); |
| 5126 if( pWInfo==0 ) goto select_end; | 5215 if( pWInfo==0 ) goto select_end; |
| 5127 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ | 5216 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ |
| 5128 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); | 5217 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); |
| 5129 } | 5218 } |
| 5130 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ | 5219 if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ |
| 5131 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo); | 5220 sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo); |
| 5132 } | 5221 } |
| 5133 if( sSort.pOrderBy ){ | 5222 if( sSort.pOrderBy ){ |
| 5134 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo); | 5223 sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo); |
| 5224 sSort.bOrderedInnerLoop = sqlite3WhereOrderedInnerLoop(pWInfo); |
| 5135 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){ | 5225 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){ |
| 5136 sSort.pOrderBy = 0; | 5226 sSort.pOrderBy = 0; |
| 5137 } | 5227 } |
| 5138 } | 5228 } |
| 5139 | 5229 |
| 5140 /* If sorting index that was created by a prior OP_OpenEphemeral | 5230 /* If sorting index that was created by a prior OP_OpenEphemeral |
| 5141 ** instruction ended up not being needed, then change the OP_OpenEphemeral | 5231 ** instruction ended up not being needed, then change the OP_OpenEphemeral |
| 5142 ** into an OP_Noop. | 5232 ** into an OP_Noop. |
| 5143 */ | 5233 */ |
| 5144 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){ | 5234 if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){ |
| (...skipping 30 matching lines...) Expand all Loading... |
| 5175 if( pGroupBy ){ | 5265 if( pGroupBy ){ |
| 5176 int k; /* Loop counter */ | 5266 int k; /* Loop counter */ |
| 5177 struct ExprList_item *pItem; /* For looping over expression in a list */ | 5267 struct ExprList_item *pItem; /* For looping over expression in a list */ |
| 5178 | 5268 |
| 5179 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ | 5269 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ |
| 5180 pItem->u.x.iAlias = 0; | 5270 pItem->u.x.iAlias = 0; |
| 5181 } | 5271 } |
| 5182 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ | 5272 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
| 5183 pItem->u.x.iAlias = 0; | 5273 pItem->u.x.iAlias = 0; |
| 5184 } | 5274 } |
| 5185 if( p->nSelectRow>100 ) p->nSelectRow = 100; | 5275 assert( 66==sqlite3LogEst(100) ); |
| 5276 if( p->nSelectRow>66 ) p->nSelectRow = 66; |
| 5186 }else{ | 5277 }else{ |
| 5187 p->nSelectRow = 1; | 5278 assert( 0==sqlite3LogEst(1) ); |
| 5279 p->nSelectRow = 0; |
| 5188 } | 5280 } |
| 5189 | 5281 |
| 5190 /* If there is both a GROUP BY and an ORDER BY clause and they are | 5282 /* If there is both a GROUP BY and an ORDER BY clause and they are |
| 5191 ** identical, then it may be possible to disable the ORDER BY clause | 5283 ** identical, then it may be possible to disable the ORDER BY clause |
| 5192 ** on the grounds that the GROUP BY will cause elements to come out | 5284 ** on the grounds that the GROUP BY will cause elements to come out |
| 5193 ** in the correct order. It also may not - the GROUP BY might use a | 5285 ** in the correct order. It also may not - the GROUP BY might use a |
| 5194 ** database index that causes rows to be grouped together as required | 5286 ** database index that causes rows to be grouped together as required |
| 5195 ** but not actually sorted. Either way, record the fact that the | 5287 ** but not actually sorted. Either way, record the fact that the |
| 5196 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp | 5288 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp |
| 5197 ** variable. */ | 5289 ** variable. */ |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5549 assert( p->pGroupBy==0 ); | 5641 assert( p->pGroupBy==0 ); |
| 5550 assert( flag==0 ); | 5642 assert( flag==0 ); |
| 5551 if( p->pHaving==0 ){ | 5643 if( p->pHaving==0 ){ |
| 5552 flag = minMaxQuery(&sAggInfo, &pMinMax); | 5644 flag = minMaxQuery(&sAggInfo, &pMinMax); |
| 5553 } | 5645 } |
| 5554 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) ); | 5646 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) ); |
| 5555 | 5647 |
| 5556 if( flag ){ | 5648 if( flag ){ |
| 5557 pMinMax = sqlite3ExprListDup(db, pMinMax, 0); | 5649 pMinMax = sqlite3ExprListDup(db, pMinMax, 0); |
| 5558 pDel = pMinMax; | 5650 pDel = pMinMax; |
| 5559 if( pMinMax && !db->mallocFailed ){ | 5651 assert( db->mallocFailed || pMinMax!=0 ); |
| 5652 if( !db->mallocFailed ){ |
| 5560 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0; | 5653 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0; |
| 5561 pMinMax->a[0].pExpr->op = TK_COLUMN; | 5654 pMinMax->a[0].pExpr->op = TK_COLUMN; |
| 5562 } | 5655 } |
| 5563 } | 5656 } |
| 5564 | 5657 |
| 5565 /* This case runs if the aggregate has no GROUP BY clause. The | 5658 /* This case runs if the aggregate has no GROUP BY clause. The |
| 5566 ** processing is much simpler since there is only a single row | 5659 ** processing is much simpler since there is only a single row |
| 5567 ** of output. | 5660 ** of output. |
| 5568 */ | 5661 */ |
| 5569 resetAccumulator(pParse, &sAggInfo); | 5662 resetAccumulator(pParse, &sAggInfo); |
| 5570 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0); | 5663 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0); |
| 5571 if( pWInfo==0 ){ | 5664 if( pWInfo==0 ){ |
| 5572 sqlite3ExprListDelete(db, pDel); | 5665 sqlite3ExprListDelete(db, pDel); |
| 5573 goto select_end; | 5666 goto select_end; |
| 5574 } | 5667 } |
| 5575 updateAccumulator(pParse, &sAggInfo); | 5668 updateAccumulator(pParse, &sAggInfo); |
| 5576 assert( pMinMax==0 || pMinMax->nExpr==1 ); | 5669 assert( pMinMax==0 || pMinMax->nExpr==1 ); |
| 5577 if( sqlite3WhereIsOrdered(pWInfo)>0 ){ | 5670 if( sqlite3WhereIsOrdered(pWInfo)>0 ){ |
| 5578 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo)); | 5671 sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo)); |
| 5579 VdbeComment((v, "%s() by index", | 5672 VdbeComment((v, "%s() by index", |
| 5580 (flag==WHERE_ORDERBY_MIN?"min":"max"))); | 5673 (flag==WHERE_ORDERBY_MIN?"min":"max"))); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5627 } | 5720 } |
| 5628 | 5721 |
| 5629 sqlite3DbFree(db, sAggInfo.aCol); | 5722 sqlite3DbFree(db, sAggInfo.aCol); |
| 5630 sqlite3DbFree(db, sAggInfo.aFunc); | 5723 sqlite3DbFree(db, sAggInfo.aFunc); |
| 5631 #if SELECTTRACE_ENABLED | 5724 #if SELECTTRACE_ENABLED |
| 5632 SELECTTRACE(1,pParse,p,("end processing\n")); | 5725 SELECTTRACE(1,pParse,p,("end processing\n")); |
| 5633 pParse->nSelectIndent--; | 5726 pParse->nSelectIndent--; |
| 5634 #endif | 5727 #endif |
| 5635 return rc; | 5728 return rc; |
| 5636 } | 5729 } |
| OLD | NEW |