| 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 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 /* Test variable that can be set to enable WHERE tracing */ | 25 /* Test variable that can be set to enable WHERE tracing */ |
| 26 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) | 26 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 27 /***/ int sqlite3WhereTrace = 0; | 27 /***/ int sqlite3WhereTrace = 0; |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 | 30 |
| 31 /* | 31 /* |
| 32 ** Return the estimated number of output rows from a WHERE clause | 32 ** Return the estimated number of output rows from a WHERE clause |
| 33 */ | 33 */ |
| 34 u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ | 34 LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 35 return sqlite3LogEstToInt(pWInfo->nRowOut); | 35 return pWInfo->nRowOut; |
| 36 } | 36 } |
| 37 | 37 |
| 38 /* | 38 /* |
| 39 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this | 39 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 40 ** WHERE clause returns outputs for DISTINCT processing. | 40 ** WHERE clause returns outputs for DISTINCT processing. |
| 41 */ | 41 */ |
| 42 int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ | 42 int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 43 return pWInfo->eDistinct; | 43 return pWInfo->eDistinct; |
| 44 } | 44 } |
| 45 | 45 |
| 46 /* | 46 /* |
| 47 ** Return TRUE if the WHERE clause returns rows in ORDER BY order. | 47 ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 48 ** Return FALSE if the output needs to be sorted. | 48 ** Return FALSE if the output needs to be sorted. |
| 49 */ | 49 */ |
| 50 int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ | 50 int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 51 return pWInfo->nOBSat; | 51 return pWInfo->nOBSat; |
| 52 } | 52 } |
| 53 | 53 |
| 54 /* | 54 /* |
| 55 ** Return TRUE if the innermost loop of the WHERE clause implementation |
| 56 ** returns rows in ORDER BY order for complete run of the inner loop. |
| 57 ** |
| 58 ** Across multiple iterations of outer loops, the output rows need not be |
| 59 ** sorted. As long as rows are sorted for just the innermost loop, this |
| 60 ** routine can return TRUE. |
| 61 */ |
| 62 int sqlite3WhereOrderedInnerLoop(WhereInfo *pWInfo){ |
| 63 return pWInfo->bOrderedInnerLoop; |
| 64 } |
| 65 |
| 66 /* |
| 55 ** Return the VDBE address or label to jump to in order to continue | 67 ** Return the VDBE address or label to jump to in order to continue |
| 56 ** immediately with the next row of a WHERE clause. | 68 ** immediately with the next row of a WHERE clause. |
| 57 */ | 69 */ |
| 58 int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ | 70 int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 59 assert( pWInfo->iContinue!=0 ); | 71 assert( pWInfo->iContinue!=0 ); |
| 60 return pWInfo->iContinue; | 72 return pWInfo->iContinue; |
| 61 } | 73 } |
| 62 | 74 |
| 63 /* | 75 /* |
| 64 ** Return the VDBE address or label to jump to in order to break | 76 ** Return the VDBE address or label to jump to in order to break |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 ** Return NULL if there are no more matching WhereTerms. | 191 ** Return NULL if there are no more matching WhereTerms. |
| 180 */ | 192 */ |
| 181 static WhereTerm *whereScanNext(WhereScan *pScan){ | 193 static WhereTerm *whereScanNext(WhereScan *pScan){ |
| 182 int iCur; /* The cursor on the LHS of the term */ | 194 int iCur; /* The cursor on the LHS of the term */ |
| 183 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */ | 195 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 184 Expr *pX; /* An expression being tested */ | 196 Expr *pX; /* An expression being tested */ |
| 185 WhereClause *pWC; /* Shorthand for pScan->pWC */ | 197 WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 186 WhereTerm *pTerm; /* The term being tested */ | 198 WhereTerm *pTerm; /* The term being tested */ |
| 187 int k = pScan->k; /* Where to start scanning */ | 199 int k = pScan->k; /* Where to start scanning */ |
| 188 | 200 |
| 189 while( pScan->iEquiv<=pScan->nEquiv ){ | 201 assert( pScan->iEquiv<=pScan->nEquiv ); |
| 202 pWC = pScan->pWC; |
| 203 while(1){ |
| 204 iColumn = pScan->aiColumn[pScan->iEquiv-1]; |
| 190 iCur = pScan->aiCur[pScan->iEquiv-1]; | 205 iCur = pScan->aiCur[pScan->iEquiv-1]; |
| 191 iColumn = pScan->aiColumn[pScan->iEquiv-1]; | 206 assert( pWC!=0 ); |
| 192 if( iColumn==XN_EXPR && pScan->pIdxExpr==0 ) return 0; | 207 do{ |
| 193 while( (pWC = pScan->pWC)!=0 ){ | |
| 194 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ | 208 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
| 195 if( pTerm->leftCursor==iCur | 209 if( pTerm->leftCursor==iCur |
| 196 && pTerm->u.leftColumn==iColumn | 210 && pTerm->u.leftColumn==iColumn |
| 197 && (iColumn!=XN_EXPR | 211 && (iColumn!=XN_EXPR |
| 198 || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0) | 212 || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0) |
| 199 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) | 213 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 200 ){ | 214 ){ |
| 201 if( (pTerm->eOperator & WO_EQUIV)!=0 | 215 if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 202 && pScan->nEquiv<ArraySize(pScan->aiCur) | 216 && pScan->nEquiv<ArraySize(pScan->aiCur) |
| 203 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN | 217 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN |
| (...skipping 29 matching lines...) Expand all Loading... |
| 233 } | 247 } |
| 234 } | 248 } |
| 235 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 | 249 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
| 236 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN | 250 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 237 && pX->iTable==pScan->aiCur[0] | 251 && pX->iTable==pScan->aiCur[0] |
| 238 && pX->iColumn==pScan->aiColumn[0] | 252 && pX->iColumn==pScan->aiColumn[0] |
| 239 ){ | 253 ){ |
| 240 testcase( pTerm->eOperator & WO_IS ); | 254 testcase( pTerm->eOperator & WO_IS ); |
| 241 continue; | 255 continue; |
| 242 } | 256 } |
| 257 pScan->pWC = pWC; |
| 243 pScan->k = k+1; | 258 pScan->k = k+1; |
| 244 return pTerm; | 259 return pTerm; |
| 245 } | 260 } |
| 246 } | 261 } |
| 247 } | 262 } |
| 248 pScan->pWC = pScan->pWC->pOuter; | 263 pWC = pWC->pOuter; |
| 249 k = 0; | 264 k = 0; |
| 250 } | 265 }while( pWC!=0 ); |
| 251 pScan->pWC = pScan->pOrigWC; | 266 if( pScan->iEquiv>=pScan->nEquiv ) break; |
| 267 pWC = pScan->pOrigWC; |
| 252 k = 0; | 268 k = 0; |
| 253 pScan->iEquiv++; | 269 pScan->iEquiv++; |
| 254 } | 270 } |
| 255 return 0; | 271 return 0; |
| 256 } | 272 } |
| 257 | 273 |
| 258 /* | 274 /* |
| 259 ** Initialize a WHERE clause scanner object. Return a pointer to the | 275 ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 260 ** first match. Return NULL if there are no matches. | 276 ** first match. Return NULL if there are no matches. |
| 261 ** | 277 ** |
| 262 ** The scanner will be searching the WHERE clause pWC. It will look | 278 ** The scanner will be searching the WHERE clause pWC. It will look |
| 263 ** for terms of the form "X <op> <expr>" where X is column iColumn of table | 279 ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 264 ** iCur. The <op> must be one of the operators described by opMask. | 280 ** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx |
| 281 ** must be one of the indexes of table iCur. |
| 282 ** |
| 283 ** The <op> must be one of the operators described by opMask. |
| 265 ** | 284 ** |
| 266 ** If the search is for X and the WHERE clause contains terms of the | 285 ** If the search is for X and the WHERE clause contains terms of the |
| 267 ** form X=Y then this routine might also return terms of the form | 286 ** form X=Y then this routine might also return terms of the form |
| 268 ** "Y <op> <expr>". The number of levels of transitivity is limited, | 287 ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 269 ** but is enough to handle most commonly occurring SQL statements. | 288 ** but is enough to handle most commonly occurring SQL statements. |
| 270 ** | 289 ** |
| 271 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with | 290 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 272 ** index pIdx. | 291 ** index pIdx. |
| 273 */ | 292 */ |
| 274 static WhereTerm *whereScanInit( | 293 static WhereTerm *whereScanInit( |
| 275 WhereScan *pScan, /* The WhereScan object being initialized */ | 294 WhereScan *pScan, /* The WhereScan object being initialized */ |
| 276 WhereClause *pWC, /* The WHERE clause to be scanned */ | 295 WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 277 int iCur, /* Cursor to scan for */ | 296 int iCur, /* Cursor to scan for */ |
| 278 int iColumn, /* Column to scan for */ | 297 int iColumn, /* Column to scan for */ |
| 279 u32 opMask, /* Operator(s) to scan for */ | 298 u32 opMask, /* Operator(s) to scan for */ |
| 280 Index *pIdx /* Must be compatible with this index */ | 299 Index *pIdx /* Must be compatible with this index */ |
| 281 ){ | 300 ){ |
| 282 int j = 0; | |
| 283 | |
| 284 /* memset(pScan, 0, sizeof(*pScan)); */ | |
| 285 pScan->pOrigWC = pWC; | 301 pScan->pOrigWC = pWC; |
| 286 pScan->pWC = pWC; | 302 pScan->pWC = pWC; |
| 287 pScan->pIdxExpr = 0; | 303 pScan->pIdxExpr = 0; |
| 304 pScan->idxaff = 0; |
| 305 pScan->zCollName = 0; |
| 288 if( pIdx ){ | 306 if( pIdx ){ |
| 289 j = iColumn; | 307 int j = iColumn; |
| 290 iColumn = pIdx->aiColumn[j]; | 308 iColumn = pIdx->aiColumn[j]; |
| 291 if( iColumn==XN_EXPR ) pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; | 309 if( iColumn==XN_EXPR ){ |
| 292 } | 310 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; |
| 293 if( pIdx && iColumn>=0 ){ | 311 pScan->zCollName = pIdx->azColl[j]; |
| 294 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; | 312 }else if( iColumn==pIdx->pTable->iPKey ){ |
| 295 pScan->zCollName = pIdx->azColl[j]; | 313 iColumn = XN_ROWID; |
| 296 }else{ | 314 }else if( iColumn>=0 ){ |
| 297 pScan->idxaff = 0; | 315 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 298 pScan->zCollName = 0; | 316 pScan->zCollName = pIdx->azColl[j]; |
| 317 } |
| 318 }else if( iColumn==XN_EXPR ){ |
| 319 return 0; |
| 299 } | 320 } |
| 300 pScan->opMask = opMask; | 321 pScan->opMask = opMask; |
| 301 pScan->k = 0; | 322 pScan->k = 0; |
| 302 pScan->aiCur[0] = iCur; | 323 pScan->aiCur[0] = iCur; |
| 303 pScan->aiColumn[0] = iColumn; | 324 pScan->aiColumn[0] = iColumn; |
| 304 pScan->nEquiv = 1; | 325 pScan->nEquiv = 1; |
| 305 pScan->iEquiv = 1; | 326 pScan->iEquiv = 1; |
| 306 return whereScanNext(pScan); | 327 return whereScanNext(pScan); |
| 307 } | 328 } |
| 308 | 329 |
| 309 /* | 330 /* |
| 310 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" | 331 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 311 ** where X is a reference to the iColumn of table iCur and <op> is one of | 332 ** where X is a reference to the iColumn of table iCur or of index pIdx |
| 312 ** the WO_xx operator codes specified by the op parameter. | 333 ** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by |
| 313 ** Return a pointer to the term. Return 0 if not found. | 334 ** the op parameter. Return a pointer to the term. Return 0 if not found. |
| 314 ** | 335 ** |
| 315 ** If pIdx!=0 then search for terms matching the iColumn-th column of pIdx | 336 ** If pIdx!=0 then it must be one of the indexes of table iCur. |
| 337 ** Search for terms matching the iColumn-th column of pIdx |
| 316 ** rather than the iColumn-th column of table iCur. | 338 ** rather than the iColumn-th column of table iCur. |
| 317 ** | 339 ** |
| 318 ** The term returned might by Y=<expr> if there is another constraint in | 340 ** The term returned might by Y=<expr> if there is another constraint in |
| 319 ** the WHERE clause that specifies that X=Y. Any such constraints will be | 341 ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 320 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The | 342 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 321 ** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11 | 343 ** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11 |
| 322 ** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10 | 344 ** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10 |
| 323 ** other equivalent values. Hence a search for X will return <expr> if X=A1 | 345 ** other equivalent values. Hence a search for X will return <expr> if X=A1 |
| 324 ** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>. | 346 ** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>. |
| 325 ** | 347 ** |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 Expr *pPartial = 0; /* Partial Index Expression */ | 649 Expr *pPartial = 0; /* Partial Index Expression */ |
| 628 int iContinue = 0; /* Jump here to skip excluded rows */ | 650 int iContinue = 0; /* Jump here to skip excluded rows */ |
| 629 struct SrcList_item *pTabItem; /* FROM clause term being indexed */ | 651 struct SrcList_item *pTabItem; /* FROM clause term being indexed */ |
| 630 int addrCounter = 0; /* Address where integer counter is initialized */ | 652 int addrCounter = 0; /* Address where integer counter is initialized */ |
| 631 int regBase; /* Array of registers where record is assembled */ | 653 int regBase; /* Array of registers where record is assembled */ |
| 632 | 654 |
| 633 /* Generate code to skip over the creation and initialization of the | 655 /* Generate code to skip over the creation and initialization of the |
| 634 ** transient index on 2nd and subsequent iterations of the loop. */ | 656 ** transient index on 2nd and subsequent iterations of the loop. */ |
| 635 v = pParse->pVdbe; | 657 v = pParse->pVdbe; |
| 636 assert( v!=0 ); | 658 assert( v!=0 ); |
| 637 addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v); | 659 addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 638 | 660 |
| 639 /* Count the number of columns that will be added to the index | 661 /* Count the number of columns that will be added to the index |
| 640 ** and used to match WHERE clause constraints */ | 662 ** and used to match WHERE clause constraints */ |
| 641 nKeyCol = 0; | 663 nKeyCol = 0; |
| 642 pTable = pSrc->pTab; | 664 pTable = pSrc->pTab; |
| 643 pWCEnd = &pWC->a[pWC->nTerm]; | 665 pWCEnd = &pWC->a[pWC->nTerm]; |
| 644 pLoop = pLevel->pWLoop; | 666 pLoop = pLevel->pWLoop; |
| 645 idxCols = 0; | 667 idxCols = 0; |
| 646 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ | 668 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 647 Expr *pExpr = pTerm->pExpr; | 669 Expr *pExpr = pTerm->pExpr; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 /* | 824 /* |
| 803 ** Allocate and populate an sqlite3_index_info structure. It is the | 825 ** Allocate and populate an sqlite3_index_info structure. It is the |
| 804 ** responsibility of the caller to eventually release the structure | 826 ** responsibility of the caller to eventually release the structure |
| 805 ** by passing the pointer returned by this function to sqlite3_free(). | 827 ** by passing the pointer returned by this function to sqlite3_free(). |
| 806 */ | 828 */ |
| 807 static sqlite3_index_info *allocateIndexInfo( | 829 static sqlite3_index_info *allocateIndexInfo( |
| 808 Parse *pParse, | 830 Parse *pParse, |
| 809 WhereClause *pWC, | 831 WhereClause *pWC, |
| 810 Bitmask mUnusable, /* Ignore terms with these prereqs */ | 832 Bitmask mUnusable, /* Ignore terms with these prereqs */ |
| 811 struct SrcList_item *pSrc, | 833 struct SrcList_item *pSrc, |
| 812 ExprList *pOrderBy | 834 ExprList *pOrderBy, |
| 835 u16 *pmNoOmit /* Mask of terms not to omit */ |
| 813 ){ | 836 ){ |
| 814 int i, j; | 837 int i, j; |
| 815 int nTerm; | 838 int nTerm; |
| 816 struct sqlite3_index_constraint *pIdxCons; | 839 struct sqlite3_index_constraint *pIdxCons; |
| 817 struct sqlite3_index_orderby *pIdxOrderBy; | 840 struct sqlite3_index_orderby *pIdxOrderBy; |
| 818 struct sqlite3_index_constraint_usage *pUsage; | 841 struct sqlite3_index_constraint_usage *pUsage; |
| 819 WhereTerm *pTerm; | 842 WhereTerm *pTerm; |
| 820 int nOrderBy; | 843 int nOrderBy; |
| 821 sqlite3_index_info *pIdxInfo; | 844 sqlite3_index_info *pIdxInfo; |
| 845 u16 mNoOmit = 0; |
| 822 | 846 |
| 823 /* Count the number of possible WHERE clause constraints referring | 847 /* Count the number of possible WHERE clause constraints referring |
| 824 ** to this virtual table */ | 848 ** to this virtual table */ |
| 825 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ | 849 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 826 if( pTerm->leftCursor != pSrc->iCursor ) continue; | 850 if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 827 if( pTerm->prereqRight & mUnusable ) continue; | 851 if( pTerm->prereqRight & mUnusable ) continue; |
| 828 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); | 852 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 829 testcase( pTerm->eOperator & WO_IN ); | 853 testcase( pTerm->eOperator & WO_IN ); |
| 830 testcase( pTerm->eOperator & WO_ISNULL ); | 854 testcase( pTerm->eOperator & WO_ISNULL ); |
| 831 testcase( pTerm->eOperator & WO_IS ); | 855 testcase( pTerm->eOperator & WO_IS ); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 /* The direct assignment in the previous line is possible only because | 924 /* The direct assignment in the previous line is possible only because |
| 901 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The | 925 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 902 ** following asserts verify this fact. */ | 926 ** following asserts verify this fact. */ |
| 903 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); | 927 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 904 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); | 928 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 905 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); | 929 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 906 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); | 930 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 907 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); | 931 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 908 assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); | 932 assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
| 909 assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); | 933 assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); |
| 934 |
| 935 if( op & (WO_LT|WO_LE|WO_GT|WO_GE) |
| 936 && sqlite3ExprIsVector(pTerm->pExpr->pRight) |
| 937 ){ |
| 938 if( i<16 ) mNoOmit |= (1 << i); |
| 939 if( op==WO_LT ) pIdxCons[j].op = WO_LE; |
| 940 if( op==WO_GT ) pIdxCons[j].op = WO_GE; |
| 941 } |
| 942 |
| 910 j++; | 943 j++; |
| 911 } | 944 } |
| 912 for(i=0; i<nOrderBy; i++){ | 945 for(i=0; i<nOrderBy; i++){ |
| 913 Expr *pExpr = pOrderBy->a[i].pExpr; | 946 Expr *pExpr = pOrderBy->a[i].pExpr; |
| 914 pIdxOrderBy[i].iColumn = pExpr->iColumn; | 947 pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 915 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; | 948 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 916 } | 949 } |
| 917 | 950 |
| 951 *pmNoOmit = mNoOmit; |
| 918 return pIdxInfo; | 952 return pIdxInfo; |
| 919 } | 953 } |
| 920 | 954 |
| 921 /* | 955 /* |
| 922 ** The table object reference passed as the second argument to this function | 956 ** The table object reference passed as the second argument to this function |
| 923 ** must represent a virtual table. This function invokes the xBestIndex() | 957 ** must represent a virtual table. This function invokes the xBestIndex() |
| 924 ** method of the virtual table with the sqlite3_index_info object that | 958 ** method of the virtual table with the sqlite3_index_info object that |
| 925 ** comes in as the 3rd argument to this function. | 959 ** comes in as the 3rd argument to this function. |
| 926 ** | 960 ** |
| 927 ** If an error occurs, pParse is populated with an error message and a | 961 ** If an error occurs, pParse is populated with an error message and a |
| 928 ** non-zero value is returned. Otherwise, 0 is returned and the output | 962 ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 929 ** part of the sqlite3_index_info structure is left populated. | 963 ** part of the sqlite3_index_info structure is left populated. |
| 930 ** | 964 ** |
| 931 ** Whether or not an error is returned, it is the responsibility of the | 965 ** Whether or not an error is returned, it is the responsibility of the |
| 932 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates | 966 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 933 ** that this is required. | 967 ** that this is required. |
| 934 */ | 968 */ |
| 935 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ | 969 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 936 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; | 970 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 937 int i; | |
| 938 int rc; | 971 int rc; |
| 939 | 972 |
| 940 TRACE_IDX_INPUTS(p); | 973 TRACE_IDX_INPUTS(p); |
| 941 rc = pVtab->pModule->xBestIndex(pVtab, p); | 974 rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 942 TRACE_IDX_OUTPUTS(p); | 975 TRACE_IDX_OUTPUTS(p); |
| 943 | 976 |
| 944 if( rc!=SQLITE_OK ){ | 977 if( rc!=SQLITE_OK ){ |
| 945 if( rc==SQLITE_NOMEM ){ | 978 if( rc==SQLITE_NOMEM ){ |
| 946 pParse->db->mallocFailed = 1; | 979 sqlite3OomFault(pParse->db); |
| 947 }else if( !pVtab->zErrMsg ){ | 980 }else if( !pVtab->zErrMsg ){ |
| 948 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); | 981 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 949 }else{ | 982 }else{ |
| 950 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); | 983 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 951 } | 984 } |
| 952 } | 985 } |
| 953 sqlite3_free(pVtab->zErrMsg); | 986 sqlite3_free(pVtab->zErrMsg); |
| 954 pVtab->zErrMsg = 0; | 987 pVtab->zErrMsg = 0; |
| 955 | 988 |
| 989 #if 0 |
| 990 /* This error is now caught by the caller. |
| 991 ** Search for "xBestIndex malfunction" below */ |
| 956 for(i=0; i<p->nConstraint; i++){ | 992 for(i=0; i<p->nConstraint; i++){ |
| 957 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ | 993 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 958 sqlite3ErrorMsg(pParse, | 994 sqlite3ErrorMsg(pParse, |
| 959 "table %s: xBestIndex returned an invalid plan", pTab->zName); | 995 "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 960 } | 996 } |
| 961 } | 997 } |
| 998 #endif |
| 962 | 999 |
| 963 return pParse->nErr; | 1000 return pParse->nErr; |
| 964 } | 1001 } |
| 965 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ | 1002 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 966 | 1003 |
| 967 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 1004 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 968 /* | 1005 /* |
| 969 ** Estimate the location of a particular key among all keys in an | 1006 ** Estimate the location of a particular key among all keys in an |
| 970 ** index. Store the results in aStat as follows: | 1007 ** index. Store the results in aStat as follows: |
| 971 ** | 1008 ** |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1180 } | 1217 } |
| 1181 } | 1218 } |
| 1182 return nRet; | 1219 return nRet; |
| 1183 } | 1220 } |
| 1184 | 1221 |
| 1185 | 1222 |
| 1186 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 1223 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 1187 /* | 1224 /* |
| 1188 ** Return the affinity for a single column of an index. | 1225 ** Return the affinity for a single column of an index. |
| 1189 */ | 1226 */ |
| 1190 static char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ | 1227 char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ |
| 1191 assert( iCol>=0 && iCol<pIdx->nColumn ); | 1228 assert( iCol>=0 && iCol<pIdx->nColumn ); |
| 1192 if( !pIdx->zColAff ){ | 1229 if( !pIdx->zColAff ){ |
| 1193 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB; | 1230 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB; |
| 1194 } | 1231 } |
| 1195 return pIdx->zColAff[iCol]; | 1232 return pIdx->zColAff[iCol]; |
| 1196 } | 1233 } |
| 1197 #endif | 1234 #endif |
| 1198 | 1235 |
| 1199 | 1236 |
| 1200 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 1237 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1357 LogEst nNew; | 1394 LogEst nNew; |
| 1358 | 1395 |
| 1359 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 1396 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 1360 Index *p = pLoop->u.btree.pIndex; | 1397 Index *p = pLoop->u.btree.pIndex; |
| 1361 int nEq = pLoop->u.btree.nEq; | 1398 int nEq = pLoop->u.btree.nEq; |
| 1362 | 1399 |
| 1363 if( p->nSample>0 && nEq<p->nSampleCol ){ | 1400 if( p->nSample>0 && nEq<p->nSampleCol ){ |
| 1364 if( nEq==pBuilder->nRecValid ){ | 1401 if( nEq==pBuilder->nRecValid ){ |
| 1365 UnpackedRecord *pRec = pBuilder->pRec; | 1402 UnpackedRecord *pRec = pBuilder->pRec; |
| 1366 tRowcnt a[2]; | 1403 tRowcnt a[2]; |
| 1367 u8 aff; | 1404 int nBtm = pLoop->u.btree.nBtm; |
| 1405 int nTop = pLoop->u.btree.nTop; |
| 1368 | 1406 |
| 1369 /* Variable iLower will be set to the estimate of the number of rows in | 1407 /* Variable iLower will be set to the estimate of the number of rows in |
| 1370 ** the index that are less than the lower bound of the range query. The | 1408 ** the index that are less than the lower bound of the range query. The |
| 1371 ** lower bound being the concatenation of $P and $L, where $P is the | 1409 ** lower bound being the concatenation of $P and $L, where $P is the |
| 1372 ** key-prefix formed by the nEq values matched against the nEq left-most | 1410 ** key-prefix formed by the nEq values matched against the nEq left-most |
| 1373 ** columns of the index, and $L is the value in pLower. | 1411 ** columns of the index, and $L is the value in pLower. |
| 1374 ** | 1412 ** |
| 1375 ** Or, if pLower is NULL or $L cannot be extracted from it (because it | 1413 ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 1376 ** is not a simple variable or literal value), the lower bound of the | 1414 ** is not a simple variable or literal value), the lower bound of the |
| 1377 ** range is $P. Due to a quirk in the way whereKeyStats() works, even | 1415 ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 1378 ** if $L is available, whereKeyStats() is called for both ($P) and | 1416 ** if $L is available, whereKeyStats() is called for both ($P) and |
| 1379 ** ($P:$L) and the larger of the two returned values is used. | 1417 ** ($P:$L) and the larger of the two returned values is used. |
| 1380 ** | 1418 ** |
| 1381 ** Similarly, iUpper is to be set to the estimate of the number of rows | 1419 ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 1382 ** less than the upper bound of the range query. Where the upper bound | 1420 ** less than the upper bound of the range query. Where the upper bound |
| 1383 ** is either ($P) or ($P:$U). Again, even if $U is available, both values | 1421 ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 1384 ** of iUpper are requested of whereKeyStats() and the smaller used. | 1422 ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 1385 ** | 1423 ** |
| 1386 ** The number of rows between the two bounds is then just iUpper-iLower. | 1424 ** The number of rows between the two bounds is then just iUpper-iLower. |
| 1387 */ | 1425 */ |
| 1388 tRowcnt iLower; /* Rows less than the lower bound */ | 1426 tRowcnt iLower; /* Rows less than the lower bound */ |
| 1389 tRowcnt iUpper; /* Rows less than the upper bound */ | 1427 tRowcnt iUpper; /* Rows less than the upper bound */ |
| 1390 int iLwrIdx = -2; /* aSample[] for the lower bound */ | 1428 int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 1391 int iUprIdx = -1; /* aSample[] for the upper bound */ | 1429 int iUprIdx = -1; /* aSample[] for the upper bound */ |
| 1392 | 1430 |
| 1393 if( pRec ){ | 1431 if( pRec ){ |
| 1394 testcase( pRec->nField!=pBuilder->nRecValid ); | 1432 testcase( pRec->nField!=pBuilder->nRecValid ); |
| 1395 pRec->nField = pBuilder->nRecValid; | 1433 pRec->nField = pBuilder->nRecValid; |
| 1396 } | 1434 } |
| 1397 aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq); | |
| 1398 assert( nEq!=p->nKeyCol || aff==SQLITE_AFF_INTEGER ); | |
| 1399 /* Determine iLower and iUpper using ($P) only. */ | 1435 /* Determine iLower and iUpper using ($P) only. */ |
| 1400 if( nEq==0 ){ | 1436 if( nEq==0 ){ |
| 1401 iLower = 0; | 1437 iLower = 0; |
| 1402 iUpper = p->nRowEst0; | 1438 iUpper = p->nRowEst0; |
| 1403 }else{ | 1439 }else{ |
| 1404 /* Note: this call could be optimized away - since the same values must | 1440 /* Note: this call could be optimized away - since the same values must |
| 1405 ** have been requested when testing key $P in whereEqualScanEst(). */ | 1441 ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 1406 whereKeyStats(pParse, p, pRec, 0, a); | 1442 whereKeyStats(pParse, p, pRec, 0, a); |
| 1407 iLower = a[0]; | 1443 iLower = a[0]; |
| 1408 iUpper = a[0] + a[1]; | 1444 iUpper = a[0] + a[1]; |
| 1409 } | 1445 } |
| 1410 | 1446 |
| 1411 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); | 1447 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 1412 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); | 1448 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
| 1413 assert( p->aSortOrder!=0 ); | 1449 assert( p->aSortOrder!=0 ); |
| 1414 if( p->aSortOrder[nEq] ){ | 1450 if( p->aSortOrder[nEq] ){ |
| 1415 /* The roles of pLower and pUpper are swapped for a DESC index */ | 1451 /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 1416 SWAP(WhereTerm*, pLower, pUpper); | 1452 SWAP(WhereTerm*, pLower, pUpper); |
| 1453 SWAP(int, nBtm, nTop); |
| 1417 } | 1454 } |
| 1418 | 1455 |
| 1419 /* If possible, improve on the iLower estimate using ($P:$L). */ | 1456 /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 1420 if( pLower ){ | 1457 if( pLower ){ |
| 1421 int bOk; /* True if value is extracted from pExpr */ | 1458 int n; /* Values extracted from pExpr */ |
| 1422 Expr *pExpr = pLower->pExpr->pRight; | 1459 Expr *pExpr = pLower->pExpr->pRight; |
| 1423 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); | 1460 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n); |
| 1424 if( rc==SQLITE_OK && bOk ){ | 1461 if( rc==SQLITE_OK && n ){ |
| 1425 tRowcnt iNew; | 1462 tRowcnt iNew; |
| 1463 u16 mask = WO_GT|WO_LE; |
| 1464 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); |
| 1426 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); | 1465 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
| 1427 iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); | 1466 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0); |
| 1428 if( iNew>iLower ) iLower = iNew; | 1467 if( iNew>iLower ) iLower = iNew; |
| 1429 nOut--; | 1468 nOut--; |
| 1430 pLower = 0; | 1469 pLower = 0; |
| 1431 } | 1470 } |
| 1432 } | 1471 } |
| 1433 | 1472 |
| 1434 /* If possible, improve on the iUpper estimate using ($P:$U). */ | 1473 /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 1435 if( pUpper ){ | 1474 if( pUpper ){ |
| 1436 int bOk; /* True if value is extracted from pExpr */ | 1475 int n; /* Values extracted from pExpr */ |
| 1437 Expr *pExpr = pUpper->pExpr->pRight; | 1476 Expr *pExpr = pUpper->pExpr->pRight; |
| 1438 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); | 1477 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n); |
| 1439 if( rc==SQLITE_OK && bOk ){ | 1478 if( rc==SQLITE_OK && n ){ |
| 1440 tRowcnt iNew; | 1479 tRowcnt iNew; |
| 1480 u16 mask = WO_GT|WO_LE; |
| 1481 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); |
| 1441 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); | 1482 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
| 1442 iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); | 1483 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0); |
| 1443 if( iNew<iUpper ) iUpper = iNew; | 1484 if( iNew<iUpper ) iUpper = iNew; |
| 1444 nOut--; | 1485 nOut--; |
| 1445 pUpper = 0; | 1486 pUpper = 0; |
| 1446 } | 1487 } |
| 1447 } | 1488 } |
| 1448 | 1489 |
| 1449 pBuilder->pRec = pRec; | 1490 pBuilder->pRec = pRec; |
| 1450 if( rc==SQLITE_OK ){ | 1491 if( rc==SQLITE_OK ){ |
| 1451 if( iUpper>iLower ){ | 1492 if( iUpper>iLower ){ |
| 1452 nNew = sqlite3LogEst(iUpper - iLower); | 1493 nNew = sqlite3LogEst(iUpper - iLower); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1522 */ | 1563 */ |
| 1523 static int whereEqualScanEst( | 1564 static int whereEqualScanEst( |
| 1524 Parse *pParse, /* Parsing & code generating context */ | 1565 Parse *pParse, /* Parsing & code generating context */ |
| 1525 WhereLoopBuilder *pBuilder, | 1566 WhereLoopBuilder *pBuilder, |
| 1526 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ | 1567 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 1527 tRowcnt *pnRow /* Write the revised row estimate here */ | 1568 tRowcnt *pnRow /* Write the revised row estimate here */ |
| 1528 ){ | 1569 ){ |
| 1529 Index *p = pBuilder->pNew->u.btree.pIndex; | 1570 Index *p = pBuilder->pNew->u.btree.pIndex; |
| 1530 int nEq = pBuilder->pNew->u.btree.nEq; | 1571 int nEq = pBuilder->pNew->u.btree.nEq; |
| 1531 UnpackedRecord *pRec = pBuilder->pRec; | 1572 UnpackedRecord *pRec = pBuilder->pRec; |
| 1532 u8 aff; /* Column affinity */ | |
| 1533 int rc; /* Subfunction return code */ | 1573 int rc; /* Subfunction return code */ |
| 1534 tRowcnt a[2]; /* Statistics */ | 1574 tRowcnt a[2]; /* Statistics */ |
| 1535 int bOk; | 1575 int bOk; |
| 1536 | 1576 |
| 1537 assert( nEq>=1 ); | 1577 assert( nEq>=1 ); |
| 1538 assert( nEq<=p->nColumn ); | 1578 assert( nEq<=p->nColumn ); |
| 1539 assert( p->aSample!=0 ); | 1579 assert( p->aSample!=0 ); |
| 1540 assert( p->nSample>0 ); | 1580 assert( p->nSample>0 ); |
| 1541 assert( pBuilder->nRecValid<nEq ); | 1581 assert( pBuilder->nRecValid<nEq ); |
| 1542 | 1582 |
| 1543 /* If values are not available for all fields of the index to the left | 1583 /* If values are not available for all fields of the index to the left |
| 1544 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ | 1584 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 1545 if( pBuilder->nRecValid<(nEq-1) ){ | 1585 if( pBuilder->nRecValid<(nEq-1) ){ |
| 1546 return SQLITE_NOTFOUND; | 1586 return SQLITE_NOTFOUND; |
| 1547 } | 1587 } |
| 1548 | 1588 |
| 1549 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() | 1589 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 1550 ** below would return the same value. */ | 1590 ** below would return the same value. */ |
| 1551 if( nEq>=p->nColumn ){ | 1591 if( nEq>=p->nColumn ){ |
| 1552 *pnRow = 1; | 1592 *pnRow = 1; |
| 1553 return SQLITE_OK; | 1593 return SQLITE_OK; |
| 1554 } | 1594 } |
| 1555 | 1595 |
| 1556 aff = sqlite3IndexColumnAffinity(pParse->db, p, nEq-1); | 1596 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk); |
| 1557 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); | |
| 1558 pBuilder->pRec = pRec; | 1597 pBuilder->pRec = pRec; |
| 1559 if( rc!=SQLITE_OK ) return rc; | 1598 if( rc!=SQLITE_OK ) return rc; |
| 1560 if( bOk==0 ) return SQLITE_NOTFOUND; | 1599 if( bOk==0 ) return SQLITE_NOTFOUND; |
| 1561 pBuilder->nRecValid = nEq; | 1600 pBuilder->nRecValid = nEq; |
| 1562 | 1601 |
| 1563 whereKeyStats(pParse, p, pRec, 0, a); | 1602 whereKeyStats(pParse, p, pRec, 0, a); |
| 1564 WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); | 1603 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n", |
| 1604 p->zName, nEq-1, (int)a[1])); |
| 1565 *pnRow = a[1]; | 1605 *pnRow = a[1]; |
| 1566 | 1606 |
| 1567 return rc; | 1607 return rc; |
| 1568 } | 1608 } |
| 1569 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ | 1609 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 1570 | 1610 |
| 1571 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 1611 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 1572 /* | 1612 /* |
| 1573 ** Estimate the number of rows that will be returned based on | 1613 ** Estimate the number of rows that will be returned based on |
| 1574 ** an IN constraint where the right-hand side of the IN operator | 1614 ** an IN constraint where the right-hand side of the IN operator |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1620 | 1660 |
| 1621 #ifdef WHERETRACE_ENABLED | 1661 #ifdef WHERETRACE_ENABLED |
| 1622 /* | 1662 /* |
| 1623 ** Print the content of a WhereTerm object | 1663 ** Print the content of a WhereTerm object |
| 1624 */ | 1664 */ |
| 1625 static void whereTermPrint(WhereTerm *pTerm, int iTerm){ | 1665 static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
| 1626 if( pTerm==0 ){ | 1666 if( pTerm==0 ){ |
| 1627 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); | 1667 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 1628 }else{ | 1668 }else{ |
| 1629 char zType[4]; | 1669 char zType[4]; |
| 1670 char zLeft[50]; |
| 1630 memcpy(zType, "...", 4); | 1671 memcpy(zType, "...", 4); |
| 1631 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; | 1672 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 1632 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; | 1673 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 1633 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; | 1674 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 1675 if( pTerm->eOperator & WO_SINGLE ){ |
| 1676 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}", |
| 1677 pTerm->leftCursor, pTerm->u.leftColumn); |
| 1678 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){ |
| 1679 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld", |
| 1680 pTerm->u.pOrInfo->indexable); |
| 1681 }else{ |
| 1682 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor); |
| 1683 } |
| 1634 sqlite3DebugPrintf( | 1684 sqlite3DebugPrintf( |
| 1635 "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n", | 1685 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x", |
| 1636 iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, | 1686 iTerm, pTerm, zType, zLeft, pTerm->truthProb, |
| 1637 pTerm->eOperator, pTerm->wtFlags); | 1687 pTerm->eOperator, pTerm->wtFlags); |
| 1688 if( pTerm->iField ){ |
| 1689 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField); |
| 1690 }else{ |
| 1691 sqlite3DebugPrintf("\n"); |
| 1692 } |
| 1638 sqlite3TreeViewExpr(0, pTerm->pExpr, 0); | 1693 sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 1639 } | 1694 } |
| 1640 } | 1695 } |
| 1641 #endif | 1696 #endif |
| 1642 | 1697 |
| 1643 #ifdef WHERETRACE_ENABLED | 1698 #ifdef WHERETRACE_ENABLED |
| 1644 /* | 1699 /* |
| 1700 ** Show the complete content of a WhereClause |
| 1701 */ |
| 1702 void sqlite3WhereClausePrint(WhereClause *pWC){ |
| 1703 int i; |
| 1704 for(i=0; i<pWC->nTerm; i++){ |
| 1705 whereTermPrint(&pWC->a[i], i); |
| 1706 } |
| 1707 } |
| 1708 #endif |
| 1709 |
| 1710 #ifdef WHERETRACE_ENABLED |
| 1711 /* |
| 1645 ** Print a WhereLoop object for debugging purposes | 1712 ** Print a WhereLoop object for debugging purposes |
| 1646 */ | 1713 */ |
| 1647 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ | 1714 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 1648 WhereInfo *pWInfo = pWC->pWInfo; | 1715 WhereInfo *pWInfo = pWC->pWInfo; |
| 1649 int nb = 1+(pWInfo->pTabList->nSrc+7)/8; | 1716 int nb = 1+(pWInfo->pTabList->nSrc+3)/4; |
| 1650 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; | 1717 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
| 1651 Table *pTab = pItem->pTab; | 1718 Table *pTab = pItem->pTab; |
| 1719 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; |
| 1652 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, | 1720 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
| 1653 p->iTab, nb, p->maskSelf, nb, p->prereq); | 1721 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); |
| 1654 sqlite3DebugPrintf(" %12s", | 1722 sqlite3DebugPrintf(" %12s", |
| 1655 pItem->zAlias ? pItem->zAlias : pTab->zName); | 1723 pItem->zAlias ? pItem->zAlias : pTab->zName); |
| 1656 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ | 1724 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 1657 const char *zName; | 1725 const char *zName; |
| 1658 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ | 1726 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
| 1659 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ | 1727 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 1660 int i = sqlite3Strlen30(zName) - 1; | 1728 int i = sqlite3Strlen30(zName) - 1; |
| 1661 while( zName[i]!='_' ) i--; | 1729 while( zName[i]!='_' ) i--; |
| 1662 zName += i; | 1730 zName += i; |
| 1663 } | 1731 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1728 whereLoopInit(p); | 1796 whereLoopInit(p); |
| 1729 } | 1797 } |
| 1730 | 1798 |
| 1731 /* | 1799 /* |
| 1732 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. | 1800 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 1733 */ | 1801 */ |
| 1734 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ | 1802 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 1735 WhereTerm **paNew; | 1803 WhereTerm **paNew; |
| 1736 if( p->nLSlot>=n ) return SQLITE_OK; | 1804 if( p->nLSlot>=n ) return SQLITE_OK; |
| 1737 n = (n+7)&~7; | 1805 n = (n+7)&~7; |
| 1738 paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); | 1806 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n); |
| 1739 if( paNew==0 ) return SQLITE_NOMEM; | 1807 if( paNew==0 ) return SQLITE_NOMEM_BKPT; |
| 1740 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); | 1808 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 1741 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); | 1809 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 1742 p->aLTerm = paNew; | 1810 p->aLTerm = paNew; |
| 1743 p->nLSlot = n; | 1811 p->nLSlot = n; |
| 1744 return SQLITE_OK; | 1812 return SQLITE_OK; |
| 1745 } | 1813 } |
| 1746 | 1814 |
| 1747 /* | 1815 /* |
| 1748 ** Transfer content from the second pLoop into the first. | 1816 ** Transfer content from the second pLoop into the first. |
| 1749 */ | 1817 */ |
| 1750 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ | 1818 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 1751 whereLoopClearUnion(db, pTo); | 1819 whereLoopClearUnion(db, pTo); |
| 1752 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ | 1820 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 1753 memset(&pTo->u, 0, sizeof(pTo->u)); | 1821 memset(&pTo->u, 0, sizeof(pTo->u)); |
| 1754 return SQLITE_NOMEM; | 1822 return SQLITE_NOMEM_BKPT; |
| 1755 } | 1823 } |
| 1756 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); | 1824 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 1757 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); | 1825 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
| 1758 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ | 1826 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 1759 pFrom->u.vtab.needFree = 0; | 1827 pFrom->u.vtab.needFree = 0; |
| 1760 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ | 1828 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 1761 pFrom->u.btree.pIndex = 0; | 1829 pFrom->u.btree.pIndex = 0; |
| 1762 } | 1830 } |
| 1763 return SQLITE_OK; | 1831 return SQLITE_OK; |
| 1764 } | 1832 } |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1968 ** | 2036 ** |
| 1969 ** (1) They have the same iTab. | 2037 ** (1) They have the same iTab. |
| 1970 ** (2) They have the same iSortIdx. | 2038 ** (2) They have the same iSortIdx. |
| 1971 ** (3) The template has same or fewer dependencies than the current loop | 2039 ** (3) The template has same or fewer dependencies than the current loop |
| 1972 ** (4) The template has the same or lower cost than the current loop | 2040 ** (4) The template has the same or lower cost than the current loop |
| 1973 */ | 2041 */ |
| 1974 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ | 2042 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
| 1975 WhereLoop **ppPrev, *p; | 2043 WhereLoop **ppPrev, *p; |
| 1976 WhereInfo *pWInfo = pBuilder->pWInfo; | 2044 WhereInfo *pWInfo = pBuilder->pWInfo; |
| 1977 sqlite3 *db = pWInfo->pParse->db; | 2045 sqlite3 *db = pWInfo->pParse->db; |
| 2046 int rc; |
| 1978 | 2047 |
| 1979 /* If pBuilder->pOrSet is defined, then only keep track of the costs | 2048 /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 1980 ** and prereqs. | 2049 ** and prereqs. |
| 1981 */ | 2050 */ |
| 1982 if( pBuilder->pOrSet!=0 ){ | 2051 if( pBuilder->pOrSet!=0 ){ |
| 1983 if( pTemplate->nLTerm ){ | 2052 if( pTemplate->nLTerm ){ |
| 1984 #if WHERETRACE_ENABLED | 2053 #if WHERETRACE_ENABLED |
| 1985 u16 n = pBuilder->pOrSet->n; | 2054 u16 n = pBuilder->pOrSet->n; |
| 1986 int x = | 2055 int x = |
| 1987 #endif | 2056 #endif |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2025 if( p!=0 ){ | 2094 if( p!=0 ){ |
| 2026 sqlite3DebugPrintf("replace: "); | 2095 sqlite3DebugPrintf("replace: "); |
| 2027 whereLoopPrint(p, pBuilder->pWC); | 2096 whereLoopPrint(p, pBuilder->pWC); |
| 2028 } | 2097 } |
| 2029 sqlite3DebugPrintf(" add: "); | 2098 sqlite3DebugPrintf(" add: "); |
| 2030 whereLoopPrint(pTemplate, pBuilder->pWC); | 2099 whereLoopPrint(pTemplate, pBuilder->pWC); |
| 2031 } | 2100 } |
| 2032 #endif | 2101 #endif |
| 2033 if( p==0 ){ | 2102 if( p==0 ){ |
| 2034 /* Allocate a new WhereLoop to add to the end of the list */ | 2103 /* Allocate a new WhereLoop to add to the end of the list */ |
| 2035 *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); | 2104 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop)); |
| 2036 if( p==0 ) return SQLITE_NOMEM; | 2105 if( p==0 ) return SQLITE_NOMEM_BKPT; |
| 2037 whereLoopInit(p); | 2106 whereLoopInit(p); |
| 2038 p->pNextLoop = 0; | 2107 p->pNextLoop = 0; |
| 2039 }else{ | 2108 }else{ |
| 2040 /* We will be overwriting WhereLoop p[]. But before we do, first | 2109 /* We will be overwriting WhereLoop p[]. But before we do, first |
| 2041 ** go through the rest of the list and delete any other entries besides | 2110 ** go through the rest of the list and delete any other entries besides |
| 2042 ** p[] that are also supplated by pTemplate */ | 2111 ** p[] that are also supplated by pTemplate */ |
| 2043 WhereLoop **ppTail = &p->pNextLoop; | 2112 WhereLoop **ppTail = &p->pNextLoop; |
| 2044 WhereLoop *pToDel; | 2113 WhereLoop *pToDel; |
| 2045 while( *ppTail ){ | 2114 while( *ppTail ){ |
| 2046 ppTail = whereLoopFindLesser(ppTail, pTemplate); | 2115 ppTail = whereLoopFindLesser(ppTail, pTemplate); |
| 2047 if( ppTail==0 ) break; | 2116 if( ppTail==0 ) break; |
| 2048 pToDel = *ppTail; | 2117 pToDel = *ppTail; |
| 2049 if( pToDel==0 ) break; | 2118 if( pToDel==0 ) break; |
| 2050 *ppTail = pToDel->pNextLoop; | 2119 *ppTail = pToDel->pNextLoop; |
| 2051 #if WHERETRACE_ENABLED /* 0x8 */ | 2120 #if WHERETRACE_ENABLED /* 0x8 */ |
| 2052 if( sqlite3WhereTrace & 0x8 ){ | 2121 if( sqlite3WhereTrace & 0x8 ){ |
| 2053 sqlite3DebugPrintf(" delete: "); | 2122 sqlite3DebugPrintf(" delete: "); |
| 2054 whereLoopPrint(pToDel, pBuilder->pWC); | 2123 whereLoopPrint(pToDel, pBuilder->pWC); |
| 2055 } | 2124 } |
| 2056 #endif | 2125 #endif |
| 2057 whereLoopDelete(db, pToDel); | 2126 whereLoopDelete(db, pToDel); |
| 2058 } | 2127 } |
| 2059 } | 2128 } |
| 2060 whereLoopXfer(db, p, pTemplate); | 2129 rc = whereLoopXfer(db, p, pTemplate); |
| 2061 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ | 2130 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 2062 Index *pIndex = p->u.btree.pIndex; | 2131 Index *pIndex = p->u.btree.pIndex; |
| 2063 if( pIndex && pIndex->tnum==0 ){ | 2132 if( pIndex && pIndex->tnum==0 ){ |
| 2064 p->u.btree.pIndex = 0; | 2133 p->u.btree.pIndex = 0; |
| 2065 } | 2134 } |
| 2066 } | 2135 } |
| 2067 return SQLITE_OK; | 2136 return rc; |
| 2068 } | 2137 } |
| 2069 | 2138 |
| 2070 /* | 2139 /* |
| 2071 ** Adjust the WhereLoop.nOut value downward to account for terms of the | 2140 ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 2072 ** WHERE clause that reference the loop but which are not used by an | 2141 ** WHERE clause that reference the loop but which are not used by an |
| 2073 ** index. | 2142 ** index. |
| 2074 * | 2143 * |
| 2075 ** For every WHERE clause term that is not used by the index | 2144 ** For every WHERE clause term that is not used by the index |
| 2076 ** and which has a truth probability assigned by one of the likelihood(), | 2145 ** and which has a truth probability assigned by one of the likelihood(), |
| 2077 ** likely(), or unlikely() SQL functions, reduce the estimated number | 2146 ** likely(), or unlikely() SQL functions, reduce the estimated number |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2135 k = 20; | 2204 k = 20; |
| 2136 } | 2205 } |
| 2137 if( iReduce<k ) iReduce = k; | 2206 if( iReduce<k ) iReduce = k; |
| 2138 } | 2207 } |
| 2139 } | 2208 } |
| 2140 } | 2209 } |
| 2141 } | 2210 } |
| 2142 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; | 2211 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; |
| 2143 } | 2212 } |
| 2144 | 2213 |
| 2214 /* |
| 2215 ** Term pTerm is a vector range comparison operation. The first comparison |
| 2216 ** in the vector can be optimized using column nEq of the index. This |
| 2217 ** function returns the total number of vector elements that can be used |
| 2218 ** as part of the range comparison. |
| 2219 ** |
| 2220 ** For example, if the query is: |
| 2221 ** |
| 2222 ** WHERE a = ? AND (b, c, d) > (?, ?, ?) |
| 2223 ** |
| 2224 ** and the index: |
| 2225 ** |
| 2226 ** CREATE INDEX ... ON (a, b, c, d, e) |
| 2227 ** |
| 2228 ** then this function would be invoked with nEq=1. The value returned in |
| 2229 ** this case is 3. |
| 2230 */ |
| 2231 static int whereRangeVectorLen( |
| 2232 Parse *pParse, /* Parsing context */ |
| 2233 int iCur, /* Cursor open on pIdx */ |
| 2234 Index *pIdx, /* The index to be used for a inequality constraint */ |
| 2235 int nEq, /* Number of prior equality constraints on same index */ |
| 2236 WhereTerm *pTerm /* The vector inequality constraint */ |
| 2237 ){ |
| 2238 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft); |
| 2239 int i; |
| 2240 |
| 2241 nCmp = MIN(nCmp, (pIdx->nColumn - nEq)); |
| 2242 for(i=1; i<nCmp; i++){ |
| 2243 /* Test if comparison i of pTerm is compatible with column (i+nEq) |
| 2244 ** of the index. If not, exit the loop. */ |
| 2245 char aff; /* Comparison affinity */ |
| 2246 char idxaff = 0; /* Indexed columns affinity */ |
| 2247 CollSeq *pColl; /* Comparison collation sequence */ |
| 2248 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr; |
| 2249 Expr *pRhs = pTerm->pExpr->pRight; |
| 2250 if( pRhs->flags & EP_xIsSelect ){ |
| 2251 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr; |
| 2252 }else{ |
| 2253 pRhs = pRhs->x.pList->a[i].pExpr; |
| 2254 } |
| 2255 |
| 2256 /* Check that the LHS of the comparison is a column reference to |
| 2257 ** the right column of the right source table. And that the sort |
| 2258 ** order of the index column is the same as the sort order of the |
| 2259 ** leftmost index column. */ |
| 2260 if( pLhs->op!=TK_COLUMN |
| 2261 || pLhs->iTable!=iCur |
| 2262 || pLhs->iColumn!=pIdx->aiColumn[i+nEq] |
| 2263 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq] |
| 2264 ){ |
| 2265 break; |
| 2266 } |
| 2267 |
| 2268 testcase( pLhs->iColumn==XN_ROWID ); |
| 2269 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs)); |
| 2270 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn); |
| 2271 if( aff!=idxaff ) break; |
| 2272 |
| 2273 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
| 2274 if( pColl==0 ) break; |
| 2275 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break; |
| 2276 } |
| 2277 return i; |
| 2278 } |
| 2279 |
| 2145 /* | 2280 /* |
| 2146 ** Adjust the cost C by the costMult facter T. This only occurs if | 2281 ** Adjust the cost C by the costMult facter T. This only occurs if |
| 2147 ** compiled with -DSQLITE_ENABLE_COSTMULT | 2282 ** compiled with -DSQLITE_ENABLE_COSTMULT |
| 2148 */ | 2283 */ |
| 2149 #ifdef SQLITE_ENABLE_COSTMULT | 2284 #ifdef SQLITE_ENABLE_COSTMULT |
| 2150 # define ApplyCostMultiplier(C,T) C += T | 2285 # define ApplyCostMultiplier(C,T) C += T |
| 2151 #else | 2286 #else |
| 2152 # define ApplyCostMultiplier(C,T) | 2287 # define ApplyCostMultiplier(C,T) |
| 2153 #endif | 2288 #endif |
| 2154 | 2289 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 2173 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ | 2308 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 2174 Parse *pParse = pWInfo->pParse; /* Parsing context */ | 2309 Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 2175 sqlite3 *db = pParse->db; /* Database connection malloc context */ | 2310 sqlite3 *db = pParse->db; /* Database connection malloc context */ |
| 2176 WhereLoop *pNew; /* Template WhereLoop under construction */ | 2311 WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 2177 WhereTerm *pTerm; /* A WhereTerm under consideration */ | 2312 WhereTerm *pTerm; /* A WhereTerm under consideration */ |
| 2178 int opMask; /* Valid operators for constraints */ | 2313 int opMask; /* Valid operators for constraints */ |
| 2179 WhereScan scan; /* Iterator for WHERE terms */ | 2314 WhereScan scan; /* Iterator for WHERE terms */ |
| 2180 Bitmask saved_prereq; /* Original value of pNew->prereq */ | 2315 Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 2181 u16 saved_nLTerm; /* Original value of pNew->nLTerm */ | 2316 u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 2182 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ | 2317 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 2318 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */ |
| 2319 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */ |
| 2183 u16 saved_nSkip; /* Original value of pNew->nSkip */ | 2320 u16 saved_nSkip; /* Original value of pNew->nSkip */ |
| 2184 u32 saved_wsFlags; /* Original value of pNew->wsFlags */ | 2321 u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 2185 LogEst saved_nOut; /* Original value of pNew->nOut */ | 2322 LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 2186 int rc = SQLITE_OK; /* Return code */ | 2323 int rc = SQLITE_OK; /* Return code */ |
| 2187 LogEst rSize; /* Number of rows in the table */ | 2324 LogEst rSize; /* Number of rows in the table */ |
| 2188 LogEst rLogSize; /* Logarithm of table size */ | 2325 LogEst rLogSize; /* Logarithm of table size */ |
| 2189 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ | 2326 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
| 2190 | 2327 |
| 2191 pNew = pBuilder->pNew; | 2328 pNew = pBuilder->pNew; |
| 2192 if( db->mallocFailed ) return SQLITE_NOMEM; | 2329 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; |
| 2330 WHERETRACE(0x800, ("BEGIN addBtreeIdx(%s), nEq=%d\n", |
| 2331 pProbe->zName, pNew->u.btree.nEq)); |
| 2193 | 2332 |
| 2194 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); | 2333 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2195 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); | 2334 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 2196 if( pNew->wsFlags & WHERE_BTM_LIMIT ){ | 2335 if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 2197 opMask = WO_LT|WO_LE; | 2336 opMask = WO_LT|WO_LE; |
| 2198 }else if( /*pProbe->tnum<=0 ||*/ (pSrc->fg.jointype & JT_LEFT)!=0 ){ | |
| 2199 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; | |
| 2200 }else{ | 2337 }else{ |
| 2338 assert( pNew->u.btree.nBtm==0 ); |
| 2201 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; | 2339 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; |
| 2202 } | 2340 } |
| 2203 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); | 2341 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 2204 | 2342 |
| 2205 assert( pNew->u.btree.nEq<pProbe->nColumn ); | 2343 assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 2206 | 2344 |
| 2207 saved_nEq = pNew->u.btree.nEq; | 2345 saved_nEq = pNew->u.btree.nEq; |
| 2346 saved_nBtm = pNew->u.btree.nBtm; |
| 2347 saved_nTop = pNew->u.btree.nTop; |
| 2208 saved_nSkip = pNew->nSkip; | 2348 saved_nSkip = pNew->nSkip; |
| 2209 saved_nLTerm = pNew->nLTerm; | 2349 saved_nLTerm = pNew->nLTerm; |
| 2210 saved_wsFlags = pNew->wsFlags; | 2350 saved_wsFlags = pNew->wsFlags; |
| 2211 saved_prereq = pNew->prereq; | 2351 saved_prereq = pNew->prereq; |
| 2212 saved_nOut = pNew->nOut; | 2352 saved_nOut = pNew->nOut; |
| 2213 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq, | 2353 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq, |
| 2214 opMask, pProbe); | 2354 opMask, pProbe); |
| 2215 pNew->rSetup = 0; | 2355 pNew->rSetup = 0; |
| 2216 rSize = pProbe->aiRowLogEst[0]; | 2356 rSize = pProbe->aiRowLogEst[0]; |
| 2217 rLogSize = estLog(rSize); | 2357 rLogSize = estLog(rSize); |
| 2218 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ | 2358 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 2219 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ | 2359 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 2220 LogEst rCostIdx; | 2360 LogEst rCostIdx; |
| 2221 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ | 2361 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 2222 int nIn = 0; | 2362 int nIn = 0; |
| 2223 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 2363 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 2224 int nRecValid = pBuilder->nRecValid; | 2364 int nRecValid = pBuilder->nRecValid; |
| 2225 #endif | 2365 #endif |
| 2226 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) | 2366 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
| 2227 && indexColumnNotNull(pProbe, saved_nEq) | 2367 && indexColumnNotNull(pProbe, saved_nEq) |
| 2228 ){ | 2368 ){ |
| 2229 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ | 2369 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 2230 } | 2370 } |
| 2231 if( pTerm->prereqRight & pNew->maskSelf ) continue; | 2371 if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 2232 | 2372 |
| 2233 /* Do not allow the upper bound of a LIKE optimization range constraint | 2373 /* Do not allow the upper bound of a LIKE optimization range constraint |
| 2234 ** to mix with a lower range bound from some other source */ | 2374 ** to mix with a lower range bound from some other source */ |
| 2235 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; | 2375 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; |
| 2236 | 2376 |
| 2377 /* Do not allow IS constraints from the WHERE clause to be used by the |
| 2378 ** right table of a LEFT JOIN. Only constraints in the ON clause are |
| 2379 ** allowed */ |
| 2380 if( (pSrc->fg.jointype & JT_LEFT)!=0 |
| 2381 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 2382 && (eOp & (WO_IS|WO_ISNULL))!=0 |
| 2383 ){ |
| 2384 testcase( eOp & WO_IS ); |
| 2385 testcase( eOp & WO_ISNULL ); |
| 2386 continue; |
| 2387 } |
| 2388 |
| 2237 pNew->wsFlags = saved_wsFlags; | 2389 pNew->wsFlags = saved_wsFlags; |
| 2238 pNew->u.btree.nEq = saved_nEq; | 2390 pNew->u.btree.nEq = saved_nEq; |
| 2391 pNew->u.btree.nBtm = saved_nBtm; |
| 2392 pNew->u.btree.nTop = saved_nTop; |
| 2239 pNew->nLTerm = saved_nLTerm; | 2393 pNew->nLTerm = saved_nLTerm; |
| 2240 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ | 2394 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 2241 pNew->aLTerm[pNew->nLTerm++] = pTerm; | 2395 pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 2242 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; | 2396 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
| 2243 | 2397 |
| 2244 assert( nInMul==0 | 2398 assert( nInMul==0 |
| 2245 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 | 2399 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| 2246 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 | 2400 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 |
| 2247 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 | 2401 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 |
| 2248 ); | 2402 ); |
| 2249 | 2403 |
| 2250 if( eOp & WO_IN ){ | 2404 if( eOp & WO_IN ){ |
| 2251 Expr *pExpr = pTerm->pExpr; | 2405 Expr *pExpr = pTerm->pExpr; |
| 2252 pNew->wsFlags |= WHERE_COLUMN_IN; | 2406 pNew->wsFlags |= WHERE_COLUMN_IN; |
| 2253 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ | 2407 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 2254 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ | 2408 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
| 2409 int i; |
| 2255 nIn = 46; assert( 46==sqlite3LogEst(25) ); | 2410 nIn = 46; assert( 46==sqlite3LogEst(25) ); |
| 2411 |
| 2412 /* The expression may actually be of the form (x, y) IN (SELECT...). |
| 2413 ** In this case there is a separate term for each of (x) and (y). |
| 2414 ** However, the nIn multiplier should only be applied once, not once |
| 2415 ** for each such term. The following loop checks that pTerm is the |
| 2416 ** first such term in use, and sets nIn back to 0 if it is not. */ |
| 2417 for(i=0; i<pNew->nLTerm-1; i++){ |
| 2418 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0; |
| 2419 } |
| 2256 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ | 2420 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 2257 /* "x IN (value, value, ...)" */ | 2421 /* "x IN (value, value, ...)" */ |
| 2258 nIn = sqlite3LogEst(pExpr->x.pList->nExpr); | 2422 nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
| 2423 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 2424 ** changes "x IN (?)" into "x=?". */ |
| 2259 } | 2425 } |
| 2260 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser | |
| 2261 ** changes "x IN (?)" into "x=?". */ | |
| 2262 | |
| 2263 }else if( eOp & (WO_EQ|WO_IS) ){ | 2426 }else if( eOp & (WO_EQ|WO_IS) ){ |
| 2264 int iCol = pProbe->aiColumn[saved_nEq]; | 2427 int iCol = pProbe->aiColumn[saved_nEq]; |
| 2265 pNew->wsFlags |= WHERE_COLUMN_EQ; | 2428 pNew->wsFlags |= WHERE_COLUMN_EQ; |
| 2266 assert( saved_nEq==pNew->u.btree.nEq ); | 2429 assert( saved_nEq==pNew->u.btree.nEq ); |
| 2267 if( iCol==XN_ROWID | 2430 if( iCol==XN_ROWID |
| 2268 || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) | 2431 || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) |
| 2269 ){ | 2432 ){ |
| 2270 if( iCol>=0 && pProbe->uniqNotNull==0 ){ | 2433 if( iCol>=0 && pProbe->uniqNotNull==0 ){ |
| 2271 pNew->wsFlags |= WHERE_UNQ_WANTED; | 2434 pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 2272 }else{ | 2435 }else{ |
| 2273 pNew->wsFlags |= WHERE_ONEROW; | 2436 pNew->wsFlags |= WHERE_ONEROW; |
| 2274 } | 2437 } |
| 2275 } | 2438 } |
| 2276 }else if( eOp & WO_ISNULL ){ | 2439 }else if( eOp & WO_ISNULL ){ |
| 2277 pNew->wsFlags |= WHERE_COLUMN_NULL; | 2440 pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 2278 }else if( eOp & (WO_GT|WO_GE) ){ | 2441 }else if( eOp & (WO_GT|WO_GE) ){ |
| 2279 testcase( eOp & WO_GT ); | 2442 testcase( eOp & WO_GT ); |
| 2280 testcase( eOp & WO_GE ); | 2443 testcase( eOp & WO_GE ); |
| 2281 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; | 2444 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 2445 pNew->u.btree.nBtm = whereRangeVectorLen( |
| 2446 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 2447 ); |
| 2282 pBtm = pTerm; | 2448 pBtm = pTerm; |
| 2283 pTop = 0; | 2449 pTop = 0; |
| 2284 if( pTerm->wtFlags & TERM_LIKEOPT ){ | 2450 if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 2285 /* Range contraints that come from the LIKE optimization are | 2451 /* Range contraints that come from the LIKE optimization are |
| 2286 ** always used in pairs. */ | 2452 ** always used in pairs. */ |
| 2287 pTop = &pTerm[1]; | 2453 pTop = &pTerm[1]; |
| 2288 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); | 2454 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 2289 assert( pTop->wtFlags & TERM_LIKEOPT ); | 2455 assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 2290 assert( pTop->eOperator==WO_LT ); | 2456 assert( pTop->eOperator==WO_LT ); |
| 2291 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ | 2457 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 2292 pNew->aLTerm[pNew->nLTerm++] = pTop; | 2458 pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 2293 pNew->wsFlags |= WHERE_TOP_LIMIT; | 2459 pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 2460 pNew->u.btree.nTop = 1; |
| 2294 } | 2461 } |
| 2295 }else{ | 2462 }else{ |
| 2296 assert( eOp & (WO_LT|WO_LE) ); | 2463 assert( eOp & (WO_LT|WO_LE) ); |
| 2297 testcase( eOp & WO_LT ); | 2464 testcase( eOp & WO_LT ); |
| 2298 testcase( eOp & WO_LE ); | 2465 testcase( eOp & WO_LE ); |
| 2299 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; | 2466 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| 2467 pNew->u.btree.nTop = whereRangeVectorLen( |
| 2468 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 2469 ); |
| 2300 pTop = pTerm; | 2470 pTop = pTerm; |
| 2301 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? | 2471 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 2302 pNew->aLTerm[pNew->nLTerm-2] : 0; | 2472 pNew->aLTerm[pNew->nLTerm-2] : 0; |
| 2303 } | 2473 } |
| 2304 | 2474 |
| 2305 /* At this point pNew->nOut is set to the number of rows expected to | 2475 /* At this point pNew->nOut is set to the number of rows expected to |
| 2306 ** be visited by the index scan before considering term pTerm, or the | 2476 ** be visited by the index scan before considering term pTerm, or the |
| 2307 ** values of nIn and nInMul. In other words, assuming that all | 2477 ** values of nIn and nInMul. In other words, assuming that all |
| 2308 ** "x IN(...)" terms are replaced with "x = ?". This block updates | 2478 ** "x IN(...)" terms are replaced with "x = ?". This block updates |
| 2309 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ | 2479 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2389 ){ | 2559 ){ |
| 2390 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); | 2560 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
| 2391 } | 2561 } |
| 2392 pNew->nOut = saved_nOut; | 2562 pNew->nOut = saved_nOut; |
| 2393 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 2563 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 2394 pBuilder->nRecValid = nRecValid; | 2564 pBuilder->nRecValid = nRecValid; |
| 2395 #endif | 2565 #endif |
| 2396 } | 2566 } |
| 2397 pNew->prereq = saved_prereq; | 2567 pNew->prereq = saved_prereq; |
| 2398 pNew->u.btree.nEq = saved_nEq; | 2568 pNew->u.btree.nEq = saved_nEq; |
| 2569 pNew->u.btree.nBtm = saved_nBtm; |
| 2570 pNew->u.btree.nTop = saved_nTop; |
| 2399 pNew->nSkip = saved_nSkip; | 2571 pNew->nSkip = saved_nSkip; |
| 2400 pNew->wsFlags = saved_wsFlags; | 2572 pNew->wsFlags = saved_wsFlags; |
| 2401 pNew->nOut = saved_nOut; | 2573 pNew->nOut = saved_nOut; |
| 2402 pNew->nLTerm = saved_nLTerm; | 2574 pNew->nLTerm = saved_nLTerm; |
| 2403 | 2575 |
| 2404 /* Consider using a skip-scan if there are no WHERE clause constraints | 2576 /* Consider using a skip-scan if there are no WHERE clause constraints |
| 2405 ** available for the left-most terms of the index, and if the average | 2577 ** available for the left-most terms of the index, and if the average |
| 2406 ** number of repeats in the left-most terms is at least 18. | 2578 ** number of repeats in the left-most terms is at least 18. |
| 2407 ** | 2579 ** |
| 2408 ** The magic number 18 is selected on the basis that scanning 17 rows | 2580 ** The magic number 18 is selected on the basis that scanning 17 rows |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2428 /* TUNING: Because uncertainties in the estimates for skip-scan queries, | 2600 /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 2429 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ | 2601 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 2430 nIter += 5; | 2602 nIter += 5; |
| 2431 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); | 2603 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 2432 pNew->nOut = saved_nOut; | 2604 pNew->nOut = saved_nOut; |
| 2433 pNew->u.btree.nEq = saved_nEq; | 2605 pNew->u.btree.nEq = saved_nEq; |
| 2434 pNew->nSkip = saved_nSkip; | 2606 pNew->nSkip = saved_nSkip; |
| 2435 pNew->wsFlags = saved_wsFlags; | 2607 pNew->wsFlags = saved_wsFlags; |
| 2436 } | 2608 } |
| 2437 | 2609 |
| 2610 WHERETRACE(0x800, ("END addBtreeIdx(%s), nEq=%d, rc=%d\n", |
| 2611 pProbe->zName, saved_nEq, rc)); |
| 2438 return rc; | 2612 return rc; |
| 2439 } | 2613 } |
| 2440 | 2614 |
| 2441 /* | 2615 /* |
| 2442 ** Return True if it is possible that pIndex might be useful in | 2616 ** Return True if it is possible that pIndex might be useful in |
| 2443 ** implementing the ORDER BY clause in pBuilder. | 2617 ** implementing the ORDER BY clause in pBuilder. |
| 2444 ** | 2618 ** |
| 2445 ** Return False if pBuilder does not contain an ORDER BY clause or | 2619 ** Return False if pBuilder does not contain an ORDER BY clause or |
| 2446 ** if there is no way for pIndex to be useful in implementing that | 2620 ** if there is no way for pIndex to be useful in implementing that |
| 2447 ** ORDER BY clause. | 2621 ** ORDER BY clause. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2510 && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) | 2684 && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) |
| 2511 ){ | 2685 ){ |
| 2512 return 1; | 2686 return 1; |
| 2513 } | 2687 } |
| 2514 } | 2688 } |
| 2515 return 0; | 2689 return 0; |
| 2516 } | 2690 } |
| 2517 | 2691 |
| 2518 /* | 2692 /* |
| 2519 ** Add all WhereLoop objects for a single table of the join where the table | 2693 ** Add all WhereLoop objects for a single table of the join where the table |
| 2520 ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be | 2694 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be |
| 2521 ** a b-tree table, not a virtual table. | 2695 ** a b-tree table, not a virtual table. |
| 2522 ** | 2696 ** |
| 2523 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function | 2697 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function |
| 2524 ** are calculated as follows: | 2698 ** are calculated as follows: |
| 2525 ** | 2699 ** |
| 2526 ** For a full scan, assuming the table (or index) contains nRow rows: | 2700 ** For a full scan, assuming the table (or index) contains nRow rows: |
| 2527 ** | 2701 ** |
| 2528 ** cost = nRow * 3.0 // full-table scan | 2702 ** cost = nRow * 3.0 // full-table scan |
| 2529 ** cost = nRow * K // scan of covering index | 2703 ** cost = nRow * K // scan of covering index |
| 2530 ** cost = nRow * (K+3.0) // scan of non-covering index | 2704 ** cost = nRow * (K+3.0) // scan of non-covering index |
| (...skipping 15 matching lines...) Expand all Loading... |
| 2546 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount | 2720 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount |
| 2547 ** of uncertainty. For this reason, scoring is designed to pick plans that | 2721 ** of uncertainty. For this reason, scoring is designed to pick plans that |
| 2548 ** "do the least harm" if the estimates are inaccurate. For example, a | 2722 ** "do the least harm" if the estimates are inaccurate. For example, a |
| 2549 ** log(nRow) factor is omitted from a non-covering index scan in order to | 2723 ** log(nRow) factor is omitted from a non-covering index scan in order to |
| 2550 ** bias the scoring in favor of using an index, since the worst-case | 2724 ** bias the scoring in favor of using an index, since the worst-case |
| 2551 ** performance of using an index is far better than the worst-case performance | 2725 ** performance of using an index is far better than the worst-case performance |
| 2552 ** of a full table scan. | 2726 ** of a full table scan. |
| 2553 */ | 2727 */ |
| 2554 static int whereLoopAddBtree( | 2728 static int whereLoopAddBtree( |
| 2555 WhereLoopBuilder *pBuilder, /* WHERE clause information */ | 2729 WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 2556 Bitmask mExtra /* Extra prerequesites for using this table */ | 2730 Bitmask mPrereq /* Extra prerequesites for using this table */ |
| 2557 ){ | 2731 ){ |
| 2558 WhereInfo *pWInfo; /* WHERE analysis context */ | 2732 WhereInfo *pWInfo; /* WHERE analysis context */ |
| 2559 Index *pProbe; /* An index we are evaluating */ | 2733 Index *pProbe; /* An index we are evaluating */ |
| 2560 Index sPk; /* A fake index object for the primary key */ | 2734 Index sPk; /* A fake index object for the primary key */ |
| 2561 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ | 2735 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ |
| 2562 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ | 2736 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 2563 SrcList *pTabList; /* The FROM clause */ | 2737 SrcList *pTabList; /* The FROM clause */ |
| 2564 struct SrcList_item *pSrc; /* The FROM clause btree term to add */ | 2738 struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
| 2565 WhereLoop *pNew; /* Template WhereLoop object */ | 2739 WhereLoop *pNew; /* Template WhereLoop object */ |
| 2566 int rc = SQLITE_OK; /* Return code */ | 2740 int rc = SQLITE_OK; /* Return code */ |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2607 sPk.pNext = pFirst; | 2781 sPk.pNext = pFirst; |
| 2608 } | 2782 } |
| 2609 pProbe = &sPk; | 2783 pProbe = &sPk; |
| 2610 } | 2784 } |
| 2611 rSize = pTab->nRowLogEst; | 2785 rSize = pTab->nRowLogEst; |
| 2612 rLogSize = estLog(rSize); | 2786 rLogSize = estLog(rSize); |
| 2613 | 2787 |
| 2614 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX | 2788 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 2615 /* Automatic indexes */ | 2789 /* Automatic indexes */ |
| 2616 if( !pBuilder->pOrSet /* Not part of an OR optimization */ | 2790 if( !pBuilder->pOrSet /* Not part of an OR optimization */ |
| 2617 && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 | 2791 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 2618 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 | 2792 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 2619 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */ | 2793 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */ |
| 2620 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */ | 2794 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */ |
| 2621 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */ | 2795 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */ |
| 2622 && !pSrc->fg.isCorrelated /* Not a correlated subquery */ | 2796 && !pSrc->fg.isCorrelated /* Not a correlated subquery */ |
| 2623 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */ | 2797 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */ |
| 2624 ){ | 2798 ){ |
| 2625 /* Generate auto-index WhereLoops */ | 2799 /* Generate auto-index WhereLoops */ |
| 2626 WhereTerm *pTerm; | 2800 WhereTerm *pTerm; |
| 2627 WhereTerm *pWCEnd = pWC->a + pWC->nTerm; | 2801 WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2639 ** tables or 1.375 (LogEst=4) for views and subqueries. The value | 2813 ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 2640 ** of X is smaller for views and subqueries so that the query planner | 2814 ** of X is smaller for views and subqueries so that the query planner |
| 2641 ** will be more aggressive about generating automatic indexes for | 2815 ** will be more aggressive about generating automatic indexes for |
| 2642 ** those objects, since there is no opportunity to add schema | 2816 ** those objects, since there is no opportunity to add schema |
| 2643 ** indexes on subqueries and views. */ | 2817 ** indexes on subqueries and views. */ |
| 2644 pNew->rSetup = rLogSize + rSize + 4; | 2818 pNew->rSetup = rLogSize + rSize + 4; |
| 2645 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ | 2819 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 2646 pNew->rSetup += 24; | 2820 pNew->rSetup += 24; |
| 2647 } | 2821 } |
| 2648 ApplyCostMultiplier(pNew->rSetup, pTab->costMult); | 2822 ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
| 2823 if( pNew->rSetup<0 ) pNew->rSetup = 0; |
| 2649 /* TUNING: Each index lookup yields 20 rows in the table. This | 2824 /* TUNING: Each index lookup yields 20 rows in the table. This |
| 2650 ** is more than the usual guess of 10 rows, since we have no way | 2825 ** is more than the usual guess of 10 rows, since we have no way |
| 2651 ** of knowing how selective the index will ultimately be. It would | 2826 ** of knowing how selective the index will ultimately be. It would |
| 2652 ** not be unreasonable to make this value much larger. */ | 2827 ** not be unreasonable to make this value much larger. */ |
| 2653 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); | 2828 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
| 2654 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); | 2829 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
| 2655 pNew->wsFlags = WHERE_AUTO_INDEX; | 2830 pNew->wsFlags = WHERE_AUTO_INDEX; |
| 2656 pNew->prereq = mExtra | pTerm->prereqRight; | 2831 pNew->prereq = mPrereq | pTerm->prereqRight; |
| 2657 rc = whereLoopInsert(pBuilder, pNew); | 2832 rc = whereLoopInsert(pBuilder, pNew); |
| 2658 } | 2833 } |
| 2659 } | 2834 } |
| 2660 } | 2835 } |
| 2661 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ | 2836 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 2662 | 2837 |
| 2663 /* Loop over all indices | 2838 /* Loop over all indices |
| 2664 */ | 2839 */ |
| 2665 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ | 2840 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
| 2666 if( pProbe->pPartIdxWhere!=0 | 2841 if( pProbe->pPartIdxWhere!=0 |
| 2667 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ | 2842 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ |
| 2668 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ | 2843 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 2669 continue; /* Partial index inappropriate for this query */ | 2844 continue; /* Partial index inappropriate for this query */ |
| 2670 } | 2845 } |
| 2671 rSize = pProbe->aiRowLogEst[0]; | 2846 rSize = pProbe->aiRowLogEst[0]; |
| 2672 pNew->u.btree.nEq = 0; | 2847 pNew->u.btree.nEq = 0; |
| 2848 pNew->u.btree.nBtm = 0; |
| 2849 pNew->u.btree.nTop = 0; |
| 2673 pNew->nSkip = 0; | 2850 pNew->nSkip = 0; |
| 2674 pNew->nLTerm = 0; | 2851 pNew->nLTerm = 0; |
| 2675 pNew->iSortIdx = 0; | 2852 pNew->iSortIdx = 0; |
| 2676 pNew->rSetup = 0; | 2853 pNew->rSetup = 0; |
| 2677 pNew->prereq = mExtra; | 2854 pNew->prereq = mPrereq; |
| 2678 pNew->nOut = rSize; | 2855 pNew->nOut = rSize; |
| 2679 pNew->u.btree.pIndex = pProbe; | 2856 pNew->u.btree.pIndex = pProbe; |
| 2680 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); | 2857 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
| 2681 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ | 2858 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 2682 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); | 2859 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
| 2683 if( pProbe->tnum<=0 ){ | 2860 if( pProbe->tnum<=0 ){ |
| 2684 /* Integer primary key index */ | 2861 /* Integer primary key index */ |
| 2685 pNew->wsFlags = WHERE_IPK; | 2862 pNew->wsFlags = WHERE_IPK; |
| 2686 | 2863 |
| 2687 /* Full table scan */ | 2864 /* Full table scan */ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2699 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; | 2876 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 2700 m = 0; | 2877 m = 0; |
| 2701 }else{ | 2878 }else{ |
| 2702 m = pSrc->colUsed & ~columnsInIndex(pProbe); | 2879 m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 2703 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; | 2880 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 2704 } | 2881 } |
| 2705 | 2882 |
| 2706 /* Full scan via index */ | 2883 /* Full scan via index */ |
| 2707 if( b | 2884 if( b |
| 2708 || !HasRowid(pTab) | 2885 || !HasRowid(pTab) |
| 2886 || pProbe->pPartIdxWhere!=0 |
| 2709 || ( m==0 | 2887 || ( m==0 |
| 2710 && pProbe->bUnordered==0 | 2888 && pProbe->bUnordered==0 |
| 2711 && (pProbe->szIdxRow<pTab->szTabRow) | 2889 && (pProbe->szIdxRow<pTab->szTabRow) |
| 2712 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 | 2890 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 2713 && sqlite3GlobalConfig.bUseCis | 2891 && sqlite3GlobalConfig.bUseCis |
| 2714 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) | 2892 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 2715 ) | 2893 ) |
| 2716 ){ | 2894 ){ |
| 2717 pNew->iSortIdx = b ? iSortIdx : 0; | 2895 pNew->iSortIdx = b ? iSortIdx : 0; |
| 2718 | 2896 |
| 2719 /* The cost of visiting the index rows is N*K, where K is | 2897 /* The cost of visiting the index rows is N*K, where K is |
| 2720 ** between 1.1 and 3.0, depending on the relative sizes of the | 2898 ** between 1.1 and 3.0, depending on the relative sizes of the |
| 2721 ** index and table rows. If this is a non-covering index scan, | 2899 ** index and table rows. */ |
| 2722 ** also add the cost of visiting table rows (N*3.0). */ | |
| 2723 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; | 2900 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; |
| 2724 if( m!=0 ){ | 2901 if( m!=0 ){ |
| 2725 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); | 2902 /* If this is a non-covering index scan, add in the cost of |
| 2903 ** doing table lookups. The cost will be 3x the number of |
| 2904 ** lookups. Take into account WHERE clause terms that can be |
| 2905 ** satisfied using just the index, and that do not require a |
| 2906 ** table lookup. */ |
| 2907 LogEst nLookup = rSize + 16; /* Base cost: N*3 */ |
| 2908 int ii; |
| 2909 int iCur = pSrc->iCursor; |
| 2910 WhereClause *pWC2 = &pWInfo->sWC; |
| 2911 for(ii=0; ii<pWC2->nTerm; ii++){ |
| 2912 WhereTerm *pTerm = &pWC2->a[ii]; |
| 2913 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){ |
| 2914 break; |
| 2915 } |
| 2916 /* pTerm can be evaluated using just the index. So reduce |
| 2917 ** the expected number of table lookups accordingly */ |
| 2918 if( pTerm->truthProb<=0 ){ |
| 2919 nLookup += pTerm->truthProb; |
| 2920 }else{ |
| 2921 nLookup--; |
| 2922 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19; |
| 2923 } |
| 2924 } |
| 2925 |
| 2926 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup); |
| 2726 } | 2927 } |
| 2727 ApplyCostMultiplier(pNew->rRun, pTab->costMult); | 2928 ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
| 2728 whereLoopOutputAdjust(pWC, pNew, rSize); | 2929 whereLoopOutputAdjust(pWC, pNew, rSize); |
| 2729 rc = whereLoopInsert(pBuilder, pNew); | 2930 rc = whereLoopInsert(pBuilder, pNew); |
| 2730 pNew->nOut = rSize; | 2931 pNew->nOut = rSize; |
| 2731 if( rc ) break; | 2932 if( rc ) break; |
| 2732 } | 2933 } |
| 2733 } | 2934 } |
| 2734 | 2935 |
| 2735 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); | 2936 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
| 2736 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | 2937 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 2737 sqlite3Stat4ProbeFree(pBuilder->pRec); | 2938 sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 2738 pBuilder->nRecValid = 0; | 2939 pBuilder->nRecValid = 0; |
| 2739 pBuilder->pRec = 0; | 2940 pBuilder->pRec = 0; |
| 2740 #endif | 2941 #endif |
| 2741 | 2942 |
| 2742 /* If there was an INDEXED BY clause, then only that one index is | 2943 /* If there was an INDEXED BY clause, then only that one index is |
| 2743 ** considered. */ | 2944 ** considered. */ |
| 2744 if( pSrc->pIBIndex ) break; | 2945 if( pSrc->pIBIndex ) break; |
| 2745 } | 2946 } |
| 2746 return rc; | 2947 return rc; |
| 2747 } | 2948 } |
| 2748 | 2949 |
| 2749 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2950 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2951 |
| 2952 /* |
| 2953 ** Argument pIdxInfo is already populated with all constraints that may |
| 2954 ** be used by the virtual table identified by pBuilder->pNew->iTab. This |
| 2955 ** function marks a subset of those constraints usable, invokes the |
| 2956 ** xBestIndex method and adds the returned plan to pBuilder. |
| 2957 ** |
| 2958 ** A constraint is marked usable if: |
| 2959 ** |
| 2960 ** * Argument mUsable indicates that its prerequisites are available, and |
| 2961 ** |
| 2962 ** * It is not one of the operators specified in the mExclude mask passed |
| 2963 ** as the fourth argument (which in practice is either WO_IN or 0). |
| 2964 ** |
| 2965 ** Argument mPrereq is a mask of tables that must be scanned before the |
| 2966 ** virtual table in question. These are added to the plans prerequisites |
| 2967 ** before it is added to pBuilder. |
| 2968 ** |
| 2969 ** Output parameter *pbIn is set to true if the plan added to pBuilder |
| 2970 ** uses one or more WO_IN terms, or false otherwise. |
| 2971 */ |
| 2972 static int whereLoopAddVirtualOne( |
| 2973 WhereLoopBuilder *pBuilder, |
| 2974 Bitmask mPrereq, /* Mask of tables that must be used. */ |
| 2975 Bitmask mUsable, /* Mask of usable tables */ |
| 2976 u16 mExclude, /* Exclude terms using these operators */ |
| 2977 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ |
| 2978 u16 mNoOmit, /* Do not omit these constraints */ |
| 2979 int *pbIn /* OUT: True if plan uses an IN(...) op */ |
| 2980 ){ |
| 2981 WhereClause *pWC = pBuilder->pWC; |
| 2982 struct sqlite3_index_constraint *pIdxCons; |
| 2983 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; |
| 2984 int i; |
| 2985 int mxTerm; |
| 2986 int rc = SQLITE_OK; |
| 2987 WhereLoop *pNew = pBuilder->pNew; |
| 2988 Parse *pParse = pBuilder->pWInfo->pParse; |
| 2989 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab]; |
| 2990 int nConstraint = pIdxInfo->nConstraint; |
| 2991 |
| 2992 assert( (mUsable & mPrereq)==mPrereq ); |
| 2993 *pbIn = 0; |
| 2994 pNew->prereq = mPrereq; |
| 2995 |
| 2996 /* Set the usable flag on the subset of constraints identified by |
| 2997 ** arguments mUsable and mExclude. */ |
| 2998 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 2999 for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 3000 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset]; |
| 3001 pIdxCons->usable = 0; |
| 3002 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight |
| 3003 && (pTerm->eOperator & mExclude)==0 |
| 3004 ){ |
| 3005 pIdxCons->usable = 1; |
| 3006 } |
| 3007 } |
| 3008 |
| 3009 /* Initialize the output fields of the sqlite3_index_info structure */ |
| 3010 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint); |
| 3011 assert( pIdxInfo->needToFreeIdxStr==0 ); |
| 3012 pIdxInfo->idxStr = 0; |
| 3013 pIdxInfo->idxNum = 0; |
| 3014 pIdxInfo->orderByConsumed = 0; |
| 3015 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 3016 pIdxInfo->estimatedRows = 25; |
| 3017 pIdxInfo->idxFlags = 0; |
| 3018 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; |
| 3019 |
| 3020 /* Invoke the virtual table xBestIndex() method */ |
| 3021 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); |
| 3022 if( rc ) return rc; |
| 3023 |
| 3024 mxTerm = -1; |
| 3025 assert( pNew->nLSlot>=nConstraint ); |
| 3026 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 3027 pNew->u.vtab.omitMask = 0; |
| 3028 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 3029 for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 3030 int iTerm; |
| 3031 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 3032 WhereTerm *pTerm; |
| 3033 int j = pIdxCons->iTermOffset; |
| 3034 if( iTerm>=nConstraint |
| 3035 || j<0 |
| 3036 || j>=pWC->nTerm |
| 3037 || pNew->aLTerm[iTerm]!=0 |
| 3038 || pIdxCons->usable==0 |
| 3039 ){ |
| 3040 rc = SQLITE_ERROR; |
| 3041 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); |
| 3042 return rc; |
| 3043 } |
| 3044 testcase( iTerm==nConstraint-1 ); |
| 3045 testcase( j==0 ); |
| 3046 testcase( j==pWC->nTerm-1 ); |
| 3047 pTerm = &pWC->a[j]; |
| 3048 pNew->prereq |= pTerm->prereqRight; |
| 3049 assert( iTerm<pNew->nLSlot ); |
| 3050 pNew->aLTerm[iTerm] = pTerm; |
| 3051 if( iTerm>mxTerm ) mxTerm = iTerm; |
| 3052 testcase( iTerm==15 ); |
| 3053 testcase( iTerm==16 ); |
| 3054 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
| 3055 if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 3056 /* A virtual table that is constrained by an IN clause may not |
| 3057 ** consume the ORDER BY clause because (1) the order of IN terms |
| 3058 ** is not necessarily related to the order of output terms and |
| 3059 ** (2) Multiple outputs from a single IN value will not merge |
| 3060 ** together. */ |
| 3061 pIdxInfo->orderByConsumed = 0; |
| 3062 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; |
| 3063 *pbIn = 1; assert( (mExclude & WO_IN)==0 ); |
| 3064 } |
| 3065 } |
| 3066 } |
| 3067 pNew->u.vtab.omitMask &= ~mNoOmit; |
| 3068 |
| 3069 pNew->nLTerm = mxTerm+1; |
| 3070 assert( pNew->nLTerm<=pNew->nLSlot ); |
| 3071 pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 3072 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 3073 pIdxInfo->needToFreeIdxStr = 0; |
| 3074 pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
| 3075 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? |
| 3076 pIdxInfo->nOrderBy : 0); |
| 3077 pNew->rSetup = 0; |
| 3078 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
| 3079 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); |
| 3080 |
| 3081 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated |
| 3082 ** that the scan will visit at most one row. Clear it otherwise. */ |
| 3083 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){ |
| 3084 pNew->wsFlags |= WHERE_ONEROW; |
| 3085 }else{ |
| 3086 pNew->wsFlags &= ~WHERE_ONEROW; |
| 3087 } |
| 3088 rc = whereLoopInsert(pBuilder, pNew); |
| 3089 if( pNew->u.vtab.needFree ){ |
| 3090 sqlite3_free(pNew->u.vtab.idxStr); |
| 3091 pNew->u.vtab.needFree = 0; |
| 3092 } |
| 3093 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n", |
| 3094 *pbIn, (sqlite3_uint64)mPrereq, |
| 3095 (sqlite3_uint64)(pNew->prereq & ~mPrereq))); |
| 3096 |
| 3097 return rc; |
| 3098 } |
| 3099 |
| 3100 |
| 2750 /* | 3101 /* |
| 2751 ** Add all WhereLoop objects for a table of the join identified by | 3102 ** Add all WhereLoop objects for a table of the join identified by |
| 2752 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. | 3103 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 2753 ** | 3104 ** |
| 2754 ** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and | 3105 ** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and |
| 2755 ** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause | 3106 ** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause |
| 2756 ** entries that occur before the virtual table in the FROM clause and are | 3107 ** entries that occur before the virtual table in the FROM clause and are |
| 2757 ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the | 3108 ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the |
| 2758 ** mUnusable mask contains all FROM clause entries that occur after the | 3109 ** mUnusable mask contains all FROM clause entries that occur after the |
| 2759 ** virtual table and are separated from it by at least one LEFT or | 3110 ** virtual table and are separated from it by at least one LEFT or |
| 2760 ** CROSS JOIN. | 3111 ** CROSS JOIN. |
| 2761 ** | 3112 ** |
| 2762 ** For example, if the query were: | 3113 ** For example, if the query were: |
| 2763 ** | 3114 ** |
| 2764 ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; | 3115 ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; |
| 2765 ** | 3116 ** |
| 2766 ** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6). | 3117 ** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6). |
| 2767 ** | 3118 ** |
| 2768 ** All the tables in mExtra must be scanned before the current virtual | 3119 ** All the tables in mPrereq must be scanned before the current virtual |
| 2769 ** table. So any terms for which all prerequisites are satisfied by | 3120 ** table. So any terms for which all prerequisites are satisfied by |
| 2770 ** mExtra may be specified as "usable" in all calls to xBestIndex. | 3121 ** mPrereq may be specified as "usable" in all calls to xBestIndex. |
| 2771 ** Conversely, all tables in mUnusable must be scanned after the current | 3122 ** Conversely, all tables in mUnusable must be scanned after the current |
| 2772 ** virtual table, so any terms for which the prerequisites overlap with | 3123 ** virtual table, so any terms for which the prerequisites overlap with |
| 2773 ** mUnusable should always be configured as "not-usable" for xBestIndex. | 3124 ** mUnusable should always be configured as "not-usable" for xBestIndex. |
| 2774 */ | 3125 */ |
| 2775 static int whereLoopAddVirtual( | 3126 static int whereLoopAddVirtual( |
| 2776 WhereLoopBuilder *pBuilder, /* WHERE clause information */ | 3127 WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 2777 Bitmask mExtra, /* Tables that must be scanned before this one */ | 3128 Bitmask mPrereq, /* Tables that must be scanned before this one */ |
| 2778 Bitmask mUnusable /* Tables that must be scanned after this one */ | 3129 Bitmask mUnusable /* Tables that must be scanned after this one */ |
| 2779 ){ | 3130 ){ |
| 3131 int rc = SQLITE_OK; /* Return code */ |
| 2780 WhereInfo *pWInfo; /* WHERE analysis context */ | 3132 WhereInfo *pWInfo; /* WHERE analysis context */ |
| 2781 Parse *pParse; /* The parsing context */ | 3133 Parse *pParse; /* The parsing context */ |
| 2782 WhereClause *pWC; /* The WHERE clause */ | 3134 WhereClause *pWC; /* The WHERE clause */ |
| 2783 struct SrcList_item *pSrc; /* The FROM clause term to search */ | 3135 struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 2784 Table *pTab; | 3136 sqlite3_index_info *p; /* Object to pass to xBestIndex() */ |
| 2785 sqlite3 *db; | 3137 int nConstraint; /* Number of constraints in p */ |
| 2786 sqlite3_index_info *pIdxInfo; | 3138 int bIn; /* True if plan uses IN(...) operator */ |
| 2787 struct sqlite3_index_constraint *pIdxCons; | |
| 2788 struct sqlite3_index_constraint_usage *pUsage; | |
| 2789 WhereTerm *pTerm; | |
| 2790 int i, j; | |
| 2791 int iTerm, mxTerm; | |
| 2792 int nConstraint; | |
| 2793 int seenIn = 0; /* True if an IN operator is seen */ | |
| 2794 int seenVar = 0; /* True if a non-constant constraint is seen */ | |
| 2795 int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ | |
| 2796 WhereLoop *pNew; | 3139 WhereLoop *pNew; |
| 2797 int rc = SQLITE_OK; | 3140 Bitmask mBest; /* Tables used by best possible plan */ |
| 3141 u16 mNoOmit; |
| 2798 | 3142 |
| 2799 assert( (mExtra & mUnusable)==0 ); | 3143 assert( (mPrereq & mUnusable)==0 ); |
| 2800 pWInfo = pBuilder->pWInfo; | 3144 pWInfo = pBuilder->pWInfo; |
| 2801 pParse = pWInfo->pParse; | 3145 pParse = pWInfo->pParse; |
| 2802 db = pParse->db; | |
| 2803 pWC = pBuilder->pWC; | 3146 pWC = pBuilder->pWC; |
| 2804 pNew = pBuilder->pNew; | 3147 pNew = pBuilder->pNew; |
| 2805 pSrc = &pWInfo->pTabList->a[pNew->iTab]; | 3148 pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
| 2806 pTab = pSrc->pTab; | 3149 assert( IsVirtual(pSrc->pTab) ); |
| 2807 assert( IsVirtual(pTab) ); | 3150 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy, |
| 2808 pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy); | 3151 &mNoOmit); |
| 2809 if( pIdxInfo==0 ) return SQLITE_NOMEM; | 3152 if( p==0 ) return SQLITE_NOMEM_BKPT; |
| 2810 pNew->prereq = 0; | |
| 2811 pNew->rSetup = 0; | 3153 pNew->rSetup = 0; |
| 2812 pNew->wsFlags = WHERE_VIRTUALTABLE; | 3154 pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 2813 pNew->nLTerm = 0; | 3155 pNew->nLTerm = 0; |
| 2814 pNew->u.vtab.needFree = 0; | 3156 pNew->u.vtab.needFree = 0; |
| 2815 pUsage = pIdxInfo->aConstraintUsage; | 3157 nConstraint = p->nConstraint; |
| 2816 nConstraint = pIdxInfo->nConstraint; | 3158 if( whereLoopResize(pParse->db, pNew, nConstraint) ){ |
| 2817 if( whereLoopResize(db, pNew, nConstraint) ){ | 3159 sqlite3DbFree(pParse->db, p); |
| 2818 sqlite3DbFree(db, pIdxInfo); | 3160 return SQLITE_NOMEM_BKPT; |
| 2819 return SQLITE_NOMEM; | |
| 2820 } | 3161 } |
| 2821 | 3162 |
| 2822 for(iPhase=0; iPhase<=3; iPhase++){ | 3163 /* First call xBestIndex() with all constraints usable. */ |
| 2823 if( !seenIn && (iPhase&1)!=0 ){ | 3164 WHERETRACE(0x40, (" VirtualOne: all usable\n")); |
| 2824 iPhase++; | 3165 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn); |
| 2825 if( iPhase>3 ) break; | 3166 |
| 2826 } | 3167 /* If the call to xBestIndex() with all terms enabled produced a plan |
| 2827 if( !seenVar && iPhase>1 ) break; | 3168 ** that does not require any source tables (IOW: a plan with mBest==0), |
| 2828 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; | 3169 ** then there is no point in making any further calls to xBestIndex() |
| 2829 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ | 3170 ** since they will all return the same result (if the xBestIndex() |
| 2830 j = pIdxCons->iTermOffset; | 3171 ** implementation is sane). */ |
| 2831 pTerm = &pWC->a[j]; | 3172 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){ |
| 2832 switch( iPhase ){ | 3173 int seenZero = 0; /* True if a plan with no prereqs seen */ |
| 2833 case 0: /* Constants without IN operator */ | 3174 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */ |
| 2834 pIdxCons->usable = 0; | 3175 Bitmask mPrev = 0; |
| 2835 if( (pTerm->eOperator & WO_IN)!=0 ){ | 3176 Bitmask mBestNoIn = 0; |
| 2836 seenIn = 1; | 3177 |
| 2837 } | 3178 /* If the plan produced by the earlier call uses an IN(...) term, call |
| 2838 if( (pTerm->prereqRight & ~mExtra)!=0 ){ | 3179 ** xBestIndex again, this time with IN(...) terms disabled. */ |
| 2839 seenVar = 1; | 3180 if( bIn ){ |
| 2840 }else if( (pTerm->eOperator & WO_IN)==0 ){ | 3181 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); |
| 2841 pIdxCons->usable = 1; | 3182 rc = whereLoopAddVirtualOne( |
| 2842 } | 3183 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn); |
| 2843 break; | 3184 assert( bIn==0 ); |
| 2844 case 1: /* Constants with IN operators */ | 3185 mBestNoIn = pNew->prereq & ~mPrereq; |
| 2845 assert( seenIn ); | 3186 if( mBestNoIn==0 ){ |
| 2846 pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0; | 3187 seenZero = 1; |
| 2847 break; | 3188 seenZeroNoIN = 1; |
| 2848 case 2: /* Variables without IN */ | |
| 2849 assert( seenVar ); | |
| 2850 pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; | |
| 2851 break; | |
| 2852 default: /* Variables with IN */ | |
| 2853 assert( seenVar && seenIn ); | |
| 2854 pIdxCons->usable = 1; | |
| 2855 break; | |
| 2856 } | 3189 } |
| 2857 } | 3190 } |
| 2858 memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); | 3191 |
| 2859 if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); | 3192 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) |
| 2860 pIdxInfo->idxStr = 0; | 3193 ** in the set of terms that apply to the current virtual table. */ |
| 2861 pIdxInfo->idxNum = 0; | 3194 while( rc==SQLITE_OK ){ |
| 2862 pIdxInfo->needToFreeIdxStr = 0; | 3195 int i; |
| 2863 pIdxInfo->orderByConsumed = 0; | 3196 Bitmask mNext = ALLBITS; |
| 2864 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; | 3197 assert( mNext>0 ); |
| 2865 pIdxInfo->estimatedRows = 25; | 3198 for(i=0; i<nConstraint; i++){ |
| 2866 pIdxInfo->idxFlags = 0; | 3199 Bitmask mThis = ( |
| 2867 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; | 3200 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq |
| 2868 rc = vtabBestIndex(pParse, pTab, pIdxInfo); | 3201 ); |
| 2869 if( rc ) goto whereLoopAddVtab_exit; | 3202 if( mThis>mPrev && mThis<mNext ) mNext = mThis; |
| 2870 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; | 3203 } |
| 2871 pNew->prereq = mExtra; | 3204 mPrev = mNext; |
| 2872 mxTerm = -1; | 3205 if( mNext==ALLBITS ) break; |
| 2873 assert( pNew->nLSlot>=nConstraint ); | 3206 if( mNext==mBest || mNext==mBestNoIn ) continue; |
| 2874 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; | 3207 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", |
| 2875 pNew->u.vtab.omitMask = 0; | 3208 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); |
| 2876 for(i=0; i<nConstraint; i++, pIdxCons++){ | 3209 rc = whereLoopAddVirtualOne( |
| 2877 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ | 3210 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn); |
| 2878 j = pIdxCons->iTermOffset; | 3211 if( pNew->prereq==mPrereq ){ |
| 2879 if( iTerm>=nConstraint | 3212 seenZero = 1; |
| 2880 || j<0 | 3213 if( bIn==0 ) seenZeroNoIN = 1; |
| 2881 || j>=pWC->nTerm | |
| 2882 || pNew->aLTerm[iTerm]!=0 | |
| 2883 ){ | |
| 2884 rc = SQLITE_ERROR; | |
| 2885 sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); | |
| 2886 goto whereLoopAddVtab_exit; | |
| 2887 } | |
| 2888 testcase( iTerm==nConstraint-1 ); | |
| 2889 testcase( j==0 ); | |
| 2890 testcase( j==pWC->nTerm-1 ); | |
| 2891 pTerm = &pWC->a[j]; | |
| 2892 pNew->prereq |= pTerm->prereqRight; | |
| 2893 assert( iTerm<pNew->nLSlot ); | |
| 2894 pNew->aLTerm[iTerm] = pTerm; | |
| 2895 if( iTerm>mxTerm ) mxTerm = iTerm; | |
| 2896 testcase( iTerm==15 ); | |
| 2897 testcase( iTerm==16 ); | |
| 2898 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; | |
| 2899 if( (pTerm->eOperator & WO_IN)!=0 ){ | |
| 2900 if( pUsage[i].omit==0 ){ | |
| 2901 /* Do not attempt to use an IN constraint if the virtual table | |
| 2902 ** says that the equivalent EQ constraint cannot be safely omitted. | |
| 2903 ** If we do attempt to use such a constraint, some rows might be | |
| 2904 ** repeated in the output. */ | |
| 2905 break; | |
| 2906 } | |
| 2907 /* A virtual table that is constrained by an IN clause may not | |
| 2908 ** consume the ORDER BY clause because (1) the order of IN terms | |
| 2909 ** is not necessarily related to the order of output terms and | |
| 2910 ** (2) Multiple outputs from a single IN value will not merge | |
| 2911 ** together. */ | |
| 2912 pIdxInfo->orderByConsumed = 0; | |
| 2913 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; | |
| 2914 } | |
| 2915 } | 3214 } |
| 2916 } | 3215 } |
| 2917 if( i>=nConstraint ){ | |
| 2918 pNew->nLTerm = mxTerm+1; | |
| 2919 assert( pNew->nLTerm<=pNew->nLSlot ); | |
| 2920 pNew->u.vtab.idxNum = pIdxInfo->idxNum; | |
| 2921 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; | |
| 2922 pIdxInfo->needToFreeIdxStr = 0; | |
| 2923 pNew->u.vtab.idxStr = pIdxInfo->idxStr; | |
| 2924 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? | |
| 2925 pIdxInfo->nOrderBy : 0); | |
| 2926 pNew->rSetup = 0; | |
| 2927 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); | |
| 2928 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); | |
| 2929 | 3216 |
| 2930 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated | 3217 /* If the calls to xBestIndex() in the above loop did not find a plan |
| 2931 ** that the scan will visit at most one row. Clear it otherwise. */ | 3218 ** that requires no source tables at all (i.e. one guaranteed to be |
| 2932 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){ | 3219 ** usable), make a call here with all source tables disabled */ |
| 2933 pNew->wsFlags |= WHERE_ONEROW; | 3220 if( rc==SQLITE_OK && seenZero==0 ){ |
| 2934 }else{ | 3221 WHERETRACE(0x40, (" VirtualOne: all disabled\n")); |
| 2935 pNew->wsFlags &= ~WHERE_ONEROW; | 3222 rc = whereLoopAddVirtualOne( |
| 2936 } | 3223 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn); |
| 2937 whereLoopInsert(pBuilder, pNew); | 3224 if( bIn==0 ) seenZeroNoIN = 1; |
| 2938 if( pNew->u.vtab.needFree ){ | |
| 2939 sqlite3_free(pNew->u.vtab.idxStr); | |
| 2940 pNew->u.vtab.needFree = 0; | |
| 2941 } | |
| 2942 } | 3225 } |
| 2943 } | |
| 2944 | 3226 |
| 2945 whereLoopAddVtab_exit: | 3227 /* If the calls to xBestIndex() have so far failed to find a plan |
| 2946 if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); | 3228 ** that requires no source tables at all and does not use an IN(...) |
| 2947 sqlite3DbFree(db, pIdxInfo); | 3229 ** operator, make a final call to obtain one here. */ |
| 3230 if( rc==SQLITE_OK && seenZeroNoIN==0 ){ |
| 3231 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); |
| 3232 rc = whereLoopAddVirtualOne( |
| 3233 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn); |
| 3234 } |
| 3235 } |
| 3236 |
| 3237 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); |
| 3238 sqlite3DbFree(pParse->db, p); |
| 2948 return rc; | 3239 return rc; |
| 2949 } | 3240 } |
| 2950 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 3241 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2951 | 3242 |
| 2952 /* | 3243 /* |
| 2953 ** Add WhereLoop entries to handle OR terms. This works for either | 3244 ** Add WhereLoop entries to handle OR terms. This works for either |
| 2954 ** btrees or virtual tables. | 3245 ** btrees or virtual tables. |
| 2955 */ | 3246 */ |
| 2956 static int whereLoopAddOr( | 3247 static int whereLoopAddOr( |
| 2957 WhereLoopBuilder *pBuilder, | 3248 WhereLoopBuilder *pBuilder, |
| 2958 Bitmask mExtra, | 3249 Bitmask mPrereq, |
| 2959 Bitmask mUnusable | 3250 Bitmask mUnusable |
| 2960 ){ | 3251 ){ |
| 2961 WhereInfo *pWInfo = pBuilder->pWInfo; | 3252 WhereInfo *pWInfo = pBuilder->pWInfo; |
| 2962 WhereClause *pWC; | 3253 WhereClause *pWC; |
| 2963 WhereLoop *pNew; | 3254 WhereLoop *pNew; |
| 2964 WhereTerm *pTerm, *pWCEnd; | 3255 WhereTerm *pTerm, *pWCEnd; |
| 2965 int rc = SQLITE_OK; | 3256 int rc = SQLITE_OK; |
| 2966 int iCur; | 3257 int iCur; |
| 2967 WhereClause tempWC; | 3258 WhereClause tempWC; |
| 2968 WhereLoopBuilder sSubBuild; | 3259 WhereLoopBuilder sSubBuild; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3002 tempWC.a = pOrTerm; | 3293 tempWC.a = pOrTerm; |
| 3003 sSubBuild.pWC = &tempWC; | 3294 sSubBuild.pWC = &tempWC; |
| 3004 }else{ | 3295 }else{ |
| 3005 continue; | 3296 continue; |
| 3006 } | 3297 } |
| 3007 sCur.n = 0; | 3298 sCur.n = 0; |
| 3008 #ifdef WHERETRACE_ENABLED | 3299 #ifdef WHERETRACE_ENABLED |
| 3009 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", | 3300 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 3010 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); | 3301 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 3011 if( sqlite3WhereTrace & 0x400 ){ | 3302 if( sqlite3WhereTrace & 0x400 ){ |
| 3012 for(i=0; i<sSubBuild.pWC->nTerm; i++){ | 3303 sqlite3WhereClausePrint(sSubBuild.pWC); |
| 3013 whereTermPrint(&sSubBuild.pWC->a[i], i); | |
| 3014 } | |
| 3015 } | 3304 } |
| 3016 #endif | 3305 #endif |
| 3017 #ifndef SQLITE_OMIT_VIRTUALTABLE | 3306 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3018 if( IsVirtual(pItem->pTab) ){ | 3307 if( IsVirtual(pItem->pTab) ){ |
| 3019 rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable); | 3308 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable); |
| 3020 }else | 3309 }else |
| 3021 #endif | 3310 #endif |
| 3022 { | 3311 { |
| 3023 rc = whereLoopAddBtree(&sSubBuild, mExtra); | 3312 rc = whereLoopAddBtree(&sSubBuild, mPrereq); |
| 3024 } | 3313 } |
| 3025 if( rc==SQLITE_OK ){ | 3314 if( rc==SQLITE_OK ){ |
| 3026 rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable); | 3315 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable); |
| 3027 } | 3316 } |
| 3028 assert( rc==SQLITE_OK || sCur.n==0 ); | 3317 assert( rc==SQLITE_OK || sCur.n==0 ); |
| 3029 if( sCur.n==0 ){ | 3318 if( sCur.n==0 ){ |
| 3030 sSum.n = 0; | 3319 sSum.n = 0; |
| 3031 break; | 3320 break; |
| 3032 }else if( once ){ | 3321 }else if( once ){ |
| 3033 whereOrMove(&sSum, &sCur); | 3322 whereOrMove(&sSum, &sCur); |
| 3034 once = 0; | 3323 once = 0; |
| 3035 }else{ | 3324 }else{ |
| 3036 WhereOrSet sPrev; | 3325 WhereOrSet sPrev; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3073 } | 3362 } |
| 3074 } | 3363 } |
| 3075 return rc; | 3364 return rc; |
| 3076 } | 3365 } |
| 3077 | 3366 |
| 3078 /* | 3367 /* |
| 3079 ** Add all WhereLoop objects for all tables | 3368 ** Add all WhereLoop objects for all tables |
| 3080 */ | 3369 */ |
| 3081 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ | 3370 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
| 3082 WhereInfo *pWInfo = pBuilder->pWInfo; | 3371 WhereInfo *pWInfo = pBuilder->pWInfo; |
| 3083 Bitmask mExtra = 0; | 3372 Bitmask mPrereq = 0; |
| 3084 Bitmask mPrior = 0; | 3373 Bitmask mPrior = 0; |
| 3085 int iTab; | 3374 int iTab; |
| 3086 SrcList *pTabList = pWInfo->pTabList; | 3375 SrcList *pTabList = pWInfo->pTabList; |
| 3087 struct SrcList_item *pItem; | 3376 struct SrcList_item *pItem; |
| 3088 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; | 3377 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; |
| 3089 sqlite3 *db = pWInfo->pParse->db; | 3378 sqlite3 *db = pWInfo->pParse->db; |
| 3090 int rc = SQLITE_OK; | 3379 int rc = SQLITE_OK; |
| 3091 WhereLoop *pNew; | 3380 WhereLoop *pNew; |
| 3092 u8 priorJointype = 0; | 3381 u8 priorJointype = 0; |
| 3093 | 3382 |
| 3094 /* Loop over the tables in the join, from left to right */ | 3383 /* Loop over the tables in the join, from left to right */ |
| 3095 pNew = pBuilder->pNew; | 3384 pNew = pBuilder->pNew; |
| 3096 whereLoopInit(pNew); | 3385 whereLoopInit(pNew); |
| 3097 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ | 3386 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ |
| 3098 Bitmask mUnusable = 0; | 3387 Bitmask mUnusable = 0; |
| 3099 pNew->iTab = iTab; | 3388 pNew->iTab = iTab; |
| 3100 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); | 3389 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 3101 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ | 3390 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 3102 /* This condition is true when pItem is the FROM clause term on the | 3391 /* This condition is true when pItem is the FROM clause term on the |
| 3103 ** right-hand-side of a LEFT or CROSS JOIN. */ | 3392 ** right-hand-side of a LEFT or CROSS JOIN. */ |
| 3104 mExtra = mPrior; | 3393 mPrereq = mPrior; |
| 3105 } | 3394 } |
| 3106 priorJointype = pItem->fg.jointype; | 3395 priorJointype = pItem->fg.jointype; |
| 3396 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3107 if( IsVirtual(pItem->pTab) ){ | 3397 if( IsVirtual(pItem->pTab) ){ |
| 3108 struct SrcList_item *p; | 3398 struct SrcList_item *p; |
| 3109 for(p=&pItem[1]; p<pEnd; p++){ | 3399 for(p=&pItem[1]; p<pEnd; p++){ |
| 3110 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){ | 3400 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){ |
| 3111 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); | 3401 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); |
| 3112 } | 3402 } |
| 3113 } | 3403 } |
| 3114 rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable); | 3404 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable); |
| 3115 }else{ | 3405 }else |
| 3116 rc = whereLoopAddBtree(pBuilder, mExtra); | 3406 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3407 { |
| 3408 rc = whereLoopAddBtree(pBuilder, mPrereq); |
| 3117 } | 3409 } |
| 3118 if( rc==SQLITE_OK ){ | 3410 if( rc==SQLITE_OK ){ |
| 3119 rc = whereLoopAddOr(pBuilder, mExtra, mUnusable); | 3411 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); |
| 3120 } | 3412 } |
| 3121 mPrior |= pNew->maskSelf; | 3413 mPrior |= pNew->maskSelf; |
| 3122 if( rc || db->mallocFailed ) break; | 3414 if( rc || db->mallocFailed ) break; |
| 3123 } | 3415 } |
| 3124 | 3416 |
| 3125 whereLoopClear(db, pNew); | 3417 whereLoopClear(db, pNew); |
| 3126 return rc; | 3418 return rc; |
| 3127 } | 3419 } |
| 3128 | 3420 |
| 3129 /* | 3421 /* |
| (...skipping 10 matching lines...) Expand all Loading... |
| 3140 ** equivalent rows appear immediately adjacent to one another. GROUP BY | 3432 ** equivalent rows appear immediately adjacent to one another. GROUP BY |
| 3141 ** and DISTINCT do not require rows to appear in any particular order as long | 3433 ** and DISTINCT do not require rows to appear in any particular order as long |
| 3142 ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT | 3434 ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT |
| 3143 ** the pOrderBy terms can be matched in any order. With ORDER BY, the | 3435 ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 3144 ** pOrderBy terms must be matched in strict left-to-right order. | 3436 ** pOrderBy terms must be matched in strict left-to-right order. |
| 3145 */ | 3437 */ |
| 3146 static i8 wherePathSatisfiesOrderBy( | 3438 static i8 wherePathSatisfiesOrderBy( |
| 3147 WhereInfo *pWInfo, /* The WHERE clause */ | 3439 WhereInfo *pWInfo, /* The WHERE clause */ |
| 3148 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ | 3440 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
| 3149 WherePath *pPath, /* The WherePath to check */ | 3441 WherePath *pPath, /* The WherePath to check */ |
| 3150 u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ | 3442 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */ |
| 3151 u16 nLoop, /* Number of entries in pPath->aLoop[] */ | 3443 u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
| 3152 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ | 3444 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
| 3153 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ | 3445 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
| 3154 ){ | 3446 ){ |
| 3155 u8 revSet; /* True if rev is known */ | 3447 u8 revSet; /* True if rev is known */ |
| 3156 u8 rev; /* Composite sort order */ | 3448 u8 rev; /* Composite sort order */ |
| 3157 u8 revIdx; /* Index sort order */ | 3449 u8 revIdx; /* Index sort order */ |
| 3158 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ | 3450 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 3159 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ | 3451 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 3160 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ | 3452 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
| 3453 u16 eqOpMask; /* Allowed equality operators */ |
| 3161 u16 nKeyCol; /* Number of key columns in pIndex */ | 3454 u16 nKeyCol; /* Number of key columns in pIndex */ |
| 3162 u16 nColumn; /* Total number of ordered columns in the index */ | 3455 u16 nColumn; /* Total number of ordered columns in the index */ |
| 3163 u16 nOrderBy; /* Number terms in the ORDER BY clause */ | 3456 u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 3164 int iLoop; /* Index of WhereLoop in pPath being processed */ | 3457 int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 3165 int i, j; /* Loop counters */ | 3458 int i, j; /* Loop counters */ |
| 3166 int iCur; /* Cursor number for current WhereLoop */ | 3459 int iCur; /* Cursor number for current WhereLoop */ |
| 3167 int iColumn; /* A column number within table iCur */ | 3460 int iColumn; /* A column number within table iCur */ |
| 3168 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ | 3461 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
| 3169 WhereTerm *pTerm; /* A single term of the WHERE clause */ | 3462 WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 3170 Expr *pOBExpr; /* An expression from the ORDER BY clause */ | 3463 Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| (...skipping 30 matching lines...) Expand all Loading... |
| 3201 assert( pOrderBy!=0 ); | 3494 assert( pOrderBy!=0 ); |
| 3202 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; | 3495 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
| 3203 | 3496 |
| 3204 nOrderBy = pOrderBy->nExpr; | 3497 nOrderBy = pOrderBy->nExpr; |
| 3205 testcase( nOrderBy==BMS-1 ); | 3498 testcase( nOrderBy==BMS-1 ); |
| 3206 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ | 3499 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 3207 isOrderDistinct = 1; | 3500 isOrderDistinct = 1; |
| 3208 obDone = MASKBIT(nOrderBy)-1; | 3501 obDone = MASKBIT(nOrderBy)-1; |
| 3209 orderDistinctMask = 0; | 3502 orderDistinctMask = 0; |
| 3210 ready = 0; | 3503 ready = 0; |
| 3504 eqOpMask = WO_EQ | WO_IS | WO_ISNULL; |
| 3505 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN; |
| 3211 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ | 3506 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
| 3212 if( iLoop>0 ) ready |= pLoop->maskSelf; | 3507 if( iLoop>0 ) ready |= pLoop->maskSelf; |
| 3213 pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; | 3508 if( iLoop<nLoop ){ |
| 3509 pLoop = pPath->aLoop[iLoop]; |
| 3510 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue; |
| 3511 }else{ |
| 3512 pLoop = pLast; |
| 3513 } |
| 3214 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ | 3514 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 3215 if( pLoop->u.vtab.isOrdered ) obSat = obDone; | 3515 if( pLoop->u.vtab.isOrdered ) obSat = obDone; |
| 3216 break; | 3516 break; |
| 3217 } | 3517 } |
| 3218 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; | 3518 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
| 3219 | 3519 |
| 3220 /* Mark off any ORDER BY term X that is a column in the table of | 3520 /* Mark off any ORDER BY term X that is a column in the table of |
| 3221 ** the current loop for which there is term in the WHERE | 3521 ** the current loop for which there is term in the WHERE |
| 3222 ** clause of the form X IS NULL or X=? that reference only outer | 3522 ** clause of the form X IS NULL or X=? that reference only outer |
| 3223 ** loops. | 3523 ** loops. |
| 3224 */ | 3524 */ |
| 3225 for(i=0; i<nOrderBy; i++){ | 3525 for(i=0; i<nOrderBy; i++){ |
| 3226 if( MASKBIT(i) & obSat ) continue; | 3526 if( MASKBIT(i) & obSat ) continue; |
| 3227 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); | 3527 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 3228 if( pOBExpr->op!=TK_COLUMN ) continue; | 3528 if( pOBExpr->op!=TK_COLUMN ) continue; |
| 3229 if( pOBExpr->iTable!=iCur ) continue; | 3529 if( pOBExpr->iTable!=iCur ) continue; |
| 3230 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, | 3530 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 3231 ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); | 3531 ~ready, eqOpMask, 0); |
| 3232 if( pTerm==0 ) continue; | 3532 if( pTerm==0 ) continue; |
| 3533 if( pTerm->eOperator==WO_IN ){ |
| 3534 /* IN terms are only valid for sorting in the ORDER BY LIMIT |
| 3535 ** optimization, and then only if they are actually used |
| 3536 ** by the query plan */ |
| 3537 assert( wctrlFlags & WHERE_ORDERBY_LIMIT ); |
| 3538 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){} |
| 3539 if( j>=pLoop->nLTerm ) continue; |
| 3540 } |
| 3233 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ | 3541 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
| 3234 const char *z1, *z2; | 3542 const char *z1, *z2; |
| 3235 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); | 3543 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 3236 if( !pColl ) pColl = db->pDfltColl; | 3544 if( !pColl ) pColl = db->pDfltColl; |
| 3237 z1 = pColl->zName; | 3545 z1 = pColl->zName; |
| 3238 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); | 3546 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 3239 if( !pColl ) pColl = db->pDfltColl; | 3547 if( !pColl ) pColl = db->pDfltColl; |
| 3240 z2 = pColl->zName; | 3548 z2 = pColl->zName; |
| 3241 if( sqlite3StrICmp(z1, z2)!=0 ) continue; | 3549 if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 3242 testcase( pTerm->pExpr->op==TK_IS ); | 3550 testcase( pTerm->pExpr->op==TK_IS ); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 3259 || !HasRowid(pIndex->pTable)); | 3567 || !HasRowid(pIndex->pTable)); |
| 3260 isOrderDistinct = IsUniqueIndex(pIndex); | 3568 isOrderDistinct = IsUniqueIndex(pIndex); |
| 3261 } | 3569 } |
| 3262 | 3570 |
| 3263 /* Loop through all columns of the index and deal with the ones | 3571 /* Loop through all columns of the index and deal with the ones |
| 3264 ** that are not constrained by == or IN. | 3572 ** that are not constrained by == or IN. |
| 3265 */ | 3573 */ |
| 3266 rev = revSet = 0; | 3574 rev = revSet = 0; |
| 3267 distinctColumns = 0; | 3575 distinctColumns = 0; |
| 3268 for(j=0; j<nColumn; j++){ | 3576 for(j=0; j<nColumn; j++){ |
| 3269 u8 bOnce; /* True to run the ORDER BY search loop */ | 3577 u8 bOnce = 1; /* True to run the ORDER BY search loop */ |
| 3270 | 3578 |
| 3271 /* Skip over == and IS NULL terms */ | 3579 assert( j>=pLoop->u.btree.nEq |
| 3272 if( j<pLoop->u.btree.nEq | 3580 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip) |
| 3273 && pLoop->nSkip==0 | 3581 ); |
| 3274 && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0 | 3582 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){ |
| 3275 ){ | 3583 u16 eOp = pLoop->aLTerm[j]->eOperator; |
| 3276 if( i & WO_ISNULL ){ | 3584 |
| 3277 testcase( isOrderDistinct ); | 3585 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when |
| 3278 isOrderDistinct = 0; | 3586 ** doing WHERE_ORDERBY_LIMIT processing). |
| 3587 ** |
| 3588 ** If the current term is a column of an ((?,?) IN (SELECT...)) |
| 3589 ** expression for which the SELECT returns more than one column, |
| 3590 ** check that it is the only column used by this loop. Otherwise, |
| 3591 ** if it is one of two or more, none of the columns can be |
| 3592 ** considered to match an ORDER BY term. */ |
| 3593 if( (eOp & eqOpMask)!=0 ){ |
| 3594 if( eOp & WO_ISNULL ){ |
| 3595 testcase( isOrderDistinct ); |
| 3596 isOrderDistinct = 0; |
| 3597 } |
| 3598 continue; |
| 3599 }else if( ALWAYS(eOp & WO_IN) ){ |
| 3600 /* ALWAYS() justification: eOp is an equality operator due to the |
| 3601 ** j<pLoop->u.btree.nEq constraint above. Any equality other |
| 3602 ** than WO_IN is captured by the previous "if". So this one |
| 3603 ** always has to be WO_IN. */ |
| 3604 Expr *pX = pLoop->aLTerm[j]->pExpr; |
| 3605 for(i=j+1; i<pLoop->u.btree.nEq; i++){ |
| 3606 if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 3607 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) ); |
| 3608 bOnce = 0; |
| 3609 break; |
| 3610 } |
| 3611 } |
| 3279 } | 3612 } |
| 3280 continue; | |
| 3281 } | 3613 } |
| 3282 | 3614 |
| 3283 /* Get the column number in the table (iColumn) and sort order | 3615 /* Get the column number in the table (iColumn) and sort order |
| 3284 ** (revIdx) for the j-th column of the index. | 3616 ** (revIdx) for the j-th column of the index. |
| 3285 */ | 3617 */ |
| 3286 if( pIndex ){ | 3618 if( pIndex ){ |
| 3287 iColumn = pIndex->aiColumn[j]; | 3619 iColumn = pIndex->aiColumn[j]; |
| 3288 revIdx = pIndex->aSortOrder[j]; | 3620 revIdx = pIndex->aSortOrder[j]; |
| 3289 if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; | 3621 if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
| 3290 }else{ | 3622 }else{ |
| 3291 iColumn = XN_ROWID; | 3623 iColumn = XN_ROWID; |
| 3292 revIdx = 0; | 3624 revIdx = 0; |
| 3293 } | 3625 } |
| 3294 | 3626 |
| 3295 /* An unconstrained column that might be NULL means that this | 3627 /* An unconstrained column that might be NULL means that this |
| 3296 ** WhereLoop is not well-ordered | 3628 ** WhereLoop is not well-ordered |
| 3297 */ | 3629 */ |
| 3298 if( isOrderDistinct | 3630 if( isOrderDistinct |
| 3299 && iColumn>=0 | 3631 && iColumn>=0 |
| 3300 && j>=pLoop->u.btree.nEq | 3632 && j>=pLoop->u.btree.nEq |
| 3301 && pIndex->pTable->aCol[iColumn].notNull==0 | 3633 && pIndex->pTable->aCol[iColumn].notNull==0 |
| 3302 ){ | 3634 ){ |
| 3303 isOrderDistinct = 0; | 3635 isOrderDistinct = 0; |
| 3304 } | 3636 } |
| 3305 | 3637 |
| 3306 /* Find the ORDER BY term that corresponds to the j-th column | 3638 /* Find the ORDER BY term that corresponds to the j-th column |
| 3307 ** of the index and mark that ORDER BY term off | 3639 ** of the index and mark that ORDER BY term off |
| 3308 */ | 3640 */ |
| 3309 bOnce = 1; | |
| 3310 isMatch = 0; | 3641 isMatch = 0; |
| 3311 for(i=0; bOnce && i<nOrderBy; i++){ | 3642 for(i=0; bOnce && i<nOrderBy; i++){ |
| 3312 if( MASKBIT(i) & obSat ) continue; | 3643 if( MASKBIT(i) & obSat ) continue; |
| 3313 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); | 3644 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 3314 testcase( wctrlFlags & WHERE_GROUPBY ); | 3645 testcase( wctrlFlags & WHERE_GROUPBY ); |
| 3315 testcase( wctrlFlags & WHERE_DISTINCTBY ); | 3646 testcase( wctrlFlags & WHERE_DISTINCTBY ); |
| 3316 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; | 3647 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
| 3317 if( iColumn>=(-1) ){ | 3648 if( iColumn>=(-1) ){ |
| 3318 if( pOBExpr->op!=TK_COLUMN ) continue; | 3649 if( pOBExpr->op!=TK_COLUMN ) continue; |
| 3319 if( pOBExpr->iTable!=iCur ) continue; | 3650 if( pOBExpr->iTable!=iCur ) continue; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 3336 ** Sort order is irrelevant for a GROUP BY clause. */ | 3667 ** Sort order is irrelevant for a GROUP BY clause. */ |
| 3337 if( revSet ){ | 3668 if( revSet ){ |
| 3338 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; | 3669 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 3339 }else{ | 3670 }else{ |
| 3340 rev = revIdx ^ pOrderBy->a[i].sortOrder; | 3671 rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 3341 if( rev ) *pRevMask |= MASKBIT(iLoop); | 3672 if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 3342 revSet = 1; | 3673 revSet = 1; |
| 3343 } | 3674 } |
| 3344 } | 3675 } |
| 3345 if( isMatch ){ | 3676 if( isMatch ){ |
| 3346 if( iColumn<0 ){ | 3677 if( iColumn==XN_ROWID ){ |
| 3347 testcase( distinctColumns==0 ); | 3678 testcase( distinctColumns==0 ); |
| 3348 distinctColumns = 1; | 3679 distinctColumns = 1; |
| 3349 } | 3680 } |
| 3350 obSat |= MASKBIT(i); | 3681 obSat |= MASKBIT(i); |
| 3351 }else{ | 3682 }else{ |
| 3352 /* No match found */ | 3683 /* No match found */ |
| 3353 if( j==0 || j<nKeyCol ){ | 3684 if( j==0 || j<nKeyCol ){ |
| 3354 testcase( isOrderDistinct!=0 ); | 3685 testcase( isOrderDistinct!=0 ); |
| 3355 isOrderDistinct = 0; | 3686 isOrderDistinct = 0; |
| 3356 } | 3687 } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3452 ** terms are out of order, then block-sorting will reduce the | 3783 ** terms are out of order, then block-sorting will reduce the |
| 3453 ** sorting cost to: | 3784 ** sorting cost to: |
| 3454 ** | 3785 ** |
| 3455 ** cost = (3.0 * N * log(N)) * (Y/X) | 3786 ** cost = (3.0 * N * log(N)) * (Y/X) |
| 3456 ** | 3787 ** |
| 3457 ** The (Y/X) term is implemented using stack variable rScale | 3788 ** The (Y/X) term is implemented using stack variable rScale |
| 3458 ** below. */ | 3789 ** below. */ |
| 3459 LogEst rScale, rSortCost; | 3790 LogEst rScale, rSortCost; |
| 3460 assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); | 3791 assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); |
| 3461 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; | 3792 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; |
| 3462 rSortCost = nRow + estLog(nRow) + rScale + 16; | 3793 rSortCost = nRow + rScale + 16; |
| 3463 | 3794 |
| 3464 /* TUNING: The cost of implementing DISTINCT using a B-TREE is | 3795 /* Multiple by log(M) where M is the number of output rows. |
| 3465 ** similar but with a larger constant of proportionality. | 3796 ** Use the LIMIT for M if it is smaller */ |
| 3466 ** Multiply by an additional factor of 3.0. */ | 3797 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){ |
| 3467 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ | 3798 nRow = pWInfo->iLimit; |
| 3468 rSortCost += 16; | |
| 3469 } | 3799 } |
| 3470 | 3800 rSortCost += estLog(nRow); |
| 3471 return rSortCost; | 3801 return rSortCost; |
| 3472 } | 3802 } |
| 3473 | 3803 |
| 3474 /* | 3804 /* |
| 3475 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine | 3805 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
| 3476 ** attempts to find the lowest cost path that visits each WhereLoop | 3806 ** attempts to find the lowest cost path that visits each WhereLoop |
| 3477 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. | 3807 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 3478 ** | 3808 ** |
| 3479 ** Assume that the total number of output rows that will need to be sorted | 3809 ** Assume that the total number of output rows that will need to be sorted |
| 3480 ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting | 3810 ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3522 ** nRowEst parameter. */ | 3852 ** nRowEst parameter. */ |
| 3523 if( pWInfo->pOrderBy==0 || nRowEst==0 ){ | 3853 if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 3524 nOrderBy = 0; | 3854 nOrderBy = 0; |
| 3525 }else{ | 3855 }else{ |
| 3526 nOrderBy = pWInfo->pOrderBy->nExpr; | 3856 nOrderBy = pWInfo->pOrderBy->nExpr; |
| 3527 } | 3857 } |
| 3528 | 3858 |
| 3529 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ | 3859 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ |
| 3530 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; | 3860 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 3531 nSpace += sizeof(LogEst) * nOrderBy; | 3861 nSpace += sizeof(LogEst) * nOrderBy; |
| 3532 pSpace = sqlite3DbMallocRaw(db, nSpace); | 3862 pSpace = sqlite3DbMallocRawNN(db, nSpace); |
| 3533 if( pSpace==0 ) return SQLITE_NOMEM; | 3863 if( pSpace==0 ) return SQLITE_NOMEM_BKPT; |
| 3534 aTo = (WherePath*)pSpace; | 3864 aTo = (WherePath*)pSpace; |
| 3535 aFrom = aTo+mxChoice; | 3865 aFrom = aTo+mxChoice; |
| 3536 memset(aFrom, 0, sizeof(aFrom[0])); | 3866 memset(aFrom, 0, sizeof(aFrom[0])); |
| 3537 pX = (WhereLoop**)(aFrom+mxChoice); | 3867 pX = (WhereLoop**)(aFrom+mxChoice); |
| 3538 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ | 3868 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
| 3539 pFrom->aLoop = pX; | 3869 pFrom->aLoop = pX; |
| 3540 } | 3870 } |
| 3541 if( nOrderBy ){ | 3871 if( nOrderBy ){ |
| 3542 /* If there is an ORDER BY clause and it is not being ignored, set up | 3872 /* If there is an ORDER BY clause and it is not being ignored, set up |
| 3543 ** space for the aSortCost[] array. Each element of the aSortCost array | 3873 ** space for the aSortCost[] array. Each element of the aSortCost array |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3578 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ | 3908 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 3579 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ | 3909 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ |
| 3580 LogEst rCost; /* Cost of path (pFrom+pWLoop) */ | 3910 LogEst rCost; /* Cost of path (pFrom+pWLoop) */ |
| 3581 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ | 3911 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ |
| 3582 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ | 3912 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 3583 Bitmask maskNew; /* Mask of src visited by (..) */ | 3913 Bitmask maskNew; /* Mask of src visited by (..) */ |
| 3584 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ | 3914 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 3585 | 3915 |
| 3586 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; | 3916 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 3587 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; | 3917 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
| 3918 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){ |
| 3919 /* Do not use an automatic index if the this loop is expected |
| 3920 ** to run less than 2 times. */ |
| 3921 assert( 10==sqlite3LogEst(2) ); |
| 3922 continue; |
| 3923 } |
| 3588 /* At this point, pWLoop is a candidate to be the next loop. | 3924 /* At this point, pWLoop is a candidate to be the next loop. |
| 3589 ** Compute its cost */ | 3925 ** Compute its cost */ |
| 3590 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); | 3926 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 3591 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); | 3927 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
| 3592 nOut = pFrom->nRow + pWLoop->nOut; | 3928 nOut = pFrom->nRow + pWLoop->nOut; |
| 3593 maskNew = pFrom->maskLoop | pWLoop->maskSelf; | 3929 maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
| 3594 if( isOrdered<0 ){ | 3930 if( isOrdered<0 ){ |
| 3595 isOrdered = wherePathSatisfiesOrderBy(pWInfo, | 3931 isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
| 3596 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, | 3932 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
| 3597 iLoop, pWLoop, &revMask); | 3933 iLoop, pWLoop, &revMask); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3770 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; | 4106 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
| 3771 pLevel->iFrom = pWLoop->iTab; | 4107 pLevel->iFrom = pWLoop->iTab; |
| 3772 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; | 4108 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
| 3773 } | 4109 } |
| 3774 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 | 4110 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 3775 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 | 4111 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 3776 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP | 4112 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
| 3777 && nRowEst | 4113 && nRowEst |
| 3778 ){ | 4114 ){ |
| 3779 Bitmask notUsed; | 4115 Bitmask notUsed; |
| 3780 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, | 4116 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinctSet, pFrom, |
| 3781 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); | 4117 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
| 3782 if( rc==pWInfo->pResultSet->nExpr ){ | 4118 if( rc==pWInfo->pDistinctSet->nExpr ){ |
| 3783 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; | 4119 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 3784 } | 4120 } |
| 3785 } | 4121 } |
| 3786 if( pWInfo->pOrderBy ){ | 4122 if( pWInfo->pOrderBy ){ |
| 3787 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ | 4123 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 3788 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ | 4124 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 3789 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; | 4125 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 3790 } | 4126 } |
| 3791 }else{ | 4127 }else{ |
| 3792 pWInfo->nOBSat = pFrom->isOrdered; | 4128 pWInfo->nOBSat = pFrom->isOrdered; |
| 3793 if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; | |
| 3794 pWInfo->revMask = pFrom->revLoop; | 4129 pWInfo->revMask = pFrom->revLoop; |
| 4130 if( pWInfo->nOBSat<=0 ){ |
| 4131 pWInfo->nOBSat = 0; |
| 4132 if( nLoop>0 ){ |
| 4133 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags; |
| 4134 if( (wsFlags & WHERE_ONEROW)==0 |
| 4135 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN) |
| 4136 ){ |
| 4137 Bitmask m = 0; |
| 4138 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom, |
| 4139 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m); |
| 4140 testcase( wsFlags & WHERE_IPK ); |
| 4141 testcase( wsFlags & WHERE_COLUMN_IN ); |
| 4142 if( rc==pWInfo->pOrderBy->nExpr ){ |
| 4143 pWInfo->bOrderedInnerLoop = 1; |
| 4144 pWInfo->revMask = m; |
| 4145 } |
| 4146 } |
| 4147 } |
| 4148 } |
| 3795 } | 4149 } |
| 3796 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) | 4150 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 3797 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 | 4151 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 |
| 3798 ){ | 4152 ){ |
| 3799 Bitmask revMask = 0; | 4153 Bitmask revMask = 0; |
| 3800 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, | 4154 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
| 3801 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask | 4155 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
| 3802 ); | 4156 ); |
| 3803 assert( pWInfo->sorted==0 ); | 4157 assert( pWInfo->sorted==0 ); |
| 3804 if( nOrder==pWInfo->pOrderBy->nExpr ){ | 4158 if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| (...skipping 25 matching lines...) Expand all Loading... |
| 3830 static int whereShortCut(WhereLoopBuilder *pBuilder){ | 4184 static int whereShortCut(WhereLoopBuilder *pBuilder){ |
| 3831 WhereInfo *pWInfo; | 4185 WhereInfo *pWInfo; |
| 3832 struct SrcList_item *pItem; | 4186 struct SrcList_item *pItem; |
| 3833 WhereClause *pWC; | 4187 WhereClause *pWC; |
| 3834 WhereTerm *pTerm; | 4188 WhereTerm *pTerm; |
| 3835 WhereLoop *pLoop; | 4189 WhereLoop *pLoop; |
| 3836 int iCur; | 4190 int iCur; |
| 3837 int j; | 4191 int j; |
| 3838 Table *pTab; | 4192 Table *pTab; |
| 3839 Index *pIdx; | 4193 Index *pIdx; |
| 3840 | 4194 |
| 3841 pWInfo = pBuilder->pWInfo; | 4195 pWInfo = pBuilder->pWInfo; |
| 3842 if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; | 4196 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0; |
| 3843 assert( pWInfo->pTabList->nSrc>=1 ); | 4197 assert( pWInfo->pTabList->nSrc>=1 ); |
| 3844 pItem = pWInfo->pTabList->a; | 4198 pItem = pWInfo->pTabList->a; |
| 3845 pTab = pItem->pTab; | 4199 pTab = pItem->pTab; |
| 3846 if( IsVirtual(pTab) ) return 0; | 4200 if( IsVirtual(pTab) ) return 0; |
| 3847 if( pItem->fg.isIndexedBy ) return 0; | 4201 if( pItem->fg.isIndexedBy ) return 0; |
| 3848 iCur = pItem->iCursor; | 4202 iCur = pItem->iCursor; |
| 3849 pWC = &pWInfo->sWC; | 4203 pWC = &pWInfo->sWC; |
| 3850 pLoop = pBuilder->pNew; | 4204 pLoop = pBuilder->pNew; |
| 3851 pLoop->wsFlags = 0; | 4205 pLoop->wsFlags = 0; |
| 3852 pLoop->nSkip = 0; | 4206 pLoop->nSkip = 0; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3979 ** end | 4333 ** end |
| 3980 ** | 4334 ** |
| 3981 ** ORDER BY CLAUSE PROCESSING | 4335 ** ORDER BY CLAUSE PROCESSING |
| 3982 ** | 4336 ** |
| 3983 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause | 4337 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 3984 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement | 4338 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
| 3985 ** if there is one. If there is no ORDER BY clause or if this routine | 4339 ** if there is one. If there is no ORDER BY clause or if this routine |
| 3986 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. | 4340 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
| 3987 ** | 4341 ** |
| 3988 ** The iIdxCur parameter is the cursor number of an index. If | 4342 ** The iIdxCur parameter is the cursor number of an index. If |
| 3989 ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index | 4343 ** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index |
| 3990 ** to use for OR clause processing. The WHERE clause should use this | 4344 ** to use for OR clause processing. The WHERE clause should use this |
| 3991 ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is | 4345 ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 3992 ** the first cursor in an array of cursors for all indices. iIdxCur should | 4346 ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 3993 ** be used to compute the appropriate cursor depending on which index is | 4347 ** be used to compute the appropriate cursor depending on which index is |
| 3994 ** used. | 4348 ** used. |
| 3995 */ | 4349 */ |
| 3996 WhereInfo *sqlite3WhereBegin( | 4350 WhereInfo *sqlite3WhereBegin( |
| 3997 Parse *pParse, /* The parser context */ | 4351 Parse *pParse, /* The parser context */ |
| 3998 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ | 4352 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
| 3999 Expr *pWhere, /* The WHERE clause */ | 4353 Expr *pWhere, /* The WHERE clause */ |
| 4000 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ | 4354 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
| 4001 ExprList *pResultSet, /* Result set of the query */ | 4355 ExprList *pDistinctSet, /* Try not to output two rows that duplicate these */ |
| 4002 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ | 4356 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ |
| 4003 int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ | 4357 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number |
| 4358 ** If WHERE_USE_LIMIT, then the limit amount */ |
| 4004 ){ | 4359 ){ |
| 4005 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ | 4360 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| 4006 int nTabList; /* Number of elements in pTabList */ | 4361 int nTabList; /* Number of elements in pTabList */ |
| 4007 WhereInfo *pWInfo; /* Will become the return value of this function */ | 4362 WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 4008 Vdbe *v = pParse->pVdbe; /* The virtual database engine */ | 4363 Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 4009 Bitmask notReady; /* Cursors that are not yet positioned */ | 4364 Bitmask notReady; /* Cursors that are not yet positioned */ |
| 4010 WhereLoopBuilder sWLB; /* The WhereLoop builder */ | 4365 WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
| 4011 WhereMaskSet *pMaskSet; /* The expression mask set */ | 4366 WhereMaskSet *pMaskSet; /* The expression mask set */ |
| 4012 WhereLevel *pLevel; /* A single level in pWInfo->a[] */ | 4367 WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
| 4013 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ | 4368 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
| 4014 int ii; /* Loop counter */ | 4369 int ii; /* Loop counter */ |
| 4015 sqlite3 *db; /* Database connection */ | 4370 sqlite3 *db; /* Database connection */ |
| 4016 int rc; /* Return code */ | 4371 int rc; /* Return code */ |
| 4017 u8 bFordelete = 0; | 4372 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */ |
| 4018 | 4373 |
| 4019 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || ( | 4374 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || ( |
| 4020 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 | 4375 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 4021 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 | 4376 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 4022 )); | 4377 )); |
| 4023 | 4378 |
| 4379 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */ |
| 4380 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 4381 || (wctrlFlags & WHERE_USE_LIMIT)==0 ); |
| 4382 |
| 4024 /* Variable initialization */ | 4383 /* Variable initialization */ |
| 4025 db = pParse->db; | 4384 db = pParse->db; |
| 4026 memset(&sWLB, 0, sizeof(sWLB)); | 4385 memset(&sWLB, 0, sizeof(sWLB)); |
| 4027 | 4386 |
| 4028 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ | 4387 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 4029 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); | 4388 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 4030 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; | 4389 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
| 4031 sWLB.pOrderBy = pOrderBy; | 4390 sWLB.pOrderBy = pOrderBy; |
| 4032 | 4391 |
| 4033 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via | 4392 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 4034 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ | 4393 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 4035 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ | 4394 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 4036 wctrlFlags &= ~WHERE_WANT_DISTINCT; | 4395 wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 4037 } | 4396 } |
| 4038 | 4397 |
| 4039 /* The number of tables in the FROM clause is limited by the number of | 4398 /* The number of tables in the FROM clause is limited by the number of |
| 4040 ** bits in a Bitmask | 4399 ** bits in a Bitmask |
| 4041 */ | 4400 */ |
| 4042 testcase( pTabList->nSrc==BMS ); | 4401 testcase( pTabList->nSrc==BMS ); |
| 4043 if( pTabList->nSrc>BMS ){ | 4402 if( pTabList->nSrc>BMS ){ |
| 4044 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); | 4403 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
| 4045 return 0; | 4404 return 0; |
| 4046 } | 4405 } |
| 4047 | 4406 |
| 4048 /* This function normally generates a nested loop for all tables in | 4407 /* This function normally generates a nested loop for all tables in |
| 4049 ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should | 4408 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should |
| 4050 ** only generate code for the first table in pTabList and assume that | 4409 ** only generate code for the first table in pTabList and assume that |
| 4051 ** any cursors associated with subsequent tables are uninitialized. | 4410 ** any cursors associated with subsequent tables are uninitialized. |
| 4052 */ | 4411 */ |
| 4053 nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; | 4412 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc; |
| 4054 | 4413 |
| 4055 /* Allocate and initialize the WhereInfo structure that will become the | 4414 /* Allocate and initialize the WhereInfo structure that will become the |
| 4056 ** return value. A single allocation is used to store the WhereInfo | 4415 ** return value. A single allocation is used to store the WhereInfo |
| 4057 ** struct, the contents of WhereInfo.a[], the WhereClause structure | 4416 ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 4058 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte | 4417 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 4059 ** field (type Bitmask) it must be aligned on an 8-byte boundary on | 4418 ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 4060 ** some architectures. Hence the ROUND8() below. | 4419 ** some architectures. Hence the ROUND8() below. |
| 4061 */ | 4420 */ |
| 4062 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); | 4421 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 4063 pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); | 4422 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop)); |
| 4064 if( db->mallocFailed ){ | 4423 if( db->mallocFailed ){ |
| 4065 sqlite3DbFree(db, pWInfo); | 4424 sqlite3DbFree(db, pWInfo); |
| 4066 pWInfo = 0; | 4425 pWInfo = 0; |
| 4067 goto whereBeginError; | 4426 goto whereBeginError; |
| 4068 } | 4427 } |
| 4069 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; | |
| 4070 pWInfo->nLevel = nTabList; | |
| 4071 pWInfo->pParse = pParse; | 4428 pWInfo->pParse = pParse; |
| 4072 pWInfo->pTabList = pTabList; | 4429 pWInfo->pTabList = pTabList; |
| 4073 pWInfo->pOrderBy = pOrderBy; | 4430 pWInfo->pOrderBy = pOrderBy; |
| 4074 pWInfo->pResultSet = pResultSet; | 4431 pWInfo->pDistinctSet = pDistinctSet; |
| 4432 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
| 4433 pWInfo->nLevel = nTabList; |
| 4075 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); | 4434 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); |
| 4076 pWInfo->wctrlFlags = wctrlFlags; | 4435 pWInfo->wctrlFlags = wctrlFlags; |
| 4436 pWInfo->iLimit = iAuxArg; |
| 4077 pWInfo->savedNQueryLoop = pParse->nQueryLoop; | 4437 pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 4438 memset(&pWInfo->nOBSat, 0, |
| 4439 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); |
| 4440 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); |
| 4078 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ | 4441 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ |
| 4079 pMaskSet = &pWInfo->sMaskSet; | 4442 pMaskSet = &pWInfo->sMaskSet; |
| 4080 sWLB.pWInfo = pWInfo; | 4443 sWLB.pWInfo = pWInfo; |
| 4081 sWLB.pWC = &pWInfo->sWC; | 4444 sWLB.pWC = &pWInfo->sWC; |
| 4082 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); | 4445 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 4083 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); | 4446 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
| 4084 whereLoopInit(sWLB.pNew); | 4447 whereLoopInit(sWLB.pNew); |
| 4085 #ifdef SQLITE_DEBUG | 4448 #ifdef SQLITE_DEBUG |
| 4086 sWLB.pNew->cId = '*'; | 4449 sWLB.pNew->cId = '*'; |
| 4087 #endif | 4450 #endif |
| (...skipping 30 matching lines...) Expand all Loading... |
| 4118 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. | 4481 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. |
| 4119 ** | 4482 ** |
| 4120 ** The rule of the previous sentence ensures thta if X is the bitmask for | 4483 ** The rule of the previous sentence ensures thta if X is the bitmask for |
| 4121 ** a table T, then X-1 is the bitmask for all other tables to the left of T. | 4484 ** a table T, then X-1 is the bitmask for all other tables to the left of T. |
| 4122 ** Knowing the bitmask for all tables to the left of a left join is | 4485 ** Knowing the bitmask for all tables to the left of a left join is |
| 4123 ** important. Ticket #3015. | 4486 ** important. Ticket #3015. |
| 4124 ** | 4487 ** |
| 4125 ** Note that bitmasks are created for all pTabList->nSrc tables in | 4488 ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 4126 ** pTabList, not just the first nTabList tables. nTabList is normally | 4489 ** pTabList, not just the first nTabList tables. nTabList is normally |
| 4127 ** equal to pTabList->nSrc but might be shortened to 1 if the | 4490 ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 4128 ** WHERE_ONETABLE_ONLY flag is set. | 4491 ** WHERE_OR_SUBCLAUSE flag is set. |
| 4129 */ | 4492 */ |
| 4130 for(ii=0; ii<pTabList->nSrc; ii++){ | 4493 for(ii=0; ii<pTabList->nSrc; ii++){ |
| 4131 createMask(pMaskSet, pTabList->a[ii].iCursor); | 4494 createMask(pMaskSet, pTabList->a[ii].iCursor); |
| 4132 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); | 4495 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); |
| 4133 } | 4496 } |
| 4134 #ifdef SQLITE_DEBUG | 4497 #ifdef SQLITE_DEBUG |
| 4135 for(ii=0; ii<pTabList->nSrc; ii++){ | 4498 for(ii=0; ii<pTabList->nSrc; ii++){ |
| 4136 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); | 4499 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); |
| 4137 assert( m==MASKBIT(ii) ); | 4500 assert( m==MASKBIT(ii) ); |
| 4138 } | 4501 } |
| 4139 #endif | 4502 #endif |
| 4140 | 4503 |
| 4141 /* Analyze all of the subexpressions. */ | 4504 /* Analyze all of the subexpressions. */ |
| 4142 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); | 4505 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 4143 if( db->mallocFailed ) goto whereBeginError; | 4506 if( db->mallocFailed ) goto whereBeginError; |
| 4144 | 4507 |
| 4145 if( wctrlFlags & WHERE_WANT_DISTINCT ){ | 4508 if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 4146 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ | 4509 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pDistinctSet) ){ |
| 4147 /* The DISTINCT marking is pointless. Ignore it. */ | 4510 /* The DISTINCT marking is pointless. Ignore it. */ |
| 4148 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | 4511 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 4149 }else if( pOrderBy==0 ){ | 4512 }else if( pOrderBy==0 ){ |
| 4150 /* Try to ORDER BY the result set to make distinct processing easier */ | 4513 /* Try to ORDER BY the result set to make distinct processing easier */ |
| 4151 pWInfo->wctrlFlags |= WHERE_DISTINCTBY; | 4514 pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
| 4152 pWInfo->pOrderBy = pResultSet; | 4515 pWInfo->pOrderBy = pDistinctSet; |
| 4153 } | 4516 } |
| 4154 } | 4517 } |
| 4155 | 4518 |
| 4156 /* Construct the WhereLoop objects */ | 4519 /* Construct the WhereLoop objects */ |
| 4157 WHERETRACE(0xffff,("*** Optimizer Start *** (wctrlFlags: 0x%x)\n", | |
| 4158 wctrlFlags)); | |
| 4159 #if defined(WHERETRACE_ENABLED) | 4520 #if defined(WHERETRACE_ENABLED) |
| 4521 if( sqlite3WhereTrace & 0xffff ){ |
| 4522 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags); |
| 4523 if( wctrlFlags & WHERE_USE_LIMIT ){ |
| 4524 sqlite3DebugPrintf(", limit: %d", iAuxArg); |
| 4525 } |
| 4526 sqlite3DebugPrintf(")\n"); |
| 4527 } |
| 4160 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ | 4528 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ |
| 4161 int i; | 4529 sqlite3WhereClausePrint(sWLB.pWC); |
| 4162 for(i=0; i<sWLB.pWC->nTerm; i++){ | |
| 4163 whereTermPrint(&sWLB.pWC->a[i], i); | |
| 4164 } | |
| 4165 } | 4530 } |
| 4166 #endif | 4531 #endif |
| 4167 | 4532 |
| 4168 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ | 4533 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 4169 rc = whereLoopAddAll(&sWLB); | 4534 rc = whereLoopAddAll(&sWLB); |
| 4170 if( rc ) goto whereBeginError; | 4535 if( rc ) goto whereBeginError; |
| 4171 | 4536 |
| 4172 #ifdef WHERETRACE_ENABLED | 4537 #ifdef WHERETRACE_ENABLED |
| 4173 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ | 4538 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ |
| 4174 WhereLoop *p; | 4539 WhereLoop *p; |
| 4175 int i; | 4540 int i; |
| 4176 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" | 4541 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 4177 "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; | 4542 "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 4178 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ | 4543 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 4179 p->cId = zLabel[i%sizeof(zLabel)]; | 4544 p->cId = zLabel[i%sizeof(zLabel)]; |
| 4180 whereLoopPrint(p, sWLB.pWC); | 4545 whereLoopPrint(p, sWLB.pWC); |
| 4181 } | 4546 } |
| 4182 } | 4547 } |
| 4183 #endif | 4548 #endif |
| 4184 | 4549 |
| 4185 wherePathSolver(pWInfo, 0); | 4550 wherePathSolver(pWInfo, 0); |
| 4186 if( db->mallocFailed ) goto whereBeginError; | 4551 if( db->mallocFailed ) goto whereBeginError; |
| 4187 if( pWInfo->pOrderBy ){ | 4552 if( pWInfo->pOrderBy ){ |
| 4188 wherePathSolver(pWInfo, pWInfo->nRowOut+1); | 4553 wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
| 4189 if( db->mallocFailed ) goto whereBeginError; | 4554 if( db->mallocFailed ) goto whereBeginError; |
| 4190 } | 4555 } |
| 4191 } | 4556 } |
| 4192 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ | 4557 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
| 4193 pWInfo->revMask = (Bitmask)(-1); | 4558 pWInfo->revMask = ALLBITS; |
| 4194 } | 4559 } |
| 4195 if( pParse->nErr || NEVER(db->mallocFailed) ){ | 4560 if( pParse->nErr || NEVER(db->mallocFailed) ){ |
| 4196 goto whereBeginError; | 4561 goto whereBeginError; |
| 4197 } | 4562 } |
| 4198 #ifdef WHERETRACE_ENABLED | 4563 #ifdef WHERETRACE_ENABLED |
| 4199 if( sqlite3WhereTrace ){ | 4564 if( sqlite3WhereTrace ){ |
| 4200 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); | 4565 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 4201 if( pWInfo->nOBSat>0 ){ | 4566 if( pWInfo->nOBSat>0 ){ |
| 4202 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); | 4567 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
| 4203 } | 4568 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 4216 } | 4581 } |
| 4217 } | 4582 } |
| 4218 sqlite3DebugPrintf("\n"); | 4583 sqlite3DebugPrintf("\n"); |
| 4219 for(ii=0; ii<pWInfo->nLevel; ii++){ | 4584 for(ii=0; ii<pWInfo->nLevel; ii++){ |
| 4220 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); | 4585 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
| 4221 } | 4586 } |
| 4222 } | 4587 } |
| 4223 #endif | 4588 #endif |
| 4224 /* Attempt to omit tables from the join that do not effect the result */ | 4589 /* Attempt to omit tables from the join that do not effect the result */ |
| 4225 if( pWInfo->nLevel>=2 | 4590 if( pWInfo->nLevel>=2 |
| 4226 && pResultSet!=0 | 4591 && pDistinctSet!=0 |
| 4227 && OptimizationEnabled(db, SQLITE_OmitNoopJoin) | 4592 && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 4228 ){ | 4593 ){ |
| 4229 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet); | 4594 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pDistinctSet); |
| 4230 if( sWLB.pOrderBy ){ | 4595 if( sWLB.pOrderBy ){ |
| 4231 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); | 4596 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); |
| 4232 } | 4597 } |
| 4233 while( pWInfo->nLevel>=2 ){ | 4598 while( pWInfo->nLevel>=2 ){ |
| 4234 WhereTerm *pTerm, *pEnd; | 4599 WhereTerm *pTerm, *pEnd; |
| 4235 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; | 4600 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
| 4236 if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break; | 4601 if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break; |
| 4237 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 | 4602 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 4238 && (pLoop->wsFlags & WHERE_ONEROW)==0 | 4603 && (pLoop->wsFlags & WHERE_ONEROW)==0 |
| 4239 ){ | 4604 ){ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 4252 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); | 4617 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 4253 pWInfo->nLevel--; | 4618 pWInfo->nLevel--; |
| 4254 nTabList--; | 4619 nTabList--; |
| 4255 } | 4620 } |
| 4256 } | 4621 } |
| 4257 WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); | 4622 WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
| 4258 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; | 4623 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
| 4259 | 4624 |
| 4260 /* If the caller is an UPDATE or DELETE statement that is requesting | 4625 /* If the caller is an UPDATE or DELETE statement that is requesting |
| 4261 ** to use a one-pass algorithm, determine if this is appropriate. | 4626 ** to use a one-pass algorithm, determine if this is appropriate. |
| 4262 ** The one-pass algorithm only works if the WHERE clause constrains | |
| 4263 ** the statement to update or delete a single row. | |
| 4264 */ | 4627 */ |
| 4265 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); | 4628 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 4266 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){ | 4629 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){ |
| 4267 int wsFlags = pWInfo->a[0].pWLoop->wsFlags; | 4630 int wsFlags = pWInfo->a[0].pWLoop->wsFlags; |
| 4268 int bOnerow = (wsFlags & WHERE_ONEROW)!=0; | 4631 int bOnerow = (wsFlags & WHERE_ONEROW)!=0; |
| 4269 if( bOnerow || ( (wctrlFlags & WHERE_ONEPASS_MULTIROW) | 4632 if( bOnerow |
| 4270 && 0==(wsFlags & WHERE_VIRTUALTABLE) | 4633 || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0 |
| 4271 )){ | 4634 && 0==(wsFlags & WHERE_VIRTUALTABLE)) |
| 4635 ){ |
| 4272 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI; | 4636 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI; |
| 4273 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){ | 4637 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){ |
| 4274 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){ | 4638 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){ |
| 4275 bFordelete = OPFLAG_FORDELETE; | 4639 bFordelete = OPFLAG_FORDELETE; |
| 4276 } | 4640 } |
| 4277 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY); | 4641 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY); |
| 4278 } | 4642 } |
| 4279 } | 4643 } |
| 4280 } | 4644 } |
| 4281 | 4645 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 4297 #ifndef SQLITE_OMIT_VIRTUALTABLE | 4661 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4298 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ | 4662 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 4299 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); | 4663 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 4300 int iCur = pTabItem->iCursor; | 4664 int iCur = pTabItem->iCursor; |
| 4301 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); | 4665 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 4302 }else if( IsVirtual(pTab) ){ | 4666 }else if( IsVirtual(pTab) ){ |
| 4303 /* noop */ | 4667 /* noop */ |
| 4304 }else | 4668 }else |
| 4305 #endif | 4669 #endif |
| 4306 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 | 4670 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 4307 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ | 4671 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){ |
| 4308 int op = OP_OpenRead; | 4672 int op = OP_OpenRead; |
| 4309 if( pWInfo->eOnePass!=ONEPASS_OFF ){ | 4673 if( pWInfo->eOnePass!=ONEPASS_OFF ){ |
| 4310 op = OP_OpenWrite; | 4674 op = OP_OpenWrite; |
| 4311 pWInfo->aiCurOnePass[0] = pTabItem->iCursor; | 4675 pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 4312 }; | 4676 }; |
| 4313 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); | 4677 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 4314 assert( pTabItem->iCursor==pLevel->iTabCur ); | 4678 assert( pTabItem->iCursor==pLevel->iTabCur ); |
| 4315 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); | 4679 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); |
| 4316 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); | 4680 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); |
| 4317 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){ | 4681 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){ |
| 4318 Bitmask b = pTabItem->colUsed; | 4682 Bitmask b = pTabItem->colUsed; |
| 4319 int n = 0; | 4683 int n = 0; |
| 4320 for(; b; b=b>>1, n++){} | 4684 for(; b; b=b>>1, n++){} |
| 4321 sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, | 4685 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32); |
| 4322 SQLITE_INT_TO_PTR(n), P4_INT32); | |
| 4323 assert( n<=pTab->nCol ); | 4686 assert( n<=pTab->nCol ); |
| 4324 } | 4687 } |
| 4325 #ifdef SQLITE_ENABLE_CURSOR_HINTS | 4688 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 4326 if( pLoop->u.btree.pIndex!=0 ){ | 4689 if( pLoop->u.btree.pIndex!=0 ){ |
| 4327 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete); | 4690 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete); |
| 4328 }else | 4691 }else |
| 4329 #endif | 4692 #endif |
| 4330 { | 4693 { |
| 4331 sqlite3VdbeChangeP5(v, bFordelete); | 4694 sqlite3VdbeChangeP5(v, bFordelete); |
| 4332 } | 4695 } |
| 4333 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK | 4696 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 4334 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, | 4697 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, |
| 4335 (const u8*)&pTabItem->colUsed, P4_INT64); | 4698 (const u8*)&pTabItem->colUsed, P4_INT64); |
| 4336 #endif | 4699 #endif |
| 4337 }else{ | 4700 }else{ |
| 4338 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); | 4701 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 4339 } | 4702 } |
| 4340 if( pLoop->wsFlags & WHERE_INDEXED ){ | 4703 if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 4341 Index *pIx = pLoop->u.btree.pIndex; | 4704 Index *pIx = pLoop->u.btree.pIndex; |
| 4342 int iIndexCur; | 4705 int iIndexCur; |
| 4343 int op = OP_OpenRead; | 4706 int op = OP_OpenRead; |
| 4344 /* iIdxCur is always set if to a positive value if ONEPASS is possible */ | 4707 /* iAuxArg is always set if to a positive value if ONEPASS is possible */ |
| 4345 assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); | 4708 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); |
| 4346 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) | 4709 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) |
| 4347 && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 | 4710 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 |
| 4348 ){ | 4711 ){ |
| 4349 /* This is one term of an OR-optimization using the PRIMARY KEY of a | 4712 /* This is one term of an OR-optimization using the PRIMARY KEY of a |
| 4350 ** WITHOUT ROWID table. No need for a separate index */ | 4713 ** WITHOUT ROWID table. No need for a separate index */ |
| 4351 iIndexCur = pLevel->iTabCur; | 4714 iIndexCur = pLevel->iTabCur; |
| 4352 op = 0; | 4715 op = 0; |
| 4353 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){ | 4716 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){ |
| 4354 Index *pJ = pTabItem->pTab->pIndex; | 4717 Index *pJ = pTabItem->pTab->pIndex; |
| 4355 iIndexCur = iIdxCur; | 4718 iIndexCur = iAuxArg; |
| 4356 assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); | 4719 assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 4357 while( ALWAYS(pJ) && pJ!=pIx ){ | 4720 while( ALWAYS(pJ) && pJ!=pIx ){ |
| 4358 iIndexCur++; | 4721 iIndexCur++; |
| 4359 pJ = pJ->pNext; | 4722 pJ = pJ->pNext; |
| 4360 } | 4723 } |
| 4361 op = OP_OpenWrite; | 4724 op = OP_OpenWrite; |
| 4362 pWInfo->aiCurOnePass[1] = iIndexCur; | 4725 pWInfo->aiCurOnePass[1] = iIndexCur; |
| 4363 }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ | 4726 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){ |
| 4364 iIndexCur = iIdxCur; | 4727 iIndexCur = iAuxArg; |
| 4365 if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx; | 4728 op = OP_ReopenIdx; |
| 4366 }else{ | 4729 }else{ |
| 4367 iIndexCur = pParse->nTab++; | 4730 iIndexCur = pParse->nTab++; |
| 4368 } | 4731 } |
| 4369 pLevel->iIdxCur = iIndexCur; | 4732 pLevel->iIdxCur = iIndexCur; |
| 4370 assert( pIx->pSchema==pTab->pSchema ); | 4733 assert( pIx->pSchema==pTab->pSchema ); |
| 4371 assert( iIndexCur>=0 ); | 4734 assert( iIndexCur>=0 ); |
| 4372 if( op ){ | 4735 if( op ){ |
| 4373 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); | 4736 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 4374 sqlite3VdbeSetP4KeyInfo(pParse, pIx); | 4737 sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
| 4375 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 | 4738 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4417 &pTabList->a[pLevel->iFrom], notReady, pLevel); | 4780 &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 4418 if( db->mallocFailed ) goto whereBeginError; | 4781 if( db->mallocFailed ) goto whereBeginError; |
| 4419 } | 4782 } |
| 4420 #endif | 4783 #endif |
| 4421 addrExplain = sqlite3WhereExplainOneScan( | 4784 addrExplain = sqlite3WhereExplainOneScan( |
| 4422 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags | 4785 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 4423 ); | 4786 ); |
| 4424 pLevel->addrBody = sqlite3VdbeCurrentAddr(v); | 4787 pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 4425 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); | 4788 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); |
| 4426 pWInfo->iContinue = pLevel->addrCont; | 4789 pWInfo->iContinue = pLevel->addrCont; |
| 4427 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ | 4790 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){ |
| 4428 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); | 4791 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); |
| 4429 } | 4792 } |
| 4430 } | 4793 } |
| 4431 | 4794 |
| 4432 /* Done. */ | 4795 /* Done. */ |
| 4433 VdbeModuleComment((v, "Begin WHERE-core")); | 4796 VdbeModuleComment((v, "Begin WHERE-core")); |
| 4434 return pWInfo; | 4797 return pWInfo; |
| 4435 | 4798 |
| 4436 /* Jump here if malloc fails */ | 4799 /* Jump here if malloc fails */ |
| 4437 whereBeginError: | 4800 whereBeginError: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4471 VdbeCoverageIf(v, pLevel->op==OP_Next); | 4834 VdbeCoverageIf(v, pLevel->op==OP_Next); |
| 4472 VdbeCoverageIf(v, pLevel->op==OP_Prev); | 4835 VdbeCoverageIf(v, pLevel->op==OP_Prev); |
| 4473 VdbeCoverageIf(v, pLevel->op==OP_VNext); | 4836 VdbeCoverageIf(v, pLevel->op==OP_VNext); |
| 4474 } | 4837 } |
| 4475 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ | 4838 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 4476 struct InLoop *pIn; | 4839 struct InLoop *pIn; |
| 4477 int j; | 4840 int j; |
| 4478 sqlite3VdbeResolveLabel(v, pLevel->addrNxt); | 4841 sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 4479 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ | 4842 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 4480 sqlite3VdbeJumpHere(v, pIn->addrInTop+1); | 4843 sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| 4481 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); | 4844 if( pIn->eEndLoopOp!=OP_Noop ){ |
| 4482 VdbeCoverage(v); | 4845 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
| 4483 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); | 4846 VdbeCoverage(v); |
| 4484 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); | 4847 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 4848 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
| 4849 } |
| 4485 sqlite3VdbeJumpHere(v, pIn->addrInTop-1); | 4850 sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
| 4486 } | 4851 } |
| 4487 } | 4852 } |
| 4488 sqlite3VdbeResolveLabel(v, pLevel->addrBrk); | 4853 sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 4489 if( pLevel->addrSkip ){ | 4854 if( pLevel->addrSkip ){ |
| 4490 sqlite3VdbeGoto(v, pLevel->addrSkip); | 4855 sqlite3VdbeGoto(v, pLevel->addrSkip); |
| 4491 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); | 4856 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
| 4492 sqlite3VdbeJumpHere(v, pLevel->addrSkip); | 4857 sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 4493 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); | 4858 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
| 4494 } | 4859 } |
| 4495 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS | 4860 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 4496 if( pLevel->addrLikeRep ){ | 4861 if( pLevel->addrLikeRep ){ |
| 4497 int op; | 4862 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1), |
| 4498 if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){ | 4863 pLevel->addrLikeRep); |
| 4499 op = OP_DecrJumpZero; | |
| 4500 }else{ | |
| 4501 op = OP_JumpZeroIncr; | |
| 4502 } | |
| 4503 sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep); | |
| 4504 VdbeCoverage(v); | 4864 VdbeCoverage(v); |
| 4505 } | 4865 } |
| 4506 #endif | 4866 #endif |
| 4507 if( pLevel->iLeftJoin ){ | 4867 if( pLevel->iLeftJoin ){ |
| 4868 int ws = pLoop->wsFlags; |
| 4508 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); | 4869 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
| 4509 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 | 4870 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 ); |
| 4510 || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); | 4871 if( (ws & WHERE_IDX_ONLY)==0 ){ |
| 4511 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ | |
| 4512 sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); | 4872 sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 4513 } | 4873 } |
| 4514 if( pLoop->wsFlags & WHERE_INDEXED ){ | 4874 if( (ws & WHERE_INDEXED) |
| 4875 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx) |
| 4876 ){ |
| 4515 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); | 4877 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
| 4516 } | 4878 } |
| 4517 if( pLevel->op==OP_Return ){ | 4879 if( pLevel->op==OP_Return ){ |
| 4518 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); | 4880 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 4519 }else{ | 4881 }else{ |
| 4520 sqlite3VdbeGoto(v, pLevel->addrFirst); | 4882 sqlite3VdbeGoto(v, pLevel->addrFirst); |
| 4521 } | 4883 } |
| 4522 sqlite3VdbeJumpHere(v, addr); | 4884 sqlite3VdbeJumpHere(v, addr); |
| 4523 } | 4885 } |
| 4524 VdbeModuleComment((v, "End WHERE-loop%d: %s", i, | 4886 VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 4543 /* For a co-routine, change all OP_Column references to the table of | 4905 /* For a co-routine, change all OP_Column references to the table of |
| 4544 ** the co-routine into OP_Copy of result contained in a register. | 4906 ** the co-routine into OP_Copy of result contained in a register. |
| 4545 ** OP_Rowid becomes OP_Null. | 4907 ** OP_Rowid becomes OP_Null. |
| 4546 */ | 4908 */ |
| 4547 if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){ | 4909 if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){ |
| 4548 translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, | 4910 translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, |
| 4549 pTabItem->regResult, 0); | 4911 pTabItem->regResult, 0); |
| 4550 continue; | 4912 continue; |
| 4551 } | 4913 } |
| 4552 | 4914 |
| 4553 /* Close all of the cursors that were opened by sqlite3WhereBegin. | |
| 4554 ** Except, do not close cursors that will be reused by the OR optimization | |
| 4555 ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors | |
| 4556 ** created for the ONEPASS optimization. | |
| 4557 */ | |
| 4558 if( (pTab->tabFlags & TF_Ephemeral)==0 | |
| 4559 && pTab->pSelect==0 | |
| 4560 && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 | |
| 4561 ){ | |
| 4562 int ws = pLoop->wsFlags; | |
| 4563 if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){ | |
| 4564 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); | |
| 4565 } | |
| 4566 if( (ws & WHERE_INDEXED)!=0 | |
| 4567 && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 | |
| 4568 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] | |
| 4569 ){ | |
| 4570 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); | |
| 4571 } | |
| 4572 } | |
| 4573 | |
| 4574 /* If this scan uses an index, make VDBE code substitutions to read data | 4915 /* If this scan uses an index, make VDBE code substitutions to read data |
| 4575 ** from the index instead of from the table where possible. In some cases | 4916 ** from the index instead of from the table where possible. In some cases |
| 4576 ** this optimization prevents the table from ever being read, which can | 4917 ** this optimization prevents the table from ever being read, which can |
| 4577 ** yield a significant performance boost. | 4918 ** yield a significant performance boost. |
| 4578 ** | 4919 ** |
| 4579 ** Calls to the code generator in between sqlite3WhereBegin and | 4920 ** Calls to the code generator in between sqlite3WhereBegin and |
| 4580 ** sqlite3WhereEnd will have created code that references the table | 4921 ** sqlite3WhereEnd will have created code that references the table |
| 4581 ** directly. This loop scans all that code looking for opcodes | 4922 ** directly. This loop scans all that code looking for opcodes |
| 4582 ** that reference the table and converts them into opcodes that | 4923 ** that reference the table and converts them into opcodes that |
| 4583 ** reference the index. | 4924 ** reference the index. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 4602 if( !HasRowid(pTab) ){ | 4943 if( !HasRowid(pTab) ){ |
| 4603 Index *pPk = sqlite3PrimaryKeyIndex(pTab); | 4944 Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 4604 x = pPk->aiColumn[x]; | 4945 x = pPk->aiColumn[x]; |
| 4605 assert( x>=0 ); | 4946 assert( x>=0 ); |
| 4606 } | 4947 } |
| 4607 x = sqlite3ColumnOfIndex(pIdx, x); | 4948 x = sqlite3ColumnOfIndex(pIdx, x); |
| 4608 if( x>=0 ){ | 4949 if( x>=0 ){ |
| 4609 pOp->p2 = x; | 4950 pOp->p2 = x; |
| 4610 pOp->p1 = pLevel->iIdxCur; | 4951 pOp->p1 = pLevel->iIdxCur; |
| 4611 } | 4952 } |
| 4612 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); | 4953 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 |
| 4954 || pWInfo->eOnePass ); |
| 4613 }else if( pOp->opcode==OP_Rowid ){ | 4955 }else if( pOp->opcode==OP_Rowid ){ |
| 4614 pOp->p1 = pLevel->iIdxCur; | 4956 pOp->p1 = pLevel->iIdxCur; |
| 4615 pOp->opcode = OP_IdxRowid; | 4957 pOp->opcode = OP_IdxRowid; |
| 4616 } | 4958 } |
| 4617 } | 4959 } |
| 4618 } | 4960 } |
| 4619 } | 4961 } |
| 4620 | 4962 |
| 4621 /* Final cleanup | 4963 /* Final cleanup |
| 4622 */ | 4964 */ |
| 4623 pParse->nQueryLoop = pWInfo->savedNQueryLoop; | 4965 pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 4624 whereInfoFree(db, pWInfo); | 4966 whereInfoFree(db, pWInfo); |
| 4625 return; | 4967 return; |
| 4626 } | 4968 } |
| OLD | NEW |