drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (c) 1999, 2000 D. Richard Hipp |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or |
| 5 | ** modify it under the terms of the GNU General Public |
| 6 | ** License as published by the Free Software Foundation; either |
| 7 | ** version 2 of the License, or (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ** General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public |
| 15 | ** License along with this library; if not, write to the |
| 16 | ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | ** Boston, MA 02111-1307, USA. |
| 18 | ** |
| 19 | ** Author contact information: |
| 20 | ** drh@hwaci.com |
| 21 | ** http://www.hwaci.com/drh/ |
| 22 | ** |
| 23 | ************************************************************************* |
| 24 | ** This module contains C code that generates VDBE code used to process |
| 25 | ** the WHERE clause of SQL statements. Also found here are subroutines |
| 26 | ** to generate VDBE code to evaluate expressions. |
| 27 | ** |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | ** $Id: where.c,v 1.5 2000/05/31 15:34:54 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 29 | */ |
| 30 | #include "sqliteInt.h" |
| 31 | |
| 32 | /* |
| 33 | ** The query generator uses an array of instances of this structure to |
| 34 | ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 35 | ** clause subexpression is separated from the others by an AND operator. |
| 36 | */ |
| 37 | typedef struct ExprInfo ExprInfo; |
| 38 | struct ExprInfo { |
| 39 | Expr *p; /* Pointer to the subexpression */ |
| 40 | int indexable; /* True if this subexprssion is usable by an index */ |
| 41 | int idxLeft; /* p->pLeft is a field in this table number. -1 if |
| 42 | ** p->pLeft is not the field of any table */ |
| 43 | int idxRight; /* p->pRight is a field in this table number. -1 if |
| 44 | ** p->pRight is not the field of any table */ |
| 45 | unsigned prereqLeft; /* Tables referenced by p->pLeft */ |
| 46 | unsigned prereqRight; /* Tables referenced by p->pRight */ |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | ** Determine the number of elements in an array. |
| 51 | */ |
| 52 | #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0])) |
| 53 | |
| 54 | /* |
| 55 | ** This routine is used to divide the WHERE expression into subexpressions |
| 56 | ** separated by the AND operator. |
| 57 | ** |
| 58 | ** aSlot[] is an array of subexpressions structures. |
| 59 | ** There are nSlot spaces left in this array. This routine attempts to |
| 60 | ** split pExpr into subexpressions and fills aSlot[] with those subexpressions. |
| 61 | ** The return value is the number of slots filled. |
| 62 | */ |
| 63 | static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){ |
| 64 | int cnt = 0; |
| 65 | if( pExpr==0 || nSlot<1 ) return 0; |
| 66 | if( nSlot==1 || pExpr->op!=TK_AND ){ |
| 67 | aSlot[0].p = pExpr; |
| 68 | return 1; |
| 69 | } |
| 70 | if( pExpr->pLeft->op!=TK_AND ){ |
| 71 | aSlot[0].p = pExpr->pLeft; |
| 72 | cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight); |
| 73 | }else{ |
| 74 | cnt = exprSplit(nSlot, aSlot, pExpr->pRight); |
| 75 | cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft); |
| 76 | } |
| 77 | return cnt; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | ** This routine walks (recursively) an expression tree and generates |
| 82 | ** a bitmask indicating which tables are used in that expression |
| 83 | ** tree. Bit 0 of the mask is set if table 0 is used. But 1 is set |
| 84 | ** if table 1 is used. And so forth. |
| 85 | ** |
| 86 | ** In order for this routine to work, the calling function must have |
| 87 | ** previously invoked sqliteExprResolveIds() on the expression. See |
| 88 | ** the header comment on that routine for additional information. |
| 89 | */ |
| 90 | static int exprTableUsage(Expr *p){ |
| 91 | unsigned int mask = 0; |
| 92 | if( p==0 ) return 0; |
| 93 | if( p->op==TK_FIELD ){ |
| 94 | return 1<<p->iTable; |
| 95 | } |
| 96 | if( p->pRight ){ |
| 97 | mask = exprTableUsage(p->pRight); |
| 98 | } |
| 99 | if( p->pLeft ){ |
| 100 | mask |= exprTableUsage(p->pLeft); |
| 101 | } |
| 102 | return mask; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | ** The input to this routine is an ExprInfo structure with only the |
| 107 | ** "p" field filled in. The job of this routine is to analyze the |
| 108 | ** subexpression and populate all the other fields of the ExprInfo |
| 109 | ** structure. |
| 110 | */ |
| 111 | static void exprAnalyze(ExprInfo *pInfo){ |
| 112 | Expr *pExpr = pInfo->p; |
| 113 | pInfo->prereqLeft = exprTableUsage(pExpr->pLeft); |
| 114 | pInfo->prereqRight = exprTableUsage(pExpr->pRight); |
| 115 | pInfo->indexable = 0; |
| 116 | pInfo->idxLeft = -1; |
| 117 | pInfo->idxRight = -1; |
| 118 | if( pExpr->op==TK_EQ && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){ |
| 119 | if( pExpr->pRight->op==TK_FIELD ){ |
| 120 | pInfo->idxRight = pExpr->pRight->iTable; |
| 121 | pInfo->indexable = 1; |
| 122 | } |
| 123 | if( pExpr->pLeft->op==TK_FIELD ){ |
| 124 | pInfo->idxLeft = pExpr->pLeft->iTable; |
| 125 | pInfo->indexable = 1; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** Generating the beginning of the loop used for WHERE clause processing. |
| 132 | ** The return value is a pointer to an (opaque) structure that contains |
| 133 | ** information needed to terminate the loop. Later, the calling routine |
| 134 | ** should invoke sqliteWhereEnd() with the return value of this function |
| 135 | ** in order to complete the WHERE clause processing. |
| 136 | ** |
| 137 | ** If an error occurs, this routine returns NULL. |
| 138 | */ |
| 139 | WhereInfo *sqliteWhereBegin( |
| 140 | Parse *pParse, /* The parser context */ |
| 141 | IdList *pTabList, /* A list of all tables */ |
| 142 | Expr *pWhere, /* The WHERE clause */ |
| 143 | int pushKey /* If TRUE, leave the table key on the stack */ |
| 144 | ){ |
| 145 | int i; /* Loop counter */ |
| 146 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 147 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 148 | int brk, cont; /* Addresses used during code generation */ |
| 149 | int *aOrder; /* Order in which pTabList entries are searched */ |
| 150 | int nExpr; /* Number of subexpressions in the WHERE clause */ |
| 151 | int loopMask; /* One bit set for each outer loop */ |
| 152 | int haveKey; /* True if KEY is on the stack */ |
| 153 | Index *aIdx[32]; /* Index to use on each nested loop. */ |
| 154 | ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */ |
| 155 | |
| 156 | /* Allocate space for aOrder[]. */ |
| 157 | aOrder = sqliteMalloc( sizeof(int) * pTabList->nId ); |
| 158 | |
| 159 | /* Allocate and initialize the WhereInfo structure that will become the |
| 160 | ** return value. |
| 161 | */ |
| 162 | pWInfo = sqliteMalloc( sizeof(WhereInfo) ); |
| 163 | if( pWInfo==0 ){ |
| 164 | sqliteFree(aOrder); |
| 165 | return 0; |
| 166 | } |
| 167 | pWInfo->pParse = pParse; |
| 168 | pWInfo->pTabList = pTabList; |
| 169 | |
| 170 | /* Split the WHERE clause into as many as 32 separate subexpressions |
| 171 | ** where each subexpression is separated by an AND operator. Any additional |
| 172 | ** subexpressions are attached in the aExpr[32] and will not enter |
| 173 | ** into the query optimizer computations. 32 is chosen as the cutoff |
| 174 | ** since that is the number of bits in an integer that we use for an |
| 175 | ** expression-used mask. |
| 176 | */ |
| 177 | memset(aExpr, 0, sizeof(aExpr)); |
| 178 | nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere); |
| 179 | |
| 180 | /* Analyze all of the subexpressions. |
| 181 | */ |
| 182 | for(i=0; i<nExpr; i++){ |
| 183 | exprAnalyze(&aExpr[i]); |
| 184 | } |
| 185 | |
| 186 | /* Figure out a good nesting order for the tables. aOrder[0] will |
| 187 | ** be the index in pTabList of the outermost table. aOrder[1] will |
| 188 | ** be the first nested loop and so on. aOrder[pTabList->nId-1] will |
| 189 | ** be the innermost loop. |
| 190 | ** |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 191 | ** Someday will put in a good algorithm here to reorder the loops |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | ** for an effiecient query. But for now, just use whatever order the |
| 193 | ** tables appear in in the pTabList. |
| 194 | */ |
| 195 | for(i=0; i<pTabList->nId; i++){ |
| 196 | aOrder[i] = i; |
| 197 | } |
| 198 | |
| 199 | /* Figure out what index to use (if any) for each nested loop. |
| 200 | ** Make aIdx[i] point to the index to use for the i-th nested loop |
| 201 | ** where i==0 is the outer loop and i==pTabList->nId-1 is the inner |
| 202 | ** loop. |
| 203 | ** |
| 204 | ** Actually, if there are more than 32 tables in the join, only the |
| 205 | ** first 32 tables are candidates for indices. |
| 206 | */ |
| 207 | loopMask = 0; |
| 208 | for(i=0; i<pTabList->nId && i<ARRAYSIZE(aIdx); i++){ |
| 209 | int idx = aOrder[i]; |
| 210 | Table *pTab = pTabList->a[idx].pTab; |
| 211 | Index *pIdx; |
| 212 | Index *pBestIdx = 0; |
| 213 | |
| 214 | /* Do a search for usable indices. Leave pBestIdx pointing to |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 215 | ** the most specific usable index. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | ** |
| 217 | ** "Most specific" means that pBestIdx is the usable index that |
| 218 | ** has the largest value for nField. A usable index is one for |
| 219 | ** which there are subexpressions to compute every field of the |
| 220 | ** index. |
| 221 | */ |
| 222 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 223 | int j; |
| 224 | int fieldMask = 0; |
| 225 | |
| 226 | if( pIdx->nField>32 ) continue; |
| 227 | for(j=0; j<nExpr; j++){ |
| 228 | if( aExpr[j].idxLeft==idx |
| 229 | && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){ |
| 230 | int iField = aExpr[j].p->pLeft->iField; |
| 231 | int k; |
| 232 | for(k=0; k<pIdx->nField; k++){ |
| 233 | if( pIdx->aiField[k]==iField ){ |
| 234 | fieldMask |= 1<<k; |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | if( aExpr[j].idxRight==idx |
| 240 | && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){ |
| 241 | int iField = aExpr[j].p->pRight->iField; |
| 242 | int k; |
| 243 | for(k=0; k<pIdx->nField; k++){ |
| 244 | if( pIdx->aiField[k]==iField ){ |
| 245 | fieldMask |= 1<<k; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | if( fieldMask + 1 == (1<<pIdx->nField) ){ |
| 252 | if( pBestIdx==0 || pBestIdx->nField<pIdx->nField ){ |
| 253 | pBestIdx = pIdx; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | aIdx[i] = pBestIdx; |
drh | 7e391e1 | 2000-05-30 20:17:49 +0000 | [diff] [blame] | 258 | loopMask |= 1<<idx; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* Open all tables in the pTabList and all indices in aIdx[]. |
| 262 | */ |
| 263 | for(i=0; i<pTabList->nId; i++){ |
| 264 | sqliteVdbeAddOp(v, OP_Open, i, 0, pTabList->a[i].pTab->zName, 0); |
| 265 | if( i<ARRAYSIZE(aIdx) && aIdx[i]!=0 ){ |
| 266 | sqliteVdbeAddOp(v, OP_Open, pTabList->nId+i, 0, aIdx[i]->zName, 0); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /* Generate the code to do the search |
| 271 | */ |
| 272 | pWInfo->iBreak = brk = sqliteVdbeMakeLabel(v); |
| 273 | loopMask = 0; |
| 274 | for(i=0; i<pTabList->nId; i++){ |
| 275 | int j, k; |
| 276 | int idx = aOrder[i]; |
| 277 | Index *pIdx = i<ARRAYSIZE(aIdx) ? aIdx[i] : 0; |
| 278 | |
| 279 | cont = sqliteVdbeMakeLabel(v); |
| 280 | if( pIdx==0 ){ |
| 281 | /* Case 1: There was no usable index. We must do a complete |
| 282 | ** scan of the table. |
| 283 | */ |
| 284 | sqliteVdbeAddOp(v, OP_Next, idx, brk, 0, cont); |
| 285 | haveKey = 0; |
| 286 | }else{ |
| 287 | /* Case 2: We do have a usable index in pIdx. |
| 288 | */ |
| 289 | for(j=0; j<pIdx->nField; j++){ |
| 290 | for(k=0; k<nExpr; k++){ |
| 291 | if( aExpr[k].p==0 ) continue; |
| 292 | if( aExpr[k].idxLeft==idx |
| 293 | && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight |
| 294 | && aExpr[k].p->pLeft->iField==pIdx->aiField[j] |
| 295 | ){ |
| 296 | sqliteExprCode(pParse, aExpr[k].p->pRight); |
| 297 | aExpr[k].p = 0; |
| 298 | break; |
| 299 | } |
| 300 | if( aExpr[k].idxRight==idx |
| 301 | && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft |
| 302 | && aExpr[k].p->pRight->iField==pIdx->aiField[j] |
| 303 | ){ |
| 304 | sqliteExprCode(pParse, aExpr[k].p->pLeft); |
| 305 | aExpr[k].p = 0; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0); |
| 311 | sqliteVdbeAddOp(v, OP_Fetch, pTabList->nId+i, 0, 0, 0); |
| 312 | sqliteVdbeAddOp(v, OP_NextIdx, pTabList->nId+i, brk, 0, cont); |
| 313 | if( i==pTabList->nId-1 && pushKey ){ |
| 314 | haveKey = 1; |
| 315 | }else{ |
| 316 | sqliteVdbeAddOp(v, OP_Fetch, idx, 0, 0, 0); |
| 317 | haveKey = 0; |
| 318 | } |
| 319 | } |
| 320 | loopMask |= 1<<idx; |
| 321 | |
| 322 | /* Insert code to test every subexpression that can be completely |
| 323 | ** computed using the current set of tables. |
| 324 | */ |
| 325 | for(j=0; j<nExpr; j++){ |
| 326 | if( aExpr[j].p==0 ) continue; |
| 327 | if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue; |
| 328 | if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue; |
| 329 | if( haveKey ){ |
| 330 | sqliteVdbeAddOp(v, OP_Fetch, idx, 0, 0, 0); |
| 331 | haveKey = 0; |
| 332 | } |
| 333 | sqliteExprIfFalse(pParse, aExpr[j].p, cont); |
| 334 | aExpr[j].p = 0; |
| 335 | } |
| 336 | brk = cont; |
| 337 | } |
| 338 | pWInfo->iContinue = cont; |
| 339 | if( pushKey && !haveKey ){ |
| 340 | sqliteVdbeAddOp(v, OP_Key, 0, 0, 0, 0); |
| 341 | } |
| 342 | sqliteFree(aOrder); |
| 343 | return pWInfo; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | ** Generate the end of the WHERE loop. |
| 348 | */ |
| 349 | void sqliteWhereEnd(WhereInfo *pWInfo){ |
| 350 | Vdbe *v = pWInfo->pParse->pVdbe; |
| 351 | sqliteVdbeAddOp(v, OP_Goto, 0, pWInfo->iContinue, 0, 0); |
| 352 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, pWInfo->iBreak); |
| 353 | sqliteFree(pWInfo); |
| 354 | return; |
| 355 | } |