| 1 |
|
| /* |
| 2 |
|
| ** 2001 September 15 |
| 3 |
|
| ** |
| 4 |
|
| ** The author disclaims copyright to this source code. In place of |
| 5 |
|
| ** a legal notice, here is a blessing: |
| 6 |
|
| ** |
| 7 |
|
| ** May you do good and not evil. |
| 8 |
|
| ** May you find forgiveness for yourself and forgive others. |
| 9 |
|
| ** May you share freely, never taking more than you give. |
| 10 |
|
| ** |
| 11 |
|
| ************************************************************************* |
| 12 |
|
| ** This module contains C code that generates VDBE code used to process |
| 13 |
|
| ** the WHERE clause of SQL statements. |
| 14 |
|
| ** |
| 15 |
|
| ** $Id: where.c,v 1.1.1.1 2004/08/08 15:03:58 matt Exp $ |
| 16 |
|
| */ |
| 17 |
|
| #include "sqliteInt.h" |
| 18 |
|
| |
| 19 |
|
| /* |
| 20 |
|
| ** The query generator uses an array of instances of this structure to |
| 21 |
|
| ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 22 |
|
| ** clause subexpression is separated from the others by an AND operator. |
| 23 |
|
| */ |
| 24 |
|
| typedef struct ExprInfo ExprInfo; |
| 25 |
|
| struct ExprInfo { |
| 26 |
|
| Expr *p; /* Pointer to the subexpression */ |
| 27 |
|
| u8 indexable; /* True if this subexprssion is usable by an index */ |
| 28 |
|
| short int idxLeft; /* p->pLeft is a column in this table number. -1 if |
| 29 |
|
| ** p->pLeft is not the column of any table */ |
| 30 |
|
| short int idxRight; /* p->pRight is a column in this table number. -1 if |
| 31 |
|
| ** p->pRight is not the column of any table */ |
| 32 |
|
| unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */ |
| 33 |
|
| unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */ |
| 34 |
|
| unsigned prereqAll; /* Bitmask of tables referenced by p */ |
| 35 |
|
| }; |
| 36 |
|
| |
| 37 |
|
| /* |
| 38 |
|
| ** An instance of the following structure keeps track of a mapping |
| 39 |
|
| ** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers |
| 40 |
|
| ** are small integers contained in SrcList_item.iCursor and Expr.iTable |
| 41 |
|
| ** fields. For any given WHERE clause, we want to track which cursors |
| 42 |
|
| ** are being used, so we assign a single bit in a 32-bit word to track |
| 43 |
|
| ** that cursor. Then a 32-bit integer is able to show the set of all |
| 44 |
|
| ** cursors being used. |
| 45 |
|
| */ |
| 46 |
|
| typedef struct ExprMaskSet ExprMaskSet; |
| 47 |
|
| struct ExprMaskSet { |
| 48 |
|
| int n; /* Number of assigned cursor values */ |
| 49 |
|
| int ix[31]; /* Cursor assigned to each bit */ |
| 50 |
|
| }; |
| 51 |
|
| |
| 52 |
|
| /* |
| 53 |
|
| ** Determine the number of elements in an array. |
| 54 |
|
| */ |
| 55 |
|
| #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0])) |
| 56 |
|
| |
| 57 |
|
| /* |
| 58 |
|
| ** This routine is used to divide the WHERE expression into subexpressions |
| 59 |
|
| ** separated by the AND operator. |
| 60 |
|
| ** |
| 61 |
|
| ** aSlot[] is an array of subexpressions structures. |
| 62 |
|
| ** There are nSlot spaces left in this array. This routine attempts to |
| 63 |
|
| ** split pExpr into subexpressions and fills aSlot[] with those subexpressions. |
| 64 |
|
| ** The return value is the number of slots filled. |
| 65 |
|
| */ |
| 66 |
144 |
| static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){ |
| 67 |
144 |
| int cnt = 0; |
| 68 |
144 |
▶ | if( pExpr==0 || nSlot<1 ) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if( pExpr==0 || nSlot<1 ) return 0; |
36 |
108 |
144 |
| if( pExpr==0 || nSlot<1 ) return 0; |
0 |
36 |
36 |
|
| 69 |
36 |
▶ | if( nSlot==1 || pExpr->op!=TK_AND ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( nSlot==1 || pExpr->op!=TK_AND ){ |
36 |
0 |
36 |
| if( nSlot==1 || pExpr->op!=TK_AND ){ |
33 |
3 |
36 |
|
| 70 |
33 |
| aSlot[0].p = pExpr; |
| 71 |
33 |
| return 1; |
| 72 |
|
| } |
| 73 |
3 |
▶ | if( pExpr->pLeft->op!=TK_AND ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pExpr->pLeft->op!=TK_AND ){ |
3 |
0 |
3 |
|
| 74 |
3 |
| aSlot[0].p = pExpr->pLeft; |
| 75 |
3 |
| cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight); |
| 76 |
|
| }else{ |
| 77 |
0 |
| cnt = exprSplit(nSlot, aSlot, pExpr->pLeft); |
| 78 |
0 |
| cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight); |
| 79 |
|
| } |
| 80 |
3 |
| return cnt; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* |
| 84 |
|
| ** Initialize an expression mask set |
| 85 |
|
| */ |
| 86 |
|
| #define initMaskSet(P) memset(P, 0, sizeof(*P)) |
| 87 |
|
| |
| 88 |
|
| /* |
| 89 |
|
| ** Return the bitmask for the given cursor. Assign a new bitmask |
| 90 |
|
| ** if this is the first time the cursor has been seen. |
| 91 |
|
| */ |
| 92 |
309 |
| static int getMask(ExprMaskSet *pMaskSet, int iCursor){ |
| 93 |
|
| int i; |
| 94 |
313 |
▶ | for(i=0; i<pMaskSet->n; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pMaskSet->n; i++){ |
194 |
119 |
313 |
|
| 95 |
194 |
▶ | if( pMaskSet->ix[i]==iCursor ) return 1<<i; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pMaskSet->ix[i]==iCursor ) return 1<<i; |
190 |
4 |
194 |
|
| 96 |
|
| } |
| 97 |
119 |
▶ | if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){ |
119 |
0 |
119 |
| if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){ |
119 |
0 |
119 |
|
| 98 |
119 |
| pMaskSet->n++; |
| 99 |
119 |
| pMaskSet->ix[i] = iCursor; |
| 100 |
119 |
| return 1<<i; |
| 101 |
|
| } |
| 102 |
0 |
| return 0; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| /* |
| 106 |
|
| ** Destroy an expression mask set |
| 107 |
|
| */ |
| 108 |
|
| #define freeMaskSet(P) /* NO-OP */ |
| 109 |
|
| |
| 110 |
|
| /* |
| 111 |
|
| ** This routine walks (recursively) an expression tree and generates |
| 112 |
|
| ** a bitmask indicating which tables are used in that expression |
| 113 |
|
| ** tree. |
| 114 |
|
| ** |
| 115 |
|
| ** In order for this routine to work, the calling function must have |
| 116 |
|
| ** previously invoked sqliteExprResolveIds() on the expression. See |
| 117 |
|
| ** the header comment on that routine for additional information. |
| 118 |
|
| ** The sqliteExprResolveIds() routines looks for column names and |
| 119 |
|
| ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to |
| 120 |
|
| ** the VDBE cursor number of the table. |
| 121 |
|
| */ |
| 122 |
182 |
| static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){ |
| 123 |
182 |
| unsigned int mask = 0; |
| 124 |
182 |
▶ | if( p==0 ) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if( p==0 ) return 0; |
16 |
166 |
182 |
|
| 125 |
166 |
▶ | if( p->op==TK_COLUMN ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( p->op==TK_COLUMN ){ |
71 |
95 |
166 |
|
| 126 |
71 |
| mask = getMask(pMaskSet, p->iTable); |
| 127 |
71 |
▶ | if( mask==0 ) mask = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if( mask==0 ) mask = -1; |
0 |
71 |
71 |
|
| 128 |
71 |
| return mask; |
| 129 |
|
| } |
| 130 |
95 |
▶ | if( p->pRight ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( p->pRight ){ |
23 |
72 |
95 |
|
| 131 |
23 |
| mask = exprTableUsage(pMaskSet, p->pRight); |
| 132 |
|
| } |
| 133 |
95 |
▶ | if( p->pLeft ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( p->pLeft ){ |
33 |
62 |
95 |
|
| 134 |
33 |
| mask |= exprTableUsage(pMaskSet, p->pLeft); |
| 135 |
|
| } |
| 136 |
95 |
▶ | if( p->pList ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( p->pList ){ |
9 |
86 |
95 |
|
| 137 |
|
| int i; |
| 138 |
27 |
▶ | for(i=0; i<p->pList->nExpr; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<p->pList->nExpr; i++){ |
18 |
9 |
27 |
|
| 139 |
18 |
| mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr); |
| 140 |
|
| } |
| 141 |
|
| } |
| 142 |
95 |
| return mask; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| /* |
| 146 |
|
| ** Return TRUE if the given operator is one of the operators that is |
| 147 |
|
| ** allowed for an indexable WHERE clause. The allowed operators are |
| 148 |
|
| ** "=", "<", ">", "<=", ">=", and "IN". |
| 149 |
|
| */ |
| 150 |
36 |
| static int allowedOp(int op){ |
| 151 |
36 |
▶ | switch( op ){ |
Branch: 1 branch
| Branch | True | False | Total |
| switch( op ){ |
29 |
7 |
36 |
|
| 152 |
29 |
| case TK_LT: |
| 153 |
|
| case TK_LE: |
| 154 |
|
| case TK_GT: |
| 155 |
|
| case TK_GE: |
| 156 |
|
| case TK_EQ: |
| 157 |
|
| case TK_IN: |
| 158 |
29 |
| return 1; |
| 159 |
7 |
| default: |
| 160 |
7 |
| return 0; |
| 161 |
|
| } |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
|
| /* |
| 165 |
|
| ** The input to this routine is an ExprInfo structure with only the |
| 166 |
|
| ** "p" field filled in. The job of this routine is to analyze the |
| 167 |
|
| ** subexpression and populate all the other fields of the ExprInfo |
| 168 |
|
| ** structure. |
| 169 |
|
| */ |
| 170 |
36 |
| static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){ |
| 171 |
36 |
| Expr *pExpr = pInfo->p; |
| 172 |
36 |
| pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
| 173 |
36 |
| pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 174 |
36 |
| pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 175 |
36 |
| pInfo->indexable = 0; |
| 176 |
36 |
| pInfo->idxLeft = -1; |
| 177 |
36 |
| pInfo->idxRight = -1; |
| 178 |
36 |
▶ | if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
29 |
7 |
36 |
| if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
29 |
0 |
29 |
|
| 179 |
29 |
▶ | if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){ |
23 |
6 |
29 |
| if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){ |
1 |
22 |
23 |
|
| 180 |
1 |
| pInfo->idxRight = pExpr->pRight->iTable; |
| 181 |
1 |
| pInfo->indexable = 1; |
| 182 |
|
| } |
| 183 |
29 |
▶ | if( pExpr->pLeft->op==TK_COLUMN ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pExpr->pLeft->op==TK_COLUMN ){ |
29 |
0 |
29 |
|
| 184 |
29 |
| pInfo->idxLeft = pExpr->pLeft->iTable; |
| 185 |
29 |
| pInfo->indexable = 1; |
| 186 |
|
| } |
| 187 |
|
| } |
| 188 |
36 |
| } |
| 189 |
|
| |
| 190 |
|
| /* |
| 191 |
|
| ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the |
| 192 |
|
| ** left-most table in the FROM clause of that same SELECT statement and |
| 193 |
|
| ** the table has a cursor number of "base". |
| 194 |
|
| ** |
| 195 |
|
| ** This routine attempts to find an index for pTab that generates the |
| 196 |
|
| ** correct record sequence for the given ORDER BY clause. The return value |
| 197 |
|
| ** is a pointer to an index that does the job. NULL is returned if the |
| 198 |
|
| ** table has no index that will generate the correct sort order. |
| 199 |
|
| ** |
| 200 |
|
| ** If there are two or more indices that generate the correct sort order |
| 201 |
|
| ** and pPreferredIdx is one of those indices, then return pPreferredIdx. |
| 202 |
|
| ** |
| 203 |
|
| ** nEqCol is the number of columns of pPreferredIdx that are used as |
| 204 |
|
| ** equality constraints. Any index returned must have exactly this same |
| 205 |
|
| ** set of columns. The ORDER BY clause only matches index columns beyond the |
| 206 |
|
| ** the first nEqCol columns. |
| 207 |
|
| ** |
| 208 |
|
| ** All terms of the ORDER BY clause must be either ASC or DESC. The |
| 209 |
|
| ** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is |
| 210 |
|
| ** set to 0 if the ORDER BY clause is all ASC. |
| 211 |
|
| */ |
| 212 |
4 |
| static Index *findSortingIndex( |
| 213 |
|
| Table *pTab, /* The table to be sorted */ |
| 214 |
|
| int base, /* Cursor number for pTab */ |
| 215 |
|
| ExprList *pOrderBy, /* The ORDER BY clause */ |
| 216 |
|
| Index *pPreferredIdx, /* Use this index, if possible and not NULL */ |
| 217 |
|
| int nEqCol, /* Number of index columns used with == constraints */ |
| 218 |
|
| int *pbRev /* Set to 1 if ORDER BY is DESC */ |
| 219 |
|
| ){ |
| 220 |
|
| int i, j; |
| 221 |
|
| Index *pMatch; |
| 222 |
|
| Index *pIdx; |
| 223 |
|
| int sortOrder; |
| 224 |
|
| |
| 225 |
|
| assert( pOrderBy!=0 ); |
| 226 |
|
| assert( pOrderBy->nExpr>0 ); |
| 227 |
4 |
| sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK; |
| 228 |
8 |
▶ | for(i=0; i<pOrderBy->nExpr; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pOrderBy->nExpr; i++){ |
7 |
1 |
8 |
|
| 229 |
|
| Expr *p; |
| 230 |
7 |
▶ | if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){ |
0 |
7 |
7 |
|
| 231 |
|
| /* Indices can only be used if all ORDER BY terms are either |
| 232 |
|
| ** DESC or ASC. Indices cannot be used on a mixture. */ |
| 233 |
0 |
| return 0; |
| 234 |
|
| } |
| 235 |
7 |
▶ | if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){ |
0 |
7 |
7 |
|
| 236 |
|
| /* Do not sort by index if there is a COLLATE clause */ |
| 237 |
0 |
| return 0; |
| 238 |
|
| } |
| 239 |
7 |
| p = pOrderBy->a[i].pExpr; |
| 240 |
7 |
▶ | if( p->op!=TK_COLUMN || p->iTable!=base ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( p->op!=TK_COLUMN || p->iTable!=base ){ |
4 |
3 |
7 |
| if( p->op!=TK_COLUMN || p->iTable!=base ){ |
0 |
4 |
4 |
|
| 241 |
|
| /* Can not use an index sort on anything that is not a column in the |
| 242 |
|
| ** left-most table of the FROM clause */ |
| 243 |
3 |
| return 0; |
| 244 |
|
| } |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
|
| /* If we get this far, it means the ORDER BY clause consists only of |
| 248 |
|
| ** ascending columns in the left-most table of the FROM clause. Now |
| 249 |
|
| ** check for a matching index. |
| 250 |
|
| */ |
| 251 |
1 |
| pMatch = 0; |
| 252 |
1 |
▶ | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
0 |
1 |
1 |
|
| 253 |
0 |
| int nExpr = pOrderBy->nExpr; |
| 254 |
0 |
▶ | if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue; |
0 |
0 |
0 |
| if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue; |
0 |
0 |
0 |
|
| 255 |
0 |
▶ | for(i=j=0; i<nEqCol; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=j=0; i<nEqCol; i++){ |
0 |
0 |
0 |
|
| 256 |
0 |
▶ | if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break; |
0 |
0 |
0 |
|
| 257 |
0 |
▶ | if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; } |
0 |
0 |
0 |
| if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; } |
0 |
0 |
0 |
|
| 258 |
|
| } |
| 259 |
0 |
▶ | if( i<nEqCol ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( i<nEqCol ) continue; |
0 |
0 |
0 |
|
| 260 |
0 |
▶ | for(i=0; i+j<nExpr; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i+j<nExpr; i++){ |
0 |
0 |
0 |
|
| 261 |
0 |
▶ | if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break; |
0 |
0 |
0 |
|
| 262 |
|
| } |
| 263 |
0 |
▶ | if( i+j>=nExpr ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( i+j>=nExpr ){ |
0 |
0 |
0 |
|
| 264 |
0 |
| pMatch = pIdx; |
| 265 |
0 |
▶ | if( pIdx==pPreferredIdx ) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pIdx==pPreferredIdx ) break; |
0 |
0 |
0 |
|
| 266 |
|
| } |
| 267 |
|
| } |
| 268 |
1 |
▶ | if( pMatch && pbRev ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pMatch && pbRev ){ |
0 |
1 |
1 |
| if( pMatch && pbRev ){ |
0 |
0 |
0 |
|
| 269 |
0 |
| *pbRev = sortOrder==SQLITE_SO_DESC; |
| 270 |
|
| } |
| 271 |
1 |
| return pMatch; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| /* |
| 275 |
|
| ** Disable a term in the WHERE clause. Except, do not disable the term |
| 276 |
|
| ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 277 |
|
| ** or USING clause of that join. |
| 278 |
|
| ** |
| 279 |
|
| ** Consider the term t2.z='ok' in the following queries: |
| 280 |
|
| ** |
| 281 |
|
| ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 282 |
|
| ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 283 |
|
| ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 284 |
|
| ** |
| 285 |
|
| ** The t2.z='ok' is disabled in the in (2) because it did not originate |
| 286 |
|
| ** in the ON clause. The term is disabled in (3) because it is not part |
| 287 |
|
| ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 288 |
|
| ** |
| 289 |
|
| ** Disabling a term causes that term to not be tested in the inner loop |
| 290 |
|
| ** of the join. Disabling is an optimization. We would get the correct |
| 291 |
|
| ** results if nothing were ever disabled, but joins might run a little |
| 292 |
|
| ** slower. The trick is to disable as much as we can without disabling |
| 293 |
|
| ** too much. If we disabled in (1), we'd get the wrong answer. |
| 294 |
|
| ** See ticket #813. |
| 295 |
|
| */ |
| 296 |
0 |
| static void disableTerm(WhereLevel *pLevel, Expr **ppExpr){ |
| 297 |
0 |
| Expr *pExpr = *ppExpr; |
| 298 |
0 |
▶ | if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){ |
0 |
0 |
0 |
| if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){ |
0 |
0 |
0 |
|
| 299 |
0 |
| *ppExpr = 0; |
| 300 |
|
| } |
| 301 |
0 |
| } |
| 302 |
|
| |
| 303 |
|
| /* |
| 304 |
|
| ** Generate the beginning of the loop used for WHERE clause processing. |
| 305 |
|
| ** The return value is a pointer to an (opaque) structure that contains |
| 306 |
|
| ** information needed to terminate the loop. Later, the calling routine |
| 307 |
|
| ** should invoke sqliteWhereEnd() with the return value of this function |
| 308 |
|
| ** in order to complete the WHERE clause processing. |
| 309 |
|
| ** |
| 310 |
|
| ** If an error occurs, this routine returns NULL. |
| 311 |
|
| ** |
| 312 |
|
| ** The basic idea is to do a nested loop, one loop for each table in |
| 313 |
|
| ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 314 |
|
| ** same as a SELECT with only a single table in the FROM clause.) For |
| 315 |
|
| ** example, if the SQL is this: |
| 316 |
|
| ** |
| 317 |
|
| ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 318 |
|
| ** |
| 319 |
|
| ** Then the code generated is conceptually like the following: |
| 320 |
|
| ** |
| 321 |
|
| ** foreach row1 in t1 do \ Code generated |
| 322 |
|
| ** foreach row2 in t2 do |-- by sqliteWhereBegin() |
| 323 |
|
| ** foreach row3 in t3 do / |
| 324 |
|
| ** ... |
| 325 |
|
| ** end \ Code generated |
| 326 |
|
| ** end |-- by sqliteWhereEnd() |
| 327 |
|
| ** end / |
| 328 |
|
| ** |
| 329 |
|
| ** There are Btree cursors associated with each table. t1 uses cursor |
| 330 |
|
| ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 331 |
|
| ** And so forth. This routine generates code to open those VDBE cursors |
| 332 |
|
| ** and sqliteWhereEnd() generates the code to close them. |
| 333 |
|
| ** |
| 334 |
|
| ** If the WHERE clause is empty, the foreach loops must each scan their |
| 335 |
|
| ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 336 |
|
| ** the tables have indices and there are terms in the WHERE clause that |
| 337 |
|
| ** refer to those indices, a complete table scan can be avoided and the |
| 338 |
|
| ** code will run much faster. Most of the work of this routine is checking |
| 339 |
|
| ** to see if there are indices that can be used to speed up the loop. |
| 340 |
|
| ** |
| 341 |
|
| ** Terms of the WHERE clause are also used to limit which rows actually |
| 342 |
|
| ** make it to the "..." in the middle of the loop. After each "foreach", |
| 343 |
|
| ** terms of the WHERE clause that use only terms in that loop and outer |
| 344 |
|
| ** loops are evaluated and if false a jump is made around all subsequent |
| 345 |
|
| ** inner loops (or around the "..." if the test occurs within the inner- |
| 346 |
|
| ** most loop) |
| 347 |
|
| ** |
| 348 |
|
| ** OUTER JOINS |
| 349 |
|
| ** |
| 350 |
|
| ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 351 |
|
| ** |
| 352 |
|
| ** foreach row1 in t1 do |
| 353 |
|
| ** flag = 0 |
| 354 |
|
| ** foreach row2 in t2 do |
| 355 |
|
| ** start: |
| 356 |
|
| ** ... |
| 357 |
|
| ** flag = 1 |
| 358 |
|
| ** end |
| 359 |
|
| ** if flag==0 then |
| 360 |
|
| ** move the row2 cursor to a null row |
| 361 |
|
| ** goto start |
| 362 |
|
| ** fi |
| 363 |
|
| ** end |
| 364 |
|
| ** |
| 365 |
|
| ** ORDER BY CLAUSE PROCESSING |
| 366 |
|
| ** |
| 367 |
|
| ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement, |
| 368 |
|
| ** if there is one. If there is no ORDER BY clause or if this routine |
| 369 |
|
| ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL. |
| 370 |
|
| ** |
| 371 |
|
| ** If an index can be used so that the natural output order of the table |
| 372 |
|
| ** scan is correct for the ORDER BY clause, then that index is used and |
| 373 |
|
| ** *ppOrderBy is set to NULL. This is an optimization that prevents an |
| 374 |
|
| ** unnecessary sort of the result set if an index appropriate for the |
| 375 |
|
| ** ORDER BY clause already exists. |
| 376 |
|
| ** |
| 377 |
|
| ** If the where clause loops cannot be arranged to provide the correct |
| 378 |
|
| ** output order, then the *ppOrderBy is unchanged. |
| 379 |
|
| */ |
| 380 |
141 |
| WhereInfo *sqliteWhereBegin( |
| 381 |
|
| Parse *pParse, /* The parser context */ |
| 382 |
|
| SrcList *pTabList, /* A list of all tables to be scanned */ |
| 383 |
|
| Expr *pWhere, /* The WHERE clause */ |
| 384 |
|
| int pushKey, /* If TRUE, leave the table key on the stack */ |
| 385 |
|
| ExprList **ppOrderBy /* An ORDER BY clause, or NULL */ |
| 386 |
|
| ){ |
| 387 |
|
| int i; /* Loop counter */ |
| 388 |
|
| WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 389 |
141 |
| Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 390 |
141 |
| int brk, cont = 0; /* Addresses used during code generation */ |
| 391 |
|
| int nExpr; /* Number of subexpressions in the WHERE clause */ |
| 392 |
|
| int loopMask; /* One bit set for each outer loop */ |
| 393 |
|
| int haveKey; /* True if KEY is on the stack */ |
| 394 |
|
| ExprMaskSet maskSet; /* The expression mask set */ |
| 395 |
|
| int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */ |
| 396 |
|
| int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */ |
| 397 |
|
| int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */ |
| 398 |
|
| ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */ |
| 399 |
|
| |
| 400 |
|
| /* pushKey is only allowed if there is a single table (as in an INSERT or |
| 401 |
|
| ** UPDATE statement) |
| 402 |
|
| */ |
| 403 |
|
| assert( pushKey==0 || pTabList->nSrc==1 ); |
| 404 |
|
| |
| 405 |
|
| /* Split the WHERE clause into separate subexpressions where each |
| 406 |
|
| ** subexpression is separated by an AND operator. If the aExpr[] |
| 407 |
|
| ** array fills up, the last entry might point to an expression which |
| 408 |
|
| ** contains additional unfactored AND operators. |
| 409 |
|
| */ |
| 410 |
141 |
| initMaskSet(&maskSet); |
| 411 |
141 |
| memset(aExpr, 0, sizeof(aExpr)); |
| 412 |
141 |
| nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere); |
| 413 |
141 |
▶ | if( nExpr==ARRAYSIZE(aExpr) ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( nExpr==ARRAYSIZE(aExpr) ){ |
0 |
141 |
141 |
|
| 414 |
0 |
| sqliteErrorMsg(pParse, "WHERE clause too complex - no more " |
| 415 |
|
| "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1); |
| 416 |
0 |
| return 0; |
| 417 |
|
| } |
| 418 |
|
| |
| 419 |
|
| /* Allocate and initialize the WhereInfo structure that will become the |
| 420 |
|
| ** return value. |
| 421 |
|
| */ |
| 422 |
141 |
| pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel)); |
| 423 |
141 |
▶ | if( sqlite_malloc_failed ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( sqlite_malloc_failed ){ |
0 |
141 |
141 |
|
| 424 |
0 |
| sqliteFree(pWInfo); |
| 425 |
0 |
| return 0; |
| 426 |
|
| } |
| 427 |
141 |
| pWInfo->pParse = pParse; |
| 428 |
141 |
| pWInfo->pTabList = pTabList; |
| 429 |
141 |
| pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab; |
| 430 |
141 |
| pWInfo->iBreak = sqliteVdbeMakeLabel(v); |
| 431 |
|
| |
| 432 |
|
| /* Special case: a WHERE clause that is constant. Evaluate the |
| 433 |
|
| ** expression and either jump over all of the code or fall thru. |
| 434 |
|
| */ |
| 435 |
141 |
▶ | if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){ |
Branch: 3 branches
| Branch | True | False | Total |
| if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){ |
33 |
108 |
141 |
| if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){ |
33 |
0 |
33 |
| if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){ |
0 |
33 |
33 |
|
| 436 |
0 |
| sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1); |
| 437 |
0 |
| pWhere = 0; |
| 438 |
|
| } |
| 439 |
|
| |
| 440 |
|
| /* Analyze all of the subexpressions. |
| 441 |
|
| */ |
| 442 |
177 |
▶ | for(i=0; i<nExpr; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<nExpr; i++){ |
36 |
141 |
177 |
|
| 443 |
36 |
| exprAnalyze(&maskSet, &aExpr[i]); |
| 444 |
|
| |
| 445 |
|
| /* If we are executing a trigger body, remove all references to |
| 446 |
|
| ** new.* and old.* tables from the prerequisite masks. |
| 447 |
|
| */ |
| 448 |
36 |
▶ | if( pParse->trigStack ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pParse->trigStack ){ |
0 |
36 |
36 |
|
| 449 |
|
| int x; |
| 450 |
0 |
▶ | if( (x = pParse->trigStack->newIdx) >= 0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (x = pParse->trigStack->newIdx) >= 0 ){ |
0 |
0 |
0 |
|
| 451 |
0 |
| int mask = ~getMask(&maskSet, x); |
| 452 |
0 |
| aExpr[i].prereqRight &= mask; |
| 453 |
0 |
| aExpr[i].prereqLeft &= mask; |
| 454 |
0 |
| aExpr[i].prereqAll &= mask; |
| 455 |
|
| } |
| 456 |
0 |
▶ | if( (x = pParse->trigStack->oldIdx) >= 0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (x = pParse->trigStack->oldIdx) >= 0 ){ |
0 |
0 |
0 |
|
| 457 |
0 |
| int mask = ~getMask(&maskSet, x); |
| 458 |
0 |
| aExpr[i].prereqRight &= mask; |
| 459 |
0 |
| aExpr[i].prereqLeft &= mask; |
| 460 |
0 |
| aExpr[i].prereqAll &= mask; |
| 461 |
|
| } |
| 462 |
|
| } |
| 463 |
|
| } |
| 464 |
|
| |
| 465 |
|
| /* Figure out what index to use (if any) for each nested loop. |
| 466 |
|
| ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested |
| 467 |
|
| ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner |
| 468 |
|
| ** loop. |
| 469 |
|
| ** |
| 470 |
|
| ** If terms exist that use the ROWID of any table, then set the |
| 471 |
|
| ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table |
| 472 |
|
| ** to the index of the term containing the ROWID. We always prefer |
| 473 |
|
| ** to use a ROWID which can directly access a table rather than an |
| 474 |
|
| ** index which requires reading an index first to get the rowid then |
| 475 |
|
| ** doing a second read of the actual database table. |
| 476 |
|
| ** |
| 477 |
|
| ** Actually, if there are more than 32 tables in the join, only the |
| 478 |
|
| ** first 32 tables are candidates for indices. This is (again) due |
| 479 |
|
| ** to the limit of 32 bits in an integer bitmask. |
| 480 |
|
| */ |
| 481 |
141 |
| loopMask = 0; |
| 482 |
260 |
▶ | for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){ |
Branch: 2 branches
| Branch | True | False | Total |
| for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){ |
119 |
141 |
260 |
| for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){ |
119 |
0 |
119 |
|
| 483 |
|
| int j; |
| 484 |
119 |
| int iCur = pTabList->a[i].iCursor; /* The cursor for this table */ |
| 485 |
119 |
| int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */ |
| 486 |
119 |
| Table *pTab = pTabList->a[i].pTab; |
| 487 |
|
| Index *pIdx; |
| 488 |
119 |
| Index *pBestIdx = 0; |
| 489 |
119 |
| int bestScore = 0; |
| 490 |
|
| |
| 491 |
|
| /* Check to see if there is an expression that uses only the |
| 492 |
|
| ** ROWID field of this table. For terms of the form ROWID==expr |
| 493 |
|
| ** set iDirectEq[i] to the index of the term. For terms of the |
| 494 |
|
| ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index. |
| 495 |
|
| ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i]. |
| 496 |
|
| ** |
| 497 |
|
| ** (Added:) Treat ROWID IN expr like ROWID=expr. |
| 498 |
|
| */ |
| 499 |
119 |
| pWInfo->a[i].iCur = -1; |
| 500 |
119 |
| iDirectEq[i] = -1; |
| 501 |
119 |
| iDirectLt[i] = -1; |
| 502 |
119 |
| iDirectGt[i] = -1; |
| 503 |
156 |
▶ | for(j=0; j<nExpr; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nExpr; j++){ |
37 |
119 |
156 |
|
| 504 |
37 |
▶ | if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0 |
Branch: 2 branches
| Branch | True | False | Total |
| if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0 |
29 |
8 |
37 |
| if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0 |
0 |
29 |
29 |
|
| 505 |
0 |
▶ | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
0 |
0 |
0 |
|
| 506 |
0 |
| switch( aExpr[j].p->op ){ |
| 507 |
0 |
| case TK_IN: |
| 508 |
0 |
| case TK_EQ: iDirectEq[i] = j; break; |
| 509 |
0 |
| case TK_LE: |
| 510 |
0 |
| case TK_LT: iDirectLt[i] = j; break; |
| 511 |
0 |
| case TK_GE: |
| 512 |
0 |
| case TK_GT: iDirectGt[i] = j; break; |
| 513 |
|
| } |
| 514 |
|
| } |
| 515 |
37 |
▶ | if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0 |
Branch: 2 branches
| Branch | True | False | Total |
| if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0 |
1 |
36 |
37 |
| if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0 |
0 |
1 |
1 |
|
| 516 |
0 |
▶ | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
0 |
0 |
0 |
|
| 517 |
0 |
| switch( aExpr[j].p->op ){ |
| 518 |
0 |
| case TK_EQ: iDirectEq[i] = j; break; |
| 519 |
0 |
| case TK_LE: |
| 520 |
0 |
| case TK_LT: iDirectGt[i] = j; break; |
| 521 |
0 |
| case TK_GE: |
| 522 |
0 |
| case TK_GT: iDirectLt[i] = j; break; |
| 523 |
|
| } |
| 524 |
|
| } |
| 525 |
|
| } |
| 526 |
119 |
▶ | if( iDirectEq[i]>=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( iDirectEq[i]>=0 ){ |
0 |
119 |
119 |
|
| 527 |
0 |
| loopMask |= mask; |
| 528 |
0 |
| pWInfo->a[i].pIdx = 0; |
| 529 |
0 |
| continue; |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
|
| /* Do a search for usable indices. Leave pBestIdx pointing to |
| 533 |
|
| ** the "best" index. pBestIdx is left set to NULL if no indices |
| 534 |
|
| ** are usable. |
| 535 |
|
| ** |
| 536 |
|
| ** The best index is determined as follows. For each of the |
| 537 |
|
| ** left-most terms that is fixed by an equality operator, add |
| 538 |
|
| ** 8 to the score. The right-most term of the index may be |
| 539 |
|
| ** constrained by an inequality. Add 1 if for an "x<..." constraint |
| 540 |
|
| ** and add 2 for an "x>..." constraint. Chose the index that |
| 541 |
|
| ** gives the best score. |
| 542 |
|
| ** |
| 543 |
|
| ** This scoring system is designed so that the score can later be |
| 544 |
|
| ** used to determine how the index is used. If the score&7 is 0 |
| 545 |
|
| ** then all constraints are equalities. If score&1 is not 0 then |
| 546 |
|
| ** there is an inequality used as a termination key. (ex: "x<...") |
| 547 |
|
| ** If score&2 is not 0 then there is an inequality used as the |
| 548 |
|
| ** start key. (ex: "x>..."). A score or 4 is the special case |
| 549 |
|
| ** of an IN operator constraint. (ex: "x IN ..."). |
| 550 |
|
| ** |
| 551 |
|
| ** The IN operator (as in "<expr> IN (...)") is treated the same as |
| 552 |
|
| ** an equality comparison except that it can only be used on the |
| 553 |
|
| ** left-most column of an index and other terms of the WHERE clause |
| 554 |
|
| ** cannot be used in conjunction with the IN operator to help satisfy |
| 555 |
|
| ** other columns of the index. |
| 556 |
|
| */ |
| 557 |
120 |
▶ | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
1 |
119 |
120 |
|
| 558 |
1 |
| int eqMask = 0; /* Index columns covered by an x=... term */ |
| 559 |
1 |
| int ltMask = 0; /* Index columns covered by an x<... term */ |
| 560 |
1 |
| int gtMask = 0; /* Index columns covered by an x>... term */ |
| 561 |
1 |
| int inMask = 0; /* Index columns covered by an x IN .. term */ |
| 562 |
|
| int nEq, m, score; |
| 563 |
|
| |
| 564 |
1 |
▶ | if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */ |
0 |
1 |
1 |
|
| 565 |
1 |
▶ | for(j=0; j<nExpr; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nExpr; j++){ |
0 |
1 |
1 |
|
| 566 |
0 |
▶ | if( aExpr[j].idxLeft==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[j].idxLeft==iCur |
0 |
0 |
0 |
|
| 567 |
0 |
▶ | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
0 |
0 |
0 |
|
| 568 |
0 |
| int iColumn = aExpr[j].p->pLeft->iColumn; |
| 569 |
|
| int k; |
| 570 |
0 |
▶ | for(k=0; k<pIdx->nColumn; k++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<pIdx->nColumn; k++){ |
0 |
0 |
0 |
|
| 571 |
0 |
▶ | if( pIdx->aiColumn[k]==iColumn ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pIdx->aiColumn[k]==iColumn ){ |
0 |
0 |
0 |
|
| 572 |
0 |
| switch( aExpr[j].p->op ){ |
| 573 |
0 |
| case TK_IN: { |
| 574 |
0 |
▶ | if( k==0 ) inMask |= 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if( k==0 ) inMask |= 1; |
0 |
0 |
0 |
|
| 575 |
0 |
| break; |
| 576 |
|
| } |
| 577 |
0 |
| case TK_EQ: { |
| 578 |
0 |
| eqMask |= 1<<k; |
| 579 |
0 |
| break; |
| 580 |
|
| } |
| 581 |
0 |
| case TK_LE: |
| 582 |
|
| case TK_LT: { |
| 583 |
0 |
| ltMask |= 1<<k; |
| 584 |
0 |
| break; |
| 585 |
|
| } |
| 586 |
0 |
| case TK_GE: |
| 587 |
|
| case TK_GT: { |
| 588 |
0 |
| gtMask |= 1<<k; |
| 589 |
0 |
| break; |
| 590 |
|
| } |
| 591 |
0 |
| default: { |
| 592 |
|
| /* CANT_HAPPEN */ |
| 593 |
|
| assert( 0 ); |
| 594 |
0 |
| break; |
| 595 |
|
| } |
| 596 |
|
| } |
| 597 |
0 |
| break; |
| 598 |
|
| } |
| 599 |
|
| } |
| 600 |
|
| } |
| 601 |
0 |
▶ | if( aExpr[j].idxRight==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[j].idxRight==iCur |
0 |
0 |
0 |
|
| 602 |
0 |
▶ | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
0 |
0 |
0 |
|
| 603 |
0 |
| int iColumn = aExpr[j].p->pRight->iColumn; |
| 604 |
|
| int k; |
| 605 |
0 |
▶ | for(k=0; k<pIdx->nColumn; k++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<pIdx->nColumn; k++){ |
0 |
0 |
0 |
|
| 606 |
0 |
▶ | if( pIdx->aiColumn[k]==iColumn ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pIdx->aiColumn[k]==iColumn ){ |
0 |
0 |
0 |
|
| 607 |
0 |
| switch( aExpr[j].p->op ){ |
| 608 |
0 |
| case TK_EQ: { |
| 609 |
0 |
| eqMask |= 1<<k; |
| 610 |
0 |
| break; |
| 611 |
|
| } |
| 612 |
0 |
| case TK_LE: |
| 613 |
|
| case TK_LT: { |
| 614 |
0 |
| gtMask |= 1<<k; |
| 615 |
0 |
| break; |
| 616 |
|
| } |
| 617 |
0 |
| case TK_GE: |
| 618 |
|
| case TK_GT: { |
| 619 |
0 |
| ltMask |= 1<<k; |
| 620 |
0 |
| break; |
| 621 |
|
| } |
| 622 |
0 |
| default: { |
| 623 |
|
| /* CANT_HAPPEN */ |
| 624 |
|
| assert( 0 ); |
| 625 |
0 |
| break; |
| 626 |
|
| } |
| 627 |
|
| } |
| 628 |
0 |
| break; |
| 629 |
|
| } |
| 630 |
|
| } |
| 631 |
|
| } |
| 632 |
|
| } |
| 633 |
|
| |
| 634 |
|
| /* The following loop ends with nEq set to the number of columns |
| 635 |
|
| ** on the left of the index with == constraints. |
| 636 |
|
| */ |
| 637 |
1 |
▶ | for(nEq=0; nEq<pIdx->nColumn; nEq++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(nEq=0; nEq<pIdx->nColumn; nEq++){ |
1 |
0 |
1 |
|
| 638 |
1 |
| m = (1<<(nEq+1))-1; |
| 639 |
1 |
▶ | if( (m & eqMask)!=m ) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if( (m & eqMask)!=m ) break; |
1 |
0 |
1 |
|
| 640 |
|
| } |
| 641 |
1 |
| score = nEq*8; /* Base score is 8 times number of == constraints */ |
| 642 |
1 |
| m = 1<<nEq; |
| 643 |
1 |
▶ | if( m & ltMask ) score++; /* Increase score for a < constraint */ |
Branch: 1 branch
| Branch | True | False | Total |
| if( m & ltMask ) score++; /* Increase score for a < constraint */ |
0 |
1 |
1 |
|
| 644 |
1 |
▶ | if( m & gtMask ) score+=2; /* Increase score for a > constraint */ |
Branch: 1 branch
| Branch | True | False | Total |
| if( m & gtMask ) score+=2; /* Increase score for a > constraint */ |
0 |
1 |
1 |
|
| 645 |
1 |
▶ | if( score==0 && inMask ) score = 4; /* Default score for IN constraint */ |
Branch: 2 branches
| Branch | True | False | Total |
| if( score==0 && inMask ) score = 4; /* Default score for IN constraint */ |
1 |
0 |
1 |
| if( score==0 && inMask ) score = 4; /* Default score for IN constraint */ |
0 |
1 |
1 |
|
| 646 |
1 |
▶ | if( score>bestScore ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( score>bestScore ){ |
0 |
1 |
1 |
|
| 647 |
0 |
| pBestIdx = pIdx; |
| 648 |
0 |
| bestScore = score; |
| 649 |
|
| } |
| 650 |
|
| } |
| 651 |
119 |
| pWInfo->a[i].pIdx = pBestIdx; |
| 652 |
119 |
| pWInfo->a[i].score = bestScore; |
| 653 |
119 |
| pWInfo->a[i].bRev = 0; |
| 654 |
119 |
| loopMask |= mask; |
| 655 |
119 |
▶ | if( pBestIdx ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pBestIdx ){ |
0 |
119 |
119 |
|
| 656 |
0 |
| pWInfo->a[i].iCur = pParse->nTab++; |
| 657 |
0 |
| pWInfo->peakNTab = pParse->nTab; |
| 658 |
|
| } |
| 659 |
|
| } |
| 660 |
|
| |
| 661 |
|
| /* Check to see if the ORDER BY clause is or can be satisfied by the |
| 662 |
|
| ** use of an index on the first table. |
| 663 |
|
| */ |
| 664 |
141 |
▶ | if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){ |
Branch: 3 branches
| Branch | True | False | Total |
| if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){ |
133 |
8 |
141 |
| if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){ |
4 |
129 |
133 |
| if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){ |
4 |
0 |
4 |
|
| 665 |
|
| Index *pSortIdx; |
| 666 |
|
| Index *pIdx; |
| 667 |
|
| Table *pTab; |
| 668 |
4 |
| int bRev = 0; |
| 669 |
|
| |
| 670 |
4 |
| pTab = pTabList->a[0].pTab; |
| 671 |
4 |
| pIdx = pWInfo->a[0].pIdx; |
| 672 |
4 |
▶ | if( pIdx && pWInfo->a[0].score==4 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pIdx && pWInfo->a[0].score==4 ){ |
0 |
4 |
4 |
| if( pIdx && pWInfo->a[0].score==4 ){ |
0 |
0 |
0 |
|
| 673 |
|
| /* If there is already an IN index on the left-most table, |
| 674 |
|
| ** it will not give the correct sort order. |
| 675 |
|
| ** So, pretend that no suitable index is found. |
| 676 |
|
| */ |
| 677 |
0 |
| pSortIdx = 0; |
| 678 |
4 |
▶ | }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){ |
Branch: 3 branches
| Branch | True | False | Total |
| }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){ |
4 |
0 |
4 |
| }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){ |
4 |
0 |
4 |
| }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){ |
0 |
4 |
4 |
|
| 679 |
|
| /* If the left-most column is accessed using its ROWID, then do |
| 680 |
|
| ** not try to sort by index. |
| 681 |
|
| */ |
| 682 |
0 |
| pSortIdx = 0; |
| 683 |
|
| }else{ |
| 684 |
4 |
| int nEqCol = (pWInfo->a[0].score+4)/8; |
| 685 |
4 |
| pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor, |
| 686 |
|
| *ppOrderBy, pIdx, nEqCol, &bRev); |
| 687 |
|
| } |
| 688 |
4 |
▶ | if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){ |
Branch: 3 branches
| Branch | True | False | Total |
| if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){ |
0 |
4 |
4 |
| if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){ |
0 |
0 |
0 |
| if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){ |
0 |
0 |
0 |
|
| 689 |
0 |
▶ | if( pIdx==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pIdx==0 ){ |
0 |
0 |
0 |
|
| 690 |
0 |
| pWInfo->a[0].pIdx = pSortIdx; |
| 691 |
0 |
| pWInfo->a[0].iCur = pParse->nTab++; |
| 692 |
0 |
| pWInfo->peakNTab = pParse->nTab; |
| 693 |
|
| } |
| 694 |
0 |
| pWInfo->a[0].bRev = bRev; |
| 695 |
0 |
| *ppOrderBy = 0; |
| 696 |
|
| } |
| 697 |
|
| } |
| 698 |
|
| |
| 699 |
|
| /* Open all tables in the pTabList and all indices used by those tables. |
| 700 |
|
| */ |
| 701 |
260 |
▶ | for(i=0; i<pTabList->nSrc; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pTabList->nSrc; i++){ |
119 |
141 |
260 |
|
| 702 |
|
| Table *pTab; |
| 703 |
|
| Index *pIx; |
| 704 |
|
| |
| 705 |
119 |
| pTab = pTabList->a[i].pTab; |
| 706 |
119 |
▶ | if( pTab->isTransient || pTab->pSelect ) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if( pTab->isTransient || pTab->pSelect ) continue; |
116 |
3 |
119 |
| if( pTab->isTransient || pTab->pSelect ) continue; |
0 |
116 |
116 |
|
| 707 |
116 |
| sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
| 708 |
116 |
| sqliteVdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum, |
| 709 |
116 |
| pTab->zName, P3_STATIC); |
| 710 |
116 |
| sqliteCodeVerifySchema(pParse, pTab->iDb); |
| 711 |
116 |
▶ | if( (pIx = pWInfo->a[i].pIdx)!=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (pIx = pWInfo->a[i].pIdx)!=0 ){ |
0 |
116 |
116 |
|
| 712 |
0 |
| sqliteVdbeAddOp(v, OP_Integer, pIx->iDb, 0); |
| 713 |
0 |
| sqliteVdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum, pIx->zName,0); |
| 714 |
|
| } |
| 715 |
|
| } |
| 716 |
|
| |
| 717 |
|
| /* Generate the code to do the search |
| 718 |
|
| */ |
| 719 |
141 |
| loopMask = 0; |
| 720 |
260 |
▶ | for(i=0; i<pTabList->nSrc; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pTabList->nSrc; i++){ |
119 |
141 |
260 |
|
| 721 |
|
| int j, k; |
| 722 |
119 |
| int iCur = pTabList->a[i].iCursor; |
| 723 |
|
| Index *pIdx; |
| 724 |
119 |
| WhereLevel *pLevel = &pWInfo->a[i]; |
| 725 |
|
| |
| 726 |
|
| /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 727 |
|
| ** initialize a memory cell that records if this table matches any |
| 728 |
|
| ** row of the left table of the join. |
| 729 |
|
| */ |
| 730 |
119 |
▶ | if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){ |
1 |
118 |
119 |
| if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){ |
0 |
1 |
1 |
|
| 731 |
0 |
▶ | if( !pParse->nMem ) pParse->nMem++; |
Branch: 1 branch
| Branch | True | False | Total |
| if( !pParse->nMem ) pParse->nMem++; |
0 |
0 |
0 |
|
| 732 |
0 |
| pLevel->iLeftJoin = pParse->nMem++; |
| 733 |
0 |
| sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 734 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
| 735 |
|
| } |
| 736 |
|
| |
| 737 |
119 |
| pIdx = pLevel->pIdx; |
| 738 |
119 |
| pLevel->inOp = OP_Noop; |
| 739 |
119 |
▶ | if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){ |
119 |
0 |
119 |
| if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){ |
0 |
119 |
119 |
|
| 740 |
|
| /* Case 1: We can directly reference a single row using an |
| 741 |
|
| ** equality comparison against the ROWID field. Or |
| 742 |
|
| ** we reference multiple rows using a "rowid IN (...)" |
| 743 |
|
| ** construct. |
| 744 |
|
| */ |
| 745 |
0 |
| k = iDirectEq[i]; |
| 746 |
|
| assert( k<nExpr ); |
| 747 |
|
| assert( aExpr[k].p!=0 ); |
| 748 |
|
| assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur ); |
| 749 |
0 |
| brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 750 |
0 |
▶ | if( aExpr[k].idxLeft==iCur ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur ){ |
0 |
0 |
0 |
|
| 751 |
0 |
| Expr *pX = aExpr[k].p; |
| 752 |
0 |
▶ | if( pX->op!=TK_IN ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pX->op!=TK_IN ){ |
0 |
0 |
0 |
|
| 753 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 754 |
0 |
▶ | }else if( pX->pList ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pX->pList ){ |
0 |
0 |
0 |
|
| 755 |
0 |
| sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk); |
| 756 |
0 |
| pLevel->inOp = OP_SetNext; |
| 757 |
0 |
| pLevel->inP1 = pX->iTable; |
| 758 |
0 |
| pLevel->inP2 = sqliteVdbeCurrentAddr(v); |
| 759 |
|
| }else{ |
| 760 |
|
| assert( pX->pSelect ); |
| 761 |
0 |
| sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk); |
| 762 |
0 |
| sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1); |
| 763 |
0 |
| pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0); |
| 764 |
0 |
| pLevel->inOp = OP_Next; |
| 765 |
0 |
| pLevel->inP1 = pX->iTable; |
| 766 |
|
| } |
| 767 |
|
| }else{ |
| 768 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 769 |
|
| } |
| 770 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 771 |
0 |
| cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 772 |
0 |
| sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk); |
| 773 |
0 |
| haveKey = 0; |
| 774 |
0 |
| sqliteVdbeAddOp(v, OP_NotExists, iCur, brk); |
| 775 |
0 |
| pLevel->op = OP_Noop; |
| 776 |
119 |
▶ | }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){ |
Branch: 3 branches
| Branch | True | False | Total |
| }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){ |
0 |
119 |
119 |
| }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){ |
0 |
0 |
0 |
| }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){ |
0 |
0 |
0 |
|
| 777 |
|
| /* Case 2: There is an index and all terms of the WHERE clause that |
| 778 |
|
| ** refer to the index use the "==" or "IN" operators. |
| 779 |
|
| */ |
| 780 |
|
| int start; |
| 781 |
|
| int testOp; |
| 782 |
0 |
| int nColumn = (pLevel->score+4)/8; |
| 783 |
0 |
| brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 784 |
0 |
▶ | for(j=0; j<nColumn; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nColumn; j++){ |
0 |
0 |
0 |
|
| 785 |
0 |
▶ | for(k=0; k<nExpr; k++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<nExpr; k++){ |
0 |
0 |
0 |
|
| 786 |
0 |
| Expr *pX = aExpr[k].p; |
| 787 |
0 |
▶ | if( pX==0 ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pX==0 ) continue; |
0 |
0 |
0 |
|
| 788 |
0 |
▶ | if( aExpr[k].idxLeft==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur |
0 |
0 |
0 |
|
| 789 |
0 |
▶ | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
0 |
0 |
0 |
|
| 790 |
0 |
▶ | && pX->pLeft->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && pX->pLeft->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 791 |
|
| ){ |
| 792 |
0 |
▶ | if( pX->op==TK_EQ ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pX->op==TK_EQ ){ |
0 |
0 |
0 |
|
| 793 |
0 |
| sqliteExprCode(pParse, pX->pRight); |
| 794 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 795 |
0 |
| break; |
| 796 |
|
| } |
| 797 |
0 |
▶ | if( pX->op==TK_IN && nColumn==1 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pX->op==TK_IN && nColumn==1 ){ |
0 |
0 |
0 |
| if( pX->op==TK_IN && nColumn==1 ){ |
0 |
0 |
0 |
|
| 798 |
0 |
▶ | if( pX->pList ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pX->pList ){ |
0 |
0 |
0 |
|
| 799 |
0 |
| sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk); |
| 800 |
0 |
| pLevel->inOp = OP_SetNext; |
| 801 |
0 |
| pLevel->inP1 = pX->iTable; |
| 802 |
0 |
| pLevel->inP2 = sqliteVdbeCurrentAddr(v); |
| 803 |
|
| }else{ |
| 804 |
|
| assert( pX->pSelect ); |
| 805 |
0 |
| sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk); |
| 806 |
0 |
| sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1); |
| 807 |
0 |
| pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0); |
| 808 |
0 |
| pLevel->inOp = OP_Next; |
| 809 |
0 |
| pLevel->inP1 = pX->iTable; |
| 810 |
|
| } |
| 811 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 812 |
0 |
| break; |
| 813 |
|
| } |
| 814 |
|
| } |
| 815 |
0 |
▶ | if( aExpr[k].idxRight==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxRight==iCur |
0 |
0 |
0 |
|
| 816 |
0 |
▶ | && aExpr[k].p->op==TK_EQ |
Branch: 1 branch
| Branch | True | False | Total |
| && aExpr[k].p->op==TK_EQ |
0 |
0 |
0 |
|
| 817 |
0 |
▶ | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
0 |
0 |
0 |
|
| 818 |
0 |
▶ | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 819 |
|
| ){ |
| 820 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 821 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 822 |
0 |
| break; |
| 823 |
|
| } |
| 824 |
|
| } |
| 825 |
|
| } |
| 826 |
0 |
| pLevel->iMem = pParse->nMem++; |
| 827 |
0 |
| cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 828 |
0 |
| sqliteVdbeAddOp(v, OP_NotNull, -nColumn, sqliteVdbeCurrentAddr(v)+3); |
| 829 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nColumn, 0); |
| 830 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, brk); |
| 831 |
0 |
| sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0); |
| 832 |
0 |
| sqliteAddIdxKeyType(v, pIdx); |
| 833 |
0 |
▶ | if( nColumn==pIdx->nColumn || pLevel->bRev ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( nColumn==pIdx->nColumn || pLevel->bRev ){ |
0 |
0 |
0 |
| if( nColumn==pIdx->nColumn || pLevel->bRev ){ |
0 |
0 |
0 |
|
| 834 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0); |
| 835 |
0 |
| testOp = OP_IdxGT; |
| 836 |
|
| }else{ |
| 837 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
| 838 |
0 |
| sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 839 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 840 |
0 |
| testOp = OP_IdxGE; |
| 841 |
|
| } |
| 842 |
0 |
▶ | if( pLevel->bRev ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->bRev ){ |
0 |
0 |
0 |
|
| 843 |
|
| /* Scan in reverse order */ |
| 844 |
0 |
| sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 845 |
0 |
| sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk); |
| 846 |
0 |
| start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 847 |
0 |
| sqliteVdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk); |
| 848 |
0 |
| pLevel->op = OP_Prev; |
| 849 |
|
| }else{ |
| 850 |
|
| /* Scan in the forward order */ |
| 851 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 852 |
0 |
| start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 853 |
0 |
| sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
| 854 |
0 |
| pLevel->op = OP_Next; |
| 855 |
|
| } |
| 856 |
0 |
| sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0); |
| 857 |
0 |
| sqliteVdbeAddOp(v, OP_IdxIsNull, nColumn, cont); |
| 858 |
0 |
| sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
| 859 |
0 |
▶ | if( i==pTabList->nSrc-1 && pushKey ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( i==pTabList->nSrc-1 && pushKey ){ |
0 |
0 |
0 |
| if( i==pTabList->nSrc-1 && pushKey ){ |
0 |
0 |
0 |
|
| 860 |
0 |
| haveKey = 1; |
| 861 |
|
| }else{ |
| 862 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0); |
| 863 |
0 |
| haveKey = 0; |
| 864 |
|
| } |
| 865 |
0 |
| pLevel->p1 = pLevel->iCur; |
| 866 |
0 |
| pLevel->p2 = start; |
| 867 |
119 |
▶ | }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
Branch: 3 branches
| Branch | True | False | Total |
| }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
119 |
0 |
119 |
| }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
119 |
0 |
119 |
| }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){ |
0 |
119 |
119 |
|
| 868 |
|
| /* Case 3: We have an inequality comparison against the ROWID field. |
| 869 |
|
| */ |
| 870 |
0 |
| int testOp = OP_Noop; |
| 871 |
|
| int start; |
| 872 |
|
| |
| 873 |
0 |
| brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 874 |
0 |
| cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 875 |
0 |
▶ | if( iDirectGt[i]>=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( iDirectGt[i]>=0 ){ |
0 |
0 |
0 |
|
| 876 |
0 |
| k = iDirectGt[i]; |
| 877 |
|
| assert( k<nExpr ); |
| 878 |
|
| assert( aExpr[k].p!=0 ); |
| 879 |
|
| assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur ); |
| 880 |
0 |
▶ | if( aExpr[k].idxLeft==iCur ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur ){ |
0 |
0 |
0 |
|
| 881 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 882 |
|
| }else{ |
| 883 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 884 |
|
| } |
| 885 |
0 |
| sqliteVdbeAddOp(v, OP_ForceInt, |
| 886 |
0 |
▶ | aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk); |
Branch: 2 branches
| Branch | True | False | Total |
| aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk); |
0 |
0 |
0 |
| aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk); |
0 |
0 |
0 |
|
| 887 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, iCur, brk); |
| 888 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 889 |
|
| }else{ |
| 890 |
0 |
| sqliteVdbeAddOp(v, OP_Rewind, iCur, brk); |
| 891 |
|
| } |
| 892 |
0 |
▶ | if( iDirectLt[i]>=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( iDirectLt[i]>=0 ){ |
0 |
0 |
0 |
|
| 893 |
0 |
| k = iDirectLt[i]; |
| 894 |
|
| assert( k<nExpr ); |
| 895 |
|
| assert( aExpr[k].p!=0 ); |
| 896 |
|
| assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur ); |
| 897 |
0 |
▶ | if( aExpr[k].idxLeft==iCur ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur ){ |
0 |
0 |
0 |
|
| 898 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 899 |
|
| }else{ |
| 900 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 901 |
|
| } |
| 902 |
|
| /* sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1); */ |
| 903 |
0 |
| pLevel->iMem = pParse->nMem++; |
| 904 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 905 |
0 |
▶ | if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
0 |
0 |
0 |
| if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){ |
0 |
0 |
0 |
|
| 906 |
0 |
| testOp = OP_Ge; |
| 907 |
|
| }else{ |
| 908 |
0 |
| testOp = OP_Gt; |
| 909 |
|
| } |
| 910 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 911 |
|
| } |
| 912 |
0 |
| start = sqliteVdbeCurrentAddr(v); |
| 913 |
0 |
| pLevel->op = OP_Next; |
| 914 |
0 |
| pLevel->p1 = iCur; |
| 915 |
0 |
| pLevel->p2 = start; |
| 916 |
0 |
▶ | if( testOp!=OP_Noop ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( testOp!=OP_Noop ){ |
0 |
0 |
0 |
|
| 917 |
0 |
| sqliteVdbeAddOp(v, OP_Recno, iCur, 0); |
| 918 |
0 |
| sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 919 |
0 |
| sqliteVdbeAddOp(v, testOp, 0, brk); |
| 920 |
|
| } |
| 921 |
0 |
| haveKey = 0; |
| 922 |
119 |
▶ | }else if( pIdx==0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pIdx==0 ){ |
119 |
0 |
119 |
|
| 923 |
|
| /* Case 4: There is no usable index. We must do a complete |
| 924 |
|
| ** scan of the entire database table. |
| 925 |
|
| */ |
| 926 |
|
| int start; |
| 927 |
|
| |
| 928 |
119 |
| brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 929 |
119 |
| cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 930 |
119 |
| sqliteVdbeAddOp(v, OP_Rewind, iCur, brk); |
| 931 |
119 |
| start = sqliteVdbeCurrentAddr(v); |
| 932 |
119 |
| pLevel->op = OP_Next; |
| 933 |
119 |
| pLevel->p1 = iCur; |
| 934 |
119 |
| pLevel->p2 = start; |
| 935 |
119 |
| haveKey = 0; |
| 936 |
|
| }else{ |
| 937 |
|
| /* Case 5: The WHERE clause term that refers to the right-most |
| 938 |
|
| ** column of the index is an inequality. For example, if |
| 939 |
|
| ** the index is on (x,y,z) and the WHERE clause is of the |
| 940 |
|
| ** form "x=5 AND y<10" then this case is used. Only the |
| 941 |
|
| ** right-most column can be an inequality - the rest must |
| 942 |
|
| ** use the "==" operator. |
| 943 |
|
| ** |
| 944 |
|
| ** This case is also used when there are no WHERE clause |
| 945 |
|
| ** constraints but an index is selected anyway, in order |
| 946 |
|
| ** to force the output order to conform to an ORDER BY. |
| 947 |
|
| */ |
| 948 |
0 |
| int score = pLevel->score; |
| 949 |
0 |
| int nEqColumn = score/8; |
| 950 |
|
| int start; |
| 951 |
|
| int leFlag, geFlag; |
| 952 |
|
| int testOp; |
| 953 |
|
| |
| 954 |
|
| /* Evaluate the equality constraints |
| 955 |
|
| */ |
| 956 |
0 |
▶ | for(j=0; j<nEqColumn; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nEqColumn; j++){ |
0 |
0 |
0 |
|
| 957 |
0 |
▶ | for(k=0; k<nExpr; k++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<nExpr; k++){ |
0 |
0 |
0 |
|
| 958 |
0 |
▶ | if( aExpr[k].p==0 ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].p==0 ) continue; |
0 |
0 |
0 |
|
| 959 |
0 |
▶ | if( aExpr[k].idxLeft==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur |
0 |
0 |
0 |
|
| 960 |
0 |
▶ | && aExpr[k].p->op==TK_EQ |
Branch: 1 branch
| Branch | True | False | Total |
| && aExpr[k].p->op==TK_EQ |
0 |
0 |
0 |
|
| 961 |
0 |
▶ | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
0 |
0 |
0 |
|
| 962 |
0 |
▶ | && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 963 |
|
| ){ |
| 964 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 965 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 966 |
0 |
| break; |
| 967 |
|
| } |
| 968 |
0 |
▶ | if( aExpr[k].idxRight==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxRight==iCur |
0 |
0 |
0 |
|
| 969 |
0 |
▶ | && aExpr[k].p->op==TK_EQ |
Branch: 1 branch
| Branch | True | False | Total |
| && aExpr[k].p->op==TK_EQ |
0 |
0 |
0 |
|
| 970 |
0 |
▶ | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
0 |
0 |
0 |
|
| 971 |
0 |
▶ | && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 972 |
|
| ){ |
| 973 |
0 |
| sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 974 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 975 |
0 |
| break; |
| 976 |
|
| } |
| 977 |
|
| } |
| 978 |
|
| } |
| 979 |
|
| |
| 980 |
|
| /* Duplicate the equality term values because they will all be |
| 981 |
|
| ** used twice: once to make the termination key and once to make the |
| 982 |
|
| ** start key. |
| 983 |
|
| */ |
| 984 |
0 |
▶ | for(j=0; j<nEqColumn; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nEqColumn; j++){ |
0 |
0 |
0 |
|
| 985 |
0 |
| sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0); |
| 986 |
|
| } |
| 987 |
|
| |
| 988 |
|
| /* Labels for the beginning and end of the loop |
| 989 |
|
| */ |
| 990 |
0 |
| cont = pLevel->cont = sqliteVdbeMakeLabel(v); |
| 991 |
0 |
| brk = pLevel->brk = sqliteVdbeMakeLabel(v); |
| 992 |
|
| |
| 993 |
|
| /* Generate the termination key. This is the key value that |
| 994 |
|
| ** will end the search. There is no termination key if there |
| 995 |
|
| ** are no equality terms and no "X<..." term. |
| 996 |
|
| ** |
| 997 |
|
| ** 2002-Dec-04: On a reverse-order scan, the so-called "termination" |
| 998 |
|
| ** key computed here really ends up being the start key. |
| 999 |
|
| */ |
| 1000 |
0 |
▶ | if( (score & 1)!=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (score & 1)!=0 ){ |
0 |
0 |
0 |
|
| 1001 |
0 |
▶ | for(k=0; k<nExpr; k++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<nExpr; k++){ |
0 |
0 |
0 |
|
| 1002 |
0 |
| Expr *pExpr = aExpr[k].p; |
| 1003 |
0 |
▶ | if( pExpr==0 ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pExpr==0 ) continue; |
0 |
0 |
0 |
|
| 1004 |
0 |
▶ | if( aExpr[k].idxLeft==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur |
0 |
0 |
0 |
|
| 1005 |
0 |
▶ | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
Branch: 2 branches
| Branch | True | False | Total |
| && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
0 |
0 |
0 |
| && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
0 |
0 |
0 |
|
| 1006 |
0 |
▶ | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
0 |
0 |
0 |
|
| 1007 |
0 |
▶ | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 1008 |
|
| ){ |
| 1009 |
0 |
| sqliteExprCode(pParse, pExpr->pRight); |
| 1010 |
0 |
| leFlag = pExpr->op==TK_LE; |
| 1011 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 1012 |
0 |
| break; |
| 1013 |
|
| } |
| 1014 |
0 |
▶ | if( aExpr[k].idxRight==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxRight==iCur |
0 |
0 |
0 |
|
| 1015 |
0 |
▶ | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
Branch: 2 branches
| Branch | True | False | Total |
| && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
0 |
0 |
0 |
| && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
0 |
0 |
0 |
|
| 1016 |
0 |
▶ | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
0 |
0 |
0 |
|
| 1017 |
0 |
▶ | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 1018 |
|
| ){ |
| 1019 |
0 |
| sqliteExprCode(pParse, pExpr->pLeft); |
| 1020 |
0 |
| leFlag = pExpr->op==TK_GE; |
| 1021 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 1022 |
0 |
| break; |
| 1023 |
|
| } |
| 1024 |
|
| } |
| 1025 |
0 |
| testOp = OP_IdxGE; |
| 1026 |
|
| }else{ |
| 1027 |
0 |
▶ | testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop; |
Branch: 1 branch
| Branch | True | False | Total |
| testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop; |
0 |
0 |
0 |
|
| 1028 |
0 |
| leFlag = 1; |
| 1029 |
|
| } |
| 1030 |
0 |
▶ | if( testOp!=OP_Noop ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( testOp!=OP_Noop ){ |
0 |
0 |
0 |
|
| 1031 |
0 |
| int nCol = nEqColumn + (score & 1); |
| 1032 |
0 |
| pLevel->iMem = pParse->nMem++; |
| 1033 |
0 |
| sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3); |
| 1034 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nCol, 0); |
| 1035 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, brk); |
| 1036 |
0 |
| sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0); |
| 1037 |
0 |
| sqliteAddIdxKeyType(v, pIdx); |
| 1038 |
0 |
▶ | if( leFlag ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( leFlag ){ |
0 |
0 |
0 |
|
| 1039 |
0 |
| sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 1040 |
|
| } |
| 1041 |
0 |
▶ | if( pLevel->bRev ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->bRev ){ |
0 |
0 |
0 |
|
| 1042 |
0 |
| sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk); |
| 1043 |
|
| }else{ |
| 1044 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 1045 |
|
| } |
| 1046 |
0 |
▶ | }else if( pLevel->bRev ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pLevel->bRev ){ |
0 |
0 |
0 |
|
| 1047 |
0 |
| sqliteVdbeAddOp(v, OP_Last, pLevel->iCur, brk); |
| 1048 |
|
| } |
| 1049 |
|
| |
| 1050 |
|
| /* Generate the start key. This is the key that defines the lower |
| 1051 |
|
| ** bound on the search. There is no start key if there are no |
| 1052 |
|
| ** equality terms and if there is no "X>..." term. In |
| 1053 |
|
| ** that case, generate a "Rewind" instruction in place of the |
| 1054 |
|
| ** start key search. |
| 1055 |
|
| ** |
| 1056 |
|
| ** 2002-Dec-04: In the case of a reverse-order search, the so-called |
| 1057 |
|
| ** "start" key really ends up being used as the termination key. |
| 1058 |
|
| */ |
| 1059 |
0 |
▶ | if( (score & 2)!=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( (score & 2)!=0 ){ |
0 |
0 |
0 |
|
| 1060 |
0 |
▶ | for(k=0; k<nExpr; k++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(k=0; k<nExpr; k++){ |
0 |
0 |
0 |
|
| 1061 |
0 |
| Expr *pExpr = aExpr[k].p; |
| 1062 |
0 |
▶ | if( pExpr==0 ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( pExpr==0 ) continue; |
0 |
0 |
0 |
|
| 1063 |
0 |
▶ | if( aExpr[k].idxLeft==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxLeft==iCur |
0 |
0 |
0 |
|
| 1064 |
0 |
▶ | && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
Branch: 2 branches
| Branch | True | False | Total |
| && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
0 |
0 |
0 |
| && (pExpr->op==TK_GT || pExpr->op==TK_GE) |
0 |
0 |
0 |
|
| 1065 |
0 |
▶ | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
0 |
0 |
0 |
|
| 1066 |
0 |
▶ | && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && pExpr->pLeft->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 1067 |
|
| ){ |
| 1068 |
0 |
| sqliteExprCode(pParse, pExpr->pRight); |
| 1069 |
0 |
| geFlag = pExpr->op==TK_GE; |
| 1070 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 1071 |
0 |
| break; |
| 1072 |
|
| } |
| 1073 |
0 |
▶ | if( aExpr[k].idxRight==iCur |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[k].idxRight==iCur |
0 |
0 |
0 |
|
| 1074 |
0 |
▶ | && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
Branch: 2 branches
| Branch | True | False | Total |
| && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
0 |
0 |
0 |
| && (pExpr->op==TK_LT || pExpr->op==TK_LE) |
0 |
0 |
0 |
|
| 1075 |
0 |
▶ | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
Branch: 1 branch
| Branch | True | False | Total |
| && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
0 |
0 |
0 |
|
| 1076 |
0 |
▶ | && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
Branch: 1 branch
| Branch | True | False | Total |
| && pExpr->pRight->iColumn==pIdx->aiColumn[j] |
0 |
0 |
0 |
|
| 1077 |
|
| ){ |
| 1078 |
0 |
| sqliteExprCode(pParse, pExpr->pLeft); |
| 1079 |
0 |
| geFlag = pExpr->op==TK_LE; |
| 1080 |
0 |
| disableTerm(pLevel, &aExpr[k].p); |
| 1081 |
0 |
| break; |
| 1082 |
|
| } |
| 1083 |
|
| } |
| 1084 |
|
| }else{ |
| 1085 |
0 |
| geFlag = 1; |
| 1086 |
|
| } |
| 1087 |
0 |
▶ | if( nEqColumn>0 || (score&2)!=0 ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( nEqColumn>0 || (score&2)!=0 ){ |
0 |
0 |
0 |
| if( nEqColumn>0 || (score&2)!=0 ){ |
0 |
0 |
0 |
|
| 1088 |
0 |
| int nCol = nEqColumn + ((score&2)!=0); |
| 1089 |
0 |
| sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3); |
| 1090 |
0 |
| sqliteVdbeAddOp(v, OP_Pop, nCol, 0); |
| 1091 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, brk); |
| 1092 |
0 |
| sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0); |
| 1093 |
0 |
| sqliteAddIdxKeyType(v, pIdx); |
| 1094 |
0 |
▶ | if( !geFlag ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( !geFlag ){ |
0 |
0 |
0 |
|
| 1095 |
0 |
| sqliteVdbeAddOp(v, OP_IncrKey, 0, 0); |
| 1096 |
|
| } |
| 1097 |
0 |
▶ | if( pLevel->bRev ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->bRev ){ |
0 |
0 |
0 |
|
| 1098 |
0 |
| pLevel->iMem = pParse->nMem++; |
| 1099 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1); |
| 1100 |
0 |
| testOp = OP_IdxLT; |
| 1101 |
|
| }else{ |
| 1102 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk); |
| 1103 |
|
| } |
| 1104 |
0 |
▶ | }else if( pLevel->bRev ){ |
Branch: 1 branch
| Branch | True | False | Total |
| }else if( pLevel->bRev ){ |
0 |
0 |
0 |
|
| 1105 |
0 |
| testOp = OP_Noop; |
| 1106 |
|
| }else{ |
| 1107 |
0 |
| sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk); |
| 1108 |
|
| } |
| 1109 |
|
| |
| 1110 |
|
| /* Generate the the top of the loop. If there is a termination |
| 1111 |
|
| ** key we have to test for that key and abort at the top of the |
| 1112 |
|
| ** loop. |
| 1113 |
|
| */ |
| 1114 |
0 |
| start = sqliteVdbeCurrentAddr(v); |
| 1115 |
0 |
▶ | if( testOp!=OP_Noop ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( testOp!=OP_Noop ){ |
0 |
0 |
0 |
|
| 1116 |
0 |
| sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0); |
| 1117 |
0 |
| sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk); |
| 1118 |
|
| } |
| 1119 |
0 |
| sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0); |
| 1120 |
0 |
| sqliteVdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont); |
| 1121 |
0 |
| sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0); |
| 1122 |
0 |
▶ | if( i==pTabList->nSrc-1 && pushKey ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( i==pTabList->nSrc-1 && pushKey ){ |
0 |
0 |
0 |
| if( i==pTabList->nSrc-1 && pushKey ){ |
0 |
0 |
0 |
|
| 1123 |
0 |
| haveKey = 1; |
| 1124 |
|
| }else{ |
| 1125 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0); |
| 1126 |
0 |
| haveKey = 0; |
| 1127 |
|
| } |
| 1128 |
|
| |
| 1129 |
|
| /* Record the instruction used to terminate the loop. |
| 1130 |
|
| */ |
| 1131 |
0 |
▶ | pLevel->op = pLevel->bRev ? OP_Prev : OP_Next; |
Branch: 1 branch
| Branch | True | False | Total |
| pLevel->op = pLevel->bRev ? OP_Prev : OP_Next; |
0 |
0 |
0 |
|
| 1132 |
0 |
| pLevel->p1 = pLevel->iCur; |
| 1133 |
0 |
| pLevel->p2 = start; |
| 1134 |
|
| } |
| 1135 |
119 |
| loopMask |= getMask(&maskSet, iCur); |
| 1136 |
|
| |
| 1137 |
|
| /* Insert code to test every subexpression that can be completely |
| 1138 |
|
| ** computed using the current set of tables. |
| 1139 |
|
| */ |
| 1140 |
156 |
▶ | for(j=0; j<nExpr; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nExpr; j++){ |
37 |
119 |
156 |
|
| 1141 |
37 |
▶ | if( aExpr[j].p==0 ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[j].p==0 ) continue; |
0 |
37 |
37 |
|
| 1142 |
37 |
▶ | if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue; |
1 |
36 |
37 |
|
| 1143 |
36 |
▶ | if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){ |
0 |
36 |
36 |
| if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){ |
0 |
0 |
0 |
|
| 1144 |
0 |
| continue; |
| 1145 |
|
| } |
| 1146 |
36 |
▶ | if( haveKey ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( haveKey ){ |
0 |
36 |
36 |
|
| 1147 |
0 |
| haveKey = 0; |
| 1148 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0); |
| 1149 |
|
| } |
| 1150 |
36 |
| sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1); |
| 1151 |
36 |
| aExpr[j].p = 0; |
| 1152 |
|
| } |
| 1153 |
119 |
| brk = cont; |
| 1154 |
|
| |
| 1155 |
|
| /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 1156 |
|
| ** at least one row of the right table has matched the left table. |
| 1157 |
|
| */ |
| 1158 |
119 |
▶ | if( pLevel->iLeftJoin ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->iLeftJoin ){ |
0 |
119 |
119 |
|
| 1159 |
0 |
| pLevel->top = sqliteVdbeCurrentAddr(v); |
| 1160 |
0 |
| sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
| 1161 |
0 |
| sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1); |
| 1162 |
0 |
▶ | for(j=0; j<nExpr; j++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<nExpr; j++){ |
0 |
0 |
0 |
|
| 1163 |
0 |
▶ | if( aExpr[j].p==0 ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( aExpr[j].p==0 ) continue; |
0 |
0 |
0 |
|
| 1164 |
0 |
▶ | if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue; |
0 |
0 |
0 |
|
| 1165 |
0 |
▶ | if( haveKey ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( haveKey ){ |
0 |
0 |
0 |
|
| 1166 |
|
| /* Cannot happen. "haveKey" can only be true if pushKey is true |
| 1167 |
|
| ** an pushKey can only be true for DELETE and UPDATE and there are |
| 1168 |
|
| ** no outer joins with DELETE and UPDATE. |
| 1169 |
|
| */ |
| 1170 |
0 |
| haveKey = 0; |
| 1171 |
0 |
| sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0); |
| 1172 |
|
| } |
| 1173 |
0 |
| sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1); |
| 1174 |
0 |
| aExpr[j].p = 0; |
| 1175 |
|
| } |
| 1176 |
|
| } |
| 1177 |
|
| } |
| 1178 |
141 |
| pWInfo->iContinue = cont; |
| 1179 |
141 |
▶ | if( pushKey && !haveKey ){ |
Branch: 2 branches
| Branch | True | False | Total |
| if( pushKey && !haveKey ){ |
6 |
135 |
141 |
| if( pushKey && !haveKey ){ |
6 |
0 |
6 |
|
| 1180 |
6 |
| sqliteVdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0); |
| 1181 |
|
| } |
| 1182 |
|
| freeMaskSet(&maskSet); |
| 1183 |
141 |
| return pWInfo; |
| 1184 |
|
| } |
| 1185 |
|
| |
| 1186 |
|
| /* |
| 1187 |
|
| ** Generate the end of the WHERE loop. See comments on |
| 1188 |
|
| ** sqliteWhereBegin() for additional information. |
| 1189 |
|
| */ |
| 1190 |
141 |
| void sqliteWhereEnd(WhereInfo *pWInfo){ |
| 1191 |
141 |
| Vdbe *v = pWInfo->pParse->pVdbe; |
| 1192 |
|
| int i; |
| 1193 |
|
| WhereLevel *pLevel; |
| 1194 |
141 |
| SrcList *pTabList = pWInfo->pTabList; |
| 1195 |
|
| |
| 1196 |
260 |
▶ | for(i=pTabList->nSrc-1; i>=0; i--){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=pTabList->nSrc-1; i>=0; i--){ |
119 |
141 |
260 |
|
| 1197 |
119 |
| pLevel = &pWInfo->a[i]; |
| 1198 |
119 |
| sqliteVdbeResolveLabel(v, pLevel->cont); |
| 1199 |
119 |
▶ | if( pLevel->op!=OP_Noop ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->op!=OP_Noop ){ |
119 |
0 |
119 |
|
| 1200 |
119 |
| sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2); |
| 1201 |
|
| } |
| 1202 |
119 |
| sqliteVdbeResolveLabel(v, pLevel->brk); |
| 1203 |
119 |
▶ | if( pLevel->inOp!=OP_Noop ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->inOp!=OP_Noop ){ |
0 |
119 |
119 |
|
| 1204 |
0 |
| sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2); |
| 1205 |
|
| } |
| 1206 |
119 |
▶ | if( pLevel->iLeftJoin ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->iLeftJoin ){ |
0 |
119 |
119 |
|
| 1207 |
|
| int addr; |
| 1208 |
0 |
| addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0); |
| 1209 |
0 |
| sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0)); |
| 1210 |
0 |
| sqliteVdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0); |
| 1211 |
0 |
▶ | if( pLevel->iCur>=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->iCur>=0 ){ |
0 |
0 |
0 |
|
| 1212 |
0 |
| sqliteVdbeAddOp(v, OP_NullRow, pLevel->iCur, 0); |
| 1213 |
|
| } |
| 1214 |
0 |
| sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top); |
| 1215 |
|
| } |
| 1216 |
|
| } |
| 1217 |
141 |
| sqliteVdbeResolveLabel(v, pWInfo->iBreak); |
| 1218 |
260 |
▶ | for(i=0; i<pTabList->nSrc; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<pTabList->nSrc; i++){ |
119 |
141 |
260 |
|
| 1219 |
119 |
| Table *pTab = pTabList->a[i].pTab; |
| 1220 |
|
| assert( pTab!=0 ); |
| 1221 |
119 |
▶ | if( pTab->isTransient || pTab->pSelect ) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if( pTab->isTransient || pTab->pSelect ) continue; |
116 |
3 |
119 |
| if( pTab->isTransient || pTab->pSelect ) continue; |
0 |
116 |
116 |
|
| 1222 |
116 |
| pLevel = &pWInfo->a[i]; |
| 1223 |
116 |
| sqliteVdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0); |
| 1224 |
116 |
▶ | if( pLevel->pIdx!=0 ){ |
Branch: 1 branch
| Branch | True | False | Total |
| if( pLevel->pIdx!=0 ){ |
0 |
116 |
116 |
|
| 1225 |
0 |
| sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0); |
| 1226 |
|
| } |
| 1227 |
|
| } |
| 1228 |
|
| #if 0 /* Never reuse a cursor */ |
| 1229 |
|
| if( pWInfo->pParse->nTab==pWInfo->peakNTab ){ |
| 1230 |
|
| pWInfo->pParse->nTab = pWInfo->savedNTab; |
| 1231 |
|
| } |
| 1232 |
|
| #endif |
| 1233 |
141 |
| sqliteFree(pWInfo); |
| 1234 |
141 |
| return; |
| 1235 |
|
| } |