OLD | NEW |
(Empty) | |
| 1 /************** Begin file wherecode.c ***************************************/ |
| 2 /* |
| 3 ** 2015-06-06 |
| 4 ** |
| 5 ** The author disclaims copyright to this source code. In place of |
| 6 ** a legal notice, here is a blessing: |
| 7 ** |
| 8 ** May you do good and not evil. |
| 9 ** May you find forgiveness for yourself and forgive others. |
| 10 ** May you share freely, never taking more than you give. |
| 11 ** |
| 12 ************************************************************************* |
| 13 ** This module contains C code that generates VDBE code used to process |
| 14 ** the WHERE clause of SQL statements. |
| 15 ** |
| 16 ** This file was split off from where.c on 2015-06-06 in order to reduce the |
| 17 ** size of where.c and make it easier to edit. This file contains the routines |
| 18 ** that actually generate the bulk of the WHERE loop code. The original where.c |
| 19 ** file retains the code that does query planning and analysis. |
| 20 */ |
| 21 /* #include "sqliteInt.h" */ |
| 22 /************** Include whereInt.h in the middle of wherecode.c **************/ |
| 23 /************** Begin file whereInt.h ****************************************/ |
| 24 /* |
| 25 ** 2013-11-12 |
| 26 ** |
| 27 ** The author disclaims copyright to this source code. In place of |
| 28 ** a legal notice, here is a blessing: |
| 29 ** |
| 30 ** May you do good and not evil. |
| 31 ** May you find forgiveness for yourself and forgive others. |
| 32 ** May you share freely, never taking more than you give. |
| 33 ** |
| 34 ************************************************************************* |
| 35 ** |
| 36 ** This file contains structure and macro definitions for the query |
| 37 ** planner logic in "where.c". These definitions are broken out into |
| 38 ** a separate source file for easier editing. |
| 39 */ |
| 40 |
| 41 /* |
| 42 ** Trace output macros |
| 43 */ |
| 44 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 45 /***/ int sqlite3WhereTrace; |
| 46 #endif |
| 47 #if defined(SQLITE_DEBUG) \ |
| 48 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE)) |
| 49 # define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X |
| 50 # define WHERETRACE_ENABLED 1 |
| 51 #else |
| 52 # define WHERETRACE(K,X) |
| 53 #endif |
| 54 |
| 55 /* Forward references |
| 56 */ |
| 57 typedef struct WhereClause WhereClause; |
| 58 typedef struct WhereMaskSet WhereMaskSet; |
| 59 typedef struct WhereOrInfo WhereOrInfo; |
| 60 typedef struct WhereAndInfo WhereAndInfo; |
| 61 typedef struct WhereLevel WhereLevel; |
| 62 typedef struct WhereLoop WhereLoop; |
| 63 typedef struct WherePath WherePath; |
| 64 typedef struct WhereTerm WhereTerm; |
| 65 typedef struct WhereLoopBuilder WhereLoopBuilder; |
| 66 typedef struct WhereScan WhereScan; |
| 67 typedef struct WhereOrCost WhereOrCost; |
| 68 typedef struct WhereOrSet WhereOrSet; |
| 69 |
| 70 /* |
| 71 ** This object contains information needed to implement a single nested |
| 72 ** loop in WHERE clause. |
| 73 ** |
| 74 ** Contrast this object with WhereLoop. This object describes the |
| 75 ** implementation of the loop. WhereLoop describes the algorithm. |
| 76 ** This object contains a pointer to the WhereLoop algorithm as one of |
| 77 ** its elements. |
| 78 ** |
| 79 ** The WhereInfo object contains a single instance of this object for |
| 80 ** each term in the FROM clause (which is to say, for each of the |
| 81 ** nested loops as implemented). The order of WhereLevel objects determines |
| 82 ** the loop nested order, with WhereInfo.a[0] being the outer loop and |
| 83 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop. |
| 84 */ |
| 85 struct WhereLevel { |
| 86 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */ |
| 87 int iTabCur; /* The VDBE cursor used to access the table */ |
| 88 int iIdxCur; /* The VDBE cursor used to access pIdx */ |
| 89 int addrBrk; /* Jump here to break out of the loop */ |
| 90 int addrNxt; /* Jump here to start the next IN combination */ |
| 91 int addrSkip; /* Jump here for next iteration of skip-scan */ |
| 92 int addrCont; /* Jump here to continue with the next loop cycle */ |
| 93 int addrFirst; /* First instruction of interior of the loop */ |
| 94 int addrBody; /* Beginning of the body of this loop */ |
| 95 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 96 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */ |
| 97 int addrLikeRep; /* LIKE range processing address */ |
| 98 #endif |
| 99 u8 iFrom; /* Which entry in the FROM clause */ |
| 100 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */ |
| 101 int p1, p2; /* Operands of the opcode used to ends the loop */ |
| 102 union { /* Information that depends on pWLoop->wsFlags */ |
| 103 struct { |
| 104 int nIn; /* Number of entries in aInLoop[] */ |
| 105 struct InLoop { |
| 106 int iCur; /* The VDBE cursor used by this IN operator */ |
| 107 int addrInTop; /* Top of the IN loop */ |
| 108 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */ |
| 109 } *aInLoop; /* Information about each nested IN operator */ |
| 110 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */ |
| 111 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */ |
| 112 } u; |
| 113 struct WhereLoop *pWLoop; /* The selected WhereLoop object */ |
| 114 Bitmask notReady; /* FROM entries not usable at this level */ |
| 115 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 116 int addrVisit; /* Address at which row is visited */ |
| 117 #endif |
| 118 }; |
| 119 |
| 120 /* |
| 121 ** Each instance of this object represents an algorithm for evaluating one |
| 122 ** term of a join. Every term of the FROM clause will have at least |
| 123 ** one corresponding WhereLoop object (unless INDEXED BY constraints |
| 124 ** prevent a query solution - which is an error) and many terms of the |
| 125 ** FROM clause will have multiple WhereLoop objects, each describing a |
| 126 ** potential way of implementing that FROM-clause term, together with |
| 127 ** dependencies and cost estimates for using the chosen algorithm. |
| 128 ** |
| 129 ** Query planning consists of building up a collection of these WhereLoop |
| 130 ** objects, then computing a particular sequence of WhereLoop objects, with |
| 131 ** one WhereLoop object per FROM clause term, that satisfy all dependencies |
| 132 ** and that minimize the overall cost. |
| 133 */ |
| 134 struct WhereLoop { |
| 135 Bitmask prereq; /* Bitmask of other loops that must run first */ |
| 136 Bitmask maskSelf; /* Bitmask identifying table iTab */ |
| 137 #ifdef SQLITE_DEBUG |
| 138 char cId; /* Symbolic ID of this loop for debugging use */ |
| 139 #endif |
| 140 u8 iTab; /* Position in FROM clause of table for this loop */ |
| 141 u8 iSortIdx; /* Sorting index number. 0==None */ |
| 142 LogEst rSetup; /* One-time setup cost (ex: create transient index) */ |
| 143 LogEst rRun; /* Cost of running each loop */ |
| 144 LogEst nOut; /* Estimated number of output rows */ |
| 145 union { |
| 146 struct { /* Information for internal btree tables */ |
| 147 u16 nEq; /* Number of equality constraints */ |
| 148 u16 nBtm; /* Size of BTM vector */ |
| 149 u16 nTop; /* Size of TOP vector */ |
| 150 Index *pIndex; /* Index used, or NULL */ |
| 151 } btree; |
| 152 struct { /* Information for virtual tables */ |
| 153 int idxNum; /* Index number */ |
| 154 u8 needFree; /* True if sqlite3_free(idxStr) is needed */ |
| 155 i8 isOrdered; /* True if satisfies ORDER BY */ |
| 156 u16 omitMask; /* Terms that may be omitted */ |
| 157 char *idxStr; /* Index identifier string */ |
| 158 } vtab; |
| 159 } u; |
| 160 u32 wsFlags; /* WHERE_* flags describing the plan */ |
| 161 u16 nLTerm; /* Number of entries in aLTerm[] */ |
| 162 u16 nSkip; /* Number of NULL aLTerm[] entries */ |
| 163 /**** whereLoopXfer() copies fields above ***********************/ |
| 164 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot) |
| 165 u16 nLSlot; /* Number of slots allocated for aLTerm[] */ |
| 166 WhereTerm **aLTerm; /* WhereTerms used */ |
| 167 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */ |
| 168 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */ |
| 169 }; |
| 170 |
| 171 /* This object holds the prerequisites and the cost of running a |
| 172 ** subquery on one operand of an OR operator in the WHERE clause. |
| 173 ** See WhereOrSet for additional information |
| 174 */ |
| 175 struct WhereOrCost { |
| 176 Bitmask prereq; /* Prerequisites */ |
| 177 LogEst rRun; /* Cost of running this subquery */ |
| 178 LogEst nOut; /* Number of outputs for this subquery */ |
| 179 }; |
| 180 |
| 181 /* The WhereOrSet object holds a set of possible WhereOrCosts that |
| 182 ** correspond to the subquery(s) of OR-clause processing. Only the |
| 183 ** best N_OR_COST elements are retained. |
| 184 */ |
| 185 #define N_OR_COST 3 |
| 186 struct WhereOrSet { |
| 187 u16 n; /* Number of valid a[] entries */ |
| 188 WhereOrCost a[N_OR_COST]; /* Set of best costs */ |
| 189 }; |
| 190 |
| 191 /* |
| 192 ** Each instance of this object holds a sequence of WhereLoop objects |
| 193 ** that implement some or all of a query plan. |
| 194 ** |
| 195 ** Think of each WhereLoop object as a node in a graph with arcs |
| 196 ** showing dependencies and costs for travelling between nodes. (That is |
| 197 ** not a completely accurate description because WhereLoop costs are a |
| 198 ** vector, not a scalar, and because dependencies are many-to-one, not |
| 199 ** one-to-one as are graph nodes. But it is a useful visualization aid.) |
| 200 ** Then a WherePath object is a path through the graph that visits some |
| 201 ** or all of the WhereLoop objects once. |
| 202 ** |
| 203 ** The "solver" works by creating the N best WherePath objects of length |
| 204 ** 1. Then using those as a basis to compute the N best WherePath objects |
| 205 ** of length 2. And so forth until the length of WherePaths equals the |
| 206 ** number of nodes in the FROM clause. The best (lowest cost) WherePath |
| 207 ** at the end is the chosen query plan. |
| 208 */ |
| 209 struct WherePath { |
| 210 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */ |
| 211 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */ |
| 212 LogEst nRow; /* Estimated number of rows generated by this path */ |
| 213 LogEst rCost; /* Total cost of this path */ |
| 214 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */ |
| 215 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */ |
| 216 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */ |
| 217 }; |
| 218 |
| 219 /* |
| 220 ** The query generator uses an array of instances of this structure to |
| 221 ** help it analyze the subexpressions of the WHERE clause. Each WHERE |
| 222 ** clause subexpression is separated from the others by AND operators, |
| 223 ** usually, or sometimes subexpressions separated by OR. |
| 224 ** |
| 225 ** All WhereTerms are collected into a single WhereClause structure. |
| 226 ** The following identity holds: |
| 227 ** |
| 228 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm |
| 229 ** |
| 230 ** When a term is of the form: |
| 231 ** |
| 232 ** X <op> <expr> |
| 233 ** |
| 234 ** where X is a column name and <op> is one of certain operators, |
| 235 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the |
| 236 ** cursor number and column number for X. WhereTerm.eOperator records |
| 237 ** the <op> using a bitmask encoding defined by WO_xxx below. The |
| 238 ** use of a bitmask encoding for the operator allows us to search |
| 239 ** quickly for terms that match any of several different operators. |
| 240 ** |
| 241 ** A WhereTerm might also be two or more subterms connected by OR: |
| 242 ** |
| 243 ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR .... |
| 244 ** |
| 245 ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR |
| 246 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that |
| 247 ** is collected about the OR clause. |
| 248 ** |
| 249 ** If a term in the WHERE clause does not match either of the two previous |
| 250 ** categories, then eOperator==0. The WhereTerm.pExpr field is still set |
| 251 ** to the original subexpression content and wtFlags is set up appropriately |
| 252 ** but no other fields in the WhereTerm object are meaningful. |
| 253 ** |
| 254 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, |
| 255 ** but they do so indirectly. A single WhereMaskSet structure translates |
| 256 ** cursor number into bits and the translated bit is stored in the prereq |
| 257 ** fields. The translation is used in order to maximize the number of |
| 258 ** bits that will fit in a Bitmask. The VDBE cursor numbers might be |
| 259 ** spread out over the non-negative integers. For example, the cursor |
| 260 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet |
| 261 ** translates these sparse cursor numbers into consecutive integers |
| 262 ** beginning with 0 in order to make the best possible use of the available |
| 263 ** bits in the Bitmask. So, in the example above, the cursor numbers |
| 264 ** would be mapped into integers 0 through 7. |
| 265 ** |
| 266 ** The number of terms in a join is limited by the number of bits |
| 267 ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite |
| 268 ** is only able to process joins with 64 or fewer tables. |
| 269 */ |
| 270 struct WhereTerm { |
| 271 Expr *pExpr; /* Pointer to the subexpression that is this term */ |
| 272 WhereClause *pWC; /* The clause this term is part of */ |
| 273 LogEst truthProb; /* Probability of truth for this expression */ |
| 274 u16 wtFlags; /* TERM_xxx bit flags. See below */ |
| 275 u16 eOperator; /* A WO_xx value describing <op> */ |
| 276 u8 nChild; /* Number of children that must disable us */ |
| 277 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */ |
| 278 int iParent; /* Disable pWC->a[iParent] when this term disabled */ |
| 279 int leftCursor; /* Cursor number of X in "X <op> <expr>" */ |
| 280 int iField; /* Field in (?,?,?) IN (SELECT...) vector */ |
| 281 union { |
| 282 int leftColumn; /* Column number of X in "X <op> <expr>" */ |
| 283 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */ |
| 284 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */ |
| 285 } u; |
| 286 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ |
| 287 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ |
| 288 }; |
| 289 |
| 290 /* |
| 291 ** Allowed values of WhereTerm.wtFlags |
| 292 */ |
| 293 #define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ |
| 294 #define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ |
| 295 #define TERM_CODED 0x04 /* This term is already coded */ |
| 296 #define TERM_COPIED 0x08 /* Has a child */ |
| 297 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ |
| 298 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ |
| 299 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */ |
| 300 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 301 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ |
| 302 #else |
| 303 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */ |
| 304 #endif |
| 305 #define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */ |
| 306 #define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */ |
| 307 #define TERM_LIKE 0x400 /* The original LIKE operator */ |
| 308 #define TERM_IS 0x800 /* Term.pExpr is an IS operator */ |
| 309 |
| 310 /* |
| 311 ** An instance of the WhereScan object is used as an iterator for locating |
| 312 ** terms in the WHERE clause that are useful to the query planner. |
| 313 */ |
| 314 struct WhereScan { |
| 315 WhereClause *pOrigWC; /* Original, innermost WhereClause */ |
| 316 WhereClause *pWC; /* WhereClause currently being scanned */ |
| 317 const char *zCollName; /* Required collating sequence, if not NULL */ |
| 318 Expr *pIdxExpr; /* Search for this index expression */ |
| 319 char idxaff; /* Must match this affinity, if zCollName!=NULL */ |
| 320 unsigned char nEquiv; /* Number of entries in aEquiv[] */ |
| 321 unsigned char iEquiv; /* Next unused slot in aEquiv[] */ |
| 322 u32 opMask; /* Acceptable operators */ |
| 323 int k; /* Resume scanning at this->pWC->a[this->k] */ |
| 324 int aiCur[11]; /* Cursors in the equivalence class */ |
| 325 i16 aiColumn[11]; /* Corresponding column number in the eq-class */ |
| 326 }; |
| 327 |
| 328 /* |
| 329 ** An instance of the following structure holds all information about a |
| 330 ** WHERE clause. Mostly this is a container for one or more WhereTerms. |
| 331 ** |
| 332 ** Explanation of pOuter: For a WHERE clause of the form |
| 333 ** |
| 334 ** a AND ((b AND c) OR (d AND e)) AND f |
| 335 ** |
| 336 ** There are separate WhereClause objects for the whole clause and for |
| 337 ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the |
| 338 ** subclauses points to the WhereClause object for the whole clause. |
| 339 */ |
| 340 struct WhereClause { |
| 341 WhereInfo *pWInfo; /* WHERE clause processing context */ |
| 342 WhereClause *pOuter; /* Outer conjunction */ |
| 343 u8 op; /* Split operator. TK_AND or TK_OR */ |
| 344 int nTerm; /* Number of terms */ |
| 345 int nSlot; /* Number of entries in a[] */ |
| 346 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ |
| 347 #if defined(SQLITE_SMALL_STACK) |
| 348 WhereTerm aStatic[1]; /* Initial static space for a[] */ |
| 349 #else |
| 350 WhereTerm aStatic[8]; /* Initial static space for a[] */ |
| 351 #endif |
| 352 }; |
| 353 |
| 354 /* |
| 355 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to |
| 356 ** a dynamically allocated instance of the following structure. |
| 357 */ |
| 358 struct WhereOrInfo { |
| 359 WhereClause wc; /* Decomposition into subterms */ |
| 360 Bitmask indexable; /* Bitmask of all indexable tables in the clause */ |
| 361 }; |
| 362 |
| 363 /* |
| 364 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to |
| 365 ** a dynamically allocated instance of the following structure. |
| 366 */ |
| 367 struct WhereAndInfo { |
| 368 WhereClause wc; /* The subexpression broken out */ |
| 369 }; |
| 370 |
| 371 /* |
| 372 ** An instance of the following structure keeps track of a mapping |
| 373 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. |
| 374 ** |
| 375 ** The VDBE cursor numbers are small integers contained in |
| 376 ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE |
| 377 ** clause, the cursor numbers might not begin with 0 and they might |
| 378 ** contain gaps in the numbering sequence. But we want to make maximum |
| 379 ** use of the bits in our bitmasks. This structure provides a mapping |
| 380 ** from the sparse cursor numbers into consecutive integers beginning |
| 381 ** with 0. |
| 382 ** |
| 383 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask |
| 384 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A. |
| 385 ** |
| 386 ** For example, if the WHERE clause expression used these VDBE |
| 387 ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure |
| 388 ** would map those cursor numbers into bits 0 through 5. |
| 389 ** |
| 390 ** Note that the mapping is not necessarily ordered. In the example |
| 391 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0, |
| 392 ** 57->5, 73->4. Or one of 719 other combinations might be used. It |
| 393 ** does not really matter. What is important is that sparse cursor |
| 394 ** numbers all get mapped into bit numbers that begin with 0 and contain |
| 395 ** no gaps. |
| 396 */ |
| 397 struct WhereMaskSet { |
| 398 int n; /* Number of assigned cursor values */ |
| 399 int ix[BMS]; /* Cursor assigned to each bit */ |
| 400 }; |
| 401 |
| 402 /* |
| 403 ** Initialize a WhereMaskSet object |
| 404 */ |
| 405 #define initMaskSet(P) (P)->n=0 |
| 406 |
| 407 /* |
| 408 ** This object is a convenience wrapper holding all information needed |
| 409 ** to construct WhereLoop objects for a particular query. |
| 410 */ |
| 411 struct WhereLoopBuilder { |
| 412 WhereInfo *pWInfo; /* Information about this WHERE */ |
| 413 WhereClause *pWC; /* WHERE clause terms */ |
| 414 ExprList *pOrderBy; /* ORDER BY clause */ |
| 415 WhereLoop *pNew; /* Template WhereLoop */ |
| 416 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */ |
| 417 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 418 UnpackedRecord *pRec; /* Probe for stat4 (if required) */ |
| 419 int nRecValid; /* Number of valid fields currently in pRec */ |
| 420 #endif |
| 421 }; |
| 422 |
| 423 /* |
| 424 ** The WHERE clause processing routine has two halves. The |
| 425 ** first part does the start of the WHERE loop and the second |
| 426 ** half does the tail of the WHERE loop. An instance of |
| 427 ** this structure is returned by the first half and passed |
| 428 ** into the second half to give some continuity. |
| 429 ** |
| 430 ** An instance of this object holds the complete state of the query |
| 431 ** planner. |
| 432 */ |
| 433 struct WhereInfo { |
| 434 Parse *pParse; /* Parsing and code generating context */ |
| 435 SrcList *pTabList; /* List of tables in the join */ |
| 436 ExprList *pOrderBy; /* The ORDER BY clause or NULL */ |
| 437 ExprList *pDistinctSet; /* DISTINCT over all these values */ |
| 438 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */ |
| 439 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */ |
| 440 int iContinue; /* Jump here to continue with next record */ |
| 441 int iBreak; /* Jump here to break out of the loop */ |
| 442 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */ |
| 443 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */ |
| 444 u8 nLevel; /* Number of nested loop */ |
| 445 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */ |
| 446 u8 sorted; /* True if really sorted (not just grouped) */ |
| 447 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */ |
| 448 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */ |
| 449 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */ |
| 450 u8 bOrderedInnerLoop; /* True if only the inner-most loop is ordered */ |
| 451 int iTop; /* The very beginning of the WHERE loop */ |
| 452 WhereLoop *pLoops; /* List of all WhereLoop objects */ |
| 453 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */ |
| 454 LogEst nRowOut; /* Estimated number of output rows */ |
| 455 WhereClause sWC; /* Decomposition of the WHERE clause */ |
| 456 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */ |
| 457 WhereLevel a[1]; /* Information about each nest loop in WHERE */ |
| 458 }; |
| 459 |
| 460 /* |
| 461 ** Private interfaces - callable only by other where.c routines. |
| 462 ** |
| 463 ** where.c: |
| 464 */ |
| 465 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet*,int); |
| 466 #ifdef WHERETRACE_ENABLED |
| 467 SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC); |
| 468 #endif |
| 469 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( |
| 470 WhereClause *pWC, /* The WHERE clause to be searched */ |
| 471 int iCur, /* Cursor number of LHS */ |
| 472 int iColumn, /* Column number of LHS */ |
| 473 Bitmask notReady, /* RHS must not overlap with this mask */ |
| 474 u32 op, /* Mask of WO_xx values describing operator */ |
| 475 Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 476 ); |
| 477 |
| 478 /* wherecode.c: */ |
| 479 #ifndef SQLITE_OMIT_EXPLAIN |
| 480 SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| 481 Parse *pParse, /* Parse context */ |
| 482 SrcList *pTabList, /* Table list this loop refers to */ |
| 483 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 484 int iLevel, /* Value for "level" column of output */ |
| 485 int iFrom, /* Value for "from" column of output */ |
| 486 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 487 ); |
| 488 #else |
| 489 # define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0 |
| 490 #endif /* SQLITE_OMIT_EXPLAIN */ |
| 491 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 492 SQLITE_PRIVATE void sqlite3WhereAddScanStatus( |
| 493 Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 494 SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 495 WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 496 int addrExplain /* Address of OP_Explain (or 0) */ |
| 497 ); |
| 498 #else |
| 499 # define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d) |
| 500 #endif |
| 501 SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart( |
| 502 WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 503 int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 504 Bitmask notReady /* Which tables are currently available */ |
| 505 ); |
| 506 |
| 507 /* whereexpr.c: */ |
| 508 SQLITE_PRIVATE void sqlite3WhereClauseInit(WhereClause*,WhereInfo*); |
| 509 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause*); |
| 510 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause*,Expr*,u8); |
| 511 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*); |
| 512 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*); |
| 513 SQLITE_PRIVATE void sqlite3WhereExprAnalyze(SrcList*, WhereClause*); |
| 514 SQLITE_PRIVATE void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereC
lause*); |
| 515 |
| 516 |
| 517 |
| 518 |
| 519 |
| 520 /* |
| 521 ** Bitmasks for the operators on WhereTerm objects. These are all |
| 522 ** operators that are of interest to the query planner. An |
| 523 ** OR-ed combination of these values can be used when searching for |
| 524 ** particular WhereTerms within a WhereClause. |
| 525 ** |
| 526 ** Value constraints: |
| 527 ** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ |
| 528 ** WO_LT == SQLITE_INDEX_CONSTRAINT_LT |
| 529 ** WO_LE == SQLITE_INDEX_CONSTRAINT_LE |
| 530 ** WO_GT == SQLITE_INDEX_CONSTRAINT_GT |
| 531 ** WO_GE == SQLITE_INDEX_CONSTRAINT_GE |
| 532 ** WO_MATCH == SQLITE_INDEX_CONSTRAINT_MATCH |
| 533 */ |
| 534 #define WO_IN 0x0001 |
| 535 #define WO_EQ 0x0002 |
| 536 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) |
| 537 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) |
| 538 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) |
| 539 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) |
| 540 #define WO_MATCH 0x0040 |
| 541 #define WO_IS 0x0080 |
| 542 #define WO_ISNULL 0x0100 |
| 543 #define WO_OR 0x0200 /* Two or more OR-connected terms */ |
| 544 #define WO_AND 0x0400 /* Two or more AND-connected terms */ |
| 545 #define WO_EQUIV 0x0800 /* Of the form A==B, both columns */ |
| 546 #define WO_NOOP 0x1000 /* This term does not restrict search space */ |
| 547 |
| 548 #define WO_ALL 0x1fff /* Mask of all possible WO_* values */ |
| 549 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */ |
| 550 |
| 551 /* |
| 552 ** These are definitions of bits in the WhereLoop.wsFlags field. |
| 553 ** The particular combination of bits in each WhereLoop help to |
| 554 ** determine the algorithm that WhereLoop represents. |
| 555 */ |
| 556 #define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */ |
| 557 #define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */ |
| 558 #define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */ |
| 559 #define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */ |
| 560 #define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */ |
| 561 #define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */ |
| 562 #define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */ |
| 563 #define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */ |
| 564 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */ |
| 565 #define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */ |
| 566 #define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */ |
| 567 #define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */ |
| 568 #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */ |
| 569 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */ |
| 570 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */ |
| 571 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 572 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 573 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 574 #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 575 |
| 576 /************** End of whereInt.h ********************************************/ |
| 577 /************** Continuing where we left off in wherecode.c ******************/ |
| 578 |
| 579 #ifndef SQLITE_OMIT_EXPLAIN |
| 580 |
| 581 /* |
| 582 ** Return the name of the i-th column of the pIdx index. |
| 583 */ |
| 584 static const char *explainIndexColumnName(Index *pIdx, int i){ |
| 585 i = pIdx->aiColumn[i]; |
| 586 if( i==XN_EXPR ) return "<expr>"; |
| 587 if( i==XN_ROWID ) return "rowid"; |
| 588 return pIdx->pTable->aCol[i].zName; |
| 589 } |
| 590 |
| 591 /* |
| 592 ** This routine is a helper for explainIndexRange() below |
| 593 ** |
| 594 ** pStr holds the text of an expression that we are building up one term |
| 595 ** at a time. This routine adds a new term to the end of the expression. |
| 596 ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 597 ** terms only. |
| 598 */ |
| 599 static void explainAppendTerm( |
| 600 StrAccum *pStr, /* The text expression being built */ |
| 601 Index *pIdx, /* Index to read column names from */ |
| 602 int nTerm, /* Number of terms */ |
| 603 int iTerm, /* Zero-based index of first term. */ |
| 604 int bAnd, /* Non-zero to append " AND " */ |
| 605 const char *zOp /* Name of the operator */ |
| 606 ){ |
| 607 int i; |
| 608 |
| 609 assert( nTerm>=1 ); |
| 610 if( bAnd ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 611 |
| 612 if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1); |
| 613 for(i=0; i<nTerm; i++){ |
| 614 if( i ) sqlite3StrAccumAppend(pStr, ",", 1); |
| 615 sqlite3StrAccumAppendAll(pStr, explainIndexColumnName(pIdx, iTerm+i)); |
| 616 } |
| 617 if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1); |
| 618 |
| 619 sqlite3StrAccumAppend(pStr, zOp, 1); |
| 620 |
| 621 if( nTerm>1 ) sqlite3StrAccumAppend(pStr, "(", 1); |
| 622 for(i=0; i<nTerm; i++){ |
| 623 if( i ) sqlite3StrAccumAppend(pStr, ",", 1); |
| 624 sqlite3StrAccumAppend(pStr, "?", 1); |
| 625 } |
| 626 if( nTerm>1 ) sqlite3StrAccumAppend(pStr, ")", 1); |
| 627 } |
| 628 |
| 629 /* |
| 630 ** Argument pLevel describes a strategy for scanning table pTab. This |
| 631 ** function appends text to pStr that describes the subset of table |
| 632 ** rows scanned by the strategy in the form of an SQL expression. |
| 633 ** |
| 634 ** For example, if the query: |
| 635 ** |
| 636 ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 637 ** |
| 638 ** is run and there is an index on (a, b), then this function returns a |
| 639 ** string similar to: |
| 640 ** |
| 641 ** "a=? AND b>?" |
| 642 */ |
| 643 static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){ |
| 644 Index *pIndex = pLoop->u.btree.pIndex; |
| 645 u16 nEq = pLoop->u.btree.nEq; |
| 646 u16 nSkip = pLoop->nSkip; |
| 647 int i, j; |
| 648 |
| 649 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 650 sqlite3StrAccumAppend(pStr, " (", 2); |
| 651 for(i=0; i<nEq; i++){ |
| 652 const char *z = explainIndexColumnName(pIndex, i); |
| 653 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 654 sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z); |
| 655 } |
| 656 |
| 657 j = i; |
| 658 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 659 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">"); |
| 660 i = 1; |
| 661 } |
| 662 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 663 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<"); |
| 664 } |
| 665 sqlite3StrAccumAppend(pStr, ")", 1); |
| 666 } |
| 667 |
| 668 /* |
| 669 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 670 ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 671 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 672 ** is added to the output to describe the table scan strategy in pLevel. |
| 673 ** |
| 674 ** If an OP_Explain opcode is added to the VM, its address is returned. |
| 675 ** Otherwise, if no OP_Explain is coded, zero is returned. |
| 676 */ |
| 677 SQLITE_PRIVATE int sqlite3WhereExplainOneScan( |
| 678 Parse *pParse, /* Parse context */ |
| 679 SrcList *pTabList, /* Table list this loop refers to */ |
| 680 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 681 int iLevel, /* Value for "level" column of output */ |
| 682 int iFrom, /* Value for "from" column of output */ |
| 683 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 684 ){ |
| 685 int ret = 0; |
| 686 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 687 if( pParse->explain==2 ) |
| 688 #endif |
| 689 { |
| 690 struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 691 Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 692 sqlite3 *db = pParse->db; /* Database handle */ |
| 693 int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 694 int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 695 WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 696 u32 flags; /* Flags that describe this loop */ |
| 697 char *zMsg; /* Text to add to EQP output */ |
| 698 StrAccum str; /* EQP output string */ |
| 699 char zBuf[100]; /* Initial space for EQP output string */ |
| 700 |
| 701 pLoop = pLevel->pWLoop; |
| 702 flags = pLoop->wsFlags; |
| 703 if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_OR_SUBCLAUSE) ) return 0; |
| 704 |
| 705 isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 706 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 707 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 708 |
| 709 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 710 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 711 if( pItem->pSelect ){ |
| 712 sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId); |
| 713 }else{ |
| 714 sqlite3XPrintf(&str, " TABLE %s", pItem->zName); |
| 715 } |
| 716 |
| 717 if( pItem->zAlias ){ |
| 718 sqlite3XPrintf(&str, " AS %s", pItem->zAlias); |
| 719 } |
| 720 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 721 const char *zFmt = 0; |
| 722 Index *pIdx; |
| 723 |
| 724 assert( pLoop->u.btree.pIndex!=0 ); |
| 725 pIdx = pLoop->u.btree.pIndex; |
| 726 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 727 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 728 if( isSearch ){ |
| 729 zFmt = "PRIMARY KEY"; |
| 730 } |
| 731 }else if( flags & WHERE_PARTIALIDX ){ |
| 732 zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 733 }else if( flags & WHERE_AUTO_INDEX ){ |
| 734 zFmt = "AUTOMATIC COVERING INDEX"; |
| 735 }else if( flags & WHERE_IDX_ONLY ){ |
| 736 zFmt = "COVERING INDEX %s"; |
| 737 }else{ |
| 738 zFmt = "INDEX %s"; |
| 739 } |
| 740 if( zFmt ){ |
| 741 sqlite3StrAccumAppend(&str, " USING ", 7); |
| 742 sqlite3XPrintf(&str, zFmt, pIdx->zName); |
| 743 explainIndexRange(&str, pLoop); |
| 744 } |
| 745 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 746 const char *zRangeOp; |
| 747 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 748 zRangeOp = "="; |
| 749 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 750 zRangeOp = ">? AND rowid<"; |
| 751 }else if( flags&WHERE_BTM_LIMIT ){ |
| 752 zRangeOp = ">"; |
| 753 }else{ |
| 754 assert( flags&WHERE_TOP_LIMIT); |
| 755 zRangeOp = "<"; |
| 756 } |
| 757 sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp); |
| 758 } |
| 759 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 760 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 761 sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s", |
| 762 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 763 } |
| 764 #endif |
| 765 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 766 if( pLoop->nOut>=10 ){ |
| 767 sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 768 }else{ |
| 769 sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 770 } |
| 771 #endif |
| 772 zMsg = sqlite3StrAccumFinish(&str); |
| 773 ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
| 774 } |
| 775 return ret; |
| 776 } |
| 777 #endif /* SQLITE_OMIT_EXPLAIN */ |
| 778 |
| 779 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 780 /* |
| 781 ** Configure the VM passed as the first argument with an |
| 782 ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 783 ** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 784 ** clause that the scan reads data from. |
| 785 ** |
| 786 ** If argument addrExplain is not 0, it must be the address of an |
| 787 ** OP_Explain instruction that describes the same loop. |
| 788 */ |
| 789 SQLITE_PRIVATE void sqlite3WhereAddScanStatus( |
| 790 Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 791 SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 792 WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 793 int addrExplain /* Address of OP_Explain (or 0) */ |
| 794 ){ |
| 795 const char *zObj = 0; |
| 796 WhereLoop *pLoop = pLvl->pWLoop; |
| 797 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 798 zObj = pLoop->u.btree.pIndex->zName; |
| 799 }else{ |
| 800 zObj = pSrclist->a[pLvl->iFrom].zName; |
| 801 } |
| 802 sqlite3VdbeScanStatus( |
| 803 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 804 ); |
| 805 } |
| 806 #endif |
| 807 |
| 808 |
| 809 /* |
| 810 ** Disable a term in the WHERE clause. Except, do not disable the term |
| 811 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 812 ** or USING clause of that join. |
| 813 ** |
| 814 ** Consider the term t2.z='ok' in the following queries: |
| 815 ** |
| 816 ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 817 ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 818 ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 819 ** |
| 820 ** The t2.z='ok' is disabled in the in (2) because it originates |
| 821 ** in the ON clause. The term is disabled in (3) because it is not part |
| 822 ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 823 ** |
| 824 ** Disabling a term causes that term to not be tested in the inner loop |
| 825 ** of the join. Disabling is an optimization. When terms are satisfied |
| 826 ** by indices, we disable them to prevent redundant tests in the inner |
| 827 ** loop. We would get the correct results if nothing were ever disabled, |
| 828 ** but joins might run a little slower. The trick is to disable as much |
| 829 ** as we can without disabling too much. If we disabled in (1), we'd get |
| 830 ** the wrong answer. See ticket #813. |
| 831 ** |
| 832 ** If all the children of a term are disabled, then that term is also |
| 833 ** automatically disabled. In this way, terms get disabled if derived |
| 834 ** virtual terms are tested first. For example: |
| 835 ** |
| 836 ** x GLOB 'abc*' AND x>='abc' AND x<'acd' |
| 837 ** \___________/ \______/ \_____/ |
| 838 ** parent child1 child2 |
| 839 ** |
| 840 ** Only the parent term was in the original WHERE clause. The child1 |
| 841 ** and child2 terms were added by the LIKE optimization. If both of |
| 842 ** the virtual child terms are valid, then testing of the parent can be |
| 843 ** skipped. |
| 844 ** |
| 845 ** Usually the parent term is marked as TERM_CODED. But if the parent |
| 846 ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. |
| 847 ** The TERM_LIKECOND marking indicates that the term should be coded inside |
| 848 ** a conditional such that is only evaluated on the second pass of a |
| 849 ** LIKE-optimization loop, when scanning BLOBs instead of strings. |
| 850 */ |
| 851 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 852 int nLoop = 0; |
| 853 while( ALWAYS(pTerm!=0) |
| 854 && (pTerm->wtFlags & TERM_CODED)==0 |
| 855 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 856 && (pLevel->notReady & pTerm->prereqAll)==0 |
| 857 ){ |
| 858 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 859 pTerm->wtFlags |= TERM_LIKECOND; |
| 860 }else{ |
| 861 pTerm->wtFlags |= TERM_CODED; |
| 862 } |
| 863 if( pTerm->iParent<0 ) break; |
| 864 pTerm = &pTerm->pWC->a[pTerm->iParent]; |
| 865 pTerm->nChild--; |
| 866 if( pTerm->nChild!=0 ) break; |
| 867 nLoop++; |
| 868 } |
| 869 } |
| 870 |
| 871 /* |
| 872 ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 873 ** to the n registers starting at base. |
| 874 ** |
| 875 ** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the |
| 876 ** beginning and end of zAff are ignored. If all entries in zAff are |
| 877 ** SQLITE_AFF_BLOB, then no code gets generated. |
| 878 ** |
| 879 ** This routine makes its own copy of zAff so that the caller is free |
| 880 ** to modify zAff after this routine returns. |
| 881 */ |
| 882 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 883 Vdbe *v = pParse->pVdbe; |
| 884 if( zAff==0 ){ |
| 885 assert( pParse->db->mallocFailed ); |
| 886 return; |
| 887 } |
| 888 assert( v!=0 ); |
| 889 |
| 890 /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning |
| 891 ** and end of the affinity string. |
| 892 */ |
| 893 while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){ |
| 894 n--; |
| 895 base++; |
| 896 zAff++; |
| 897 } |
| 898 while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){ |
| 899 n--; |
| 900 } |
| 901 |
| 902 /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 903 if( n>0 ){ |
| 904 sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n); |
| 905 sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 906 } |
| 907 } |
| 908 |
| 909 /* |
| 910 ** Expression pRight, which is the RHS of a comparison operation, is |
| 911 ** either a vector of n elements or, if n==1, a scalar expression. |
| 912 ** Before the comparison operation, affinity zAff is to be applied |
| 913 ** to the pRight values. This function modifies characters within the |
| 914 ** affinity string to SQLITE_AFF_BLOB if either: |
| 915 ** |
| 916 ** * the comparison will be performed with no affinity, or |
| 917 ** * the affinity change in zAff is guaranteed not to change the value. |
| 918 */ |
| 919 static void updateRangeAffinityStr( |
| 920 Expr *pRight, /* RHS of comparison */ |
| 921 int n, /* Number of vector elements in comparison */ |
| 922 char *zAff /* Affinity string to modify */ |
| 923 ){ |
| 924 int i; |
| 925 for(i=0; i<n; i++){ |
| 926 Expr *p = sqlite3VectorFieldSubexpr(pRight, i); |
| 927 if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB |
| 928 || sqlite3ExprNeedsNoAffinityChange(p, zAff[i]) |
| 929 ){ |
| 930 zAff[i] = SQLITE_AFF_BLOB; |
| 931 } |
| 932 } |
| 933 } |
| 934 |
| 935 /* |
| 936 ** Generate code for a single equality term of the WHERE clause. An equality |
| 937 ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 938 ** coded. |
| 939 ** |
| 940 ** The current value for the constraint is left in a register, the index |
| 941 ** of which is returned. An attempt is made store the result in iTarget but |
| 942 ** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the |
| 943 ** constraint is a TK_EQ or TK_IS, then the current value might be left in |
| 944 ** some other register and it is the caller's responsibility to compensate. |
| 945 ** |
| 946 ** For a constraint of the form X=expr, the expression is evaluated in |
| 947 ** straight-line code. For constraints of the form X IN (...) |
| 948 ** this routine sets up a loop that will iterate over all values of X. |
| 949 */ |
| 950 static int codeEqualityTerm( |
| 951 Parse *pParse, /* The parsing context */ |
| 952 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 953 WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 954 int iEq, /* Index of the equality term within this level */ |
| 955 int bRev, /* True for reverse-order IN operations */ |
| 956 int iTarget /* Attempt to leave results in this register */ |
| 957 ){ |
| 958 Expr *pX = pTerm->pExpr; |
| 959 Vdbe *v = pParse->pVdbe; |
| 960 int iReg; /* Register holding results */ |
| 961 |
| 962 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm ); |
| 963 assert( iTarget>0 ); |
| 964 if( pX->op==TK_EQ || pX->op==TK_IS ){ |
| 965 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
| 966 }else if( pX->op==TK_ISNULL ){ |
| 967 iReg = iTarget; |
| 968 sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
| 969 #ifndef SQLITE_OMIT_SUBQUERY |
| 970 }else{ |
| 971 int eType = IN_INDEX_NOOP; |
| 972 int iTab; |
| 973 struct InLoop *pIn; |
| 974 WhereLoop *pLoop = pLevel->pWLoop; |
| 975 int i; |
| 976 int nEq = 0; |
| 977 int *aiMap = 0; |
| 978 |
| 979 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 980 && pLoop->u.btree.pIndex!=0 |
| 981 && pLoop->u.btree.pIndex->aSortOrder[iEq] |
| 982 ){ |
| 983 testcase( iEq==0 ); |
| 984 testcase( bRev ); |
| 985 bRev = !bRev; |
| 986 } |
| 987 assert( pX->op==TK_IN ); |
| 988 iReg = iTarget; |
| 989 |
| 990 for(i=0; i<iEq; i++){ |
| 991 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){ |
| 992 disableTerm(pLevel, pTerm); |
| 993 return iTarget; |
| 994 } |
| 995 } |
| 996 for(i=iEq;i<pLoop->nLTerm; i++){ |
| 997 if( ALWAYS(pLoop->aLTerm[i]) && pLoop->aLTerm[i]->pExpr==pX ) nEq++; |
| 998 } |
| 999 |
| 1000 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){ |
| 1001 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0); |
| 1002 }else{ |
| 1003 Select *pSelect = pX->x.pSelect; |
| 1004 sqlite3 *db = pParse->db; |
| 1005 u16 savedDbOptFlags = db->dbOptFlags; |
| 1006 ExprList *pOrigRhs = pSelect->pEList; |
| 1007 ExprList *pOrigLhs = pX->pLeft->x.pList; |
| 1008 ExprList *pRhs = 0; /* New Select.pEList for RHS */ |
| 1009 ExprList *pLhs = 0; /* New pX->pLeft vector */ |
| 1010 |
| 1011 for(i=iEq;i<pLoop->nLTerm; i++){ |
| 1012 if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 1013 int iField = pLoop->aLTerm[i]->iField - 1; |
| 1014 Expr *pNewRhs = sqlite3ExprDup(db, pOrigRhs->a[iField].pExpr, 0); |
| 1015 Expr *pNewLhs = sqlite3ExprDup(db, pOrigLhs->a[iField].pExpr, 0); |
| 1016 |
| 1017 pRhs = sqlite3ExprListAppend(pParse, pRhs, pNewRhs); |
| 1018 pLhs = sqlite3ExprListAppend(pParse, pLhs, pNewLhs); |
| 1019 } |
| 1020 } |
| 1021 if( !db->mallocFailed ){ |
| 1022 Expr *pLeft = pX->pLeft; |
| 1023 |
| 1024 if( pSelect->pOrderBy ){ |
| 1025 /* If the SELECT statement has an ORDER BY clause, zero the |
| 1026 ** iOrderByCol variables. These are set to non-zero when an |
| 1027 ** ORDER BY term exactly matches one of the terms of the |
| 1028 ** result-set. Since the result-set of the SELECT statement may |
| 1029 ** have been modified or reordered, these variables are no longer |
| 1030 ** set correctly. Since setting them is just an optimization, |
| 1031 ** it's easiest just to zero them here. */ |
| 1032 ExprList *pOrderBy = pSelect->pOrderBy; |
| 1033 for(i=0; i<pOrderBy->nExpr; i++){ |
| 1034 pOrderBy->a[i].u.x.iOrderByCol = 0; |
| 1035 } |
| 1036 } |
| 1037 |
| 1038 /* Take care here not to generate a TK_VECTOR containing only a |
| 1039 ** single value. Since the parser never creates such a vector, some |
| 1040 ** of the subroutines do not handle this case. */ |
| 1041 if( pLhs->nExpr==1 ){ |
| 1042 pX->pLeft = pLhs->a[0].pExpr; |
| 1043 }else{ |
| 1044 pLeft->x.pList = pLhs; |
| 1045 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int) * nEq); |
| 1046 testcase( aiMap==0 ); |
| 1047 } |
| 1048 pSelect->pEList = pRhs; |
| 1049 db->dbOptFlags |= SQLITE_QueryFlattener; |
| 1050 eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap); |
| 1051 db->dbOptFlags = savedDbOptFlags; |
| 1052 testcase( aiMap!=0 && aiMap[0]!=0 ); |
| 1053 pSelect->pEList = pOrigRhs; |
| 1054 pLeft->x.pList = pOrigLhs; |
| 1055 pX->pLeft = pLeft; |
| 1056 } |
| 1057 sqlite3ExprListDelete(pParse->db, pLhs); |
| 1058 sqlite3ExprListDelete(pParse->db, pRhs); |
| 1059 } |
| 1060 |
| 1061 if( eType==IN_INDEX_INDEX_DESC ){ |
| 1062 testcase( bRev ); |
| 1063 bRev = !bRev; |
| 1064 } |
| 1065 iTab = pX->iTable; |
| 1066 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 1067 VdbeCoverageIf(v, bRev); |
| 1068 VdbeCoverageIf(v, !bRev); |
| 1069 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 1070 |
| 1071 pLoop->wsFlags |= WHERE_IN_ABLE; |
| 1072 if( pLevel->u.in.nIn==0 ){ |
| 1073 pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 1074 } |
| 1075 |
| 1076 i = pLevel->u.in.nIn; |
| 1077 pLevel->u.in.nIn += nEq; |
| 1078 pLevel->u.in.aInLoop = |
| 1079 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 1080 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 1081 pIn = pLevel->u.in.aInLoop; |
| 1082 if( pIn ){ |
| 1083 int iMap = 0; /* Index in aiMap[] */ |
| 1084 pIn += i; |
| 1085 for(i=iEq;i<pLoop->nLTerm; i++){ |
| 1086 if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 1087 int iOut = iReg + i - iEq; |
| 1088 if( eType==IN_INDEX_ROWID ){ |
| 1089 testcase( nEq>1 ); /* Happens with a UNIQUE index on ROWID */ |
| 1090 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut); |
| 1091 }else{ |
| 1092 int iCol = aiMap ? aiMap[iMap++] : 0; |
| 1093 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut); |
| 1094 } |
| 1095 sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v); |
| 1096 if( i==iEq ){ |
| 1097 pIn->iCur = iTab; |
| 1098 pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; |
| 1099 }else{ |
| 1100 pIn->eEndLoopOp = OP_Noop; |
| 1101 } |
| 1102 pIn++; |
| 1103 } |
| 1104 } |
| 1105 }else{ |
| 1106 pLevel->u.in.nIn = 0; |
| 1107 } |
| 1108 sqlite3DbFree(pParse->db, aiMap); |
| 1109 #endif |
| 1110 } |
| 1111 disableTerm(pLevel, pTerm); |
| 1112 return iReg; |
| 1113 } |
| 1114 |
| 1115 /* |
| 1116 ** Generate code that will evaluate all == and IN constraints for an |
| 1117 ** index scan. |
| 1118 ** |
| 1119 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 1120 ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 1121 ** The index has as many as three equality constraints, but in this |
| 1122 ** example, the third "c" value is an inequality. So only two |
| 1123 ** constraints are coded. This routine will generate code to evaluate |
| 1124 ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 1125 ** in consecutive registers and the index of the first register is returned. |
| 1126 ** |
| 1127 ** In the example above nEq==2. But this subroutine works for any value |
| 1128 ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
| 1129 ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 1130 ** compute the affinity string. |
| 1131 ** |
| 1132 ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 1133 ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 1134 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 1135 ** occurs after the nEq quality constraints. |
| 1136 ** |
| 1137 ** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 1138 ** the index of the first memory cell in that range. The code that |
| 1139 ** calls this routine will use that memory range to store keys for |
| 1140 ** start and termination conditions of the loop. |
| 1141 ** key value of the loop. If one or more IN operators appear, then |
| 1142 ** this routine allocates an additional nEq memory cells for internal |
| 1143 ** use. |
| 1144 ** |
| 1145 ** Before returning, *pzAff is set to point to a buffer containing a |
| 1146 ** copy of the column affinity string of the index allocated using |
| 1147 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 1148 ** with equality constraints that use BLOB or NONE affinity are set to |
| 1149 ** SQLITE_AFF_BLOB. This is to deal with SQL such as the following: |
| 1150 ** |
| 1151 ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 1152 ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 1153 ** |
| 1154 ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 1155 ** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity, |
| 1156 ** no conversion should be attempted before using a t2.b value as part of |
| 1157 ** a key to search the index. Hence the first byte in the returned affinity |
| 1158 ** string in this example would be set to SQLITE_AFF_BLOB. |
| 1159 */ |
| 1160 static int codeAllEqualityTerms( |
| 1161 Parse *pParse, /* Parsing context */ |
| 1162 WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 1163 int bRev, /* Reverse the order of IN operators */ |
| 1164 int nExtraReg, /* Number of extra registers to allocate */ |
| 1165 char **pzAff /* OUT: Set to point to affinity string */ |
| 1166 ){ |
| 1167 u16 nEq; /* The number of == or IN constraints to code */ |
| 1168 u16 nSkip; /* Number of left-most columns to skip */ |
| 1169 Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 1170 Index *pIdx; /* The index being used for this loop */ |
| 1171 WhereTerm *pTerm; /* A single constraint term */ |
| 1172 WhereLoop *pLoop; /* The WhereLoop object */ |
| 1173 int j; /* Loop counter */ |
| 1174 int regBase; /* Base register */ |
| 1175 int nReg; /* Number of registers to allocate */ |
| 1176 char *zAff; /* Affinity string to return */ |
| 1177 |
| 1178 /* This module is only called on query plans that use an index. */ |
| 1179 pLoop = pLevel->pWLoop; |
| 1180 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 1181 nEq = pLoop->u.btree.nEq; |
| 1182 nSkip = pLoop->nSkip; |
| 1183 pIdx = pLoop->u.btree.pIndex; |
| 1184 assert( pIdx!=0 ); |
| 1185 |
| 1186 /* Figure out how many memory cells we will need then allocate them. |
| 1187 */ |
| 1188 regBase = pParse->nMem + 1; |
| 1189 nReg = pLoop->u.btree.nEq + nExtraReg; |
| 1190 pParse->nMem += nReg; |
| 1191 |
| 1192 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx)); |
| 1193 assert( zAff!=0 || pParse->db->mallocFailed ); |
| 1194 |
| 1195 if( nSkip ){ |
| 1196 int iIdxCur = pLevel->iIdxCur; |
| 1197 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 1198 VdbeCoverageIf(v, bRev==0); |
| 1199 VdbeCoverageIf(v, bRev!=0); |
| 1200 VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
| 1201 j = sqlite3VdbeAddOp0(v, OP_Goto); |
| 1202 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
| 1203 iIdxCur, 0, regBase, nSkip); |
| 1204 VdbeCoverageIf(v, bRev==0); |
| 1205 VdbeCoverageIf(v, bRev!=0); |
| 1206 sqlite3VdbeJumpHere(v, j); |
| 1207 for(j=0; j<nSkip; j++){ |
| 1208 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 1209 testcase( pIdx->aiColumn[j]==XN_EXPR ); |
| 1210 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j))); |
| 1211 } |
| 1212 } |
| 1213 |
| 1214 /* Evaluate the equality constraints |
| 1215 */ |
| 1216 assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
| 1217 for(j=nSkip; j<nEq; j++){ |
| 1218 int r1; |
| 1219 pTerm = pLoop->aLTerm[j]; |
| 1220 assert( pTerm!=0 ); |
| 1221 /* The following testcase is true for indices with redundant columns. |
| 1222 ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 1223 testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 1224 testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 1225 r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 1226 if( r1!=regBase+j ){ |
| 1227 if( nReg==1 ){ |
| 1228 sqlite3ReleaseTempReg(pParse, regBase); |
| 1229 regBase = r1; |
| 1230 }else{ |
| 1231 sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 1232 } |
| 1233 } |
| 1234 if( pTerm->eOperator & WO_IN ){ |
| 1235 if( pTerm->pExpr->flags & EP_xIsSelect ){ |
| 1236 /* No affinity ever needs to be (or should be) applied to a value |
| 1237 ** from the RHS of an "? IN (SELECT ...)" expression. The |
| 1238 ** sqlite3FindInIndex() routine has already ensured that the |
| 1239 ** affinity of the comparison has been applied to the value. */ |
| 1240 if( zAff ) zAff[j] = SQLITE_AFF_BLOB; |
| 1241 } |
| 1242 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 1243 Expr *pRight = pTerm->pExpr->pRight; |
| 1244 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
| 1245 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 1246 VdbeCoverage(v); |
| 1247 } |
| 1248 if( zAff ){ |
| 1249 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ |
| 1250 zAff[j] = SQLITE_AFF_BLOB; |
| 1251 } |
| 1252 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 1253 zAff[j] = SQLITE_AFF_BLOB; |
| 1254 } |
| 1255 } |
| 1256 } |
| 1257 } |
| 1258 *pzAff = zAff; |
| 1259 return regBase; |
| 1260 } |
| 1261 |
| 1262 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 1263 /* |
| 1264 ** If the most recently coded instruction is a constant range constraint |
| 1265 ** (a string literal) that originated from the LIKE optimization, then |
| 1266 ** set P3 and P5 on the OP_String opcode so that the string will be cast |
| 1267 ** to a BLOB at appropriate times. |
| 1268 ** |
| 1269 ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 1270 ** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 1271 ** scan loop run twice, once for strings and a second time for BLOBs. |
| 1272 ** The OP_String opcodes on the second pass convert the upper and lower |
| 1273 ** bound string constants to blobs. This routine makes the necessary changes |
| 1274 ** to the OP_String opcodes for that to happen. |
| 1275 ** |
| 1276 ** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then |
| 1277 ** only the one pass through the string space is required, so this routine |
| 1278 ** becomes a no-op. |
| 1279 */ |
| 1280 static void whereLikeOptimizationStringFixup( |
| 1281 Vdbe *v, /* prepared statement under construction */ |
| 1282 WhereLevel *pLevel, /* The loop that contains the LIKE operator */ |
| 1283 WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 1284 ){ |
| 1285 if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 1286 VdbeOp *pOp; |
| 1287 assert( pLevel->iLikeRepCntr>0 ); |
| 1288 pOp = sqlite3VdbeGetOp(v, -1); |
| 1289 assert( pOp!=0 ); |
| 1290 assert( pOp->opcode==OP_String8 |
| 1291 || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
| 1292 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */ |
| 1293 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */ |
| 1294 } |
| 1295 } |
| 1296 #else |
| 1297 # define whereLikeOptimizationStringFixup(A,B,C) |
| 1298 #endif |
| 1299 |
| 1300 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 1301 /* |
| 1302 ** Information is passed from codeCursorHint() down to individual nodes of |
| 1303 ** the expression tree (by sqlite3WalkExpr()) using an instance of this |
| 1304 ** structure. |
| 1305 */ |
| 1306 struct CCurHint { |
| 1307 int iTabCur; /* Cursor for the main table */ |
| 1308 int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */ |
| 1309 Index *pIdx; /* The index used to access the table */ |
| 1310 }; |
| 1311 |
| 1312 /* |
| 1313 ** This function is called for every node of an expression that is a candidate |
| 1314 ** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference |
| 1315 ** the table CCurHint.iTabCur, verify that the same column can be |
| 1316 ** accessed through the index. If it cannot, then set pWalker->eCode to 1. |
| 1317 */ |
| 1318 static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){ |
| 1319 struct CCurHint *pHint = pWalker->u.pCCurHint; |
| 1320 assert( pHint->pIdx!=0 ); |
| 1321 if( pExpr->op==TK_COLUMN |
| 1322 && pExpr->iTable==pHint->iTabCur |
| 1323 && sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn)<0 |
| 1324 ){ |
| 1325 pWalker->eCode = 1; |
| 1326 } |
| 1327 return WRC_Continue; |
| 1328 } |
| 1329 |
| 1330 /* |
| 1331 ** Test whether or not expression pExpr, which was part of a WHERE clause, |
| 1332 ** should be included in the cursor-hint for a table that is on the rhs |
| 1333 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the |
| 1334 ** expression is not suitable. |
| 1335 ** |
| 1336 ** An expression is unsuitable if it might evaluate to non NULL even if |
| 1337 ** a TK_COLUMN node that does affect the value of the expression is set |
| 1338 ** to NULL. For example: |
| 1339 ** |
| 1340 ** col IS NULL |
| 1341 ** col IS NOT NULL |
| 1342 ** coalesce(col, 1) |
| 1343 ** CASE WHEN col THEN 0 ELSE 1 END |
| 1344 */ |
| 1345 static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){ |
| 1346 if( pExpr->op==TK_IS |
| 1347 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT |
| 1348 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE |
| 1349 ){ |
| 1350 pWalker->eCode = 1; |
| 1351 }else if( pExpr->op==TK_FUNCTION ){ |
| 1352 int d1; |
| 1353 char d2[3]; |
| 1354 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){ |
| 1355 pWalker->eCode = 1; |
| 1356 } |
| 1357 } |
| 1358 |
| 1359 return WRC_Continue; |
| 1360 } |
| 1361 |
| 1362 |
| 1363 /* |
| 1364 ** This function is called on every node of an expression tree used as an |
| 1365 ** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN |
| 1366 ** that accesses any table other than the one identified by |
| 1367 ** CCurHint.iTabCur, then do the following: |
| 1368 ** |
| 1369 ** 1) allocate a register and code an OP_Column instruction to read |
| 1370 ** the specified column into the new register, and |
| 1371 ** |
| 1372 ** 2) transform the expression node to a TK_REGISTER node that reads |
| 1373 ** from the newly populated register. |
| 1374 ** |
| 1375 ** Also, if the node is a TK_COLUMN that does access the table idenified |
| 1376 ** by pCCurHint.iTabCur, and an index is being used (which we will |
| 1377 ** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into |
| 1378 ** an access of the index rather than the original table. |
| 1379 */ |
| 1380 static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ |
| 1381 int rc = WRC_Continue; |
| 1382 struct CCurHint *pHint = pWalker->u.pCCurHint; |
| 1383 if( pExpr->op==TK_COLUMN ){ |
| 1384 if( pExpr->iTable!=pHint->iTabCur ){ |
| 1385 Vdbe *v = pWalker->pParse->pVdbe; |
| 1386 int reg = ++pWalker->pParse->nMem; /* Register for column value */ |
| 1387 sqlite3ExprCodeGetColumnOfTable( |
| 1388 v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg |
| 1389 ); |
| 1390 pExpr->op = TK_REGISTER; |
| 1391 pExpr->iTable = reg; |
| 1392 }else if( pHint->pIdx!=0 ){ |
| 1393 pExpr->iTable = pHint->iIdxCur; |
| 1394 pExpr->iColumn = sqlite3ColumnOfIndex(pHint->pIdx, pExpr->iColumn); |
| 1395 assert( pExpr->iColumn>=0 ); |
| 1396 } |
| 1397 }else if( pExpr->op==TK_AGG_FUNCTION ){ |
| 1398 /* An aggregate function in the WHERE clause of a query means this must |
| 1399 ** be a correlated sub-query, and expression pExpr is an aggregate from |
| 1400 ** the parent context. Do not walk the function arguments in this case. |
| 1401 ** |
| 1402 ** todo: It should be possible to replace this node with a TK_REGISTER |
| 1403 ** expression, as the result of the expression must be stored in a |
| 1404 ** register at this point. The same holds for TK_AGG_COLUMN nodes. */ |
| 1405 rc = WRC_Prune; |
| 1406 } |
| 1407 return rc; |
| 1408 } |
| 1409 |
| 1410 /* |
| 1411 ** Insert an OP_CursorHint instruction if it is appropriate to do so. |
| 1412 */ |
| 1413 static void codeCursorHint( |
| 1414 struct SrcList_item *pTabItem, /* FROM clause item */ |
| 1415 WhereInfo *pWInfo, /* The where clause */ |
| 1416 WhereLevel *pLevel, /* Which loop to provide hints for */ |
| 1417 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */ |
| 1418 ){ |
| 1419 Parse *pParse = pWInfo->pParse; |
| 1420 sqlite3 *db = pParse->db; |
| 1421 Vdbe *v = pParse->pVdbe; |
| 1422 Expr *pExpr = 0; |
| 1423 WhereLoop *pLoop = pLevel->pWLoop; |
| 1424 int iCur; |
| 1425 WhereClause *pWC; |
| 1426 WhereTerm *pTerm; |
| 1427 int i, j; |
| 1428 struct CCurHint sHint; |
| 1429 Walker sWalker; |
| 1430 |
| 1431 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return; |
| 1432 iCur = pLevel->iTabCur; |
| 1433 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor ); |
| 1434 sHint.iTabCur = iCur; |
| 1435 sHint.iIdxCur = pLevel->iIdxCur; |
| 1436 sHint.pIdx = pLoop->u.btree.pIndex; |
| 1437 memset(&sWalker, 0, sizeof(sWalker)); |
| 1438 sWalker.pParse = pParse; |
| 1439 sWalker.u.pCCurHint = &sHint; |
| 1440 pWC = &pWInfo->sWC; |
| 1441 for(i=0; i<pWC->nTerm; i++){ |
| 1442 pTerm = &pWC->a[i]; |
| 1443 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 1444 if( pTerm->prereqAll & pLevel->notReady ) continue; |
| 1445 |
| 1446 /* Any terms specified as part of the ON(...) clause for any LEFT |
| 1447 ** JOIN for which the current table is not the rhs are omitted |
| 1448 ** from the cursor-hint. |
| 1449 ** |
| 1450 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms |
| 1451 ** that were specified as part of the WHERE clause must be excluded. |
| 1452 ** This is to address the following: |
| 1453 ** |
| 1454 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL; |
| 1455 ** |
| 1456 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its |
| 1457 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is |
| 1458 ** pushed down to the cursor, this row is filtered out, causing |
| 1459 ** SQLite to synthesize a row of NULL values. Which does match the |
| 1460 ** WHERE clause, and so the query returns a row. Which is incorrect. |
| 1461 ** |
| 1462 ** For the same reason, WHERE terms such as: |
| 1463 ** |
| 1464 ** WHERE 1 = (t2.c IS NULL) |
| 1465 ** |
| 1466 ** are also excluded. See codeCursorHintIsOrFunction() for details. |
| 1467 */ |
| 1468 if( pTabItem->fg.jointype & JT_LEFT ){ |
| 1469 Expr *pExpr = pTerm->pExpr; |
| 1470 if( !ExprHasProperty(pExpr, EP_FromJoin) |
| 1471 || pExpr->iRightJoinTable!=pTabItem->iCursor |
| 1472 ){ |
| 1473 sWalker.eCode = 0; |
| 1474 sWalker.xExprCallback = codeCursorHintIsOrFunction; |
| 1475 sqlite3WalkExpr(&sWalker, pTerm->pExpr); |
| 1476 if( sWalker.eCode ) continue; |
| 1477 } |
| 1478 }else{ |
| 1479 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue; |
| 1480 } |
| 1481 |
| 1482 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize |
| 1483 ** the cursor. These terms are not needed as hints for a pure range |
| 1484 ** scan (that has no == terms) so omit them. */ |
| 1485 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){ |
| 1486 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){} |
| 1487 if( j<pLoop->nLTerm ) continue; |
| 1488 } |
| 1489 |
| 1490 /* No subqueries or non-deterministic functions allowed */ |
| 1491 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue; |
| 1492 |
| 1493 /* For an index scan, make sure referenced columns are actually in |
| 1494 ** the index. */ |
| 1495 if( sHint.pIdx!=0 ){ |
| 1496 sWalker.eCode = 0; |
| 1497 sWalker.xExprCallback = codeCursorHintCheckExpr; |
| 1498 sqlite3WalkExpr(&sWalker, pTerm->pExpr); |
| 1499 if( sWalker.eCode ) continue; |
| 1500 } |
| 1501 |
| 1502 /* If we survive all prior tests, that means this term is worth hinting */ |
| 1503 pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); |
| 1504 } |
| 1505 if( pExpr!=0 ){ |
| 1506 sWalker.xExprCallback = codeCursorHintFixExpr; |
| 1507 sqlite3WalkExpr(&sWalker, pExpr); |
| 1508 sqlite3VdbeAddOp4(v, OP_CursorHint, |
| 1509 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0, |
| 1510 (const char*)pExpr, P4_EXPR); |
| 1511 } |
| 1512 } |
| 1513 #else |
| 1514 # define codeCursorHint(A,B,C,D) /* No-op */ |
| 1515 #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
| 1516 |
| 1517 /* |
| 1518 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains |
| 1519 ** a rowid value just read from cursor iIdxCur, open on index pIdx. This |
| 1520 ** function generates code to do a deferred seek of cursor iCur to the |
| 1521 ** rowid stored in register iRowid. |
| 1522 ** |
| 1523 ** Normally, this is just: |
| 1524 ** |
| 1525 ** OP_Seek $iCur $iRowid |
| 1526 ** |
| 1527 ** However, if the scan currently being coded is a branch of an OR-loop and |
| 1528 ** the statement currently being coded is a SELECT, then P3 of the OP_Seek |
| 1529 ** is set to iIdxCur and P4 is set to point to an array of integers |
| 1530 ** containing one entry for each column of the table cursor iCur is open |
| 1531 ** on. For each table column, if the column is the i'th column of the |
| 1532 ** index, then the corresponding array entry is set to (i+1). If the column |
| 1533 ** does not appear in the index at all, the array entry is set to 0. |
| 1534 */ |
| 1535 static void codeDeferredSeek( |
| 1536 WhereInfo *pWInfo, /* Where clause context */ |
| 1537 Index *pIdx, /* Index scan is using */ |
| 1538 int iCur, /* Cursor for IPK b-tree */ |
| 1539 int iIdxCur /* Index cursor */ |
| 1540 ){ |
| 1541 Parse *pParse = pWInfo->pParse; /* Parse context */ |
| 1542 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */ |
| 1543 |
| 1544 assert( iIdxCur>0 ); |
| 1545 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 ); |
| 1546 |
| 1547 sqlite3VdbeAddOp3(v, OP_Seek, iIdxCur, 0, iCur); |
| 1548 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE) |
| 1549 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask) |
| 1550 ){ |
| 1551 int i; |
| 1552 Table *pTab = pIdx->pTable; |
| 1553 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1)); |
| 1554 if( ai ){ |
| 1555 ai[0] = pTab->nCol; |
| 1556 for(i=0; i<pIdx->nColumn-1; i++){ |
| 1557 assert( pIdx->aiColumn[i]<pTab->nCol ); |
| 1558 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1; |
| 1559 } |
| 1560 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY); |
| 1561 } |
| 1562 } |
| 1563 } |
| 1564 |
| 1565 /* |
| 1566 ** If the expression passed as the second argument is a vector, generate |
| 1567 ** code to write the first nReg elements of the vector into an array |
| 1568 ** of registers starting with iReg. |
| 1569 ** |
| 1570 ** If the expression is not a vector, then nReg must be passed 1. In |
| 1571 ** this case, generate code to evaluate the expression and leave the |
| 1572 ** result in register iReg. |
| 1573 */ |
| 1574 static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){ |
| 1575 assert( nReg>0 ); |
| 1576 if( sqlite3ExprIsVector(p) ){ |
| 1577 #ifndef SQLITE_OMIT_SUBQUERY |
| 1578 if( (p->flags & EP_xIsSelect) ){ |
| 1579 Vdbe *v = pParse->pVdbe; |
| 1580 int iSelect = sqlite3CodeSubselect(pParse, p, 0, 0); |
| 1581 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1); |
| 1582 }else |
| 1583 #endif |
| 1584 { |
| 1585 int i; |
| 1586 ExprList *pList = p->x.pList; |
| 1587 assert( nReg<=pList->nExpr ); |
| 1588 for(i=0; i<nReg; i++){ |
| 1589 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i); |
| 1590 } |
| 1591 } |
| 1592 }else{ |
| 1593 assert( nReg==1 ); |
| 1594 sqlite3ExprCode(pParse, p, iReg); |
| 1595 } |
| 1596 } |
| 1597 |
| 1598 /* |
| 1599 ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 1600 ** implementation described by pWInfo. |
| 1601 */ |
| 1602 SQLITE_PRIVATE Bitmask sqlite3WhereCodeOneLoopStart( |
| 1603 WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 1604 int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 1605 Bitmask notReady /* Which tables are currently available */ |
| 1606 ){ |
| 1607 int j, k; /* Loop counters */ |
| 1608 int iCur; /* The VDBE cursor for the table */ |
| 1609 int addrNxt; /* Where to jump to continue with the next IN case */ |
| 1610 int omitTable; /* True if we use the index only */ |
| 1611 int bRev; /* True if we need to scan in reverse order */ |
| 1612 WhereLevel *pLevel; /* The where level to be coded */ |
| 1613 WhereLoop *pLoop; /* The WhereLoop object being coded */ |
| 1614 WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 1615 WhereTerm *pTerm; /* A WHERE clause term */ |
| 1616 Parse *pParse; /* Parsing context */ |
| 1617 sqlite3 *db; /* Database connection */ |
| 1618 Vdbe *v; /* The prepared stmt under constructions */ |
| 1619 struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
| 1620 int addrBrk; /* Jump here to break out of the loop */ |
| 1621 int addrCont; /* Jump here to continue with next cycle */ |
| 1622 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 1623 int iReleaseReg = 0; /* Temp register to free before returning */ |
| 1624 |
| 1625 pParse = pWInfo->pParse; |
| 1626 v = pParse->pVdbe; |
| 1627 pWC = &pWInfo->sWC; |
| 1628 db = pParse->db; |
| 1629 pLevel = &pWInfo->a[iLevel]; |
| 1630 pLoop = pLevel->pWLoop; |
| 1631 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 1632 iCur = pTabItem->iCursor; |
| 1633 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); |
| 1634 bRev = (pWInfo->revMask>>iLevel)&1; |
| 1635 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
| 1636 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0; |
| 1637 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
| 1638 |
| 1639 /* Create labels for the "break" and "continue" instructions |
| 1640 ** for the current loop. Jump to addrBrk to break out of a loop. |
| 1641 ** Jump to cont to go immediately to the next iteration of the |
| 1642 ** loop. |
| 1643 ** |
| 1644 ** When there is an IN operator, we also have a "addrNxt" label that |
| 1645 ** means to continue with the next IN value combination. When |
| 1646 ** there are no IN operators in the constraints, the "addrNxt" label |
| 1647 ** is the same as "addrBrk". |
| 1648 */ |
| 1649 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 1650 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 1651 |
| 1652 /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 1653 ** initialize a memory cell that records if this table matches any |
| 1654 ** row of the left table of the join. |
| 1655 */ |
| 1656 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){ |
| 1657 pLevel->iLeftJoin = ++pParse->nMem; |
| 1658 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 1659 VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 1660 } |
| 1661 |
| 1662 /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 1663 if( pTabItem->fg.viaCoroutine ){ |
| 1664 int regYield = pTabItem->regReturn; |
| 1665 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
| 1666 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
| 1667 VdbeCoverage(v); |
| 1668 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
| 1669 pLevel->op = OP_Goto; |
| 1670 }else |
| 1671 |
| 1672 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1673 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 1674 /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
| 1675 ** to access the data. |
| 1676 */ |
| 1677 int iReg; /* P3 Value for OP_VFilter */ |
| 1678 int addrNotFound; |
| 1679 int nConstraint = pLoop->nLTerm; |
| 1680 int iIn; /* Counter for IN constraints */ |
| 1681 |
| 1682 sqlite3ExprCachePush(pParse); |
| 1683 iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 1684 addrNotFound = pLevel->addrBrk; |
| 1685 for(j=0; j<nConstraint; j++){ |
| 1686 int iTarget = iReg+j+2; |
| 1687 pTerm = pLoop->aLTerm[j]; |
| 1688 if( NEVER(pTerm==0) ) continue; |
| 1689 if( pTerm->eOperator & WO_IN ){ |
| 1690 codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 1691 addrNotFound = pLevel->addrNxt; |
| 1692 }else{ |
| 1693 Expr *pRight = pTerm->pExpr->pRight; |
| 1694 codeExprOrVector(pParse, pRight, iTarget, 1); |
| 1695 } |
| 1696 } |
| 1697 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 1698 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
| 1699 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 1700 pLoop->u.vtab.idxStr, |
| 1701 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC); |
| 1702 VdbeCoverage(v); |
| 1703 pLoop->u.vtab.needFree = 0; |
| 1704 pLevel->p1 = iCur; |
| 1705 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext; |
| 1706 pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 1707 iIn = pLevel->u.in.nIn; |
| 1708 for(j=nConstraint-1; j>=0; j--){ |
| 1709 pTerm = pLoop->aLTerm[j]; |
| 1710 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 1711 disableTerm(pLevel, pTerm); |
| 1712 }else if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 1713 Expr *pCompare; /* The comparison operator */ |
| 1714 Expr *pRight; /* RHS of the comparison */ |
| 1715 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ |
| 1716 |
| 1717 /* Reload the constraint value into reg[iReg+j+2]. The same value |
| 1718 ** was loaded into the same register prior to the OP_VFilter, but |
| 1719 ** the xFilter implementation might have changed the datatype or |
| 1720 ** encoding of the value in the register, so it *must* be reloaded. */ |
| 1721 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed ); |
| 1722 if( !db->mallocFailed ){ |
| 1723 assert( iIn>0 ); |
| 1724 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[--iIn].addrInTop); |
| 1725 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid ); |
| 1726 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 ); |
| 1727 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 ); |
| 1728 testcase( pOp->opcode==OP_Rowid ); |
| 1729 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); |
| 1730 } |
| 1731 |
| 1732 /* Generate code that will continue to the next row if |
| 1733 ** the IN constraint is not satisfied */ |
| 1734 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0); |
| 1735 assert( pCompare!=0 || db->mallocFailed ); |
| 1736 if( pCompare ){ |
| 1737 pCompare->pLeft = pTerm->pExpr->pLeft; |
| 1738 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0); |
| 1739 if( pRight ){ |
| 1740 pRight->iTable = iReg+j+2; |
| 1741 sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0); |
| 1742 } |
| 1743 pCompare->pLeft = 0; |
| 1744 sqlite3ExprDelete(db, pCompare); |
| 1745 } |
| 1746 } |
| 1747 } |
| 1748 /* These registers need to be preserved in case there is an IN operator |
| 1749 ** loop. So we could deallocate the registers here (and potentially |
| 1750 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems |
| 1751 ** simpler and safer to simply not reuse the registers. |
| 1752 ** |
| 1753 ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 1754 */ |
| 1755 sqlite3ExprCachePop(pParse); |
| 1756 }else |
| 1757 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1758 |
| 1759 if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 1760 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 1761 ){ |
| 1762 /* Case 2: We can directly reference a single row using an |
| 1763 ** equality comparison against the ROWID field. Or |
| 1764 ** we reference multiple rows using a "rowid IN (...)" |
| 1765 ** construct. |
| 1766 */ |
| 1767 assert( pLoop->u.btree.nEq==1 ); |
| 1768 pTerm = pLoop->aLTerm[0]; |
| 1769 assert( pTerm!=0 ); |
| 1770 assert( pTerm->pExpr!=0 ); |
| 1771 assert( omitTable==0 ); |
| 1772 testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 1773 iReleaseReg = ++pParse->nMem; |
| 1774 iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 1775 if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 1776 addrNxt = pLevel->addrNxt; |
| 1777 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg); |
| 1778 VdbeCoverage(v); |
| 1779 sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
| 1780 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 1781 VdbeComment((v, "pk")); |
| 1782 pLevel->op = OP_Noop; |
| 1783 }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 1784 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 1785 ){ |
| 1786 /* Case 3: We have an inequality comparison against the ROWID field. |
| 1787 */ |
| 1788 int testOp = OP_Noop; |
| 1789 int start; |
| 1790 int memEndValue = 0; |
| 1791 WhereTerm *pStart, *pEnd; |
| 1792 |
| 1793 assert( omitTable==0 ); |
| 1794 j = 0; |
| 1795 pStart = pEnd = 0; |
| 1796 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 1797 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
| 1798 assert( pStart!=0 || pEnd!=0 ); |
| 1799 if( bRev ){ |
| 1800 pTerm = pStart; |
| 1801 pStart = pEnd; |
| 1802 pEnd = pTerm; |
| 1803 } |
| 1804 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd); |
| 1805 if( pStart ){ |
| 1806 Expr *pX; /* The expression that defines the start bound */ |
| 1807 int r1, rTemp; /* Registers for holding the start boundary */ |
| 1808 int op; /* Cursor seek operation */ |
| 1809 |
| 1810 /* The following constant maps TK_xx codes into corresponding |
| 1811 ** seek opcodes. It depends on a particular ordering of TK_xx |
| 1812 */ |
| 1813 const u8 aMoveOp[] = { |
| 1814 /* TK_GT */ OP_SeekGT, |
| 1815 /* TK_LE */ OP_SeekLE, |
| 1816 /* TK_LT */ OP_SeekLT, |
| 1817 /* TK_GE */ OP_SeekGE |
| 1818 }; |
| 1819 assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 1820 assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 1821 assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 1822 |
| 1823 assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
| 1824 testcase( pStart->wtFlags & TERM_VIRTUAL ); |
| 1825 pX = pStart->pExpr; |
| 1826 assert( pX!=0 ); |
| 1827 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
| 1828 if( sqlite3ExprIsVector(pX->pRight) ){ |
| 1829 r1 = rTemp = sqlite3GetTempReg(pParse); |
| 1830 codeExprOrVector(pParse, pX->pRight, r1, 1); |
| 1831 op = aMoveOp[(pX->op - TK_GT) | 0x0001]; |
| 1832 }else{ |
| 1833 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 1834 disableTerm(pLevel, pStart); |
| 1835 op = aMoveOp[(pX->op - TK_GT)]; |
| 1836 } |
| 1837 sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1); |
| 1838 VdbeComment((v, "pk")); |
| 1839 VdbeCoverageIf(v, pX->op==TK_GT); |
| 1840 VdbeCoverageIf(v, pX->op==TK_LE); |
| 1841 VdbeCoverageIf(v, pX->op==TK_LT); |
| 1842 VdbeCoverageIf(v, pX->op==TK_GE); |
| 1843 sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 1844 sqlite3ReleaseTempReg(pParse, rTemp); |
| 1845 }else{ |
| 1846 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
| 1847 VdbeCoverageIf(v, bRev==0); |
| 1848 VdbeCoverageIf(v, bRev!=0); |
| 1849 } |
| 1850 if( pEnd ){ |
| 1851 Expr *pX; |
| 1852 pX = pEnd->pExpr; |
| 1853 assert( pX!=0 ); |
| 1854 assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 1855 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
| 1856 testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
| 1857 memEndValue = ++pParse->nMem; |
| 1858 codeExprOrVector(pParse, pX->pRight, memEndValue, 1); |
| 1859 if( 0==sqlite3ExprIsVector(pX->pRight) |
| 1860 && (pX->op==TK_LT || pX->op==TK_GT) |
| 1861 ){ |
| 1862 testOp = bRev ? OP_Le : OP_Ge; |
| 1863 }else{ |
| 1864 testOp = bRev ? OP_Lt : OP_Gt; |
| 1865 } |
| 1866 if( 0==sqlite3ExprIsVector(pX->pRight) ){ |
| 1867 disableTerm(pLevel, pEnd); |
| 1868 } |
| 1869 } |
| 1870 start = sqlite3VdbeCurrentAddr(v); |
| 1871 pLevel->op = bRev ? OP_Prev : OP_Next; |
| 1872 pLevel->p1 = iCur; |
| 1873 pLevel->p2 = start; |
| 1874 assert( pLevel->p5==0 ); |
| 1875 if( testOp!=OP_Noop ){ |
| 1876 iRowidReg = ++pParse->nMem; |
| 1877 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
| 1878 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 1879 sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 1880 VdbeCoverageIf(v, testOp==OP_Le); |
| 1881 VdbeCoverageIf(v, testOp==OP_Lt); |
| 1882 VdbeCoverageIf(v, testOp==OP_Ge); |
| 1883 VdbeCoverageIf(v, testOp==OP_Gt); |
| 1884 sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 1885 } |
| 1886 }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 1887 /* Case 4: A scan using an index. |
| 1888 ** |
| 1889 ** The WHERE clause may contain zero or more equality |
| 1890 ** terms ("==" or "IN" operators) that refer to the N |
| 1891 ** left-most columns of the index. It may also contain |
| 1892 ** inequality constraints (>, <, >= or <=) on the indexed |
| 1893 ** column that immediately follows the N equalities. Only |
| 1894 ** the right-most column can be an inequality - the rest must |
| 1895 ** use the "==" and "IN" operators. For example, if the |
| 1896 ** index is on (x,y,z), then the following clauses are all |
| 1897 ** optimized: |
| 1898 ** |
| 1899 ** x=5 |
| 1900 ** x=5 AND y=10 |
| 1901 ** x=5 AND y<10 |
| 1902 ** x=5 AND y>5 AND y<10 |
| 1903 ** x=5 AND y=5 AND z<=10 |
| 1904 ** |
| 1905 ** The z<10 term of the following cannot be used, only |
| 1906 ** the x=5 term: |
| 1907 ** |
| 1908 ** x=5 AND z<10 |
| 1909 ** |
| 1910 ** N may be zero if there are inequality constraints. |
| 1911 ** If there are no inequality constraints, then N is at |
| 1912 ** least one. |
| 1913 ** |
| 1914 ** This case is also used when there are no WHERE clause |
| 1915 ** constraints but an index is selected anyway, in order |
| 1916 ** to force the output order to conform to an ORDER BY. |
| 1917 */ |
| 1918 static const u8 aStartOp[] = { |
| 1919 0, |
| 1920 0, |
| 1921 OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 1922 OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 1923 OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 1924 OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 1925 OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 1926 OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
| 1927 }; |
| 1928 static const u8 aEndOp[] = { |
| 1929 OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 1930 OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 1931 OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 1932 OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
| 1933 }; |
| 1934 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
| 1935 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */ |
| 1936 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */ |
| 1937 int regBase; /* Base register holding constraint values */ |
| 1938 WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 1939 WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 1940 int startEq; /* True if range start uses ==, >= or <= */ |
| 1941 int endEq; /* True if range end uses ==, >= or <= */ |
| 1942 int start_constraints; /* Start of range is constrained */ |
| 1943 int nConstraint; /* Number of constraint terms */ |
| 1944 Index *pIdx; /* The index we will be using */ |
| 1945 int iIdxCur; /* The VDBE cursor for the index */ |
| 1946 int nExtraReg = 0; /* Number of extra registers needed */ |
| 1947 int op; /* Instruction opcode */ |
| 1948 char *zStartAff; /* Affinity for start of range constraint */ |
| 1949 char *zEndAff = 0; /* Affinity for end of range constraint */ |
| 1950 u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 1951 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
| 1952 |
| 1953 pIdx = pLoop->u.btree.pIndex; |
| 1954 iIdxCur = pLevel->iIdxCur; |
| 1955 assert( nEq>=pLoop->nSkip ); |
| 1956 |
| 1957 /* If this loop satisfies a sort order (pOrderBy) request that |
| 1958 ** was passed to this function to implement a "SELECT min(x) ..." |
| 1959 ** query, then the caller will only allow the loop to run for |
| 1960 ** a single iteration. This means that the first row returned |
| 1961 ** should not have a NULL value stored in 'x'. If column 'x' is |
| 1962 ** the first one after the nEq equality constraints in the index, |
| 1963 ** this requires some special handling. |
| 1964 */ |
| 1965 assert( pWInfo->pOrderBy==0 |
| 1966 || pWInfo->pOrderBy->nExpr==1 |
| 1967 || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
| 1968 if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
| 1969 && pWInfo->nOBSat>0 |
| 1970 && (pIdx->nKeyCol>nEq) |
| 1971 ){ |
| 1972 assert( pLoop->nSkip==0 ); |
| 1973 bSeekPastNull = 1; |
| 1974 nExtraReg = 1; |
| 1975 } |
| 1976 |
| 1977 /* Find any inequality constraint terms for the start and end |
| 1978 ** of the range. |
| 1979 */ |
| 1980 j = nEq; |
| 1981 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 1982 pRangeStart = pLoop->aLTerm[j++]; |
| 1983 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm); |
| 1984 /* Like optimization range constraints always occur in pairs */ |
| 1985 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || |
| 1986 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); |
| 1987 } |
| 1988 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 1989 pRangeEnd = pLoop->aLTerm[j++]; |
| 1990 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop); |
| 1991 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 1992 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
| 1993 assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 1994 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
| 1995 pLevel->iLikeRepCntr = (u32)++pParse->nMem; |
| 1996 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr); |
| 1997 VdbeComment((v, "LIKE loop counter")); |
| 1998 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); |
| 1999 /* iLikeRepCntr actually stores 2x the counter register number. The |
| 2000 ** bottom bit indicates whether the search order is ASC or DESC. */ |
| 2001 testcase( bRev ); |
| 2002 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 2003 assert( (bRev & ~1)==0 ); |
| 2004 pLevel->iLikeRepCntr <<=1; |
| 2005 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC); |
| 2006 } |
| 2007 #endif |
| 2008 if( pRangeStart==0 ){ |
| 2009 j = pIdx->aiColumn[nEq]; |
| 2010 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){ |
| 2011 bSeekPastNull = 1; |
| 2012 } |
| 2013 } |
| 2014 } |
| 2015 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
| 2016 |
| 2017 /* If we are doing a reverse order scan on an ascending index, or |
| 2018 ** a forward order scan on a descending index, interchange the |
| 2019 ** start and end terms (pRangeStart and pRangeEnd). |
| 2020 */ |
| 2021 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 2022 || (bRev && pIdx->nKeyCol==nEq) |
| 2023 ){ |
| 2024 SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 2025 SWAP(u8, bSeekPastNull, bStopAtNull); |
| 2026 SWAP(u8, nBtm, nTop); |
| 2027 } |
| 2028 |
| 2029 /* Generate code to evaluate all constraint terms using == or IN |
| 2030 ** and store the values of those terms in an array of registers |
| 2031 ** starting at regBase. |
| 2032 */ |
| 2033 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd); |
| 2034 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 2035 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 2036 if( zStartAff && nTop ){ |
| 2037 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]); |
| 2038 } |
| 2039 addrNxt = pLevel->addrNxt; |
| 2040 |
| 2041 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 2042 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 2043 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 2044 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
| 2045 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 2046 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 2047 start_constraints = pRangeStart || nEq>0; |
| 2048 |
| 2049 /* Seek the index cursor to the start of the range. */ |
| 2050 nConstraint = nEq; |
| 2051 if( pRangeStart ){ |
| 2052 Expr *pRight = pRangeStart->pExpr->pRight; |
| 2053 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm); |
| 2054 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
| 2055 if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
| 2056 && sqlite3ExprCanBeNull(pRight) |
| 2057 ){ |
| 2058 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 2059 VdbeCoverage(v); |
| 2060 } |
| 2061 if( zStartAff ){ |
| 2062 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]); |
| 2063 } |
| 2064 nConstraint += nBtm; |
| 2065 testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
| 2066 if( sqlite3ExprIsVector(pRight)==0 ){ |
| 2067 disableTerm(pLevel, pRangeStart); |
| 2068 }else{ |
| 2069 startEq = 1; |
| 2070 } |
| 2071 bSeekPastNull = 0; |
| 2072 }else if( bSeekPastNull ){ |
| 2073 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 2074 nConstraint++; |
| 2075 startEq = 0; |
| 2076 start_constraints = 1; |
| 2077 } |
| 2078 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
| 2079 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){ |
| 2080 /* The skip-scan logic inside the call to codeAllEqualityConstraints() |
| 2081 ** above has already left the cursor sitting on the correct row, |
| 2082 ** so no further seeking is needed */ |
| 2083 }else{ |
| 2084 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 2085 assert( op!=0 ); |
| 2086 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 2087 VdbeCoverage(v); |
| 2088 VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 2089 VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 2090 VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 2091 VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 2092 VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 2093 VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
| 2094 } |
| 2095 |
| 2096 /* Load the value for the inequality constraint at the end of the |
| 2097 ** range (if any). |
| 2098 */ |
| 2099 nConstraint = nEq; |
| 2100 if( pRangeEnd ){ |
| 2101 Expr *pRight = pRangeEnd->pExpr->pRight; |
| 2102 sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
| 2103 codeExprOrVector(pParse, pRight, regBase+nEq, nTop); |
| 2104 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
| 2105 if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
| 2106 && sqlite3ExprCanBeNull(pRight) |
| 2107 ){ |
| 2108 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 2109 VdbeCoverage(v); |
| 2110 } |
| 2111 if( zEndAff ){ |
| 2112 updateRangeAffinityStr(pRight, nTop, zEndAff); |
| 2113 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff); |
| 2114 }else{ |
| 2115 assert( pParse->db->mallocFailed ); |
| 2116 } |
| 2117 nConstraint += nTop; |
| 2118 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
| 2119 |
| 2120 if( sqlite3ExprIsVector(pRight)==0 ){ |
| 2121 disableTerm(pLevel, pRangeEnd); |
| 2122 }else{ |
| 2123 endEq = 1; |
| 2124 } |
| 2125 }else if( bStopAtNull ){ |
| 2126 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 2127 endEq = 0; |
| 2128 nConstraint++; |
| 2129 } |
| 2130 sqlite3DbFree(db, zStartAff); |
| 2131 sqlite3DbFree(db, zEndAff); |
| 2132 |
| 2133 /* Top of the loop body */ |
| 2134 pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 2135 |
| 2136 /* Check if the index cursor is past the end of the range. */ |
| 2137 if( nConstraint ){ |
| 2138 op = aEndOp[bRev*2 + endEq]; |
| 2139 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 2140 testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 2141 testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 2142 testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 2143 testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
| 2144 } |
| 2145 |
| 2146 /* Seek the table cursor, if required */ |
| 2147 if( omitTable ){ |
| 2148 /* pIdx is a covering index. No need to access the main table. */ |
| 2149 }else if( HasRowid(pIdx->pTable) ){ |
| 2150 if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || ( |
| 2151 (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE) |
| 2152 && (pWInfo->eOnePass==ONEPASS_SINGLE) |
| 2153 )){ |
| 2154 iRowidReg = ++pParse->nMem; |
| 2155 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
| 2156 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
| 2157 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg); |
| 2158 VdbeCoverage(v); |
| 2159 }else{ |
| 2160 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur); |
| 2161 } |
| 2162 }else if( iCur!=iIdxCur ){ |
| 2163 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 2164 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 2165 for(j=0; j<pPk->nKeyCol; j++){ |
| 2166 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 2167 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 2168 } |
| 2169 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
| 2170 iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
| 2171 } |
| 2172 |
| 2173 /* Record the instruction used to terminate the loop. */ |
| 2174 if( pLoop->wsFlags & WHERE_ONEROW ){ |
| 2175 pLevel->op = OP_Noop; |
| 2176 }else if( bRev ){ |
| 2177 pLevel->op = OP_Prev; |
| 2178 }else{ |
| 2179 pLevel->op = OP_Next; |
| 2180 } |
| 2181 pLevel->p1 = iIdxCur; |
| 2182 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
| 2183 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
| 2184 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 2185 }else{ |
| 2186 assert( pLevel->p5==0 ); |
| 2187 } |
| 2188 }else |
| 2189 |
| 2190 #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
| 2191 if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 2192 /* Case 5: Two or more separately indexed terms connected by OR |
| 2193 ** |
| 2194 ** Example: |
| 2195 ** |
| 2196 ** CREATE TABLE t1(a,b,c,d); |
| 2197 ** CREATE INDEX i1 ON t1(a); |
| 2198 ** CREATE INDEX i2 ON t1(b); |
| 2199 ** CREATE INDEX i3 ON t1(c); |
| 2200 ** |
| 2201 ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 2202 ** |
| 2203 ** In the example, there are three indexed terms connected by OR. |
| 2204 ** The top of the loop looks like this: |
| 2205 ** |
| 2206 ** Null 1 # Zero the rowset in reg 1 |
| 2207 ** |
| 2208 ** Then, for each indexed term, the following. The arguments to |
| 2209 ** RowSetTest are such that the rowid of the current row is inserted |
| 2210 ** into the RowSet. If it is already present, control skips the |
| 2211 ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
| 2212 ** |
| 2213 ** sqlite3WhereBegin(<term>) |
| 2214 ** RowSetTest # Insert rowid into rowset |
| 2215 ** Gosub 2 A |
| 2216 ** sqlite3WhereEnd() |
| 2217 ** |
| 2218 ** Following the above, code to terminate the loop. Label A, the target |
| 2219 ** of the Gosub above, jumps to the instruction right after the Goto. |
| 2220 ** |
| 2221 ** Null 1 # Zero the rowset in reg 1 |
| 2222 ** Goto B # The loop is finished. |
| 2223 ** |
| 2224 ** A: <loop body> # Return data, whatever. |
| 2225 ** |
| 2226 ** Return 2 # Jump back to the Gosub |
| 2227 ** |
| 2228 ** B: <after the loop> |
| 2229 ** |
| 2230 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
| 2231 ** use an ephemeral index instead of a RowSet to record the primary |
| 2232 ** keys of the rows we have already seen. |
| 2233 ** |
| 2234 */ |
| 2235 WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
| 2236 SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
| 2237 Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 2238 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
| 2239 |
| 2240 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
| 2241 int regRowset = 0; /* Register for RowSet object */ |
| 2242 int regRowid = 0; /* Register holding rowid */ |
| 2243 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 2244 int iRetInit; /* Address of regReturn init */ |
| 2245 int untestedTerms = 0; /* Some terms not completely tested */ |
| 2246 int ii; /* Loop counter */ |
| 2247 u16 wctrlFlags; /* Flags for sub-WHERE clause */ |
| 2248 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
| 2249 Table *pTab = pTabItem->pTab; |
| 2250 |
| 2251 pTerm = pLoop->aLTerm[0]; |
| 2252 assert( pTerm!=0 ); |
| 2253 assert( pTerm->eOperator & WO_OR ); |
| 2254 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 2255 pOrWc = &pTerm->u.pOrInfo->wc; |
| 2256 pLevel->op = OP_Return; |
| 2257 pLevel->p1 = regReturn; |
| 2258 |
| 2259 /* Set up a new SrcList in pOrTab containing the table being scanned |
| 2260 ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 2261 ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 2262 */ |
| 2263 if( pWInfo->nLevel>1 ){ |
| 2264 int nNotReady; /* The number of notReady tables */ |
| 2265 struct SrcList_item *origSrc; /* Original list of tables */ |
| 2266 nNotReady = pWInfo->nLevel - iLevel - 1; |
| 2267 pOrTab = sqlite3StackAllocRaw(db, |
| 2268 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 2269 if( pOrTab==0 ) return notReady; |
| 2270 pOrTab->nAlloc = (u8)(nNotReady + 1); |
| 2271 pOrTab->nSrc = pOrTab->nAlloc; |
| 2272 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 2273 origSrc = pWInfo->pTabList->a; |
| 2274 for(k=1; k<=nNotReady; k++){ |
| 2275 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 2276 } |
| 2277 }else{ |
| 2278 pOrTab = pWInfo->pTabList; |
| 2279 } |
| 2280 |
| 2281 /* Initialize the rowset register to contain NULL. An SQL NULL is |
| 2282 ** equivalent to an empty rowset. Or, create an ephemeral index |
| 2283 ** capable of holding primary keys in the case of a WITHOUT ROWID. |
| 2284 ** |
| 2285 ** Also initialize regReturn to contain the address of the instruction |
| 2286 ** immediately following the OP_Return at the bottom of the loop. This |
| 2287 ** is required in a few obscure LEFT JOIN cases where control jumps |
| 2288 ** over the top of the loop into the body of it. In this case the |
| 2289 ** correct response for the end-of-loop code (the OP_Return) is to |
| 2290 ** fall through to the next instruction, just as an OP_Next does if |
| 2291 ** called on an uninitialized cursor. |
| 2292 */ |
| 2293 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 2294 if( HasRowid(pTab) ){ |
| 2295 regRowset = ++pParse->nMem; |
| 2296 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 2297 }else{ |
| 2298 Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 2299 regRowset = pParse->nTab++; |
| 2300 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 2301 sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 2302 } |
| 2303 regRowid = ++pParse->nMem; |
| 2304 } |
| 2305 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 2306 |
| 2307 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 2308 ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 2309 ** That way, terms in y that are factored into the disjunction will |
| 2310 ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 2311 ** |
| 2312 ** Actually, each subexpression is converted to "xN AND w" where w is |
| 2313 ** the "interesting" terms of z - terms that did not originate in the |
| 2314 ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 2315 ** indices. |
| 2316 ** |
| 2317 ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 2318 ** is not contained in the ON clause of a LEFT JOIN. |
| 2319 ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
| 2320 */ |
| 2321 if( pWC->nTerm>1 ){ |
| 2322 int iTerm; |
| 2323 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 2324 Expr *pExpr = pWC->a[iTerm].pExpr; |
| 2325 if( &pWC->a[iTerm] == pTerm ) continue; |
| 2326 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
| 2327 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 2328 testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); |
| 2329 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue; |
| 2330 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 2331 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 2332 pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 2333 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
| 2334 } |
| 2335 if( pAndExpr ){ |
| 2336 pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr); |
| 2337 } |
| 2338 } |
| 2339 |
| 2340 /* Run a separate WHERE clause for each term of the OR clause. After |
| 2341 ** eliminating duplicates from other WHERE clauses, the action for each |
| 2342 ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 2343 */ |
| 2344 wctrlFlags = WHERE_OR_SUBCLAUSE | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE); |
| 2345 for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 2346 WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 2347 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 2348 WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 2349 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| 2350 int jmp1 = 0; /* Address of jump operation */ |
| 2351 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
| 2352 pAndExpr->pLeft = pOrExpr; |
| 2353 pOrExpr = pAndExpr; |
| 2354 } |
| 2355 /* Loop through table entries that match term pOrTerm. */ |
| 2356 WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
| 2357 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
| 2358 wctrlFlags, iCovCur); |
| 2359 assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
| 2360 if( pSubWInfo ){ |
| 2361 WhereLoop *pSubLoop; |
| 2362 int addrExplain = sqlite3WhereExplainOneScan( |
| 2363 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 2364 ); |
| 2365 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 2366 |
| 2367 /* This is the sub-WHERE clause body. First skip over |
| 2368 ** duplicate rows from prior sub-WHERE clauses, and record the |
| 2369 ** rowid (or PRIMARY KEY) for the current row so that the same |
| 2370 ** row will be skipped in subsequent sub-WHERE clauses. |
| 2371 */ |
| 2372 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 2373 int r; |
| 2374 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 2375 if( HasRowid(pTab) ){ |
| 2376 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); |
| 2377 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, |
| 2378 r,iSet); |
| 2379 VdbeCoverage(v); |
| 2380 }else{ |
| 2381 Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 2382 int nPk = pPk->nKeyCol; |
| 2383 int iPk; |
| 2384 |
| 2385 /* Read the PK into an array of temp registers. */ |
| 2386 r = sqlite3GetTempRange(pParse, nPk); |
| 2387 for(iPk=0; iPk<nPk; iPk++){ |
| 2388 int iCol = pPk->aiColumn[iPk]; |
| 2389 sqlite3ExprCodeGetColumnToReg(pParse, pTab, iCol, iCur, r+iPk); |
| 2390 } |
| 2391 |
| 2392 /* Check if the temp table already contains this key. If so, |
| 2393 ** the row has already been included in the result set and |
| 2394 ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 2395 ** insert the key into the temp table and proceed with processing |
| 2396 ** the row. |
| 2397 ** |
| 2398 ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 2399 ** is zero, assume that the key cannot already be present in |
| 2400 ** the temp table. And if iSet is -1, assume that there is no |
| 2401 ** need to insert the key into the temp table, as it will never |
| 2402 ** be tested for. */ |
| 2403 if( iSet ){ |
| 2404 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
| 2405 VdbeCoverage(v); |
| 2406 } |
| 2407 if( iSet>=0 ){ |
| 2408 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 2409 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid, |
| 2410 r, nPk); |
| 2411 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2412 } |
| 2413 |
| 2414 /* Release the array of temp registers */ |
| 2415 sqlite3ReleaseTempRange(pParse, r, nPk); |
| 2416 } |
| 2417 } |
| 2418 |
| 2419 /* Invoke the main loop body as a subroutine */ |
| 2420 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 2421 |
| 2422 /* Jump here (skipping the main loop body subroutine) if the |
| 2423 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
| 2424 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1); |
| 2425 |
| 2426 /* The pSubWInfo->untestedTerms flag means that this OR term |
| 2427 ** contained one or more AND term from a notReady table. The |
| 2428 ** terms from the notReady table could not be tested and will |
| 2429 ** need to be tested later. |
| 2430 */ |
| 2431 if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 2432 |
| 2433 /* If all of the OR-connected terms are optimized using the same |
| 2434 ** index, and the index is opened using the same cursor number |
| 2435 ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 2436 ** be possible to use that index as a covering index. |
| 2437 ** |
| 2438 ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 2439 ** uses an index, and this is either the first OR-connected term |
| 2440 ** processed or the index is the same as that used by all previous |
| 2441 ** terms, set pCov to the candidate covering index. Otherwise, set |
| 2442 ** pCov to NULL to indicate that no candidate covering index will |
| 2443 ** be available. |
| 2444 */ |
| 2445 pSubLoop = pSubWInfo->a[0].pWLoop; |
| 2446 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 2447 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 2448 && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
| 2449 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
| 2450 ){ |
| 2451 assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 2452 pCov = pSubLoop->u.btree.pIndex; |
| 2453 }else{ |
| 2454 pCov = 0; |
| 2455 } |
| 2456 |
| 2457 /* Finish the loop through table entries that match term pOrTerm. */ |
| 2458 sqlite3WhereEnd(pSubWInfo); |
| 2459 } |
| 2460 } |
| 2461 } |
| 2462 pLevel->u.pCovidx = pCov; |
| 2463 if( pCov ) pLevel->iIdxCur = iCovCur; |
| 2464 if( pAndExpr ){ |
| 2465 pAndExpr->pLeft = 0; |
| 2466 sqlite3ExprDelete(db, pAndExpr); |
| 2467 } |
| 2468 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
| 2469 sqlite3VdbeGoto(v, pLevel->addrBrk); |
| 2470 sqlite3VdbeResolveLabel(v, iLoopBody); |
| 2471 |
| 2472 if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
| 2473 if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 2474 }else |
| 2475 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 2476 |
| 2477 { |
| 2478 /* Case 6: There is no usable index. We must do a complete |
| 2479 ** scan of the entire table. |
| 2480 */ |
| 2481 static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 2482 static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 2483 assert( bRev==0 || bRev==1 ); |
| 2484 if( pTabItem->fg.isRecursive ){ |
| 2485 /* Tables marked isRecursive have only a single row that is stored in |
| 2486 ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
| 2487 pLevel->op = OP_Noop; |
| 2488 }else{ |
| 2489 codeCursorHint(pTabItem, pWInfo, pLevel, 0); |
| 2490 pLevel->op = aStep[bRev]; |
| 2491 pLevel->p1 = iCur; |
| 2492 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
| 2493 VdbeCoverageIf(v, bRev==0); |
| 2494 VdbeCoverageIf(v, bRev!=0); |
| 2495 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 2496 } |
| 2497 } |
| 2498 |
| 2499 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2500 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 2501 #endif |
| 2502 |
| 2503 /* Insert code to test every subexpression that can be completely |
| 2504 ** computed using the current set of tables. |
| 2505 */ |
| 2506 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 2507 Expr *pE; |
| 2508 int skipLikeAddr = 0; |
| 2509 testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 2510 testcase( pTerm->wtFlags & TERM_CODED ); |
| 2511 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 2512 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 2513 testcase( pWInfo->untestedTerms==0 |
| 2514 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ); |
| 2515 pWInfo->untestedTerms = 1; |
| 2516 continue; |
| 2517 } |
| 2518 pE = pTerm->pExpr; |
| 2519 assert( pE!=0 ); |
| 2520 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 2521 continue; |
| 2522 } |
| 2523 if( pTerm->wtFlags & TERM_LIKECOND ){ |
| 2524 /* If the TERM_LIKECOND flag is set, that means that the range search |
| 2525 ** is sufficient to guarantee that the LIKE operator is true, so we |
| 2526 ** can skip the call to the like(A,B) function. But this only works |
| 2527 ** for strings. So do not skip the call to the function on the pass |
| 2528 ** that compares BLOBs. */ |
| 2529 #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 2530 continue; |
| 2531 #else |
| 2532 u32 x = pLevel->iLikeRepCntr; |
| 2533 assert( x>0 ); |
| 2534 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)? OP_IfNot : OP_If, (int)(x>>1)); |
| 2535 VdbeCoverage(v); |
| 2536 #endif |
| 2537 } |
| 2538 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
| 2539 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); |
| 2540 pTerm->wtFlags |= TERM_CODED; |
| 2541 } |
| 2542 |
| 2543 /* Insert code to test for implied constraints based on transitivity |
| 2544 ** of the "==" operator. |
| 2545 ** |
| 2546 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 2547 ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 2548 ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 2549 ** the implied "t1.a=123" constraint. |
| 2550 */ |
| 2551 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 2552 Expr *pE, sEAlt; |
| 2553 WhereTerm *pAlt; |
| 2554 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 2555 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; |
| 2556 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; |
| 2557 if( pTerm->leftCursor!=iCur ) continue; |
| 2558 if( pLevel->iLeftJoin ) continue; |
| 2559 pE = pTerm->pExpr; |
| 2560 assert( !ExprHasProperty(pE, EP_FromJoin) ); |
| 2561 assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
| 2562 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady, |
| 2563 WO_EQ|WO_IN|WO_IS, 0); |
| 2564 if( pAlt==0 ) continue; |
| 2565 if( pAlt->wtFlags & (TERM_CODED) ) continue; |
| 2566 testcase( pAlt->eOperator & WO_EQ ); |
| 2567 testcase( pAlt->eOperator & WO_IS ); |
| 2568 testcase( pAlt->eOperator & WO_IN ); |
| 2569 VdbeModuleComment((v, "begin transitive constraint")); |
| 2570 sEAlt = *pAlt->pExpr; |
| 2571 sEAlt.pLeft = pE->pLeft; |
| 2572 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 2573 } |
| 2574 |
| 2575 /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 2576 ** at least one row of the right table has matched the left table. |
| 2577 */ |
| 2578 if( pLevel->iLeftJoin ){ |
| 2579 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 2580 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 2581 VdbeComment((v, "record LEFT JOIN hit")); |
| 2582 sqlite3ExprCacheClear(pParse); |
| 2583 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
| 2584 testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 2585 testcase( pTerm->wtFlags & TERM_CODED ); |
| 2586 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 2587 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 2588 assert( pWInfo->untestedTerms ); |
| 2589 continue; |
| 2590 } |
| 2591 assert( pTerm->pExpr ); |
| 2592 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 2593 pTerm->wtFlags |= TERM_CODED; |
| 2594 } |
| 2595 } |
| 2596 |
| 2597 return pLevel->notReady; |
| 2598 } |
| 2599 |
| 2600 /************** End of wherecode.c *******************************************/ |
| 2601 /************** Begin file whereexpr.c ***************************************/ |
| 2602 /* |
| 2603 ** 2015-06-08 |
| 2604 ** |
| 2605 ** The author disclaims copyright to this source code. In place of |
| 2606 ** a legal notice, here is a blessing: |
| 2607 ** |
| 2608 ** May you do good and not evil. |
| 2609 ** May you find forgiveness for yourself and forgive others. |
| 2610 ** May you share freely, never taking more than you give. |
| 2611 ** |
| 2612 ************************************************************************* |
| 2613 ** This module contains C code that generates VDBE code used to process |
| 2614 ** the WHERE clause of SQL statements. |
| 2615 ** |
| 2616 ** This file was originally part of where.c but was split out to improve |
| 2617 ** readability and editabiliity. This file contains utility routines for |
| 2618 ** analyzing Expr objects in the WHERE clause. |
| 2619 */ |
| 2620 /* #include "sqliteInt.h" */ |
| 2621 /* #include "whereInt.h" */ |
| 2622 |
| 2623 /* Forward declarations */ |
| 2624 static void exprAnalyze(SrcList*, WhereClause*, int); |
| 2625 |
| 2626 /* |
| 2627 ** Deallocate all memory associated with a WhereOrInfo object. |
| 2628 */ |
| 2629 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
| 2630 sqlite3WhereClauseClear(&p->wc); |
| 2631 sqlite3DbFree(db, p); |
| 2632 } |
| 2633 |
| 2634 /* |
| 2635 ** Deallocate all memory associated with a WhereAndInfo object. |
| 2636 */ |
| 2637 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
| 2638 sqlite3WhereClauseClear(&p->wc); |
| 2639 sqlite3DbFree(db, p); |
| 2640 } |
| 2641 |
| 2642 /* |
| 2643 ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 2644 ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 2645 ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 2646 ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 2647 ** allocation error. The memory allocation failure will be recorded in |
| 2648 ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 2649 ** |
| 2650 ** This routine will increase the size of the pWC->a[] array as necessary. |
| 2651 ** |
| 2652 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
| 2653 ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 2654 ** This is true even if this routine fails to allocate a new WhereTerm. |
| 2655 ** |
| 2656 ** WARNING: This routine might reallocate the space used to store |
| 2657 ** WhereTerms. All pointers to WhereTerms should be invalidated after |
| 2658 ** calling this routine. Such pointers may be reinitialized by referencing |
| 2659 ** the pWC->a[] array. |
| 2660 */ |
| 2661 static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ |
| 2662 WhereTerm *pTerm; |
| 2663 int idx; |
| 2664 testcase( wtFlags & TERM_VIRTUAL ); |
| 2665 if( pWC->nTerm>=pWC->nSlot ){ |
| 2666 WhereTerm *pOld = pWC->a; |
| 2667 sqlite3 *db = pWC->pWInfo->pParse->db; |
| 2668 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
| 2669 if( pWC->a==0 ){ |
| 2670 if( wtFlags & TERM_DYNAMIC ){ |
| 2671 sqlite3ExprDelete(db, p); |
| 2672 } |
| 2673 pWC->a = pOld; |
| 2674 return 0; |
| 2675 } |
| 2676 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 2677 if( pOld!=pWC->aStatic ){ |
| 2678 sqlite3DbFree(db, pOld); |
| 2679 } |
| 2680 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
| 2681 } |
| 2682 pTerm = &pWC->a[idx = pWC->nTerm++]; |
| 2683 if( p && ExprHasProperty(p, EP_Unlikely) ){ |
| 2684 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
| 2685 }else{ |
| 2686 pTerm->truthProb = 1; |
| 2687 } |
| 2688 pTerm->pExpr = sqlite3ExprSkipCollate(p); |
| 2689 pTerm->wtFlags = wtFlags; |
| 2690 pTerm->pWC = pWC; |
| 2691 pTerm->iParent = -1; |
| 2692 memset(&pTerm->eOperator, 0, |
| 2693 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator)); |
| 2694 return idx; |
| 2695 } |
| 2696 |
| 2697 /* |
| 2698 ** Return TRUE if the given operator is one of the operators that is |
| 2699 ** allowed for an indexable WHERE clause term. The allowed operators are |
| 2700 ** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL" |
| 2701 */ |
| 2702 static int allowedOp(int op){ |
| 2703 assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 2704 assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 2705 assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 2706 assert( TK_GE==TK_EQ+4 ); |
| 2707 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS; |
| 2708 } |
| 2709 |
| 2710 /* |
| 2711 ** Commute a comparison operator. Expressions of the form "X op Y" |
| 2712 ** are converted into "Y op X". |
| 2713 ** |
| 2714 ** If left/right precedence rules come into play when determining the |
| 2715 ** collating sequence, then COLLATE operators are adjusted to ensure |
| 2716 ** that the collating sequence does not change. For example: |
| 2717 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on |
| 2718 ** the left hand side of a comparison overrides any collation sequence |
| 2719 ** attached to the right. For the same reason the EP_Collate flag |
| 2720 ** is not commuted. |
| 2721 */ |
| 2722 static void exprCommute(Parse *pParse, Expr *pExpr){ |
| 2723 u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 2724 u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
| 2725 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
| 2726 if( expRight==expLeft ){ |
| 2727 /* Either X and Y both have COLLATE operator or neither do */ |
| 2728 if( expRight ){ |
| 2729 /* Both X and Y have COLLATE operators. Make sure X is always |
| 2730 ** used by clearing the EP_Collate flag from Y. */ |
| 2731 pExpr->pRight->flags &= ~EP_Collate; |
| 2732 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 2733 /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 2734 ** collating sequence. So add the EP_Collate marker on X to cause |
| 2735 ** it to be searched first. */ |
| 2736 pExpr->pLeft->flags |= EP_Collate; |
| 2737 } |
| 2738 } |
| 2739 SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 2740 if( pExpr->op>=TK_GT ){ |
| 2741 assert( TK_LT==TK_GT+2 ); |
| 2742 assert( TK_GE==TK_LE+2 ); |
| 2743 assert( TK_GT>TK_EQ ); |
| 2744 assert( TK_GT<TK_LE ); |
| 2745 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 2746 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
| 2747 } |
| 2748 } |
| 2749 |
| 2750 /* |
| 2751 ** Translate from TK_xx operator to WO_xx bitmask. |
| 2752 */ |
| 2753 static u16 operatorMask(int op){ |
| 2754 u16 c; |
| 2755 assert( allowedOp(op) ); |
| 2756 if( op==TK_IN ){ |
| 2757 c = WO_IN; |
| 2758 }else if( op==TK_ISNULL ){ |
| 2759 c = WO_ISNULL; |
| 2760 }else if( op==TK_IS ){ |
| 2761 c = WO_IS; |
| 2762 }else{ |
| 2763 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 2764 c = (u16)(WO_EQ<<(op-TK_EQ)); |
| 2765 } |
| 2766 assert( op!=TK_ISNULL || c==WO_ISNULL ); |
| 2767 assert( op!=TK_IN || c==WO_IN ); |
| 2768 assert( op!=TK_EQ || c==WO_EQ ); |
| 2769 assert( op!=TK_LT || c==WO_LT ); |
| 2770 assert( op!=TK_LE || c==WO_LE ); |
| 2771 assert( op!=TK_GT || c==WO_GT ); |
| 2772 assert( op!=TK_GE || c==WO_GE ); |
| 2773 assert( op!=TK_IS || c==WO_IS ); |
| 2774 return c; |
| 2775 } |
| 2776 |
| 2777 |
| 2778 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 2779 /* |
| 2780 ** Check to see if the given expression is a LIKE or GLOB operator that |
| 2781 ** can be optimized using inequality constraints. Return TRUE if it is |
| 2782 ** so and false if not. |
| 2783 ** |
| 2784 ** In order for the operator to be optimizible, the RHS must be a string |
| 2785 ** literal that does not begin with a wildcard. The LHS must be a column |
| 2786 ** that may only be NULL, a string, or a BLOB, never a number. (This means |
| 2787 ** that virtual tables cannot participate in the LIKE optimization.) The |
| 2788 ** collating sequence for the column on the LHS must be appropriate for |
| 2789 ** the operator. |
| 2790 */ |
| 2791 static int isLikeOrGlob( |
| 2792 Parse *pParse, /* Parsing and code generating context */ |
| 2793 Expr *pExpr, /* Test this expression */ |
| 2794 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
| 2795 int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 2796 int *pnoCase /* True if uppercase is equivalent to lowercase */ |
| 2797 ){ |
| 2798 const char *z = 0; /* String on RHS of LIKE operator */ |
| 2799 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 2800 ExprList *pList; /* List of operands to the LIKE operator */ |
| 2801 int c; /* One character in z[] */ |
| 2802 int cnt; /* Number of non-wildcard prefix characters */ |
| 2803 char wc[3]; /* Wildcard characters */ |
| 2804 sqlite3 *db = pParse->db; /* Database connection */ |
| 2805 sqlite3_value *pVal = 0; |
| 2806 int op; /* Opcode of pRight */ |
| 2807 int rc; /* Result code to return */ |
| 2808 |
| 2809 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
| 2810 return 0; |
| 2811 } |
| 2812 #ifdef SQLITE_EBCDIC |
| 2813 if( *pnoCase ) return 0; |
| 2814 #endif |
| 2815 pList = pExpr->x.pList; |
| 2816 pLeft = pList->a[1].pExpr; |
| 2817 if( pLeft->op!=TK_COLUMN |
| 2818 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 2819 || IsVirtual(pLeft->pTab) /* Value might be numeric */ |
| 2820 ){ |
| 2821 /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 2822 ** be the name of an indexed column with TEXT affinity. */ |
| 2823 return 0; |
| 2824 } |
| 2825 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
| 2826 |
| 2827 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
| 2828 op = pRight->op; |
| 2829 if( op==TK_VARIABLE ){ |
| 2830 Vdbe *pReprepare = pParse->pReprepare; |
| 2831 int iCol = pRight->iColumn; |
| 2832 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB); |
| 2833 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 2834 z = (char *)sqlite3_value_text(pVal); |
| 2835 } |
| 2836 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
| 2837 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 2838 }else if( op==TK_STRING ){ |
| 2839 z = pRight->u.zToken; |
| 2840 } |
| 2841 if( z ){ |
| 2842 cnt = 0; |
| 2843 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ |
| 2844 cnt++; |
| 2845 } |
| 2846 if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
| 2847 Expr *pPrefix; |
| 2848 *pisComplete = c==wc[0] && z[cnt+1]==0; |
| 2849 pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 2850 if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 2851 *ppPrefix = pPrefix; |
| 2852 if( op==TK_VARIABLE ){ |
| 2853 Vdbe *v = pParse->pVdbe; |
| 2854 sqlite3VdbeSetVarmask(v, pRight->iColumn); |
| 2855 if( *pisComplete && pRight->u.zToken[1] ){ |
| 2856 /* If the rhs of the LIKE expression is a variable, and the current |
| 2857 ** value of the variable means there is no need to invoke the LIKE |
| 2858 ** function, then no OP_Variable will be added to the program. |
| 2859 ** This causes problems for the sqlite3_bind_parameter_name() |
| 2860 ** API. To work around them, add a dummy OP_Variable here. |
| 2861 */ |
| 2862 int r1 = sqlite3GetTempReg(pParse); |
| 2863 sqlite3ExprCodeTarget(pParse, pRight, r1); |
| 2864 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
| 2865 sqlite3ReleaseTempReg(pParse, r1); |
| 2866 } |
| 2867 } |
| 2868 }else{ |
| 2869 z = 0; |
| 2870 } |
| 2871 } |
| 2872 |
| 2873 rc = (z!=0); |
| 2874 sqlite3ValueFree(pVal); |
| 2875 return rc; |
| 2876 } |
| 2877 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 2878 |
| 2879 |
| 2880 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 2881 /* |
| 2882 ** Check to see if the given expression is of the form |
| 2883 ** |
| 2884 ** column OP expr |
| 2885 ** |
| 2886 ** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a |
| 2887 ** column of a virtual table. |
| 2888 ** |
| 2889 ** If it is then return TRUE. If not, return FALSE. |
| 2890 */ |
| 2891 static int isMatchOfColumn( |
| 2892 Expr *pExpr, /* Test this expression */ |
| 2893 unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */ |
| 2894 ){ |
| 2895 static const struct Op2 { |
| 2896 const char *zOp; |
| 2897 unsigned char eOp2; |
| 2898 } aOp[] = { |
| 2899 { "match", SQLITE_INDEX_CONSTRAINT_MATCH }, |
| 2900 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB }, |
| 2901 { "like", SQLITE_INDEX_CONSTRAINT_LIKE }, |
| 2902 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP } |
| 2903 }; |
| 2904 ExprList *pList; |
| 2905 Expr *pCol; /* Column reference */ |
| 2906 int i; |
| 2907 |
| 2908 if( pExpr->op!=TK_FUNCTION ){ |
| 2909 return 0; |
| 2910 } |
| 2911 pList = pExpr->x.pList; |
| 2912 if( pList==0 || pList->nExpr!=2 ){ |
| 2913 return 0; |
| 2914 } |
| 2915 pCol = pList->a[1].pExpr; |
| 2916 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){ |
| 2917 return 0; |
| 2918 } |
| 2919 for(i=0; i<ArraySize(aOp); i++){ |
| 2920 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){ |
| 2921 *peOp2 = aOp[i].eOp2; |
| 2922 return 1; |
| 2923 } |
| 2924 } |
| 2925 return 0; |
| 2926 } |
| 2927 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 2928 |
| 2929 /* |
| 2930 ** If the pBase expression originated in the ON or USING clause of |
| 2931 ** a join, then transfer the appropriate markings over to derived. |
| 2932 */ |
| 2933 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
| 2934 if( pDerived ){ |
| 2935 pDerived->flags |= pBase->flags & EP_FromJoin; |
| 2936 pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 2937 } |
| 2938 } |
| 2939 |
| 2940 /* |
| 2941 ** Mark term iChild as being a child of term iParent |
| 2942 */ |
| 2943 static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 2944 pWC->a[iChild].iParent = iParent; |
| 2945 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 2946 pWC->a[iParent].nChild++; |
| 2947 } |
| 2948 |
| 2949 /* |
| 2950 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not |
| 2951 ** a conjunction, then return just pTerm when N==0. If N is exceeds |
| 2952 ** the number of available subterms, return NULL. |
| 2953 */ |
| 2954 static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ |
| 2955 if( pTerm->eOperator!=WO_AND ){ |
| 2956 return N==0 ? pTerm : 0; |
| 2957 } |
| 2958 if( N<pTerm->u.pAndInfo->wc.nTerm ){ |
| 2959 return &pTerm->u.pAndInfo->wc.a[N]; |
| 2960 } |
| 2961 return 0; |
| 2962 } |
| 2963 |
| 2964 /* |
| 2965 ** Subterms pOne and pTwo are contained within WHERE clause pWC. The |
| 2966 ** two subterms are in disjunction - they are OR-ed together. |
| 2967 ** |
| 2968 ** If these two terms are both of the form: "A op B" with the same |
| 2969 ** A and B values but different operators and if the operators are |
| 2970 ** compatible (if one is = and the other is <, for example) then |
| 2971 ** add a new virtual AND term to pWC that is the combination of the |
| 2972 ** two. |
| 2973 ** |
| 2974 ** Some examples: |
| 2975 ** |
| 2976 ** x<y OR x=y --> x<=y |
| 2977 ** x=y OR x=y --> x=y |
| 2978 ** x<=y OR x<y --> x<=y |
| 2979 ** |
| 2980 ** The following is NOT generated: |
| 2981 ** |
| 2982 ** x<y OR x>y --> x!=y |
| 2983 */ |
| 2984 static void whereCombineDisjuncts( |
| 2985 SrcList *pSrc, /* the FROM clause */ |
| 2986 WhereClause *pWC, /* The complete WHERE clause */ |
| 2987 WhereTerm *pOne, /* First disjunct */ |
| 2988 WhereTerm *pTwo /* Second disjunct */ |
| 2989 ){ |
| 2990 u16 eOp = pOne->eOperator | pTwo->eOperator; |
| 2991 sqlite3 *db; /* Database connection (for malloc) */ |
| 2992 Expr *pNew; /* New virtual expression */ |
| 2993 int op; /* Operator for the combined expression */ |
| 2994 int idxNew; /* Index in pWC of the next virtual term */ |
| 2995 |
| 2996 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 2997 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 2998 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp |
| 2999 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; |
| 3000 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); |
| 3001 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); |
| 3002 if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; |
| 3003 if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return; |
| 3004 /* If we reach this point, it means the two subterms can be combined */ |
| 3005 if( (eOp & (eOp-1))!=0 ){ |
| 3006 if( eOp & (WO_LT|WO_LE) ){ |
| 3007 eOp = WO_LE; |
| 3008 }else{ |
| 3009 assert( eOp & (WO_GT|WO_GE) ); |
| 3010 eOp = WO_GE; |
| 3011 } |
| 3012 } |
| 3013 db = pWC->pWInfo->pParse->db; |
| 3014 pNew = sqlite3ExprDup(db, pOne->pExpr, 0); |
| 3015 if( pNew==0 ) return; |
| 3016 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); } |
| 3017 pNew->op = op; |
| 3018 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 3019 exprAnalyze(pSrc, pWC, idxNew); |
| 3020 } |
| 3021 |
| 3022 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 3023 /* |
| 3024 ** Analyze a term that consists of two or more OR-connected |
| 3025 ** subterms. So in: |
| 3026 ** |
| 3027 ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 3028 ** ^^^^^^^^^^^^^^^^^^^^ |
| 3029 ** |
| 3030 ** This routine analyzes terms such as the middle term in the above example. |
| 3031 ** A WhereOrTerm object is computed and attached to the term under |
| 3032 ** analysis, regardless of the outcome of the analysis. Hence: |
| 3033 ** |
| 3034 ** WhereTerm.wtFlags |= TERM_ORINFO |
| 3035 ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
| 3036 ** |
| 3037 ** The term being analyzed must have two or more of OR-connected subterms. |
| 3038 ** A single subterm might be a set of AND-connected sub-subterms. |
| 3039 ** Examples of terms under analysis: |
| 3040 ** |
| 3041 ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 3042 ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 3043 ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 3044 ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 3045 ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) |
| 3046 ** (F) x>A OR (x=A AND y>=B) |
| 3047 ** |
| 3048 ** CASE 1: |
| 3049 ** |
| 3050 ** If all subterms are of the form T.C=expr for some single column of C and |
| 3051 ** a single table T (as shown in example B above) then create a new virtual |
| 3052 ** term that is an equivalent IN expression. In other words, if the term |
| 3053 ** being analyzed is: |
| 3054 ** |
| 3055 ** x = expr1 OR expr2 = x OR x = expr3 |
| 3056 ** |
| 3057 ** then create a new virtual term like this: |
| 3058 ** |
| 3059 ** x IN (expr1,expr2,expr3) |
| 3060 ** |
| 3061 ** CASE 2: |
| 3062 ** |
| 3063 ** If there are exactly two disjuncts and one side has x>A and the other side |
| 3064 ** has x=A (for the same x and A) then add a new virtual conjunct term to the |
| 3065 ** WHERE clause of the form "x>=A". Example: |
| 3066 ** |
| 3067 ** x>A OR (x=A AND y>B) adds: x>=A |
| 3068 ** |
| 3069 ** The added conjunct can sometimes be helpful in query planning. |
| 3070 ** |
| 3071 ** CASE 3: |
| 3072 ** |
| 3073 ** If all subterms are indexable by a single table T, then set |
| 3074 ** |
| 3075 ** WhereTerm.eOperator = WO_OR |
| 3076 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 3077 ** |
| 3078 ** A subterm is "indexable" if it is of the form |
| 3079 ** "T.C <op> <expr>" where C is any column of table T and |
| 3080 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 3081 ** A subterm is also indexable if it is an AND of two or more |
| 3082 ** subsubterms at least one of which is indexable. Indexable AND |
| 3083 ** subterms have their eOperator set to WO_AND and they have |
| 3084 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 3085 ** |
| 3086 ** From another point of view, "indexable" means that the subterm could |
| 3087 ** potentially be used with an index if an appropriate index exists. |
| 3088 ** This analysis does not consider whether or not the index exists; that |
| 3089 ** is decided elsewhere. This analysis only looks at whether subterms |
| 3090 ** appropriate for indexing exist. |
| 3091 ** |
| 3092 ** All examples A through E above satisfy case 3. But if a term |
| 3093 ** also satisfies case 1 (such as B) we know that the optimizer will |
| 3094 ** always prefer case 1, so in that case we pretend that case 3 is not |
| 3095 ** satisfied. |
| 3096 ** |
| 3097 ** It might be the case that multiple tables are indexable. For example, |
| 3098 ** (E) above is indexable on tables P, Q, and R. |
| 3099 ** |
| 3100 ** Terms that satisfy case 3 are candidates for lookup by using |
| 3101 ** separate indices to find rowids for each subterm and composing |
| 3102 ** the union of all rowids using a RowSet object. This is similar |
| 3103 ** to "bitmap indices" in other database engines. |
| 3104 ** |
| 3105 ** OTHERWISE: |
| 3106 ** |
| 3107 ** If none of cases 1, 2, or 3 apply, then leave the eOperator set to |
| 3108 ** zero. This term is not useful for search. |
| 3109 */ |
| 3110 static void exprAnalyzeOrTerm( |
| 3111 SrcList *pSrc, /* the FROM clause */ |
| 3112 WhereClause *pWC, /* the complete WHERE clause */ |
| 3113 int idxTerm /* Index of the OR-term to be analyzed */ |
| 3114 ){ |
| 3115 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 3116 Parse *pParse = pWInfo->pParse; /* Parser context */ |
| 3117 sqlite3 *db = pParse->db; /* Database connection */ |
| 3118 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 3119 Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
| 3120 int i; /* Loop counters */ |
| 3121 WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 3122 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 3123 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 3124 Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 3125 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
| 3126 |
| 3127 /* |
| 3128 ** Break the OR clause into its separate subterms. The subterms are |
| 3129 ** stored in a WhereClause structure containing within the WhereOrInfo |
| 3130 ** object that is attached to the original OR clause term. |
| 3131 */ |
| 3132 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 3133 assert( pExpr->op==TK_OR ); |
| 3134 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
| 3135 if( pOrInfo==0 ) return; |
| 3136 pTerm->wtFlags |= TERM_ORINFO; |
| 3137 pOrWc = &pOrInfo->wc; |
| 3138 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic)); |
| 3139 sqlite3WhereClauseInit(pOrWc, pWInfo); |
| 3140 sqlite3WhereSplit(pOrWc, pExpr, TK_OR); |
| 3141 sqlite3WhereExprAnalyze(pSrc, pOrWc); |
| 3142 if( db->mallocFailed ) return; |
| 3143 assert( pOrWc->nTerm>=2 ); |
| 3144 |
| 3145 /* |
| 3146 ** Compute the set of tables that might satisfy cases 1 or 3. |
| 3147 */ |
| 3148 indexable = ~(Bitmask)0; |
| 3149 chngToIN = ~(Bitmask)0; |
| 3150 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 3151 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
| 3152 WhereAndInfo *pAndInfo; |
| 3153 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
| 3154 chngToIN = 0; |
| 3155 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo)); |
| 3156 if( pAndInfo ){ |
| 3157 WhereClause *pAndWC; |
| 3158 WhereTerm *pAndTerm; |
| 3159 int j; |
| 3160 Bitmask b = 0; |
| 3161 pOrTerm->u.pAndInfo = pAndInfo; |
| 3162 pOrTerm->wtFlags |= TERM_ANDINFO; |
| 3163 pOrTerm->eOperator = WO_AND; |
| 3164 pAndWC = &pAndInfo->wc; |
| 3165 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic)); |
| 3166 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo); |
| 3167 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 3168 sqlite3WhereExprAnalyze(pSrc, pAndWC); |
| 3169 pAndWC->pOuter = pWC; |
| 3170 if( !db->mallocFailed ){ |
| 3171 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 3172 assert( pAndTerm->pExpr ); |
| 3173 if( allowedOp(pAndTerm->pExpr->op) |
| 3174 || pAndTerm->eOperator==WO_MATCH |
| 3175 ){ |
| 3176 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
| 3177 } |
| 3178 } |
| 3179 } |
| 3180 indexable &= b; |
| 3181 } |
| 3182 }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 3183 /* Skip this term for now. We revisit it when we process the |
| 3184 ** corresponding TERM_VIRTUAL term */ |
| 3185 }else{ |
| 3186 Bitmask b; |
| 3187 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
| 3188 if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 3189 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
| 3190 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor); |
| 3191 } |
| 3192 indexable &= b; |
| 3193 if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
| 3194 chngToIN = 0; |
| 3195 }else{ |
| 3196 chngToIN &= b; |
| 3197 } |
| 3198 } |
| 3199 } |
| 3200 |
| 3201 /* |
| 3202 ** Record the set of tables that satisfy case 3. The set might be |
| 3203 ** empty. |
| 3204 */ |
| 3205 pOrInfo->indexable = indexable; |
| 3206 pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
| 3207 |
| 3208 /* For a two-way OR, attempt to implementation case 2. |
| 3209 */ |
| 3210 if( indexable && pOrWc->nTerm==2 ){ |
| 3211 int iOne = 0; |
| 3212 WhereTerm *pOne; |
| 3213 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ |
| 3214 int iTwo = 0; |
| 3215 WhereTerm *pTwo; |
| 3216 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ |
| 3217 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); |
| 3218 } |
| 3219 } |
| 3220 } |
| 3221 |
| 3222 /* |
| 3223 ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 3224 ** we have to do some additional checking to see if case 1 really |
| 3225 ** is satisfied. |
| 3226 ** |
| 3227 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 3228 ** that there is no possibility of transforming the OR clause into an |
| 3229 ** IN operator because one or more terms in the OR clause contain |
| 3230 ** something other than == on a column in the single table. The 1-bit |
| 3231 ** case means that every term of the OR clause is of the form |
| 3232 ** "table.column=expr" for some single table. The one bit that is set |
| 3233 ** will correspond to the common table. We still need to check to make |
| 3234 ** sure the same column is used on all terms. The 2-bit case is when |
| 3235 ** the all terms are of the form "table1.column=table2.column". It |
| 3236 ** might be possible to form an IN operator with either table1.column |
| 3237 ** or table2.column as the LHS if either is common to every term of |
| 3238 ** the OR clause. |
| 3239 ** |
| 3240 ** Note that terms of the form "table.column1=table.column2" (the |
| 3241 ** same table on both sizes of the ==) cannot be optimized. |
| 3242 */ |
| 3243 if( chngToIN ){ |
| 3244 int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 3245 int iColumn = -1; /* Column index on lhs of IN operator */ |
| 3246 int iCursor = -1; /* Table cursor common to all terms */ |
| 3247 int j = 0; /* Loop counter */ |
| 3248 |
| 3249 /* Search for a table and column that appears on one side or the |
| 3250 ** other of the == operator in every subterm. That table and column |
| 3251 ** will be recorded in iCursor and iColumn. There might not be any |
| 3252 ** such table and column. Set okToChngToIN if an appropriate table |
| 3253 ** and column is found but leave okToChngToIN false if not found. |
| 3254 */ |
| 3255 for(j=0; j<2 && !okToChngToIN; j++){ |
| 3256 pOrTerm = pOrWc->a; |
| 3257 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
| 3258 assert( pOrTerm->eOperator & WO_EQ ); |
| 3259 pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 3260 if( pOrTerm->leftCursor==iCursor ){ |
| 3261 /* This is the 2-bit case and we are on the second iteration and |
| 3262 ** current term is from the first iteration. So skip this term. */ |
| 3263 assert( j==1 ); |
| 3264 continue; |
| 3265 } |
| 3266 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet, |
| 3267 pOrTerm->leftCursor))==0 ){ |
| 3268 /* This term must be of the form t1.a==t2.b where t2 is in the |
| 3269 ** chngToIN set but t1 is not. This term will be either preceded |
| 3270 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 3271 ** and use its inversion. */ |
| 3272 testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 3273 testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 3274 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 3275 continue; |
| 3276 } |
| 3277 iColumn = pOrTerm->u.leftColumn; |
| 3278 iCursor = pOrTerm->leftCursor; |
| 3279 break; |
| 3280 } |
| 3281 if( i<0 ){ |
| 3282 /* No candidate table+column was found. This can only occur |
| 3283 ** on the second iteration */ |
| 3284 assert( j==1 ); |
| 3285 assert( IsPowerOfTwo(chngToIN) ); |
| 3286 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) ); |
| 3287 break; |
| 3288 } |
| 3289 testcase( j==1 ); |
| 3290 |
| 3291 /* We have found a candidate table and column. Check to see if that |
| 3292 ** table and column is common to every term in the OR clause */ |
| 3293 okToChngToIN = 1; |
| 3294 for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
| 3295 assert( pOrTerm->eOperator & WO_EQ ); |
| 3296 if( pOrTerm->leftCursor!=iCursor ){ |
| 3297 pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 3298 }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 3299 okToChngToIN = 0; |
| 3300 }else{ |
| 3301 int affLeft, affRight; |
| 3302 /* If the right-hand side is also a column, then the affinities |
| 3303 ** of both right and left sides must be such that no type |
| 3304 ** conversions are required on the right. (Ticket #2249) |
| 3305 */ |
| 3306 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 3307 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 3308 if( affRight!=0 && affRight!=affLeft ){ |
| 3309 okToChngToIN = 0; |
| 3310 }else{ |
| 3311 pOrTerm->wtFlags |= TERM_OR_OK; |
| 3312 } |
| 3313 } |
| 3314 } |
| 3315 } |
| 3316 |
| 3317 /* At this point, okToChngToIN is true if original pTerm satisfies |
| 3318 ** case 1. In that case, construct a new virtual term that is |
| 3319 ** pTerm converted into an IN operator. |
| 3320 */ |
| 3321 if( okToChngToIN ){ |
| 3322 Expr *pDup; /* A transient duplicate expression */ |
| 3323 ExprList *pList = 0; /* The RHS of the IN operator */ |
| 3324 Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 3325 Expr *pNew; /* The complete IN operator */ |
| 3326 |
| 3327 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 3328 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
| 3329 assert( pOrTerm->eOperator & WO_EQ ); |
| 3330 assert( pOrTerm->leftCursor==iCursor ); |
| 3331 assert( pOrTerm->u.leftColumn==iColumn ); |
| 3332 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
| 3333 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
| 3334 pLeft = pOrTerm->pExpr->pLeft; |
| 3335 } |
| 3336 assert( pLeft!=0 ); |
| 3337 pDup = sqlite3ExprDup(db, pLeft, 0); |
| 3338 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0); |
| 3339 if( pNew ){ |
| 3340 int idxNew; |
| 3341 transferJoinMarkings(pNew, pExpr); |
| 3342 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 3343 pNew->x.pList = pList; |
| 3344 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 3345 testcase( idxNew==0 ); |
| 3346 exprAnalyze(pSrc, pWC, idxNew); |
| 3347 pTerm = &pWC->a[idxTerm]; |
| 3348 markTermAsChild(pWC, idxNew, idxTerm); |
| 3349 }else{ |
| 3350 sqlite3ExprListDelete(db, pList); |
| 3351 } |
| 3352 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ |
| 3353 } |
| 3354 } |
| 3355 } |
| 3356 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
| 3357 |
| 3358 /* |
| 3359 ** We already know that pExpr is a binary operator where both operands are |
| 3360 ** column references. This routine checks to see if pExpr is an equivalence |
| 3361 ** relation: |
| 3362 ** 1. The SQLITE_Transitive optimization must be enabled |
| 3363 ** 2. Must be either an == or an IS operator |
| 3364 ** 3. Not originating in the ON clause of an OUTER JOIN |
| 3365 ** 4. The affinities of A and B must be compatible |
| 3366 ** 5a. Both operands use the same collating sequence OR |
| 3367 ** 5b. The overall collating sequence is BINARY |
| 3368 ** If this routine returns TRUE, that means that the RHS can be substituted |
| 3369 ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. |
| 3370 ** This is an optimization. No harm comes from returning 0. But if 1 is |
| 3371 ** returned when it should not be, then incorrect answers might result. |
| 3372 */ |
| 3373 static int termIsEquivalence(Parse *pParse, Expr *pExpr){ |
| 3374 char aff1, aff2; |
| 3375 CollSeq *pColl; |
| 3376 const char *zColl1, *zColl2; |
| 3377 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; |
| 3378 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; |
| 3379 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; |
| 3380 aff1 = sqlite3ExprAffinity(pExpr->pLeft); |
| 3381 aff2 = sqlite3ExprAffinity(pExpr->pRight); |
| 3382 if( aff1!=aff2 |
| 3383 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) |
| 3384 ){ |
| 3385 return 0; |
| 3386 } |
| 3387 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); |
| 3388 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1; |
| 3389 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
| 3390 zColl1 = pColl ? pColl->zName : 0; |
| 3391 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); |
| 3392 zColl2 = pColl ? pColl->zName : 0; |
| 3393 return sqlite3_stricmp(zColl1, zColl2)==0; |
| 3394 } |
| 3395 |
| 3396 /* |
| 3397 ** Recursively walk the expressions of a SELECT statement and generate |
| 3398 ** a bitmask indicating which tables are used in that expression |
| 3399 ** tree. |
| 3400 */ |
| 3401 static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){ |
| 3402 Bitmask mask = 0; |
| 3403 while( pS ){ |
| 3404 SrcList *pSrc = pS->pSrc; |
| 3405 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList); |
| 3406 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy); |
| 3407 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy); |
| 3408 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere); |
| 3409 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving); |
| 3410 if( ALWAYS(pSrc!=0) ){ |
| 3411 int i; |
| 3412 for(i=0; i<pSrc->nSrc; i++){ |
| 3413 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect); |
| 3414 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn); |
| 3415 } |
| 3416 } |
| 3417 pS = pS->pPrior; |
| 3418 } |
| 3419 return mask; |
| 3420 } |
| 3421 |
| 3422 /* |
| 3423 ** Expression pExpr is one operand of a comparison operator that might |
| 3424 ** be useful for indexing. This routine checks to see if pExpr appears |
| 3425 ** in any index. Return TRUE (1) if pExpr is an indexed term and return |
| 3426 ** FALSE (0) if not. If TRUE is returned, also set *piCur to the cursor |
| 3427 ** number of the table that is indexed and *piColumn to the column number |
| 3428 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being |
| 3429 ** indexed. |
| 3430 ** |
| 3431 ** If pExpr is a TK_COLUMN column reference, then this routine always returns |
| 3432 ** true even if that particular column is not indexed, because the column |
| 3433 ** might be added to an automatic index later. |
| 3434 */ |
| 3435 static int exprMightBeIndexed( |
| 3436 SrcList *pFrom, /* The FROM clause */ |
| 3437 int op, /* The specific comparison operator */ |
| 3438 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */ |
| 3439 Expr *pExpr, /* An operand of a comparison operator */ |
| 3440 int *piCur, /* Write the referenced table cursor number here */ |
| 3441 int *piColumn /* Write the referenced table column number here */ |
| 3442 ){ |
| 3443 Index *pIdx; |
| 3444 int i; |
| 3445 int iCur; |
| 3446 |
| 3447 /* If this expression is a vector to the left or right of a |
| 3448 ** inequality constraint (>, <, >= or <=), perform the processing |
| 3449 ** on the first element of the vector. */ |
| 3450 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE ); |
| 3451 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE ); |
| 3452 assert( op<=TK_GE ); |
| 3453 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){ |
| 3454 pExpr = pExpr->x.pList->a[0].pExpr; |
| 3455 } |
| 3456 |
| 3457 if( pExpr->op==TK_COLUMN ){ |
| 3458 *piCur = pExpr->iTable; |
| 3459 *piColumn = pExpr->iColumn; |
| 3460 return 1; |
| 3461 } |
| 3462 if( mPrereq==0 ) return 0; /* No table references */ |
| 3463 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */ |
| 3464 for(i=0; mPrereq>1; i++, mPrereq>>=1){} |
| 3465 iCur = pFrom->a[i].iCursor; |
| 3466 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 3467 if( pIdx->aColExpr==0 ) continue; |
| 3468 for(i=0; i<pIdx->nKeyCol; i++){ |
| 3469 if( pIdx->aiColumn[i]!=XN_EXPR ) continue; |
| 3470 if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){ |
| 3471 *piCur = iCur; |
| 3472 *piColumn = XN_EXPR; |
| 3473 return 1; |
| 3474 } |
| 3475 } |
| 3476 } |
| 3477 return 0; |
| 3478 } |
| 3479 |
| 3480 /* |
| 3481 ** The input to this routine is an WhereTerm structure with only the |
| 3482 ** "pExpr" field filled in. The job of this routine is to analyze the |
| 3483 ** subexpression and populate all the other fields of the WhereTerm |
| 3484 ** structure. |
| 3485 ** |
| 3486 ** If the expression is of the form "<expr> <op> X" it gets commuted |
| 3487 ** to the standard form of "X <op> <expr>". |
| 3488 ** |
| 3489 ** If the expression is of the form "X <op> Y" where both X and Y are |
| 3490 ** columns, then the original expression is unchanged and a new virtual |
| 3491 ** term of the form "Y <op> X" is added to the WHERE clause and |
| 3492 ** analyzed separately. The original term is marked with TERM_COPIED |
| 3493 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 3494 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 3495 ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 3496 ** and the copy has idxParent set to the index of the original term. |
| 3497 */ |
| 3498 static void exprAnalyze( |
| 3499 SrcList *pSrc, /* the FROM clause */ |
| 3500 WhereClause *pWC, /* the WHERE clause */ |
| 3501 int idxTerm /* Index of the term to be analyzed */ |
| 3502 ){ |
| 3503 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 3504 WhereTerm *pTerm; /* The term to be analyzed */ |
| 3505 WhereMaskSet *pMaskSet; /* Set of table index masks */ |
| 3506 Expr *pExpr; /* The expression to be analyzed */ |
| 3507 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 3508 Bitmask prereqAll; /* Prerequesites of pExpr */ |
| 3509 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
| 3510 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 3511 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
| 3512 int noCase = 0; /* uppercase equivalent to lowercase */ |
| 3513 int op; /* Top-level operator. pExpr->op */ |
| 3514 Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 3515 sqlite3 *db = pParse->db; /* Database connection */ |
| 3516 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */ |
| 3517 int nLeft; /* Number of elements on left side vector */ |
| 3518 |
| 3519 if( db->mallocFailed ){ |
| 3520 return; |
| 3521 } |
| 3522 pTerm = &pWC->a[idxTerm]; |
| 3523 pMaskSet = &pWInfo->sMaskSet; |
| 3524 pExpr = pTerm->pExpr; |
| 3525 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
| 3526 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft); |
| 3527 op = pExpr->op; |
| 3528 if( op==TK_IN ){ |
| 3529 assert( pExpr->pRight==0 ); |
| 3530 if( sqlite3ExprCheckIN(pParse, pExpr) ) return; |
| 3531 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 3532 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect); |
| 3533 }else{ |
| 3534 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList); |
| 3535 } |
| 3536 }else if( op==TK_ISNULL ){ |
| 3537 pTerm->prereqRight = 0; |
| 3538 }else{ |
| 3539 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight); |
| 3540 } |
| 3541 prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr); |
| 3542 if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 3543 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable); |
| 3544 prereqAll |= x; |
| 3545 extraRight = x-1; /* ON clause terms may not be used with an index |
| 3546 ** on left table of a LEFT JOIN. Ticket #3015 */ |
| 3547 if( (prereqAll>>1)>=x ){ |
| 3548 sqlite3ErrorMsg(pParse, "ON clause references tables to its right"); |
| 3549 return; |
| 3550 } |
| 3551 } |
| 3552 pTerm->prereqAll = prereqAll; |
| 3553 pTerm->leftCursor = -1; |
| 3554 pTerm->iParent = -1; |
| 3555 pTerm->eOperator = 0; |
| 3556 if( allowedOp(op) ){ |
| 3557 int iCur, iColumn; |
| 3558 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 3559 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
| 3560 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
| 3561 |
| 3562 if( pTerm->iField>0 ){ |
| 3563 assert( op==TK_IN ); |
| 3564 assert( pLeft->op==TK_VECTOR ); |
| 3565 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr; |
| 3566 } |
| 3567 |
| 3568 if( exprMightBeIndexed(pSrc, op, prereqLeft, pLeft, &iCur, &iColumn) ){ |
| 3569 pTerm->leftCursor = iCur; |
| 3570 pTerm->u.leftColumn = iColumn; |
| 3571 pTerm->eOperator = operatorMask(op) & opMask; |
| 3572 } |
| 3573 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; |
| 3574 if( pRight |
| 3575 && exprMightBeIndexed(pSrc, op, pTerm->prereqRight, pRight, &iCur,&iColumn) |
| 3576 ){ |
| 3577 WhereTerm *pNew; |
| 3578 Expr *pDup; |
| 3579 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
| 3580 assert( pTerm->iField==0 ); |
| 3581 if( pTerm->leftCursor>=0 ){ |
| 3582 int idxNew; |
| 3583 pDup = sqlite3ExprDup(db, pExpr, 0); |
| 3584 if( db->mallocFailed ){ |
| 3585 sqlite3ExprDelete(db, pDup); |
| 3586 return; |
| 3587 } |
| 3588 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 3589 if( idxNew==0 ) return; |
| 3590 pNew = &pWC->a[idxNew]; |
| 3591 markTermAsChild(pWC, idxNew, idxTerm); |
| 3592 if( op==TK_IS ) pNew->wtFlags |= TERM_IS; |
| 3593 pTerm = &pWC->a[idxTerm]; |
| 3594 pTerm->wtFlags |= TERM_COPIED; |
| 3595 |
| 3596 if( termIsEquivalence(pParse, pDup) ){ |
| 3597 pTerm->eOperator |= WO_EQUIV; |
| 3598 eExtraOp = WO_EQUIV; |
| 3599 } |
| 3600 }else{ |
| 3601 pDup = pExpr; |
| 3602 pNew = pTerm; |
| 3603 } |
| 3604 exprCommute(pParse, pDup); |
| 3605 pNew->leftCursor = iCur; |
| 3606 pNew->u.leftColumn = iColumn; |
| 3607 testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 3608 pNew->prereqRight = prereqLeft | extraRight; |
| 3609 pNew->prereqAll = prereqAll; |
| 3610 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
| 3611 } |
| 3612 } |
| 3613 |
| 3614 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
| 3615 /* If a term is the BETWEEN operator, create two new virtual terms |
| 3616 ** that define the range that the BETWEEN implements. For example: |
| 3617 ** |
| 3618 ** a BETWEEN b AND c |
| 3619 ** |
| 3620 ** is converted into: |
| 3621 ** |
| 3622 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 3623 ** |
| 3624 ** The two new terms are added onto the end of the WhereClause object. |
| 3625 ** The new terms are "dynamic" and are children of the original BETWEEN |
| 3626 ** term. That means that if the BETWEEN term is coded, the children are |
| 3627 ** skipped. Or, if the children are satisfied by an index, the original |
| 3628 ** BETWEEN term is skipped. |
| 3629 */ |
| 3630 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
| 3631 ExprList *pList = pExpr->x.pList; |
| 3632 int i; |
| 3633 static const u8 ops[] = {TK_GE, TK_LE}; |
| 3634 assert( pList!=0 ); |
| 3635 assert( pList->nExpr==2 ); |
| 3636 for(i=0; i<2; i++){ |
| 3637 Expr *pNewExpr; |
| 3638 int idxNew; |
| 3639 pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 3640 sqlite3ExprDup(db, pExpr->pLeft, 0), |
| 3641 sqlite3ExprDup(db, pList->a[i].pExpr, 0)); |
| 3642 transferJoinMarkings(pNewExpr, pExpr); |
| 3643 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 3644 testcase( idxNew==0 ); |
| 3645 exprAnalyze(pSrc, pWC, idxNew); |
| 3646 pTerm = &pWC->a[idxTerm]; |
| 3647 markTermAsChild(pWC, idxNew, idxTerm); |
| 3648 } |
| 3649 } |
| 3650 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
| 3651 |
| 3652 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 3653 /* Analyze a term that is composed of two or more subterms connected by |
| 3654 ** an OR operator. |
| 3655 */ |
| 3656 else if( pExpr->op==TK_OR ){ |
| 3657 assert( pWC->op==TK_AND ); |
| 3658 exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
| 3659 pTerm = &pWC->a[idxTerm]; |
| 3660 } |
| 3661 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 3662 |
| 3663 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 3664 /* Add constraints to reduce the search space on a LIKE or GLOB |
| 3665 ** operator. |
| 3666 ** |
| 3667 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints |
| 3668 ** |
| 3669 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' |
| 3670 ** |
| 3671 ** The last character of the prefix "abc" is incremented to form the |
| 3672 ** termination condition "abd". If case is not significant (the default |
| 3673 ** for LIKE) then the lower-bound is made all uppercase and the upper- |
| 3674 ** bound is made all lowercase so that the bounds also work when comparing |
| 3675 ** BLOBs. |
| 3676 */ |
| 3677 if( pWC->op==TK_AND |
| 3678 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 3679 ){ |
| 3680 Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 3681 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 3682 Expr *pNewExpr1; |
| 3683 Expr *pNewExpr2; |
| 3684 int idxNew1; |
| 3685 int idxNew2; |
| 3686 const char *zCollSeqName; /* Name of collating sequence */ |
| 3687 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; |
| 3688 |
| 3689 pLeft = pExpr->x.pList->a[1].pExpr; |
| 3690 pStr2 = sqlite3ExprDup(db, pStr1, 0); |
| 3691 |
| 3692 /* Convert the lower bound to upper-case and the upper bound to |
| 3693 ** lower-case (upper-case is less than lower-case in ASCII) so that |
| 3694 ** the range constraints also work for BLOBs |
| 3695 */ |
| 3696 if( noCase && !pParse->db->mallocFailed ){ |
| 3697 int i; |
| 3698 char c; |
| 3699 pTerm->wtFlags |= TERM_LIKE; |
| 3700 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ |
| 3701 pStr1->u.zToken[i] = sqlite3Toupper(c); |
| 3702 pStr2->u.zToken[i] = sqlite3Tolower(c); |
| 3703 } |
| 3704 } |
| 3705 |
| 3706 if( !db->mallocFailed ){ |
| 3707 u8 c, *pC; /* Last character before the first wildcard */ |
| 3708 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
| 3709 c = *pC; |
| 3710 if( noCase ){ |
| 3711 /* The point is to increment the last character before the first |
| 3712 ** wildcard. But if we increment '@', that will push it into the |
| 3713 ** alphabetic range where case conversions will mess up the |
| 3714 ** inequality. To avoid this, make sure to also run the full |
| 3715 ** LIKE on all candidate expressions by clearing the isComplete flag |
| 3716 */ |
| 3717 if( c=='A'-1 ) isComplete = 0; |
| 3718 c = sqlite3UpperToLower[c]; |
| 3719 } |
| 3720 *pC = c + 1; |
| 3721 } |
| 3722 zCollSeqName = noCase ? "NOCASE" : "BINARY"; |
| 3723 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
| 3724 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
| 3725 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), |
| 3726 pStr1); |
| 3727 transferJoinMarkings(pNewExpr1, pExpr); |
| 3728 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); |
| 3729 testcase( idxNew1==0 ); |
| 3730 exprAnalyze(pSrc, pWC, idxNew1); |
| 3731 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
| 3732 pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
| 3733 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), |
| 3734 pStr2); |
| 3735 transferJoinMarkings(pNewExpr2, pExpr); |
| 3736 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); |
| 3737 testcase( idxNew2==0 ); |
| 3738 exprAnalyze(pSrc, pWC, idxNew2); |
| 3739 pTerm = &pWC->a[idxTerm]; |
| 3740 if( isComplete ){ |
| 3741 markTermAsChild(pWC, idxNew1, idxTerm); |
| 3742 markTermAsChild(pWC, idxNew2, idxTerm); |
| 3743 } |
| 3744 } |
| 3745 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 3746 |
| 3747 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3748 /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 3749 ** current expression is of the form: column MATCH expr. |
| 3750 ** This information is used by the xBestIndex methods of |
| 3751 ** virtual tables. The native query optimizer does not attempt |
| 3752 ** to do anything with MATCH functions. |
| 3753 */ |
| 3754 if( pWC->op==TK_AND && isMatchOfColumn(pExpr, &eOp2) ){ |
| 3755 int idxNew; |
| 3756 Expr *pRight, *pLeft; |
| 3757 WhereTerm *pNewTerm; |
| 3758 Bitmask prereqColumn, prereqExpr; |
| 3759 |
| 3760 pRight = pExpr->x.pList->a[0].pExpr; |
| 3761 pLeft = pExpr->x.pList->a[1].pExpr; |
| 3762 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight); |
| 3763 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft); |
| 3764 if( (prereqExpr & prereqColumn)==0 ){ |
| 3765 Expr *pNewExpr; |
| 3766 pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 3767 0, sqlite3ExprDup(db, pRight, 0)); |
| 3768 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
| 3769 testcase( idxNew==0 ); |
| 3770 pNewTerm = &pWC->a[idxNew]; |
| 3771 pNewTerm->prereqRight = prereqExpr; |
| 3772 pNewTerm->leftCursor = pLeft->iTable; |
| 3773 pNewTerm->u.leftColumn = pLeft->iColumn; |
| 3774 pNewTerm->eOperator = WO_MATCH; |
| 3775 pNewTerm->eMatchOp = eOp2; |
| 3776 markTermAsChild(pWC, idxNew, idxTerm); |
| 3777 pTerm = &pWC->a[idxTerm]; |
| 3778 pTerm->wtFlags |= TERM_COPIED; |
| 3779 pNewTerm->prereqAll = pTerm->prereqAll; |
| 3780 } |
| 3781 } |
| 3782 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3783 |
| 3784 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create |
| 3785 ** new terms for each component comparison - "a = ?" and "b = ?". The |
| 3786 ** new terms completely replace the original vector comparison, which is |
| 3787 ** no longer used. |
| 3788 ** |
| 3789 ** This is only required if at least one side of the comparison operation |
| 3790 ** is not a sub-select. */ |
| 3791 if( pWC->op==TK_AND |
| 3792 && (pExpr->op==TK_EQ || pExpr->op==TK_IS) |
| 3793 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1 |
| 3794 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft |
| 3795 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 |
| 3796 || (pExpr->pRight->flags & EP_xIsSelect)==0) |
| 3797 ){ |
| 3798 int i; |
| 3799 for(i=0; i<nLeft; i++){ |
| 3800 int idxNew; |
| 3801 Expr *pNew; |
| 3802 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i); |
| 3803 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i); |
| 3804 |
| 3805 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight); |
| 3806 transferJoinMarkings(pNew, pExpr); |
| 3807 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC); |
| 3808 exprAnalyze(pSrc, pWC, idxNew); |
| 3809 } |
| 3810 pTerm = &pWC->a[idxTerm]; |
| 3811 pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */ |
| 3812 pTerm->eOperator = 0; |
| 3813 } |
| 3814 |
| 3815 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create |
| 3816 ** a virtual term for each vector component. The expression object |
| 3817 ** used by each such virtual term is pExpr (the full vector IN(...) |
| 3818 ** expression). The WhereTerm.iField variable identifies the index within |
| 3819 ** the vector on the LHS that the virtual term represents. |
| 3820 ** |
| 3821 ** This only works if the RHS is a simple SELECT, not a compound |
| 3822 */ |
| 3823 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0 |
| 3824 && pExpr->pLeft->op==TK_VECTOR |
| 3825 && pExpr->x.pSelect->pPrior==0 |
| 3826 ){ |
| 3827 int i; |
| 3828 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){ |
| 3829 int idxNew; |
| 3830 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL); |
| 3831 pWC->a[idxNew].iField = i+1; |
| 3832 exprAnalyze(pSrc, pWC, idxNew); |
| 3833 markTermAsChild(pWC, idxNew, idxTerm); |
| 3834 } |
| 3835 } |
| 3836 |
| 3837 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 3838 /* When sqlite_stat3 histogram data is available an operator of the |
| 3839 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 3840 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 3841 ** virtual term of that form. |
| 3842 ** |
| 3843 ** Note that the virtual term must be tagged with TERM_VNULL. |
| 3844 */ |
| 3845 if( pExpr->op==TK_NOTNULL |
| 3846 && pExpr->pLeft->op==TK_COLUMN |
| 3847 && pExpr->pLeft->iColumn>=0 |
| 3848 && OptimizationEnabled(db, SQLITE_Stat34) |
| 3849 ){ |
| 3850 Expr *pNewExpr; |
| 3851 Expr *pLeft = pExpr->pLeft; |
| 3852 int idxNew; |
| 3853 WhereTerm *pNewTerm; |
| 3854 |
| 3855 pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 3856 sqlite3ExprDup(db, pLeft, 0), |
| 3857 sqlite3ExprAlloc(db, TK_NULL, 0, 0)); |
| 3858 |
| 3859 idxNew = whereClauseInsert(pWC, pNewExpr, |
| 3860 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
| 3861 if( idxNew ){ |
| 3862 pNewTerm = &pWC->a[idxNew]; |
| 3863 pNewTerm->prereqRight = 0; |
| 3864 pNewTerm->leftCursor = pLeft->iTable; |
| 3865 pNewTerm->u.leftColumn = pLeft->iColumn; |
| 3866 pNewTerm->eOperator = WO_GT; |
| 3867 markTermAsChild(pWC, idxNew, idxTerm); |
| 3868 pTerm = &pWC->a[idxTerm]; |
| 3869 pTerm->wtFlags |= TERM_COPIED; |
| 3870 pNewTerm->prereqAll = pTerm->prereqAll; |
| 3871 } |
| 3872 } |
| 3873 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 3874 |
| 3875 /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 3876 ** an index for tables to the left of the join. |
| 3877 */ |
| 3878 testcase( pTerm!=&pWC->a[idxTerm] ); |
| 3879 pTerm = &pWC->a[idxTerm]; |
| 3880 pTerm->prereqRight |= extraRight; |
| 3881 } |
| 3882 |
| 3883 /*************************************************************************** |
| 3884 ** Routines with file scope above. Interface to the rest of the where.c |
| 3885 ** subsystem follows. |
| 3886 ***************************************************************************/ |
| 3887 |
| 3888 /* |
| 3889 ** This routine identifies subexpressions in the WHERE clause where |
| 3890 ** each subexpression is separated by the AND operator or some other |
| 3891 ** operator specified in the op parameter. The WhereClause structure |
| 3892 ** is filled with pointers to subexpressions. For example: |
| 3893 ** |
| 3894 ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 3895 ** \________/ \_______________/ \________________/ |
| 3896 ** slot[0] slot[1] slot[2] |
| 3897 ** |
| 3898 ** The original WHERE clause in pExpr is unaltered. All this routine |
| 3899 ** does is make slot[] entries point to substructure within pExpr. |
| 3900 ** |
| 3901 ** In the previous sentence and in the diagram, "slot[]" refers to |
| 3902 ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
| 3903 ** all terms of the WHERE clause. |
| 3904 */ |
| 3905 SQLITE_PRIVATE void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 3906 Expr *pE2 = sqlite3ExprSkipCollate(pExpr); |
| 3907 pWC->op = op; |
| 3908 if( pE2==0 ) return; |
| 3909 if( pE2->op!=op ){ |
| 3910 whereClauseInsert(pWC, pExpr, 0); |
| 3911 }else{ |
| 3912 sqlite3WhereSplit(pWC, pE2->pLeft, op); |
| 3913 sqlite3WhereSplit(pWC, pE2->pRight, op); |
| 3914 } |
| 3915 } |
| 3916 |
| 3917 /* |
| 3918 ** Initialize a preallocated WhereClause structure. |
| 3919 */ |
| 3920 SQLITE_PRIVATE void sqlite3WhereClauseInit( |
| 3921 WhereClause *pWC, /* The WhereClause to be initialized */ |
| 3922 WhereInfo *pWInfo /* The WHERE processing context */ |
| 3923 ){ |
| 3924 pWC->pWInfo = pWInfo; |
| 3925 pWC->pOuter = 0; |
| 3926 pWC->nTerm = 0; |
| 3927 pWC->nSlot = ArraySize(pWC->aStatic); |
| 3928 pWC->a = pWC->aStatic; |
| 3929 } |
| 3930 |
| 3931 /* |
| 3932 ** Deallocate a WhereClause structure. The WhereClause structure |
| 3933 ** itself is not freed. This routine is the inverse of |
| 3934 ** sqlite3WhereClauseInit(). |
| 3935 */ |
| 3936 SQLITE_PRIVATE void sqlite3WhereClauseClear(WhereClause *pWC){ |
| 3937 int i; |
| 3938 WhereTerm *a; |
| 3939 sqlite3 *db = pWC->pWInfo->pParse->db; |
| 3940 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
| 3941 if( a->wtFlags & TERM_DYNAMIC ){ |
| 3942 sqlite3ExprDelete(db, a->pExpr); |
| 3943 } |
| 3944 if( a->wtFlags & TERM_ORINFO ){ |
| 3945 whereOrInfoDelete(db, a->u.pOrInfo); |
| 3946 }else if( a->wtFlags & TERM_ANDINFO ){ |
| 3947 whereAndInfoDelete(db, a->u.pAndInfo); |
| 3948 } |
| 3949 } |
| 3950 if( pWC->a!=pWC->aStatic ){ |
| 3951 sqlite3DbFree(db, pWC->a); |
| 3952 } |
| 3953 } |
| 3954 |
| 3955 |
| 3956 /* |
| 3957 ** These routines walk (recursively) an expression tree and generate |
| 3958 ** a bitmask indicating which tables are used in that expression |
| 3959 ** tree. |
| 3960 */ |
| 3961 SQLITE_PRIVATE Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){ |
| 3962 Bitmask mask; |
| 3963 if( p==0 ) return 0; |
| 3964 if( p->op==TK_COLUMN ){ |
| 3965 mask = sqlite3WhereGetMask(pMaskSet, p->iTable); |
| 3966 return mask; |
| 3967 } |
| 3968 assert( !ExprHasProperty(p, EP_TokenOnly) ); |
| 3969 mask = p->pRight ? sqlite3WhereExprUsage(pMaskSet, p->pRight) : 0; |
| 3970 if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft); |
| 3971 if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 3972 mask |= exprSelectUsage(pMaskSet, p->x.pSelect); |
| 3973 }else if( p->x.pList ){ |
| 3974 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList); |
| 3975 } |
| 3976 return mask; |
| 3977 } |
| 3978 SQLITE_PRIVATE Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprLis
t *pList){ |
| 3979 int i; |
| 3980 Bitmask mask = 0; |
| 3981 if( pList ){ |
| 3982 for(i=0; i<pList->nExpr; i++){ |
| 3983 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr); |
| 3984 } |
| 3985 } |
| 3986 return mask; |
| 3987 } |
| 3988 |
| 3989 |
| 3990 /* |
| 3991 ** Call exprAnalyze on all terms in a WHERE clause. |
| 3992 ** |
| 3993 ** Note that exprAnalyze() might add new virtual terms onto the |
| 3994 ** end of the WHERE clause. We do not want to analyze these new |
| 3995 ** virtual terms, so start analyzing at the end and work forward |
| 3996 ** so that the added virtual terms are never processed. |
| 3997 */ |
| 3998 SQLITE_PRIVATE void sqlite3WhereExprAnalyze( |
| 3999 SrcList *pTabList, /* the FROM clause */ |
| 4000 WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 4001 ){ |
| 4002 int i; |
| 4003 for(i=pWC->nTerm-1; i>=0; i--){ |
| 4004 exprAnalyze(pTabList, pWC, i); |
| 4005 } |
| 4006 } |
| 4007 |
| 4008 /* |
| 4009 ** For table-valued-functions, transform the function arguments into |
| 4010 ** new WHERE clause terms. |
| 4011 ** |
| 4012 ** Each function argument translates into an equality constraint against |
| 4013 ** a HIDDEN column in the table. |
| 4014 */ |
| 4015 SQLITE_PRIVATE void sqlite3WhereTabFuncArgs( |
| 4016 Parse *pParse, /* Parsing context */ |
| 4017 struct SrcList_item *pItem, /* The FROM clause term to process */ |
| 4018 WhereClause *pWC /* Xfer function arguments to here */ |
| 4019 ){ |
| 4020 Table *pTab; |
| 4021 int j, k; |
| 4022 ExprList *pArgs; |
| 4023 Expr *pColRef; |
| 4024 Expr *pTerm; |
| 4025 if( pItem->fg.isTabFunc==0 ) return; |
| 4026 pTab = pItem->pTab; |
| 4027 assert( pTab!=0 ); |
| 4028 pArgs = pItem->u1.pFuncArg; |
| 4029 if( pArgs==0 ) return; |
| 4030 for(j=k=0; j<pArgs->nExpr; j++){ |
| 4031 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;} |
| 4032 if( k>=pTab->nCol ){ |
| 4033 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d", |
| 4034 pTab->zName, j); |
| 4035 return; |
| 4036 } |
| 4037 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0); |
| 4038 if( pColRef==0 ) return; |
| 4039 pColRef->iTable = pItem->iCursor; |
| 4040 pColRef->iColumn = k++; |
| 4041 pColRef->pTab = pTab; |
| 4042 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef, |
| 4043 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0)); |
| 4044 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC); |
| 4045 } |
| 4046 } |
| 4047 |
| 4048 /************** End of whereexpr.c *******************************************/ |
| 4049 /************** Begin file where.c *******************************************/ |
| 4050 /* |
| 4051 ** 2001 September 15 |
| 4052 ** |
| 4053 ** The author disclaims copyright to this source code. In place of |
| 4054 ** a legal notice, here is a blessing: |
| 4055 ** |
| 4056 ** May you do good and not evil. |
| 4057 ** May you find forgiveness for yourself and forgive others. |
| 4058 ** May you share freely, never taking more than you give. |
| 4059 ** |
| 4060 ************************************************************************* |
| 4061 ** This module contains C code that generates VDBE code used to process |
| 4062 ** the WHERE clause of SQL statements. This module is responsible for |
| 4063 ** generating the code that loops through a table looking for applicable |
| 4064 ** rows. Indices are selected and used to speed the search when doing |
| 4065 ** so is applicable. Because this module is responsible for selecting |
| 4066 ** indices, you might also think of this module as the "query optimizer". |
| 4067 */ |
| 4068 /* #include "sqliteInt.h" */ |
| 4069 /* #include "whereInt.h" */ |
| 4070 |
| 4071 /* Forward declaration of methods */ |
| 4072 static int whereLoopResize(sqlite3*, WhereLoop*, int); |
| 4073 |
| 4074 /* Test variable that can be set to enable WHERE tracing */ |
| 4075 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 4076 /***/ int sqlite3WhereTrace = 0; |
| 4077 #endif |
| 4078 |
| 4079 |
| 4080 /* |
| 4081 ** Return the estimated number of output rows from a WHERE clause |
| 4082 */ |
| 4083 SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
| 4084 return pWInfo->nRowOut; |
| 4085 } |
| 4086 |
| 4087 /* |
| 4088 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 4089 ** WHERE clause returns outputs for DISTINCT processing. |
| 4090 */ |
| 4091 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 4092 return pWInfo->eDistinct; |
| 4093 } |
| 4094 |
| 4095 /* |
| 4096 ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 4097 ** Return FALSE if the output needs to be sorted. |
| 4098 */ |
| 4099 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
| 4100 return pWInfo->nOBSat; |
| 4101 } |
| 4102 |
| 4103 /* |
| 4104 ** Return TRUE if the innermost loop of the WHERE clause implementation |
| 4105 ** returns rows in ORDER BY order for complete run of the inner loop. |
| 4106 ** |
| 4107 ** Across multiple iterations of outer loops, the output rows need not be |
| 4108 ** sorted. As long as rows are sorted for just the innermost loop, this |
| 4109 ** routine can return TRUE. |
| 4110 */ |
| 4111 SQLITE_PRIVATE int sqlite3WhereOrderedInnerLoop(WhereInfo *pWInfo){ |
| 4112 return pWInfo->bOrderedInnerLoop; |
| 4113 } |
| 4114 |
| 4115 /* |
| 4116 ** Return the VDBE address or label to jump to in order to continue |
| 4117 ** immediately with the next row of a WHERE clause. |
| 4118 */ |
| 4119 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
| 4120 assert( pWInfo->iContinue!=0 ); |
| 4121 return pWInfo->iContinue; |
| 4122 } |
| 4123 |
| 4124 /* |
| 4125 ** Return the VDBE address or label to jump to in order to break |
| 4126 ** out of a WHERE loop. |
| 4127 */ |
| 4128 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 4129 return pWInfo->iBreak; |
| 4130 } |
| 4131 |
| 4132 /* |
| 4133 ** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to |
| 4134 ** operate directly on the rowis returned by a WHERE clause. Return |
| 4135 ** ONEPASS_SINGLE (1) if the statement can operation directly because only |
| 4136 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass |
| 4137 ** optimization can be used on multiple |
| 4138 ** |
| 4139 ** If the ONEPASS optimization is used (if this routine returns true) |
| 4140 ** then also write the indices of open cursors used by ONEPASS |
| 4141 ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 4142 ** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 4143 ** Either value may be -1, indicating that cursor is not used. |
| 4144 ** Any cursors returned will have been opened for writing. |
| 4145 ** |
| 4146 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 4147 ** unable to use the ONEPASS optimization. |
| 4148 */ |
| 4149 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 4150 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
| 4151 #ifdef WHERETRACE_ENABLED |
| 4152 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){ |
| 4153 sqlite3DebugPrintf("%s cursors: %d %d\n", |
| 4154 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI", |
| 4155 aiCur[0], aiCur[1]); |
| 4156 } |
| 4157 #endif |
| 4158 return pWInfo->eOnePass; |
| 4159 } |
| 4160 |
| 4161 /* |
| 4162 ** Move the content of pSrc into pDest |
| 4163 */ |
| 4164 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 4165 pDest->n = pSrc->n; |
| 4166 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 4167 } |
| 4168 |
| 4169 /* |
| 4170 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 4171 ** |
| 4172 ** The new entry might overwrite an existing entry, or it might be |
| 4173 ** appended, or it might be discarded. Do whatever is the right thing |
| 4174 ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 4175 */ |
| 4176 static int whereOrInsert( |
| 4177 WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 4178 Bitmask prereq, /* Prerequisites of the new entry */ |
| 4179 LogEst rRun, /* Run-cost of the new entry */ |
| 4180 LogEst nOut /* Number of outputs for the new entry */ |
| 4181 ){ |
| 4182 u16 i; |
| 4183 WhereOrCost *p; |
| 4184 for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 4185 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 4186 goto whereOrInsert_done; |
| 4187 } |
| 4188 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 4189 return 0; |
| 4190 } |
| 4191 } |
| 4192 if( pSet->n<N_OR_COST ){ |
| 4193 p = &pSet->a[pSet->n++]; |
| 4194 p->nOut = nOut; |
| 4195 }else{ |
| 4196 p = pSet->a; |
| 4197 for(i=1; i<pSet->n; i++){ |
| 4198 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 4199 } |
| 4200 if( p->rRun<=rRun ) return 0; |
| 4201 } |
| 4202 whereOrInsert_done: |
| 4203 p->prereq = prereq; |
| 4204 p->rRun = rRun; |
| 4205 if( p->nOut>nOut ) p->nOut = nOut; |
| 4206 return 1; |
| 4207 } |
| 4208 |
| 4209 /* |
| 4210 ** Return the bitmask for the given cursor number. Return 0 if |
| 4211 ** iCursor is not in the set. |
| 4212 */ |
| 4213 SQLITE_PRIVATE Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 4214 int i; |
| 4215 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
| 4216 for(i=0; i<pMaskSet->n; i++){ |
| 4217 if( pMaskSet->ix[i]==iCursor ){ |
| 4218 return MASKBIT(i); |
| 4219 } |
| 4220 } |
| 4221 return 0; |
| 4222 } |
| 4223 |
| 4224 /* |
| 4225 ** Create a new mask for cursor iCursor. |
| 4226 ** |
| 4227 ** There is one cursor per table in the FROM clause. The number of |
| 4228 ** tables in the FROM clause is limited by a test early in the |
| 4229 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
| 4230 ** array will never overflow. |
| 4231 */ |
| 4232 static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
| 4233 assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
| 4234 pMaskSet->ix[pMaskSet->n++] = iCursor; |
| 4235 } |
| 4236 |
| 4237 /* |
| 4238 ** Advance to the next WhereTerm that matches according to the criteria |
| 4239 ** established when the pScan object was initialized by whereScanInit(). |
| 4240 ** Return NULL if there are no more matching WhereTerms. |
| 4241 */ |
| 4242 static WhereTerm *whereScanNext(WhereScan *pScan){ |
| 4243 int iCur; /* The cursor on the LHS of the term */ |
| 4244 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 4245 Expr *pX; /* An expression being tested */ |
| 4246 WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 4247 WhereTerm *pTerm; /* The term being tested */ |
| 4248 int k = pScan->k; /* Where to start scanning */ |
| 4249 |
| 4250 assert( pScan->iEquiv<=pScan->nEquiv ); |
| 4251 pWC = pScan->pWC; |
| 4252 while(1){ |
| 4253 iColumn = pScan->aiColumn[pScan->iEquiv-1]; |
| 4254 iCur = pScan->aiCur[pScan->iEquiv-1]; |
| 4255 assert( pWC!=0 ); |
| 4256 do{ |
| 4257 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
| 4258 if( pTerm->leftCursor==iCur |
| 4259 && pTerm->u.leftColumn==iColumn |
| 4260 && (iColumn!=XN_EXPR |
| 4261 || sqlite3ExprCompare(pTerm->pExpr->pLeft,pScan->pIdxExpr,iCur)==0) |
| 4262 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 4263 ){ |
| 4264 if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 4265 && pScan->nEquiv<ArraySize(pScan->aiCur) |
| 4266 && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN |
| 4267 ){ |
| 4268 int j; |
| 4269 for(j=0; j<pScan->nEquiv; j++){ |
| 4270 if( pScan->aiCur[j]==pX->iTable |
| 4271 && pScan->aiColumn[j]==pX->iColumn ){ |
| 4272 break; |
| 4273 } |
| 4274 } |
| 4275 if( j==pScan->nEquiv ){ |
| 4276 pScan->aiCur[j] = pX->iTable; |
| 4277 pScan->aiColumn[j] = pX->iColumn; |
| 4278 pScan->nEquiv++; |
| 4279 } |
| 4280 } |
| 4281 if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 4282 /* Verify the affinity and collating sequence match */ |
| 4283 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 4284 CollSeq *pColl; |
| 4285 Parse *pParse = pWC->pWInfo->pParse; |
| 4286 pX = pTerm->pExpr; |
| 4287 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 4288 continue; |
| 4289 } |
| 4290 assert(pX->pLeft); |
| 4291 pColl = sqlite3BinaryCompareCollSeq(pParse, |
| 4292 pX->pLeft, pX->pRight); |
| 4293 if( pColl==0 ) pColl = pParse->db->pDfltColl; |
| 4294 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 4295 continue; |
| 4296 } |
| 4297 } |
| 4298 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
| 4299 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 4300 && pX->iTable==pScan->aiCur[0] |
| 4301 && pX->iColumn==pScan->aiColumn[0] |
| 4302 ){ |
| 4303 testcase( pTerm->eOperator & WO_IS ); |
| 4304 continue; |
| 4305 } |
| 4306 pScan->pWC = pWC; |
| 4307 pScan->k = k+1; |
| 4308 return pTerm; |
| 4309 } |
| 4310 } |
| 4311 } |
| 4312 pWC = pWC->pOuter; |
| 4313 k = 0; |
| 4314 }while( pWC!=0 ); |
| 4315 if( pScan->iEquiv>=pScan->nEquiv ) break; |
| 4316 pWC = pScan->pOrigWC; |
| 4317 k = 0; |
| 4318 pScan->iEquiv++; |
| 4319 } |
| 4320 return 0; |
| 4321 } |
| 4322 |
| 4323 /* |
| 4324 ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 4325 ** first match. Return NULL if there are no matches. |
| 4326 ** |
| 4327 ** The scanner will be searching the WHERE clause pWC. It will look |
| 4328 ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 4329 ** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx |
| 4330 ** must be one of the indexes of table iCur. |
| 4331 ** |
| 4332 ** The <op> must be one of the operators described by opMask. |
| 4333 ** |
| 4334 ** If the search is for X and the WHERE clause contains terms of the |
| 4335 ** form X=Y then this routine might also return terms of the form |
| 4336 ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 4337 ** but is enough to handle most commonly occurring SQL statements. |
| 4338 ** |
| 4339 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 4340 ** index pIdx. |
| 4341 */ |
| 4342 static WhereTerm *whereScanInit( |
| 4343 WhereScan *pScan, /* The WhereScan object being initialized */ |
| 4344 WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 4345 int iCur, /* Cursor to scan for */ |
| 4346 int iColumn, /* Column to scan for */ |
| 4347 u32 opMask, /* Operator(s) to scan for */ |
| 4348 Index *pIdx /* Must be compatible with this index */ |
| 4349 ){ |
| 4350 pScan->pOrigWC = pWC; |
| 4351 pScan->pWC = pWC; |
| 4352 pScan->pIdxExpr = 0; |
| 4353 pScan->idxaff = 0; |
| 4354 pScan->zCollName = 0; |
| 4355 if( pIdx ){ |
| 4356 int j = iColumn; |
| 4357 iColumn = pIdx->aiColumn[j]; |
| 4358 if( iColumn==XN_EXPR ){ |
| 4359 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; |
| 4360 pScan->zCollName = pIdx->azColl[j]; |
| 4361 }else if( iColumn==pIdx->pTable->iPKey ){ |
| 4362 iColumn = XN_ROWID; |
| 4363 }else if( iColumn>=0 ){ |
| 4364 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 4365 pScan->zCollName = pIdx->azColl[j]; |
| 4366 } |
| 4367 }else if( iColumn==XN_EXPR ){ |
| 4368 return 0; |
| 4369 } |
| 4370 pScan->opMask = opMask; |
| 4371 pScan->k = 0; |
| 4372 pScan->aiCur[0] = iCur; |
| 4373 pScan->aiColumn[0] = iColumn; |
| 4374 pScan->nEquiv = 1; |
| 4375 pScan->iEquiv = 1; |
| 4376 return whereScanNext(pScan); |
| 4377 } |
| 4378 |
| 4379 /* |
| 4380 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 4381 ** where X is a reference to the iColumn of table iCur or of index pIdx |
| 4382 ** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by |
| 4383 ** the op parameter. Return a pointer to the term. Return 0 if not found. |
| 4384 ** |
| 4385 ** If pIdx!=0 then it must be one of the indexes of table iCur. |
| 4386 ** Search for terms matching the iColumn-th column of pIdx |
| 4387 ** rather than the iColumn-th column of table iCur. |
| 4388 ** |
| 4389 ** The term returned might by Y=<expr> if there is another constraint in |
| 4390 ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 4391 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 4392 ** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11 |
| 4393 ** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10 |
| 4394 ** other equivalent values. Hence a search for X will return <expr> if X=A1 |
| 4395 ** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>. |
| 4396 ** |
| 4397 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 4398 ** then try for the one with no dependencies on <expr> - in other words where |
| 4399 ** <expr> is a constant expression of some kind. Only return entries of |
| 4400 ** the form "X <op> Y" where Y is a column in another table if no terms of |
| 4401 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 4402 ** exist, try to return a term that does not use WO_EQUIV. |
| 4403 */ |
| 4404 SQLITE_PRIVATE WhereTerm *sqlite3WhereFindTerm( |
| 4405 WhereClause *pWC, /* The WHERE clause to be searched */ |
| 4406 int iCur, /* Cursor number of LHS */ |
| 4407 int iColumn, /* Column number of LHS */ |
| 4408 Bitmask notReady, /* RHS must not overlap with this mask */ |
| 4409 u32 op, /* Mask of WO_xx values describing operator */ |
| 4410 Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 4411 ){ |
| 4412 WhereTerm *pResult = 0; |
| 4413 WhereTerm *p; |
| 4414 WhereScan scan; |
| 4415 |
| 4416 p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
| 4417 op &= WO_EQ|WO_IS; |
| 4418 while( p ){ |
| 4419 if( (p->prereqRight & notReady)==0 ){ |
| 4420 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ |
| 4421 testcase( p->eOperator & WO_IS ); |
| 4422 return p; |
| 4423 } |
| 4424 if( pResult==0 ) pResult = p; |
| 4425 } |
| 4426 p = whereScanNext(&scan); |
| 4427 } |
| 4428 return pResult; |
| 4429 } |
| 4430 |
| 4431 /* |
| 4432 ** This function searches pList for an entry that matches the iCol-th column |
| 4433 ** of index pIdx. |
| 4434 ** |
| 4435 ** If such an expression is found, its index in pList->a[] is returned. If |
| 4436 ** no expression is found, -1 is returned. |
| 4437 */ |
| 4438 static int findIndexCol( |
| 4439 Parse *pParse, /* Parse context */ |
| 4440 ExprList *pList, /* Expression list to search */ |
| 4441 int iBase, /* Cursor for table associated with pIdx */ |
| 4442 Index *pIdx, /* Index to match column of */ |
| 4443 int iCol /* Column of index to match */ |
| 4444 ){ |
| 4445 int i; |
| 4446 const char *zColl = pIdx->azColl[iCol]; |
| 4447 |
| 4448 for(i=0; i<pList->nExpr; i++){ |
| 4449 Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
| 4450 if( p->op==TK_COLUMN |
| 4451 && p->iColumn==pIdx->aiColumn[iCol] |
| 4452 && p->iTable==iBase |
| 4453 ){ |
| 4454 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
| 4455 if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
| 4456 return i; |
| 4457 } |
| 4458 } |
| 4459 } |
| 4460 |
| 4461 return -1; |
| 4462 } |
| 4463 |
| 4464 /* |
| 4465 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL |
| 4466 */ |
| 4467 static int indexColumnNotNull(Index *pIdx, int iCol){ |
| 4468 int j; |
| 4469 assert( pIdx!=0 ); |
| 4470 assert( iCol>=0 && iCol<pIdx->nColumn ); |
| 4471 j = pIdx->aiColumn[iCol]; |
| 4472 if( j>=0 ){ |
| 4473 return pIdx->pTable->aCol[j].notNull; |
| 4474 }else if( j==(-1) ){ |
| 4475 return 1; |
| 4476 }else{ |
| 4477 assert( j==(-2) ); |
| 4478 return 0; /* Assume an indexed expression can always yield a NULL */ |
| 4479 |
| 4480 } |
| 4481 } |
| 4482 |
| 4483 /* |
| 4484 ** Return true if the DISTINCT expression-list passed as the third argument |
| 4485 ** is redundant. |
| 4486 ** |
| 4487 ** A DISTINCT list is redundant if any subset of the columns in the |
| 4488 ** DISTINCT list are collectively unique and individually non-null. |
| 4489 */ |
| 4490 static int isDistinctRedundant( |
| 4491 Parse *pParse, /* Parsing context */ |
| 4492 SrcList *pTabList, /* The FROM clause */ |
| 4493 WhereClause *pWC, /* The WHERE clause */ |
| 4494 ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
| 4495 ){ |
| 4496 Table *pTab; |
| 4497 Index *pIdx; |
| 4498 int i; |
| 4499 int iBase; |
| 4500 |
| 4501 /* If there is more than one table or sub-select in the FROM clause of |
| 4502 ** this query, then it will not be possible to show that the DISTINCT |
| 4503 ** clause is redundant. */ |
| 4504 if( pTabList->nSrc!=1 ) return 0; |
| 4505 iBase = pTabList->a[0].iCursor; |
| 4506 pTab = pTabList->a[0].pTab; |
| 4507 |
| 4508 /* If any of the expressions is an IPK column on table iBase, then return |
| 4509 ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 4510 ** current SELECT is a correlated sub-query. |
| 4511 */ |
| 4512 for(i=0; i<pDistinct->nExpr; i++){ |
| 4513 Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
| 4514 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
| 4515 } |
| 4516 |
| 4517 /* Loop through all indices on the table, checking each to see if it makes |
| 4518 ** the DISTINCT qualifier redundant. It does so if: |
| 4519 ** |
| 4520 ** 1. The index is itself UNIQUE, and |
| 4521 ** |
| 4522 ** 2. All of the columns in the index are either part of the pDistinct |
| 4523 ** list, or else the WHERE clause contains a term of the form "col=X", |
| 4524 ** where X is a constant value. The collation sequences of the |
| 4525 ** comparison and select-list expressions must match those of the index. |
| 4526 ** |
| 4527 ** 3. All of those index columns for which the WHERE clause does not |
| 4528 ** contain a "col=X" term are subject to a NOT NULL constraint. |
| 4529 */ |
| 4530 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 4531 if( !IsUniqueIndex(pIdx) ) continue; |
| 4532 for(i=0; i<pIdx->nKeyCol; i++){ |
| 4533 if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 4534 if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break; |
| 4535 if( indexColumnNotNull(pIdx, i)==0 ) break; |
| 4536 } |
| 4537 } |
| 4538 if( i==pIdx->nKeyCol ){ |
| 4539 /* This index implies that the DISTINCT qualifier is redundant. */ |
| 4540 return 1; |
| 4541 } |
| 4542 } |
| 4543 |
| 4544 return 0; |
| 4545 } |
| 4546 |
| 4547 |
| 4548 /* |
| 4549 ** Estimate the logarithm of the input value to base 2. |
| 4550 */ |
| 4551 static LogEst estLog(LogEst N){ |
| 4552 return N<=10 ? 0 : sqlite3LogEst(N) - 33; |
| 4553 } |
| 4554 |
| 4555 /* |
| 4556 ** Convert OP_Column opcodes to OP_Copy in previously generated code. |
| 4557 ** |
| 4558 ** This routine runs over generated VDBE code and translates OP_Column |
| 4559 ** opcodes into OP_Copy when the table is being accessed via co-routine |
| 4560 ** instead of via table lookup. |
| 4561 ** |
| 4562 ** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on |
| 4563 ** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero, |
| 4564 ** then each OP_Rowid is transformed into an instruction to increment the |
| 4565 ** value stored in its output register. |
| 4566 */ |
| 4567 static void translateColumnToCopy( |
| 4568 Vdbe *v, /* The VDBE containing code to translate */ |
| 4569 int iStart, /* Translate from this opcode to the end */ |
| 4570 int iTabCur, /* OP_Column/OP_Rowid references to this table */ |
| 4571 int iRegister, /* The first column is in this register */ |
| 4572 int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */ |
| 4573 ){ |
| 4574 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart); |
| 4575 int iEnd = sqlite3VdbeCurrentAddr(v); |
| 4576 for(; iStart<iEnd; iStart++, pOp++){ |
| 4577 if( pOp->p1!=iTabCur ) continue; |
| 4578 if( pOp->opcode==OP_Column ){ |
| 4579 pOp->opcode = OP_Copy; |
| 4580 pOp->p1 = pOp->p2 + iRegister; |
| 4581 pOp->p2 = pOp->p3; |
| 4582 pOp->p3 = 0; |
| 4583 }else if( pOp->opcode==OP_Rowid ){ |
| 4584 if( bIncrRowid ){ |
| 4585 /* Increment the value stored in the P2 operand of the OP_Rowid. */ |
| 4586 pOp->opcode = OP_AddImm; |
| 4587 pOp->p1 = pOp->p2; |
| 4588 pOp->p2 = 1; |
| 4589 }else{ |
| 4590 pOp->opcode = OP_Null; |
| 4591 pOp->p1 = 0; |
| 4592 pOp->p3 = 0; |
| 4593 } |
| 4594 } |
| 4595 } |
| 4596 } |
| 4597 |
| 4598 /* |
| 4599 ** Two routines for printing the content of an sqlite3_index_info |
| 4600 ** structure. Used for testing and debugging only. If neither |
| 4601 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 4602 ** are no-ops. |
| 4603 */ |
| 4604 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
| 4605 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 4606 int i; |
| 4607 if( !sqlite3WhereTrace ) return; |
| 4608 for(i=0; i<p->nConstraint; i++){ |
| 4609 sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 4610 i, |
| 4611 p->aConstraint[i].iColumn, |
| 4612 p->aConstraint[i].iTermOffset, |
| 4613 p->aConstraint[i].op, |
| 4614 p->aConstraint[i].usable); |
| 4615 } |
| 4616 for(i=0; i<p->nOrderBy; i++){ |
| 4617 sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 4618 i, |
| 4619 p->aOrderBy[i].iColumn, |
| 4620 p->aOrderBy[i].desc); |
| 4621 } |
| 4622 } |
| 4623 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 4624 int i; |
| 4625 if( !sqlite3WhereTrace ) return; |
| 4626 for(i=0; i<p->nConstraint; i++){ |
| 4627 sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 4628 i, |
| 4629 p->aConstraintUsage[i].argvIndex, |
| 4630 p->aConstraintUsage[i].omit); |
| 4631 } |
| 4632 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 4633 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 4634 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 4635 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
| 4636 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); |
| 4637 } |
| 4638 #else |
| 4639 #define TRACE_IDX_INPUTS(A) |
| 4640 #define TRACE_IDX_OUTPUTS(A) |
| 4641 #endif |
| 4642 |
| 4643 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 4644 /* |
| 4645 ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 4646 ** could be used with an index to access pSrc, assuming an appropriate |
| 4647 ** index existed. |
| 4648 */ |
| 4649 static int termCanDriveIndex( |
| 4650 WhereTerm *pTerm, /* WHERE clause term to check */ |
| 4651 struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 4652 Bitmask notReady /* Tables in outer loops of the join */ |
| 4653 ){ |
| 4654 char aff; |
| 4655 if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
| 4656 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; |
| 4657 if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
| 4658 if( pTerm->u.leftColumn<0 ) return 0; |
| 4659 aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 4660 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
| 4661 testcase( pTerm->pExpr->op==TK_IS ); |
| 4662 return 1; |
| 4663 } |
| 4664 #endif |
| 4665 |
| 4666 |
| 4667 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 4668 /* |
| 4669 ** Generate code to construct the Index object for an automatic index |
| 4670 ** and to set up the WhereLevel object pLevel so that the code generator |
| 4671 ** makes use of the automatic index. |
| 4672 */ |
| 4673 static void constructAutomaticIndex( |
| 4674 Parse *pParse, /* The parsing context */ |
| 4675 WhereClause *pWC, /* The WHERE clause */ |
| 4676 struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 4677 Bitmask notReady, /* Mask of cursors that are not available */ |
| 4678 WhereLevel *pLevel /* Write new index here */ |
| 4679 ){ |
| 4680 int nKeyCol; /* Number of columns in the constructed index */ |
| 4681 WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 4682 WhereTerm *pWCEnd; /* End of pWC->a[] */ |
| 4683 Index *pIdx; /* Object describing the transient index */ |
| 4684 Vdbe *v; /* Prepared statement under construction */ |
| 4685 int addrInit; /* Address of the initialization bypass jump */ |
| 4686 Table *pTable; /* The table being indexed */ |
| 4687 int addrTop; /* Top of the index fill loop */ |
| 4688 int regRecord; /* Register holding an index record */ |
| 4689 int n; /* Column counter */ |
| 4690 int i; /* Loop counter */ |
| 4691 int mxBitCol; /* Maximum column in pSrc->colUsed */ |
| 4692 CollSeq *pColl; /* Collating sequence to on a column */ |
| 4693 WhereLoop *pLoop; /* The Loop object */ |
| 4694 char *zNotUsed; /* Extra space on the end of pIdx */ |
| 4695 Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 4696 Bitmask extraCols; /* Bitmap of additional columns */ |
| 4697 u8 sentWarning = 0; /* True if a warnning has been issued */ |
| 4698 Expr *pPartial = 0; /* Partial Index Expression */ |
| 4699 int iContinue = 0; /* Jump here to skip excluded rows */ |
| 4700 struct SrcList_item *pTabItem; /* FROM clause term being indexed */ |
| 4701 int addrCounter = 0; /* Address where integer counter is initialized */ |
| 4702 int regBase; /* Array of registers where record is assembled */ |
| 4703 |
| 4704 /* Generate code to skip over the creation and initialization of the |
| 4705 ** transient index on 2nd and subsequent iterations of the loop. */ |
| 4706 v = pParse->pVdbe; |
| 4707 assert( v!=0 ); |
| 4708 addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
| 4709 |
| 4710 /* Count the number of columns that will be added to the index |
| 4711 ** and used to match WHERE clause constraints */ |
| 4712 nKeyCol = 0; |
| 4713 pTable = pSrc->pTab; |
| 4714 pWCEnd = &pWC->a[pWC->nTerm]; |
| 4715 pLoop = pLevel->pWLoop; |
| 4716 idxCols = 0; |
| 4717 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 4718 Expr *pExpr = pTerm->pExpr; |
| 4719 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */ |
| 4720 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */ |
| 4721 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ |
| 4722 if( pLoop->prereq==0 |
| 4723 && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
| 4724 && !ExprHasProperty(pExpr, EP_FromJoin) |
| 4725 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){ |
| 4726 pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
| 4727 sqlite3ExprDup(pParse->db, pExpr, 0)); |
| 4728 } |
| 4729 if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 4730 int iCol = pTerm->u.leftColumn; |
| 4731 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 4732 testcase( iCol==BMS ); |
| 4733 testcase( iCol==BMS-1 ); |
| 4734 if( !sentWarning ){ |
| 4735 sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 4736 "automatic index on %s(%s)", pTable->zName, |
| 4737 pTable->aCol[iCol].zName); |
| 4738 sentWarning = 1; |
| 4739 } |
| 4740 if( (idxCols & cMask)==0 ){ |
| 4741 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 4742 goto end_auto_index_create; |
| 4743 } |
| 4744 pLoop->aLTerm[nKeyCol++] = pTerm; |
| 4745 idxCols |= cMask; |
| 4746 } |
| 4747 } |
| 4748 } |
| 4749 assert( nKeyCol>0 ); |
| 4750 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
| 4751 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
| 4752 | WHERE_AUTO_INDEX; |
| 4753 |
| 4754 /* Count the number of additional columns needed to create a |
| 4755 ** covering index. A "covering index" is an index that contains all |
| 4756 ** columns that are needed by the query. With a covering index, the |
| 4757 ** original table never needs to be accessed. Automatic indices must |
| 4758 ** be a covering index because the index will not be updated if the |
| 4759 ** original table changes and the index and table cannot both be used |
| 4760 ** if they go out of sync. |
| 4761 */ |
| 4762 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
| 4763 mxBitCol = MIN(BMS-1,pTable->nCol); |
| 4764 testcase( pTable->nCol==BMS-1 ); |
| 4765 testcase( pTable->nCol==BMS-2 ); |
| 4766 for(i=0; i<mxBitCol; i++){ |
| 4767 if( extraCols & MASKBIT(i) ) nKeyCol++; |
| 4768 } |
| 4769 if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 4770 nKeyCol += pTable->nCol - BMS + 1; |
| 4771 } |
| 4772 |
| 4773 /* Construct the Index object to describe this index */ |
| 4774 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
| 4775 if( pIdx==0 ) goto end_auto_index_create; |
| 4776 pLoop->u.btree.pIndex = pIdx; |
| 4777 pIdx->zName = "auto-index"; |
| 4778 pIdx->pTable = pTable; |
| 4779 n = 0; |
| 4780 idxCols = 0; |
| 4781 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
| 4782 if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 4783 int iCol = pTerm->u.leftColumn; |
| 4784 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| 4785 testcase( iCol==BMS-1 ); |
| 4786 testcase( iCol==BMS ); |
| 4787 if( (idxCols & cMask)==0 ){ |
| 4788 Expr *pX = pTerm->pExpr; |
| 4789 idxCols |= cMask; |
| 4790 pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 4791 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 4792 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY; |
| 4793 n++; |
| 4794 } |
| 4795 } |
| 4796 } |
| 4797 assert( (u32)n==pLoop->u.btree.nEq ); |
| 4798 |
| 4799 /* Add additional columns needed to make the automatic index into |
| 4800 ** a covering index */ |
| 4801 for(i=0; i<mxBitCol; i++){ |
| 4802 if( extraCols & MASKBIT(i) ){ |
| 4803 pIdx->aiColumn[n] = i; |
| 4804 pIdx->azColl[n] = sqlite3StrBINARY; |
| 4805 n++; |
| 4806 } |
| 4807 } |
| 4808 if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
| 4809 for(i=BMS-1; i<pTable->nCol; i++){ |
| 4810 pIdx->aiColumn[n] = i; |
| 4811 pIdx->azColl[n] = sqlite3StrBINARY; |
| 4812 n++; |
| 4813 } |
| 4814 } |
| 4815 assert( n==nKeyCol ); |
| 4816 pIdx->aiColumn[n] = XN_ROWID; |
| 4817 pIdx->azColl[n] = sqlite3StrBINARY; |
| 4818 |
| 4819 /* Create the automatic index */ |
| 4820 assert( pLevel->iIdxCur>=0 ); |
| 4821 pLevel->iIdxCur = pParse->nTab++; |
| 4822 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 4823 sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 4824 VdbeComment((v, "for %s", pTable->zName)); |
| 4825 |
| 4826 /* Fill the automatic index with content */ |
| 4827 sqlite3ExprCachePush(pParse); |
| 4828 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom]; |
| 4829 if( pTabItem->fg.viaCoroutine ){ |
| 4830 int regYield = pTabItem->regReturn; |
| 4831 addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0); |
| 4832 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
| 4833 addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield); |
| 4834 VdbeCoverage(v); |
| 4835 VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
| 4836 }else{ |
| 4837 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
| 4838 } |
| 4839 if( pPartial ){ |
| 4840 iContinue = sqlite3VdbeMakeLabel(v); |
| 4841 sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
| 4842 pLoop->wsFlags |= WHERE_PARTIALIDX; |
| 4843 } |
| 4844 regRecord = sqlite3GetTempReg(pParse); |
| 4845 regBase = sqlite3GenerateIndexKey( |
| 4846 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0 |
| 4847 ); |
| 4848 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 4849 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 4850 if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
| 4851 if( pTabItem->fg.viaCoroutine ){ |
| 4852 sqlite3VdbeChangeP2(v, addrCounter, regBase+n); |
| 4853 translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult, 1); |
| 4854 sqlite3VdbeGoto(v, addrTop); |
| 4855 pTabItem->fg.viaCoroutine = 0; |
| 4856 }else{ |
| 4857 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
| 4858 } |
| 4859 sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
| 4860 sqlite3VdbeJumpHere(v, addrTop); |
| 4861 sqlite3ReleaseTempReg(pParse, regRecord); |
| 4862 sqlite3ExprCachePop(pParse); |
| 4863 |
| 4864 /* Jump here when skipping the initialization */ |
| 4865 sqlite3VdbeJumpHere(v, addrInit); |
| 4866 |
| 4867 end_auto_index_create: |
| 4868 sqlite3ExprDelete(pParse->db, pPartial); |
| 4869 } |
| 4870 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 4871 |
| 4872 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4873 /* |
| 4874 ** Allocate and populate an sqlite3_index_info structure. It is the |
| 4875 ** responsibility of the caller to eventually release the structure |
| 4876 ** by passing the pointer returned by this function to sqlite3_free(). |
| 4877 */ |
| 4878 static sqlite3_index_info *allocateIndexInfo( |
| 4879 Parse *pParse, |
| 4880 WhereClause *pWC, |
| 4881 Bitmask mUnusable, /* Ignore terms with these prereqs */ |
| 4882 struct SrcList_item *pSrc, |
| 4883 ExprList *pOrderBy, |
| 4884 u16 *pmNoOmit /* Mask of terms not to omit */ |
| 4885 ){ |
| 4886 int i, j; |
| 4887 int nTerm; |
| 4888 struct sqlite3_index_constraint *pIdxCons; |
| 4889 struct sqlite3_index_orderby *pIdxOrderBy; |
| 4890 struct sqlite3_index_constraint_usage *pUsage; |
| 4891 WhereTerm *pTerm; |
| 4892 int nOrderBy; |
| 4893 sqlite3_index_info *pIdxInfo; |
| 4894 u16 mNoOmit = 0; |
| 4895 |
| 4896 /* Count the number of possible WHERE clause constraints referring |
| 4897 ** to this virtual table */ |
| 4898 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4899 if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 4900 if( pTerm->prereqRight & mUnusable ) continue; |
| 4901 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 4902 testcase( pTerm->eOperator & WO_IN ); |
| 4903 testcase( pTerm->eOperator & WO_ISNULL ); |
| 4904 testcase( pTerm->eOperator & WO_IS ); |
| 4905 testcase( pTerm->eOperator & WO_ALL ); |
| 4906 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
| 4907 if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 4908 assert( pTerm->u.leftColumn>=(-1) ); |
| 4909 nTerm++; |
| 4910 } |
| 4911 |
| 4912 /* If the ORDER BY clause contains only columns in the current |
| 4913 ** virtual table then allocate space for the aOrderBy part of |
| 4914 ** the sqlite3_index_info structure. |
| 4915 */ |
| 4916 nOrderBy = 0; |
| 4917 if( pOrderBy ){ |
| 4918 int n = pOrderBy->nExpr; |
| 4919 for(i=0; i<n; i++){ |
| 4920 Expr *pExpr = pOrderBy->a[i].pExpr; |
| 4921 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 4922 } |
| 4923 if( i==n){ |
| 4924 nOrderBy = n; |
| 4925 } |
| 4926 } |
| 4927 |
| 4928 /* Allocate the sqlite3_index_info structure |
| 4929 */ |
| 4930 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 4931 + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 4932 + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 4933 if( pIdxInfo==0 ){ |
| 4934 sqlite3ErrorMsg(pParse, "out of memory"); |
| 4935 return 0; |
| 4936 } |
| 4937 |
| 4938 /* Initialize the structure. The sqlite3_index_info structure contains |
| 4939 ** many fields that are declared "const" to prevent xBestIndex from |
| 4940 ** changing them. We have to do some funky casting in order to |
| 4941 ** initialize those fields. |
| 4942 */ |
| 4943 pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 4944 pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 4945 pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 4946 *(int*)&pIdxInfo->nConstraint = nTerm; |
| 4947 *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 4948 *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 4949 *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 4950 *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 4951 pUsage; |
| 4952 |
| 4953 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 4954 u8 op; |
| 4955 if( pTerm->leftCursor != pSrc->iCursor ) continue; |
| 4956 if( pTerm->prereqRight & mUnusable ) continue; |
| 4957 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 4958 testcase( pTerm->eOperator & WO_IN ); |
| 4959 testcase( pTerm->eOperator & WO_IS ); |
| 4960 testcase( pTerm->eOperator & WO_ISNULL ); |
| 4961 testcase( pTerm->eOperator & WO_ALL ); |
| 4962 if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
| 4963 if( pTerm->wtFlags & TERM_VNULL ) continue; |
| 4964 assert( pTerm->u.leftColumn>=(-1) ); |
| 4965 pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 4966 pIdxCons[j].iTermOffset = i; |
| 4967 op = (u8)pTerm->eOperator & WO_ALL; |
| 4968 if( op==WO_IN ) op = WO_EQ; |
| 4969 if( op==WO_MATCH ){ |
| 4970 op = pTerm->eMatchOp; |
| 4971 } |
| 4972 pIdxCons[j].op = op; |
| 4973 /* The direct assignment in the previous line is possible only because |
| 4974 ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 4975 ** following asserts verify this fact. */ |
| 4976 assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 4977 assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 4978 assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 4979 assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 4980 assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 4981 assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
| 4982 assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); |
| 4983 |
| 4984 if( op & (WO_LT|WO_LE|WO_GT|WO_GE) |
| 4985 && sqlite3ExprIsVector(pTerm->pExpr->pRight) |
| 4986 ){ |
| 4987 if( i<16 ) mNoOmit |= (1 << i); |
| 4988 if( op==WO_LT ) pIdxCons[j].op = WO_LE; |
| 4989 if( op==WO_GT ) pIdxCons[j].op = WO_GE; |
| 4990 } |
| 4991 |
| 4992 j++; |
| 4993 } |
| 4994 for(i=0; i<nOrderBy; i++){ |
| 4995 Expr *pExpr = pOrderBy->a[i].pExpr; |
| 4996 pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 4997 pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 4998 } |
| 4999 |
| 5000 *pmNoOmit = mNoOmit; |
| 5001 return pIdxInfo; |
| 5002 } |
| 5003 |
| 5004 /* |
| 5005 ** The table object reference passed as the second argument to this function |
| 5006 ** must represent a virtual table. This function invokes the xBestIndex() |
| 5007 ** method of the virtual table with the sqlite3_index_info object that |
| 5008 ** comes in as the 3rd argument to this function. |
| 5009 ** |
| 5010 ** If an error occurs, pParse is populated with an error message and a |
| 5011 ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 5012 ** part of the sqlite3_index_info structure is left populated. |
| 5013 ** |
| 5014 ** Whether or not an error is returned, it is the responsibility of the |
| 5015 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 5016 ** that this is required. |
| 5017 */ |
| 5018 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
| 5019 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
| 5020 int rc; |
| 5021 |
| 5022 TRACE_IDX_INPUTS(p); |
| 5023 rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 5024 TRACE_IDX_OUTPUTS(p); |
| 5025 |
| 5026 if( rc!=SQLITE_OK ){ |
| 5027 if( rc==SQLITE_NOMEM ){ |
| 5028 sqlite3OomFault(pParse->db); |
| 5029 }else if( !pVtab->zErrMsg ){ |
| 5030 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 5031 }else{ |
| 5032 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 5033 } |
| 5034 } |
| 5035 sqlite3_free(pVtab->zErrMsg); |
| 5036 pVtab->zErrMsg = 0; |
| 5037 |
| 5038 #if 0 |
| 5039 /* This error is now caught by the caller. |
| 5040 ** Search for "xBestIndex malfunction" below */ |
| 5041 for(i=0; i<p->nConstraint; i++){ |
| 5042 if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 5043 sqlite3ErrorMsg(pParse, |
| 5044 "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 5045 } |
| 5046 } |
| 5047 #endif |
| 5048 |
| 5049 return pParse->nErr; |
| 5050 } |
| 5051 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
| 5052 |
| 5053 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 5054 /* |
| 5055 ** Estimate the location of a particular key among all keys in an |
| 5056 ** index. Store the results in aStat as follows: |
| 5057 ** |
| 5058 ** aStat[0] Est. number of rows less than pRec |
| 5059 ** aStat[1] Est. number of rows equal to pRec |
| 5060 ** |
| 5061 ** Return the index of the sample that is the smallest sample that |
| 5062 ** is greater than or equal to pRec. Note that this index is not an index |
| 5063 ** into the aSample[] array - it is an index into a virtual set of samples |
| 5064 ** based on the contents of aSample[] and the number of fields in record |
| 5065 ** pRec. |
| 5066 */ |
| 5067 static int whereKeyStats( |
| 5068 Parse *pParse, /* Database connection */ |
| 5069 Index *pIdx, /* Index to consider domain of */ |
| 5070 UnpackedRecord *pRec, /* Vector of values to consider */ |
| 5071 int roundUp, /* Round up if true. Round down if false */ |
| 5072 tRowcnt *aStat /* OUT: stats written here */ |
| 5073 ){ |
| 5074 IndexSample *aSample = pIdx->aSample; |
| 5075 int iCol; /* Index of required stats in anEq[] etc. */ |
| 5076 int i; /* Index of first sample >= pRec */ |
| 5077 int iSample; /* Smallest sample larger than or equal to pRec */ |
| 5078 int iMin = 0; /* Smallest sample not yet tested */ |
| 5079 int iTest; /* Next sample to test */ |
| 5080 int res; /* Result of comparison operation */ |
| 5081 int nField; /* Number of fields in pRec */ |
| 5082 tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */ |
| 5083 |
| 5084 #ifndef SQLITE_DEBUG |
| 5085 UNUSED_PARAMETER( pParse ); |
| 5086 #endif |
| 5087 assert( pRec!=0 ); |
| 5088 assert( pIdx->nSample>0 ); |
| 5089 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); |
| 5090 |
| 5091 /* Do a binary search to find the first sample greater than or equal |
| 5092 ** to pRec. If pRec contains a single field, the set of samples to search |
| 5093 ** is simply the aSample[] array. If the samples in aSample[] contain more |
| 5094 ** than one fields, all fields following the first are ignored. |
| 5095 ** |
| 5096 ** If pRec contains N fields, where N is more than one, then as well as the |
| 5097 ** samples in aSample[] (truncated to N fields), the search also has to |
| 5098 ** consider prefixes of those samples. For example, if the set of samples |
| 5099 ** in aSample is: |
| 5100 ** |
| 5101 ** aSample[0] = (a, 5) |
| 5102 ** aSample[1] = (a, 10) |
| 5103 ** aSample[2] = (b, 5) |
| 5104 ** aSample[3] = (c, 100) |
| 5105 ** aSample[4] = (c, 105) |
| 5106 ** |
| 5107 ** Then the search space should ideally be the samples above and the |
| 5108 ** unique prefixes [a], [b] and [c]. But since that is hard to organize, |
| 5109 ** the code actually searches this set: |
| 5110 ** |
| 5111 ** 0: (a) |
| 5112 ** 1: (a, 5) |
| 5113 ** 2: (a, 10) |
| 5114 ** 3: (a, 10) |
| 5115 ** 4: (b) |
| 5116 ** 5: (b, 5) |
| 5117 ** 6: (c) |
| 5118 ** 7: (c, 100) |
| 5119 ** 8: (c, 105) |
| 5120 ** 9: (c, 105) |
| 5121 ** |
| 5122 ** For each sample in the aSample[] array, N samples are present in the |
| 5123 ** effective sample array. In the above, samples 0 and 1 are based on |
| 5124 ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. |
| 5125 ** |
| 5126 ** Often, sample i of each block of N effective samples has (i+1) fields. |
| 5127 ** Except, each sample may be extended to ensure that it is greater than or |
| 5128 ** equal to the previous sample in the array. For example, in the above, |
| 5129 ** sample 2 is the first sample of a block of N samples, so at first it |
| 5130 ** appears that it should be 1 field in size. However, that would make it |
| 5131 ** smaller than sample 1, so the binary search would not work. As a result, |
| 5132 ** it is extended to two fields. The duplicates that this creates do not |
| 5133 ** cause any problems. |
| 5134 */ |
| 5135 nField = pRec->nField; |
| 5136 iCol = 0; |
| 5137 iSample = pIdx->nSample * nField; |
| 5138 do{ |
| 5139 int iSamp; /* Index in aSample[] of test sample */ |
| 5140 int n; /* Number of fields in test sample */ |
| 5141 |
| 5142 iTest = (iMin+iSample)/2; |
| 5143 iSamp = iTest / nField; |
| 5144 if( iSamp>0 ){ |
| 5145 /* The proposed effective sample is a prefix of sample aSample[iSamp]. |
| 5146 ** Specifically, the shortest prefix of at least (1 + iTest%nField) |
| 5147 ** fields that is greater than the previous effective sample. */ |
| 5148 for(n=(iTest % nField) + 1; n<nField; n++){ |
| 5149 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break; |
| 5150 } |
| 5151 }else{ |
| 5152 n = iTest + 1; |
| 5153 } |
| 5154 |
| 5155 pRec->nField = n; |
| 5156 res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec); |
| 5157 if( res<0 ){ |
| 5158 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1]; |
| 5159 iMin = iTest+1; |
| 5160 }else if( res==0 && n<nField ){ |
| 5161 iLower = aSample[iSamp].anLt[n-1]; |
| 5162 iMin = iTest+1; |
| 5163 res = -1; |
| 5164 }else{ |
| 5165 iSample = iTest; |
| 5166 iCol = n-1; |
| 5167 } |
| 5168 }while( res && iMin<iSample ); |
| 5169 i = iSample / nField; |
| 5170 |
| 5171 #ifdef SQLITE_DEBUG |
| 5172 /* The following assert statements check that the binary search code |
| 5173 ** above found the right answer. This block serves no purpose other |
| 5174 ** than to invoke the asserts. */ |
| 5175 if( pParse->db->mallocFailed==0 ){ |
| 5176 if( res==0 ){ |
| 5177 /* If (res==0) is true, then pRec must be equal to sample i. */ |
| 5178 assert( i<pIdx->nSample ); |
| 5179 assert( iCol==nField-1 ); |
| 5180 pRec->nField = nField; |
| 5181 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 5182 || pParse->db->mallocFailed |
| 5183 ); |
| 5184 }else{ |
| 5185 /* Unless i==pIdx->nSample, indicating that pRec is larger than |
| 5186 ** all samples in the aSample[] array, pRec must be smaller than the |
| 5187 ** (iCol+1) field prefix of sample i. */ |
| 5188 assert( i<=pIdx->nSample && i>=0 ); |
| 5189 pRec->nField = iCol+1; |
| 5190 assert( i==pIdx->nSample |
| 5191 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 5192 || pParse->db->mallocFailed ); |
| 5193 |
| 5194 /* if i==0 and iCol==0, then record pRec is smaller than all samples |
| 5195 ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must |
| 5196 ** be greater than or equal to the (iCol) field prefix of sample i. |
| 5197 ** If (i>0), then pRec must also be greater than sample (i-1). */ |
| 5198 if( iCol>0 ){ |
| 5199 pRec->nField = iCol; |
| 5200 assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 |
| 5201 || pParse->db->mallocFailed ); |
| 5202 } |
| 5203 if( i>0 ){ |
| 5204 pRec->nField = nField; |
| 5205 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 5206 || pParse->db->mallocFailed ); |
| 5207 } |
| 5208 } |
| 5209 } |
| 5210 #endif /* ifdef SQLITE_DEBUG */ |
| 5211 |
| 5212 if( res==0 ){ |
| 5213 /* Record pRec is equal to sample i */ |
| 5214 assert( iCol==nField-1 ); |
| 5215 aStat[0] = aSample[i].anLt[iCol]; |
| 5216 aStat[1] = aSample[i].anEq[iCol]; |
| 5217 }else{ |
| 5218 /* At this point, the (iCol+1) field prefix of aSample[i] is the first |
| 5219 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec |
| 5220 ** is larger than all samples in the array. */ |
| 5221 tRowcnt iUpper, iGap; |
| 5222 if( i>=pIdx->nSample ){ |
| 5223 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
| 5224 }else{ |
| 5225 iUpper = aSample[i].anLt[iCol]; |
| 5226 } |
| 5227 |
| 5228 if( iLower>=iUpper ){ |
| 5229 iGap = 0; |
| 5230 }else{ |
| 5231 iGap = iUpper - iLower; |
| 5232 } |
| 5233 if( roundUp ){ |
| 5234 iGap = (iGap*2)/3; |
| 5235 }else{ |
| 5236 iGap = iGap/3; |
| 5237 } |
| 5238 aStat[0] = iLower + iGap; |
| 5239 aStat[1] = pIdx->aAvgEq[iCol]; |
| 5240 } |
| 5241 |
| 5242 /* Restore the pRec->nField value before returning. */ |
| 5243 pRec->nField = nField; |
| 5244 return i; |
| 5245 } |
| 5246 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 5247 |
| 5248 /* |
| 5249 ** If it is not NULL, pTerm is a term that provides an upper or lower |
| 5250 ** bound on a range scan. Without considering pTerm, it is estimated |
| 5251 ** that the scan will visit nNew rows. This function returns the number |
| 5252 ** estimated to be visited after taking pTerm into account. |
| 5253 ** |
| 5254 ** If the user explicitly specified a likelihood() value for this term, |
| 5255 ** then the return value is the likelihood multiplied by the number of |
| 5256 ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term |
| 5257 ** has a likelihood of 0.50, and any other term a likelihood of 0.25. |
| 5258 */ |
| 5259 static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ |
| 5260 LogEst nRet = nNew; |
| 5261 if( pTerm ){ |
| 5262 if( pTerm->truthProb<=0 ){ |
| 5263 nRet += pTerm->truthProb; |
| 5264 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ |
| 5265 nRet -= 20; assert( 20==sqlite3LogEst(4) ); |
| 5266 } |
| 5267 } |
| 5268 return nRet; |
| 5269 } |
| 5270 |
| 5271 |
| 5272 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 5273 /* |
| 5274 ** Return the affinity for a single column of an index. |
| 5275 */ |
| 5276 SQLITE_PRIVATE char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCo
l){ |
| 5277 assert( iCol>=0 && iCol<pIdx->nColumn ); |
| 5278 if( !pIdx->zColAff ){ |
| 5279 if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB; |
| 5280 } |
| 5281 return pIdx->zColAff[iCol]; |
| 5282 } |
| 5283 #endif |
| 5284 |
| 5285 |
| 5286 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 5287 /* |
| 5288 ** This function is called to estimate the number of rows visited by a |
| 5289 ** range-scan on a skip-scan index. For example: |
| 5290 ** |
| 5291 ** CREATE INDEX i1 ON t1(a, b, c); |
| 5292 ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; |
| 5293 ** |
| 5294 ** Value pLoop->nOut is currently set to the estimated number of rows |
| 5295 ** visited for scanning (a=? AND b=?). This function reduces that estimate |
| 5296 ** by some factor to account for the (c BETWEEN ? AND ?) expression based |
| 5297 ** on the stat4 data for the index. this scan will be peformed multiple |
| 5298 ** times (once for each (a,b) combination that matches a=?) is dealt with |
| 5299 ** by the caller. |
| 5300 ** |
| 5301 ** It does this by scanning through all stat4 samples, comparing values |
| 5302 ** extracted from pLower and pUpper with the corresponding column in each |
| 5303 ** sample. If L and U are the number of samples found to be less than or |
| 5304 ** equal to the values extracted from pLower and pUpper respectively, and |
| 5305 ** N is the total number of samples, the pLoop->nOut value is adjusted |
| 5306 ** as follows: |
| 5307 ** |
| 5308 ** nOut = nOut * ( min(U - L, 1) / N ) |
| 5309 ** |
| 5310 ** If pLower is NULL, or a value cannot be extracted from the term, L is |
| 5311 ** set to zero. If pUpper is NULL, or a value cannot be extracted from it, |
| 5312 ** U is set to N. |
| 5313 ** |
| 5314 ** Normally, this function sets *pbDone to 1 before returning. However, |
| 5315 ** if no value can be extracted from either pLower or pUpper (and so the |
| 5316 ** estimate of the number of rows delivered remains unchanged), *pbDone |
| 5317 ** is left as is. |
| 5318 ** |
| 5319 ** If an error occurs, an SQLite error code is returned. Otherwise, |
| 5320 ** SQLITE_OK. |
| 5321 */ |
| 5322 static int whereRangeSkipScanEst( |
| 5323 Parse *pParse, /* Parsing & code generating context */ |
| 5324 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 5325 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 5326 WhereLoop *pLoop, /* Update the .nOut value of this loop */ |
| 5327 int *pbDone /* Set to true if at least one expr. value extracted */ |
| 5328 ){ |
| 5329 Index *p = pLoop->u.btree.pIndex; |
| 5330 int nEq = pLoop->u.btree.nEq; |
| 5331 sqlite3 *db = pParse->db; |
| 5332 int nLower = -1; |
| 5333 int nUpper = p->nSample+1; |
| 5334 int rc = SQLITE_OK; |
| 5335 u8 aff = sqlite3IndexColumnAffinity(db, p, nEq); |
| 5336 CollSeq *pColl; |
| 5337 |
| 5338 sqlite3_value *p1 = 0; /* Value extracted from pLower */ |
| 5339 sqlite3_value *p2 = 0; /* Value extracted from pUpper */ |
| 5340 sqlite3_value *pVal = 0; /* Value extracted from record */ |
| 5341 |
| 5342 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]); |
| 5343 if( pLower ){ |
| 5344 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1); |
| 5345 nLower = 0; |
| 5346 } |
| 5347 if( pUpper && rc==SQLITE_OK ){ |
| 5348 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2); |
| 5349 nUpper = p2 ? 0 : p->nSample; |
| 5350 } |
| 5351 |
| 5352 if( p1 || p2 ){ |
| 5353 int i; |
| 5354 int nDiff; |
| 5355 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){ |
| 5356 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal); |
| 5357 if( rc==SQLITE_OK && p1 ){ |
| 5358 int res = sqlite3MemCompare(p1, pVal, pColl); |
| 5359 if( res>=0 ) nLower++; |
| 5360 } |
| 5361 if( rc==SQLITE_OK && p2 ){ |
| 5362 int res = sqlite3MemCompare(p2, pVal, pColl); |
| 5363 if( res>=0 ) nUpper++; |
| 5364 } |
| 5365 } |
| 5366 nDiff = (nUpper - nLower); |
| 5367 if( nDiff<=0 ) nDiff = 1; |
| 5368 |
| 5369 /* If there is both an upper and lower bound specified, and the |
| 5370 ** comparisons indicate that they are close together, use the fallback |
| 5371 ** method (assume that the scan visits 1/64 of the rows) for estimating |
| 5372 ** the number of rows visited. Otherwise, estimate the number of rows |
| 5373 ** using the method described in the header comment for this function. */ |
| 5374 if( nDiff!=1 || pUpper==0 || pLower==0 ){ |
| 5375 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); |
| 5376 pLoop->nOut -= nAdjust; |
| 5377 *pbDone = 1; |
| 5378 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", |
| 5379 nLower, nUpper, nAdjust*-1, pLoop->nOut)); |
| 5380 } |
| 5381 |
| 5382 }else{ |
| 5383 assert( *pbDone==0 ); |
| 5384 } |
| 5385 |
| 5386 sqlite3ValueFree(p1); |
| 5387 sqlite3ValueFree(p2); |
| 5388 sqlite3ValueFree(pVal); |
| 5389 |
| 5390 return rc; |
| 5391 } |
| 5392 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 5393 |
| 5394 /* |
| 5395 ** This function is used to estimate the number of rows that will be visited |
| 5396 ** by scanning an index for a range of values. The range may have an upper |
| 5397 ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 5398 ** and lower bounds are represented by pLower and pUpper respectively. For |
| 5399 ** example, assuming that index p is on t1(a): |
| 5400 ** |
| 5401 ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 5402 ** |_____| |_____| |
| 5403 ** | | |
| 5404 ** pLower pUpper |
| 5405 ** |
| 5406 ** If either of the upper or lower bound is not present, then NULL is passed in |
| 5407 ** place of the corresponding WhereTerm. |
| 5408 ** |
| 5409 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index |
| 5410 ** column subject to the range constraint. Or, equivalently, the number of |
| 5411 ** equality constraints optimized by the proposed index scan. For example, |
| 5412 ** assuming index p is on t1(a, b), and the SQL query is: |
| 5413 ** |
| 5414 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 5415 ** |
| 5416 ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 5417 ** left-most column of the index). Or, if the query is: |
| 5418 ** |
| 5419 ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 5420 ** |
| 5421 ** then nEq is set to 0. |
| 5422 ** |
| 5423 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
| 5424 ** number of rows that the index scan is expected to visit without |
| 5425 ** considering the range constraints. If nEq is 0, then *pnOut is the number of |
| 5426 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) |
| 5427 ** to account for the range constraints pLower and pUpper. |
| 5428 ** |
| 5429 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be |
| 5430 ** used, a single range inequality reduces the search space by a factor of 4. |
| 5431 ** and a pair of constraints (x>? AND x<?) reduces the expected number of |
| 5432 ** rows visited by a factor of 64. |
| 5433 */ |
| 5434 static int whereRangeScanEst( |
| 5435 Parse *pParse, /* Parsing & code generating context */ |
| 5436 WhereLoopBuilder *pBuilder, |
| 5437 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 5438 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 5439 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ |
| 5440 ){ |
| 5441 int rc = SQLITE_OK; |
| 5442 int nOut = pLoop->nOut; |
| 5443 LogEst nNew; |
| 5444 |
| 5445 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 5446 Index *p = pLoop->u.btree.pIndex; |
| 5447 int nEq = pLoop->u.btree.nEq; |
| 5448 |
| 5449 if( p->nSample>0 && nEq<p->nSampleCol ){ |
| 5450 if( nEq==pBuilder->nRecValid ){ |
| 5451 UnpackedRecord *pRec = pBuilder->pRec; |
| 5452 tRowcnt a[2]; |
| 5453 int nBtm = pLoop->u.btree.nBtm; |
| 5454 int nTop = pLoop->u.btree.nTop; |
| 5455 |
| 5456 /* Variable iLower will be set to the estimate of the number of rows in |
| 5457 ** the index that are less than the lower bound of the range query. The |
| 5458 ** lower bound being the concatenation of $P and $L, where $P is the |
| 5459 ** key-prefix formed by the nEq values matched against the nEq left-most |
| 5460 ** columns of the index, and $L is the value in pLower. |
| 5461 ** |
| 5462 ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 5463 ** is not a simple variable or literal value), the lower bound of the |
| 5464 ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 5465 ** if $L is available, whereKeyStats() is called for both ($P) and |
| 5466 ** ($P:$L) and the larger of the two returned values is used. |
| 5467 ** |
| 5468 ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 5469 ** less than the upper bound of the range query. Where the upper bound |
| 5470 ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 5471 ** of iUpper are requested of whereKeyStats() and the smaller used. |
| 5472 ** |
| 5473 ** The number of rows between the two bounds is then just iUpper-iLower. |
| 5474 */ |
| 5475 tRowcnt iLower; /* Rows less than the lower bound */ |
| 5476 tRowcnt iUpper; /* Rows less than the upper bound */ |
| 5477 int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 5478 int iUprIdx = -1; /* aSample[] for the upper bound */ |
| 5479 |
| 5480 if( pRec ){ |
| 5481 testcase( pRec->nField!=pBuilder->nRecValid ); |
| 5482 pRec->nField = pBuilder->nRecValid; |
| 5483 } |
| 5484 /* Determine iLower and iUpper using ($P) only. */ |
| 5485 if( nEq==0 ){ |
| 5486 iLower = 0; |
| 5487 iUpper = p->nRowEst0; |
| 5488 }else{ |
| 5489 /* Note: this call could be optimized away - since the same values must |
| 5490 ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 5491 whereKeyStats(pParse, p, pRec, 0, a); |
| 5492 iLower = a[0]; |
| 5493 iUpper = a[0] + a[1]; |
| 5494 } |
| 5495 |
| 5496 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 5497 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
| 5498 assert( p->aSortOrder!=0 ); |
| 5499 if( p->aSortOrder[nEq] ){ |
| 5500 /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 5501 SWAP(WhereTerm*, pLower, pUpper); |
| 5502 SWAP(int, nBtm, nTop); |
| 5503 } |
| 5504 |
| 5505 /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 5506 if( pLower ){ |
| 5507 int n; /* Values extracted from pExpr */ |
| 5508 Expr *pExpr = pLower->pExpr->pRight; |
| 5509 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n); |
| 5510 if( rc==SQLITE_OK && n ){ |
| 5511 tRowcnt iNew; |
| 5512 u16 mask = WO_GT|WO_LE; |
| 5513 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); |
| 5514 iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
| 5515 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0); |
| 5516 if( iNew>iLower ) iLower = iNew; |
| 5517 nOut--; |
| 5518 pLower = 0; |
| 5519 } |
| 5520 } |
| 5521 |
| 5522 /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 5523 if( pUpper ){ |
| 5524 int n; /* Values extracted from pExpr */ |
| 5525 Expr *pExpr = pUpper->pExpr->pRight; |
| 5526 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n); |
| 5527 if( rc==SQLITE_OK && n ){ |
| 5528 tRowcnt iNew; |
| 5529 u16 mask = WO_GT|WO_LE; |
| 5530 if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); |
| 5531 iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
| 5532 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0); |
| 5533 if( iNew<iUpper ) iUpper = iNew; |
| 5534 nOut--; |
| 5535 pUpper = 0; |
| 5536 } |
| 5537 } |
| 5538 |
| 5539 pBuilder->pRec = pRec; |
| 5540 if( rc==SQLITE_OK ){ |
| 5541 if( iUpper>iLower ){ |
| 5542 nNew = sqlite3LogEst(iUpper - iLower); |
| 5543 /* TUNING: If both iUpper and iLower are derived from the same |
| 5544 ** sample, then assume they are 4x more selective. This brings |
| 5545 ** the estimated selectivity more in line with what it would be |
| 5546 ** if estimated without the use of STAT3/4 tables. */ |
| 5547 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
| 5548 }else{ |
| 5549 nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 5550 } |
| 5551 if( nNew<nOut ){ |
| 5552 nOut = nNew; |
| 5553 } |
| 5554 WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n", |
| 5555 (u32)iLower, (u32)iUpper, nOut)); |
| 5556 } |
| 5557 }else{ |
| 5558 int bDone = 0; |
| 5559 rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone); |
| 5560 if( bDone ) return rc; |
| 5561 } |
| 5562 } |
| 5563 #else |
| 5564 UNUSED_PARAMETER(pParse); |
| 5565 UNUSED_PARAMETER(pBuilder); |
| 5566 assert( pLower || pUpper ); |
| 5567 #endif |
| 5568 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
| 5569 nNew = whereRangeAdjust(pLower, nOut); |
| 5570 nNew = whereRangeAdjust(pUpper, nNew); |
| 5571 |
| 5572 /* TUNING: If there is both an upper and lower limit and neither limit |
| 5573 ** has an application-defined likelihood(), assume the range is |
| 5574 ** reduced by an additional 75%. This means that, by default, an open-ended |
| 5575 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 5576 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 5577 ** match 1/64 of the index. */ |
| 5578 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 5579 nNew -= 20; |
| 5580 } |
| 5581 |
| 5582 nOut -= (pLower!=0) + (pUpper!=0); |
| 5583 if( nNew<10 ) nNew = 10; |
| 5584 if( nNew<nOut ) nOut = nNew; |
| 5585 #if defined(WHERETRACE_ENABLED) |
| 5586 if( pLoop->nOut>nOut ){ |
| 5587 WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", |
| 5588 pLoop->nOut, nOut)); |
| 5589 } |
| 5590 #endif |
| 5591 pLoop->nOut = (LogEst)nOut; |
| 5592 return rc; |
| 5593 } |
| 5594 |
| 5595 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 5596 /* |
| 5597 ** Estimate the number of rows that will be returned based on |
| 5598 ** an equality constraint x=VALUE and where that VALUE occurs in |
| 5599 ** the histogram data. This only works when x is the left-most |
| 5600 ** column of an index and sqlite_stat3 histogram data is available |
| 5601 ** for that index. When pExpr==NULL that means the constraint is |
| 5602 ** "x IS NULL" instead of "x=VALUE". |
| 5603 ** |
| 5604 ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 5605 ** If unable to make an estimate, leave *pnRow unchanged and return |
| 5606 ** non-zero. |
| 5607 ** |
| 5608 ** This routine can fail if it is unable to load a collating sequence |
| 5609 ** required for string comparison, or if unable to allocate memory |
| 5610 ** for a UTF conversion required for comparison. The error is stored |
| 5611 ** in the pParse structure. |
| 5612 */ |
| 5613 static int whereEqualScanEst( |
| 5614 Parse *pParse, /* Parsing & code generating context */ |
| 5615 WhereLoopBuilder *pBuilder, |
| 5616 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
| 5617 tRowcnt *pnRow /* Write the revised row estimate here */ |
| 5618 ){ |
| 5619 Index *p = pBuilder->pNew->u.btree.pIndex; |
| 5620 int nEq = pBuilder->pNew->u.btree.nEq; |
| 5621 UnpackedRecord *pRec = pBuilder->pRec; |
| 5622 int rc; /* Subfunction return code */ |
| 5623 tRowcnt a[2]; /* Statistics */ |
| 5624 int bOk; |
| 5625 |
| 5626 assert( nEq>=1 ); |
| 5627 assert( nEq<=p->nColumn ); |
| 5628 assert( p->aSample!=0 ); |
| 5629 assert( p->nSample>0 ); |
| 5630 assert( pBuilder->nRecValid<nEq ); |
| 5631 |
| 5632 /* If values are not available for all fields of the index to the left |
| 5633 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 5634 if( pBuilder->nRecValid<(nEq-1) ){ |
| 5635 return SQLITE_NOTFOUND; |
| 5636 } |
| 5637 |
| 5638 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 5639 ** below would return the same value. */ |
| 5640 if( nEq>=p->nColumn ){ |
| 5641 *pnRow = 1; |
| 5642 return SQLITE_OK; |
| 5643 } |
| 5644 |
| 5645 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk); |
| 5646 pBuilder->pRec = pRec; |
| 5647 if( rc!=SQLITE_OK ) return rc; |
| 5648 if( bOk==0 ) return SQLITE_NOTFOUND; |
| 5649 pBuilder->nRecValid = nEq; |
| 5650 |
| 5651 whereKeyStats(pParse, p, pRec, 0, a); |
| 5652 WHERETRACE(0x10,("equality scan regions %s(%d): %d\n", |
| 5653 p->zName, nEq-1, (int)a[1])); |
| 5654 *pnRow = a[1]; |
| 5655 |
| 5656 return rc; |
| 5657 } |
| 5658 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 5659 |
| 5660 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 5661 /* |
| 5662 ** Estimate the number of rows that will be returned based on |
| 5663 ** an IN constraint where the right-hand side of the IN operator |
| 5664 ** is a list of values. Example: |
| 5665 ** |
| 5666 ** WHERE x IN (1,2,3,4) |
| 5667 ** |
| 5668 ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 5669 ** If unable to make an estimate, leave *pnRow unchanged and return |
| 5670 ** non-zero. |
| 5671 ** |
| 5672 ** This routine can fail if it is unable to load a collating sequence |
| 5673 ** required for string comparison, or if unable to allocate memory |
| 5674 ** for a UTF conversion required for comparison. The error is stored |
| 5675 ** in the pParse structure. |
| 5676 */ |
| 5677 static int whereInScanEst( |
| 5678 Parse *pParse, /* Parsing & code generating context */ |
| 5679 WhereLoopBuilder *pBuilder, |
| 5680 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ |
| 5681 tRowcnt *pnRow /* Write the revised row estimate here */ |
| 5682 ){ |
| 5683 Index *p = pBuilder->pNew->u.btree.pIndex; |
| 5684 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
| 5685 int nRecValid = pBuilder->nRecValid; |
| 5686 int rc = SQLITE_OK; /* Subfunction return code */ |
| 5687 tRowcnt nEst; /* Number of rows for a single term */ |
| 5688 tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 5689 int i; /* Loop counter */ |
| 5690 |
| 5691 assert( p->aSample!=0 ); |
| 5692 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
| 5693 nEst = nRow0; |
| 5694 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
| 5695 nRowEst += nEst; |
| 5696 pBuilder->nRecValid = nRecValid; |
| 5697 } |
| 5698 |
| 5699 if( rc==SQLITE_OK ){ |
| 5700 if( nRowEst > nRow0 ) nRowEst = nRow0; |
| 5701 *pnRow = nRowEst; |
| 5702 WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); |
| 5703 } |
| 5704 assert( pBuilder->nRecValid==nRecValid ); |
| 5705 return rc; |
| 5706 } |
| 5707 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
| 5708 |
| 5709 |
| 5710 #ifdef WHERETRACE_ENABLED |
| 5711 /* |
| 5712 ** Print the content of a WhereTerm object |
| 5713 */ |
| 5714 static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
| 5715 if( pTerm==0 ){ |
| 5716 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 5717 }else{ |
| 5718 char zType[4]; |
| 5719 char zLeft[50]; |
| 5720 memcpy(zType, "...", 4); |
| 5721 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 5722 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 5723 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
| 5724 if( pTerm->eOperator & WO_SINGLE ){ |
| 5725 sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}", |
| 5726 pTerm->leftCursor, pTerm->u.leftColumn); |
| 5727 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){ |
| 5728 sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld", |
| 5729 pTerm->u.pOrInfo->indexable); |
| 5730 }else{ |
| 5731 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor); |
| 5732 } |
| 5733 sqlite3DebugPrintf( |
| 5734 "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x", |
| 5735 iTerm, pTerm, zType, zLeft, pTerm->truthProb, |
| 5736 pTerm->eOperator, pTerm->wtFlags); |
| 5737 if( pTerm->iField ){ |
| 5738 sqlite3DebugPrintf(" iField=%d\n", pTerm->iField); |
| 5739 }else{ |
| 5740 sqlite3DebugPrintf("\n"); |
| 5741 } |
| 5742 sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 5743 } |
| 5744 } |
| 5745 #endif |
| 5746 |
| 5747 #ifdef WHERETRACE_ENABLED |
| 5748 /* |
| 5749 ** Show the complete content of a WhereClause |
| 5750 */ |
| 5751 SQLITE_PRIVATE void sqlite3WhereClausePrint(WhereClause *pWC){ |
| 5752 int i; |
| 5753 for(i=0; i<pWC->nTerm; i++){ |
| 5754 whereTermPrint(&pWC->a[i], i); |
| 5755 } |
| 5756 } |
| 5757 #endif |
| 5758 |
| 5759 #ifdef WHERETRACE_ENABLED |
| 5760 /* |
| 5761 ** Print a WhereLoop object for debugging purposes |
| 5762 */ |
| 5763 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 5764 WhereInfo *pWInfo = pWC->pWInfo; |
| 5765 int nb = 1+(pWInfo->pTabList->nSrc+3)/4; |
| 5766 struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
| 5767 Table *pTab = pItem->pTab; |
| 5768 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; |
| 5769 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
| 5770 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); |
| 5771 sqlite3DebugPrintf(" %12s", |
| 5772 pItem->zAlias ? pItem->zAlias : pTab->zName); |
| 5773 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 5774 const char *zName; |
| 5775 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
| 5776 if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 5777 int i = sqlite3Strlen30(zName) - 1; |
| 5778 while( zName[i]!='_' ) i--; |
| 5779 zName += i; |
| 5780 } |
| 5781 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
| 5782 }else{ |
| 5783 sqlite3DebugPrintf("%20s",""); |
| 5784 } |
| 5785 }else{ |
| 5786 char *z; |
| 5787 if( p->u.vtab.idxStr ){ |
| 5788 z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 5789 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
| 5790 }else{ |
| 5791 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
| 5792 } |
| 5793 sqlite3DebugPrintf(" %-19s", z); |
| 5794 sqlite3_free(z); |
| 5795 } |
| 5796 if( p->wsFlags & WHERE_SKIPSCAN ){ |
| 5797 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
| 5798 }else{ |
| 5799 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 5800 } |
| 5801 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
| 5802 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| 5803 int i; |
| 5804 for(i=0; i<p->nLTerm; i++){ |
| 5805 whereTermPrint(p->aLTerm[i], i); |
| 5806 } |
| 5807 } |
| 5808 } |
| 5809 #endif |
| 5810 |
| 5811 /* |
| 5812 ** Convert bulk memory into a valid WhereLoop that can be passed |
| 5813 ** to whereLoopClear harmlessly. |
| 5814 */ |
| 5815 static void whereLoopInit(WhereLoop *p){ |
| 5816 p->aLTerm = p->aLTermSpace; |
| 5817 p->nLTerm = 0; |
| 5818 p->nLSlot = ArraySize(p->aLTermSpace); |
| 5819 p->wsFlags = 0; |
| 5820 } |
| 5821 |
| 5822 /* |
| 5823 ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 5824 */ |
| 5825 static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
| 5826 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
| 5827 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 5828 sqlite3_free(p->u.vtab.idxStr); |
| 5829 p->u.vtab.needFree = 0; |
| 5830 p->u.vtab.idxStr = 0; |
| 5831 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
| 5832 sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 5833 sqlite3DbFree(db, p->u.btree.pIndex); |
| 5834 p->u.btree.pIndex = 0; |
| 5835 } |
| 5836 } |
| 5837 } |
| 5838 |
| 5839 /* |
| 5840 ** Deallocate internal memory used by a WhereLoop object |
| 5841 */ |
| 5842 static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 5843 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 5844 whereLoopClearUnion(db, p); |
| 5845 whereLoopInit(p); |
| 5846 } |
| 5847 |
| 5848 /* |
| 5849 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 5850 */ |
| 5851 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 5852 WhereTerm **paNew; |
| 5853 if( p->nLSlot>=n ) return SQLITE_OK; |
| 5854 n = (n+7)&~7; |
| 5855 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n); |
| 5856 if( paNew==0 ) return SQLITE_NOMEM_BKPT; |
| 5857 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 5858 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 5859 p->aLTerm = paNew; |
| 5860 p->nLSlot = n; |
| 5861 return SQLITE_OK; |
| 5862 } |
| 5863 |
| 5864 /* |
| 5865 ** Transfer content from the second pLoop into the first. |
| 5866 */ |
| 5867 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
| 5868 whereLoopClearUnion(db, pTo); |
| 5869 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 5870 memset(&pTo->u, 0, sizeof(pTo->u)); |
| 5871 return SQLITE_NOMEM_BKPT; |
| 5872 } |
| 5873 memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 5874 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
| 5875 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 5876 pFrom->u.vtab.needFree = 0; |
| 5877 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 5878 pFrom->u.btree.pIndex = 0; |
| 5879 } |
| 5880 return SQLITE_OK; |
| 5881 } |
| 5882 |
| 5883 /* |
| 5884 ** Delete a WhereLoop object |
| 5885 */ |
| 5886 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
| 5887 whereLoopClear(db, p); |
| 5888 sqlite3DbFree(db, p); |
| 5889 } |
| 5890 |
| 5891 /* |
| 5892 ** Free a WhereInfo structure |
| 5893 */ |
| 5894 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 5895 if( ALWAYS(pWInfo) ){ |
| 5896 int i; |
| 5897 for(i=0; i<pWInfo->nLevel; i++){ |
| 5898 WhereLevel *pLevel = &pWInfo->a[i]; |
| 5899 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ |
| 5900 sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 5901 } |
| 5902 } |
| 5903 sqlite3WhereClauseClear(&pWInfo->sWC); |
| 5904 while( pWInfo->pLoops ){ |
| 5905 WhereLoop *p = pWInfo->pLoops; |
| 5906 pWInfo->pLoops = p->pNextLoop; |
| 5907 whereLoopDelete(db, p); |
| 5908 } |
| 5909 sqlite3DbFree(db, pWInfo); |
| 5910 } |
| 5911 } |
| 5912 |
| 5913 /* |
| 5914 ** Return TRUE if all of the following are true: |
| 5915 ** |
| 5916 ** (1) X has the same or lower cost that Y |
| 5917 ** (2) X is a proper subset of Y |
| 5918 ** (3) X skips at least as many columns as Y |
| 5919 ** |
| 5920 ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 5921 ** than Y and that every WHERE clause term used by X is also used |
| 5922 ** by Y. |
| 5923 ** |
| 5924 ** If X is a proper subset of Y then Y is a better choice and ought |
| 5925 ** to have a lower cost. This routine returns TRUE when that cost |
| 5926 ** relationship is inverted and needs to be adjusted. The third rule |
| 5927 ** was added because if X uses skip-scan less than Y it still might |
| 5928 ** deserve a lower cost even if it is a proper subset of Y. |
| 5929 */ |
| 5930 static int whereLoopCheaperProperSubset( |
| 5931 const WhereLoop *pX, /* First WhereLoop to compare */ |
| 5932 const WhereLoop *pY /* Compare against this WhereLoop */ |
| 5933 ){ |
| 5934 int i, j; |
| 5935 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 5936 return 0; /* X is not a subset of Y */ |
| 5937 } |
| 5938 if( pY->nSkip > pX->nSkip ) return 0; |
| 5939 if( pX->rRun >= pY->rRun ){ |
| 5940 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 5941 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
| 5942 } |
| 5943 for(i=pX->nLTerm-1; i>=0; i--){ |
| 5944 if( pX->aLTerm[i]==0 ) continue; |
| 5945 for(j=pY->nLTerm-1; j>=0; j--){ |
| 5946 if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 5947 } |
| 5948 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 5949 } |
| 5950 return 1; /* All conditions meet */ |
| 5951 } |
| 5952 |
| 5953 /* |
| 5954 ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so |
| 5955 ** that: |
| 5956 ** |
| 5957 ** (1) pTemplate costs less than any other WhereLoops that are a proper |
| 5958 ** subset of pTemplate |
| 5959 ** |
| 5960 ** (2) pTemplate costs more than any other WhereLoops for which pTemplate |
| 5961 ** is a proper subset. |
| 5962 ** |
| 5963 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 5964 ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 5965 ** also used by Y. |
| 5966 */ |
| 5967 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 5968 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
| 5969 for(; p; p=p->pNextLoop){ |
| 5970 if( p->iTab!=pTemplate->iTab ) continue; |
| 5971 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
| 5972 if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 5973 /* Adjust pTemplate cost downward so that it is cheaper than its |
| 5974 ** subset p. */ |
| 5975 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 5976 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
| 5977 pTemplate->rRun = p->rRun; |
| 5978 pTemplate->nOut = p->nOut - 1; |
| 5979 }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 5980 /* Adjust pTemplate cost upward so that it is costlier than p since |
| 5981 ** pTemplate is a proper subset of p */ |
| 5982 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 5983 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
| 5984 pTemplate->rRun = p->rRun; |
| 5985 pTemplate->nOut = p->nOut + 1; |
| 5986 } |
| 5987 } |
| 5988 } |
| 5989 |
| 5990 /* |
| 5991 ** Search the list of WhereLoops in *ppPrev looking for one that can be |
| 5992 ** supplanted by pTemplate. |
| 5993 ** |
| 5994 ** Return NULL if the WhereLoop list contains an entry that can supplant |
| 5995 ** pTemplate, in other words if pTemplate does not belong on the list. |
| 5996 ** |
| 5997 ** If pX is a WhereLoop that pTemplate can supplant, then return the |
| 5998 ** link that points to pX. |
| 5999 ** |
| 6000 ** If pTemplate cannot supplant any existing element of the list but needs |
| 6001 ** to be added to the list, then return a pointer to the tail of the list. |
| 6002 */ |
| 6003 static WhereLoop **whereLoopFindLesser( |
| 6004 WhereLoop **ppPrev, |
| 6005 const WhereLoop *pTemplate |
| 6006 ){ |
| 6007 WhereLoop *p; |
| 6008 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
| 6009 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 6010 /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 6011 ** then those WhereLoops need to be considered separately. Neither is |
| 6012 ** a candidate to replace the other. */ |
| 6013 continue; |
| 6014 } |
| 6015 /* In the current implementation, the rSetup value is either zero |
| 6016 ** or the cost of building an automatic index (NlogN) and the NlogN |
| 6017 ** is the same for compatible WhereLoops. */ |
| 6018 assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 6019 || p->rSetup==pTemplate->rSetup ); |
| 6020 |
| 6021 /* whereLoopAddBtree() always generates and inserts the automatic index |
| 6022 ** case first. Hence compatible candidate WhereLoops never have a larger |
| 6023 ** rSetup. Call this SETUP-INVARIANT */ |
| 6024 assert( p->rSetup>=pTemplate->rSetup ); |
| 6025 |
| 6026 /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 6027 ** UNIQUE constraint) with one or more == constraints is better |
| 6028 ** than an automatic index. Unless it is a skip-scan. */ |
| 6029 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
| 6030 && (pTemplate->nSkip)==0 |
| 6031 && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 6032 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 6033 && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 6034 ){ |
| 6035 break; |
| 6036 } |
| 6037 |
| 6038 /* If existing WhereLoop p is better than pTemplate, pTemplate can be |
| 6039 ** discarded. WhereLoop p is better if: |
| 6040 ** (1) p has no more dependencies than pTemplate, and |
| 6041 ** (2) p has an equal or lower cost than pTemplate |
| 6042 */ |
| 6043 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */ |
| 6044 && p->rSetup<=pTemplate->rSetup /* (2a) */ |
| 6045 && p->rRun<=pTemplate->rRun /* (2b) */ |
| 6046 && p->nOut<=pTemplate->nOut /* (2c) */ |
| 6047 ){ |
| 6048 return 0; /* Discard pTemplate */ |
| 6049 } |
| 6050 |
| 6051 /* If pTemplate is always better than p, then cause p to be overwritten |
| 6052 ** with pTemplate. pTemplate is better than p if: |
| 6053 ** (1) pTemplate has no more dependences than p, and |
| 6054 ** (2) pTemplate has an equal or lower cost than p. |
| 6055 */ |
| 6056 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ |
| 6057 && p->rRun>=pTemplate->rRun /* (2a) */ |
| 6058 && p->nOut>=pTemplate->nOut /* (2b) */ |
| 6059 ){ |
| 6060 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ |
| 6061 break; /* Cause p to be overwritten by pTemplate */ |
| 6062 } |
| 6063 } |
| 6064 return ppPrev; |
| 6065 } |
| 6066 |
| 6067 /* |
| 6068 ** Insert or replace a WhereLoop entry using the template supplied. |
| 6069 ** |
| 6070 ** An existing WhereLoop entry might be overwritten if the new template |
| 6071 ** is better and has fewer dependencies. Or the template will be ignored |
| 6072 ** and no insert will occur if an existing WhereLoop is faster and has |
| 6073 ** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 6074 ** added based on the template. |
| 6075 ** |
| 6076 ** If pBuilder->pOrSet is not NULL then we care about only the |
| 6077 ** prerequisites and rRun and nOut costs of the N best loops. That |
| 6078 ** information is gathered in the pBuilder->pOrSet object. This special |
| 6079 ** processing mode is used only for OR clause processing. |
| 6080 ** |
| 6081 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
| 6082 ** still might overwrite similar loops with the new template if the |
| 6083 ** new template is better. Loops may be overwritten if the following |
| 6084 ** conditions are met: |
| 6085 ** |
| 6086 ** (1) They have the same iTab. |
| 6087 ** (2) They have the same iSortIdx. |
| 6088 ** (3) The template has same or fewer dependencies than the current loop |
| 6089 ** (4) The template has the same or lower cost than the current loop |
| 6090 */ |
| 6091 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
| 6092 WhereLoop **ppPrev, *p; |
| 6093 WhereInfo *pWInfo = pBuilder->pWInfo; |
| 6094 sqlite3 *db = pWInfo->pParse->db; |
| 6095 int rc; |
| 6096 |
| 6097 /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 6098 ** and prereqs. |
| 6099 */ |
| 6100 if( pBuilder->pOrSet!=0 ){ |
| 6101 if( pTemplate->nLTerm ){ |
| 6102 #if WHERETRACE_ENABLED |
| 6103 u16 n = pBuilder->pOrSet->n; |
| 6104 int x = |
| 6105 #endif |
| 6106 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 6107 pTemplate->nOut); |
| 6108 #if WHERETRACE_ENABLED /* 0x8 */ |
| 6109 if( sqlite3WhereTrace & 0x8 ){ |
| 6110 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
| 6111 whereLoopPrint(pTemplate, pBuilder->pWC); |
| 6112 } |
| 6113 #endif |
| 6114 } |
| 6115 return SQLITE_OK; |
| 6116 } |
| 6117 |
| 6118 /* Look for an existing WhereLoop to replace with pTemplate |
| 6119 */ |
| 6120 whereLoopAdjustCost(pWInfo->pLoops, pTemplate); |
| 6121 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); |
| 6122 |
| 6123 if( ppPrev==0 ){ |
| 6124 /* There already exists a WhereLoop on the list that is better |
| 6125 ** than pTemplate, so just ignore pTemplate */ |
| 6126 #if WHERETRACE_ENABLED /* 0x8 */ |
| 6127 if( sqlite3WhereTrace & 0x8 ){ |
| 6128 sqlite3DebugPrintf(" skip: "); |
| 6129 whereLoopPrint(pTemplate, pBuilder->pWC); |
| 6130 } |
| 6131 #endif |
| 6132 return SQLITE_OK; |
| 6133 }else{ |
| 6134 p = *ppPrev; |
| 6135 } |
| 6136 |
| 6137 /* If we reach this point it means that either p[] should be overwritten |
| 6138 ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 6139 ** WhereLoop and insert it. |
| 6140 */ |
| 6141 #if WHERETRACE_ENABLED /* 0x8 */ |
| 6142 if( sqlite3WhereTrace & 0x8 ){ |
| 6143 if( p!=0 ){ |
| 6144 sqlite3DebugPrintf("replace: "); |
| 6145 whereLoopPrint(p, pBuilder->pWC); |
| 6146 } |
| 6147 sqlite3DebugPrintf(" add: "); |
| 6148 whereLoopPrint(pTemplate, pBuilder->pWC); |
| 6149 } |
| 6150 #endif |
| 6151 if( p==0 ){ |
| 6152 /* Allocate a new WhereLoop to add to the end of the list */ |
| 6153 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop)); |
| 6154 if( p==0 ) return SQLITE_NOMEM_BKPT; |
| 6155 whereLoopInit(p); |
| 6156 p->pNextLoop = 0; |
| 6157 }else{ |
| 6158 /* We will be overwriting WhereLoop p[]. But before we do, first |
| 6159 ** go through the rest of the list and delete any other entries besides |
| 6160 ** p[] that are also supplated by pTemplate */ |
| 6161 WhereLoop **ppTail = &p->pNextLoop; |
| 6162 WhereLoop *pToDel; |
| 6163 while( *ppTail ){ |
| 6164 ppTail = whereLoopFindLesser(ppTail, pTemplate); |
| 6165 if( ppTail==0 ) break; |
| 6166 pToDel = *ppTail; |
| 6167 if( pToDel==0 ) break; |
| 6168 *ppTail = pToDel->pNextLoop; |
| 6169 #if WHERETRACE_ENABLED /* 0x8 */ |
| 6170 if( sqlite3WhereTrace & 0x8 ){ |
| 6171 sqlite3DebugPrintf(" delete: "); |
| 6172 whereLoopPrint(pToDel, pBuilder->pWC); |
| 6173 } |
| 6174 #endif |
| 6175 whereLoopDelete(db, pToDel); |
| 6176 } |
| 6177 } |
| 6178 rc = whereLoopXfer(db, p, pTemplate); |
| 6179 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
| 6180 Index *pIndex = p->u.btree.pIndex; |
| 6181 if( pIndex && pIndex->tnum==0 ){ |
| 6182 p->u.btree.pIndex = 0; |
| 6183 } |
| 6184 } |
| 6185 return rc; |
| 6186 } |
| 6187 |
| 6188 /* |
| 6189 ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 6190 ** WHERE clause that reference the loop but which are not used by an |
| 6191 ** index. |
| 6192 * |
| 6193 ** For every WHERE clause term that is not used by the index |
| 6194 ** and which has a truth probability assigned by one of the likelihood(), |
| 6195 ** likely(), or unlikely() SQL functions, reduce the estimated number |
| 6196 ** of output rows by the probability specified. |
| 6197 ** |
| 6198 ** TUNING: For every WHERE clause term that is not used by the index |
| 6199 ** and which does not have an assigned truth probability, heuristics |
| 6200 ** described below are used to try to estimate the truth probability. |
| 6201 ** TODO --> Perhaps this is something that could be improved by better |
| 6202 ** table statistics. |
| 6203 ** |
| 6204 ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% |
| 6205 ** value corresponds to -1 in LogEst notation, so this means decrement |
| 6206 ** the WhereLoop.nOut field for every such WHERE clause term. |
| 6207 ** |
| 6208 ** Heuristic 2: If there exists one or more WHERE clause terms of the |
| 6209 ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the |
| 6210 ** final output row estimate is no greater than 1/4 of the total number |
| 6211 ** of rows in the table. In other words, assume that x==EXPR will filter |
| 6212 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the |
| 6213 ** "x" column is boolean or else -1 or 0 or 1 is a common default value |
| 6214 ** on the "x" column and so in that case only cap the output row estimate |
| 6215 ** at 1/2 instead of 1/4. |
| 6216 */ |
| 6217 static void whereLoopOutputAdjust( |
| 6218 WhereClause *pWC, /* The WHERE clause */ |
| 6219 WhereLoop *pLoop, /* The loop to adjust downward */ |
| 6220 LogEst nRow /* Number of rows in the entire table */ |
| 6221 ){ |
| 6222 WhereTerm *pTerm, *pX; |
| 6223 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
| 6224 int i, j, k; |
| 6225 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ |
| 6226 |
| 6227 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 6228 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
| 6229 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
| 6230 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 6231 if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
| 6232 for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 6233 pX = pLoop->aLTerm[j]; |
| 6234 if( pX==0 ) continue; |
| 6235 if( pX==pTerm ) break; |
| 6236 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 6237 } |
| 6238 if( j<0 ){ |
| 6239 if( pTerm->truthProb<=0 ){ |
| 6240 /* If a truth probability is specified using the likelihood() hints, |
| 6241 ** then use the probability provided by the application. */ |
| 6242 pLoop->nOut += pTerm->truthProb; |
| 6243 }else{ |
| 6244 /* In the absence of explicit truth probabilities, use heuristics to |
| 6245 ** guess a reasonable truth probability. */ |
| 6246 pLoop->nOut--; |
| 6247 if( pTerm->eOperator&(WO_EQ|WO_IS) ){ |
| 6248 Expr *pRight = pTerm->pExpr->pRight; |
| 6249 testcase( pTerm->pExpr->op==TK_IS ); |
| 6250 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ |
| 6251 k = 10; |
| 6252 }else{ |
| 6253 k = 20; |
| 6254 } |
| 6255 if( iReduce<k ) iReduce = k; |
| 6256 } |
| 6257 } |
| 6258 } |
| 6259 } |
| 6260 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; |
| 6261 } |
| 6262 |
| 6263 /* |
| 6264 ** Term pTerm is a vector range comparison operation. The first comparison |
| 6265 ** in the vector can be optimized using column nEq of the index. This |
| 6266 ** function returns the total number of vector elements that can be used |
| 6267 ** as part of the range comparison. |
| 6268 ** |
| 6269 ** For example, if the query is: |
| 6270 ** |
| 6271 ** WHERE a = ? AND (b, c, d) > (?, ?, ?) |
| 6272 ** |
| 6273 ** and the index: |
| 6274 ** |
| 6275 ** CREATE INDEX ... ON (a, b, c, d, e) |
| 6276 ** |
| 6277 ** then this function would be invoked with nEq=1. The value returned in |
| 6278 ** this case is 3. |
| 6279 */ |
| 6280 static int whereRangeVectorLen( |
| 6281 Parse *pParse, /* Parsing context */ |
| 6282 int iCur, /* Cursor open on pIdx */ |
| 6283 Index *pIdx, /* The index to be used for a inequality constraint */ |
| 6284 int nEq, /* Number of prior equality constraints on same index */ |
| 6285 WhereTerm *pTerm /* The vector inequality constraint */ |
| 6286 ){ |
| 6287 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft); |
| 6288 int i; |
| 6289 |
| 6290 nCmp = MIN(nCmp, (pIdx->nColumn - nEq)); |
| 6291 for(i=1; i<nCmp; i++){ |
| 6292 /* Test if comparison i of pTerm is compatible with column (i+nEq) |
| 6293 ** of the index. If not, exit the loop. */ |
| 6294 char aff; /* Comparison affinity */ |
| 6295 char idxaff = 0; /* Indexed columns affinity */ |
| 6296 CollSeq *pColl; /* Comparison collation sequence */ |
| 6297 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr; |
| 6298 Expr *pRhs = pTerm->pExpr->pRight; |
| 6299 if( pRhs->flags & EP_xIsSelect ){ |
| 6300 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr; |
| 6301 }else{ |
| 6302 pRhs = pRhs->x.pList->a[i].pExpr; |
| 6303 } |
| 6304 |
| 6305 /* Check that the LHS of the comparison is a column reference to |
| 6306 ** the right column of the right source table. And that the sort |
| 6307 ** order of the index column is the same as the sort order of the |
| 6308 ** leftmost index column. */ |
| 6309 if( pLhs->op!=TK_COLUMN |
| 6310 || pLhs->iTable!=iCur |
| 6311 || pLhs->iColumn!=pIdx->aiColumn[i+nEq] |
| 6312 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq] |
| 6313 ){ |
| 6314 break; |
| 6315 } |
| 6316 |
| 6317 testcase( pLhs->iColumn==XN_ROWID ); |
| 6318 aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs)); |
| 6319 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn); |
| 6320 if( aff!=idxaff ) break; |
| 6321 |
| 6322 pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
| 6323 if( pColl==0 ) break; |
| 6324 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break; |
| 6325 } |
| 6326 return i; |
| 6327 } |
| 6328 |
| 6329 /* |
| 6330 ** Adjust the cost C by the costMult facter T. This only occurs if |
| 6331 ** compiled with -DSQLITE_ENABLE_COSTMULT |
| 6332 */ |
| 6333 #ifdef SQLITE_ENABLE_COSTMULT |
| 6334 # define ApplyCostMultiplier(C,T) C += T |
| 6335 #else |
| 6336 # define ApplyCostMultiplier(C,T) |
| 6337 #endif |
| 6338 |
| 6339 /* |
| 6340 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the |
| 6341 ** index pIndex. Try to match one more. |
| 6342 ** |
| 6343 ** When this function is called, pBuilder->pNew->nOut contains the |
| 6344 ** number of rows expected to be visited by filtering using the nEq |
| 6345 ** terms only. If it is modified, this value is restored before this |
| 6346 ** function returns. |
| 6347 ** |
| 6348 ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 6349 ** INTEGER PRIMARY KEY. |
| 6350 */ |
| 6351 static int whereLoopAddBtreeIndex( |
| 6352 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 6353 struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 6354 Index *pProbe, /* An index on pSrc */ |
| 6355 LogEst nInMul /* log(Number of iterations due to IN) */ |
| 6356 ){ |
| 6357 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 6358 Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 6359 sqlite3 *db = pParse->db; /* Database connection malloc context */ |
| 6360 WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 6361 WhereTerm *pTerm; /* A WhereTerm under consideration */ |
| 6362 int opMask; /* Valid operators for constraints */ |
| 6363 WhereScan scan; /* Iterator for WHERE terms */ |
| 6364 Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 6365 u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
| 6366 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
| 6367 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */ |
| 6368 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */ |
| 6369 u16 saved_nSkip; /* Original value of pNew->nSkip */ |
| 6370 u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
| 6371 LogEst saved_nOut; /* Original value of pNew->nOut */ |
| 6372 int rc = SQLITE_OK; /* Return code */ |
| 6373 LogEst rSize; /* Number of rows in the table */ |
| 6374 LogEst rLogSize; /* Logarithm of table size */ |
| 6375 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
| 6376 |
| 6377 pNew = pBuilder->pNew; |
| 6378 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; |
| 6379 WHERETRACE(0x800, ("BEGIN addBtreeIdx(%s), nEq=%d\n", |
| 6380 pProbe->zName, pNew->u.btree.nEq)); |
| 6381 |
| 6382 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 6383 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 6384 if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 6385 opMask = WO_LT|WO_LE; |
| 6386 }else{ |
| 6387 assert( pNew->u.btree.nBtm==0 ); |
| 6388 opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; |
| 6389 } |
| 6390 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
| 6391 |
| 6392 assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 6393 |
| 6394 saved_nEq = pNew->u.btree.nEq; |
| 6395 saved_nBtm = pNew->u.btree.nBtm; |
| 6396 saved_nTop = pNew->u.btree.nTop; |
| 6397 saved_nSkip = pNew->nSkip; |
| 6398 saved_nLTerm = pNew->nLTerm; |
| 6399 saved_wsFlags = pNew->wsFlags; |
| 6400 saved_prereq = pNew->prereq; |
| 6401 saved_nOut = pNew->nOut; |
| 6402 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq, |
| 6403 opMask, pProbe); |
| 6404 pNew->rSetup = 0; |
| 6405 rSize = pProbe->aiRowLogEst[0]; |
| 6406 rLogSize = estLog(rSize); |
| 6407 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
| 6408 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
| 6409 LogEst rCostIdx; |
| 6410 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
| 6411 int nIn = 0; |
| 6412 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 6413 int nRecValid = pBuilder->nRecValid; |
| 6414 #endif |
| 6415 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
| 6416 && indexColumnNotNull(pProbe, saved_nEq) |
| 6417 ){ |
| 6418 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 6419 } |
| 6420 if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 6421 |
| 6422 /* Do not allow the upper bound of a LIKE optimization range constraint |
| 6423 ** to mix with a lower range bound from some other source */ |
| 6424 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; |
| 6425 |
| 6426 /* Do not allow IS constraints from the WHERE clause to be used by the |
| 6427 ** right table of a LEFT JOIN. Only constraints in the ON clause are |
| 6428 ** allowed */ |
| 6429 if( (pSrc->fg.jointype & JT_LEFT)!=0 |
| 6430 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 6431 && (eOp & (WO_IS|WO_ISNULL))!=0 |
| 6432 ){ |
| 6433 testcase( eOp & WO_IS ); |
| 6434 testcase( eOp & WO_ISNULL ); |
| 6435 continue; |
| 6436 } |
| 6437 |
| 6438 pNew->wsFlags = saved_wsFlags; |
| 6439 pNew->u.btree.nEq = saved_nEq; |
| 6440 pNew->u.btree.nBtm = saved_nBtm; |
| 6441 pNew->u.btree.nTop = saved_nTop; |
| 6442 pNew->nLTerm = saved_nLTerm; |
| 6443 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 6444 pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 6445 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
| 6446 |
| 6447 assert( nInMul==0 |
| 6448 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| 6449 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 |
| 6450 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 |
| 6451 ); |
| 6452 |
| 6453 if( eOp & WO_IN ){ |
| 6454 Expr *pExpr = pTerm->pExpr; |
| 6455 pNew->wsFlags |= WHERE_COLUMN_IN; |
| 6456 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 6457 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
| 6458 int i; |
| 6459 nIn = 46; assert( 46==sqlite3LogEst(25) ); |
| 6460 |
| 6461 /* The expression may actually be of the form (x, y) IN (SELECT...). |
| 6462 ** In this case there is a separate term for each of (x) and (y). |
| 6463 ** However, the nIn multiplier should only be applied once, not once |
| 6464 ** for each such term. The following loop checks that pTerm is the |
| 6465 ** first such term in use, and sets nIn back to 0 if it is not. */ |
| 6466 for(i=0; i<pNew->nLTerm-1; i++){ |
| 6467 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0; |
| 6468 } |
| 6469 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 6470 /* "x IN (value, value, ...)" */ |
| 6471 nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
| 6472 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 6473 ** changes "x IN (?)" into "x=?". */ |
| 6474 } |
| 6475 }else if( eOp & (WO_EQ|WO_IS) ){ |
| 6476 int iCol = pProbe->aiColumn[saved_nEq]; |
| 6477 pNew->wsFlags |= WHERE_COLUMN_EQ; |
| 6478 assert( saved_nEq==pNew->u.btree.nEq ); |
| 6479 if( iCol==XN_ROWID |
| 6480 || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1) |
| 6481 ){ |
| 6482 if( iCol>=0 && pProbe->uniqNotNull==0 ){ |
| 6483 pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 6484 }else{ |
| 6485 pNew->wsFlags |= WHERE_ONEROW; |
| 6486 } |
| 6487 } |
| 6488 }else if( eOp & WO_ISNULL ){ |
| 6489 pNew->wsFlags |= WHERE_COLUMN_NULL; |
| 6490 }else if( eOp & (WO_GT|WO_GE) ){ |
| 6491 testcase( eOp & WO_GT ); |
| 6492 testcase( eOp & WO_GE ); |
| 6493 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
| 6494 pNew->u.btree.nBtm = whereRangeVectorLen( |
| 6495 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 6496 ); |
| 6497 pBtm = pTerm; |
| 6498 pTop = 0; |
| 6499 if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 6500 /* Range contraints that come from the LIKE optimization are |
| 6501 ** always used in pairs. */ |
| 6502 pTop = &pTerm[1]; |
| 6503 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 6504 assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 6505 assert( pTop->eOperator==WO_LT ); |
| 6506 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 6507 pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 6508 pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 6509 pNew->u.btree.nTop = 1; |
| 6510 } |
| 6511 }else{ |
| 6512 assert( eOp & (WO_LT|WO_LE) ); |
| 6513 testcase( eOp & WO_LT ); |
| 6514 testcase( eOp & WO_LE ); |
| 6515 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
| 6516 pNew->u.btree.nTop = whereRangeVectorLen( |
| 6517 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm |
| 6518 ); |
| 6519 pTop = pTerm; |
| 6520 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
| 6521 pNew->aLTerm[pNew->nLTerm-2] : 0; |
| 6522 } |
| 6523 |
| 6524 /* At this point pNew->nOut is set to the number of rows expected to |
| 6525 ** be visited by the index scan before considering term pTerm, or the |
| 6526 ** values of nIn and nInMul. In other words, assuming that all |
| 6527 ** "x IN(...)" terms are replaced with "x = ?". This block updates |
| 6528 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ |
| 6529 assert( pNew->nOut==saved_nOut ); |
| 6530 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 6531 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 |
| 6532 ** data, using some other estimate. */ |
| 6533 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
| 6534 }else{ |
| 6535 int nEq = ++pNew->u.btree.nEq; |
| 6536 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) ); |
| 6537 |
| 6538 assert( pNew->nOut==saved_nOut ); |
| 6539 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){ |
| 6540 assert( (eOp & WO_IN) || nIn==0 ); |
| 6541 testcase( eOp & WO_IN ); |
| 6542 pNew->nOut += pTerm->truthProb; |
| 6543 pNew->nOut -= nIn; |
| 6544 }else{ |
| 6545 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 6546 tRowcnt nOut = 0; |
| 6547 if( nInMul==0 |
| 6548 && pProbe->nSample |
| 6549 && pNew->u.btree.nEq<=pProbe->nSampleCol |
| 6550 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
| 6551 ){ |
| 6552 Expr *pExpr = pTerm->pExpr; |
| 6553 if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ |
| 6554 testcase( eOp & WO_EQ ); |
| 6555 testcase( eOp & WO_IS ); |
| 6556 testcase( eOp & WO_ISNULL ); |
| 6557 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
| 6558 }else{ |
| 6559 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
| 6560 } |
| 6561 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 6562 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ |
| 6563 if( nOut ){ |
| 6564 pNew->nOut = sqlite3LogEst(nOut); |
| 6565 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; |
| 6566 pNew->nOut -= nIn; |
| 6567 } |
| 6568 } |
| 6569 if( nOut==0 ) |
| 6570 #endif |
| 6571 { |
| 6572 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); |
| 6573 if( eOp & WO_ISNULL ){ |
| 6574 /* TUNING: If there is no likelihood() value, assume that a |
| 6575 ** "col IS NULL" expression matches twice as many rows |
| 6576 ** as (col=?). */ |
| 6577 pNew->nOut += 10; |
| 6578 } |
| 6579 } |
| 6580 } |
| 6581 } |
| 6582 |
| 6583 /* Set rCostIdx to the cost of visiting selected rows in index. Add |
| 6584 ** it to pNew->rRun, which is currently set to the cost of the index |
| 6585 ** seek only. Then, if this is a non-covering index, add the cost of |
| 6586 ** visiting the rows in the main table. */ |
| 6587 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; |
| 6588 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); |
| 6589 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
| 6590 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); |
| 6591 } |
| 6592 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); |
| 6593 |
| 6594 nOutUnadjusted = pNew->nOut; |
| 6595 pNew->rRun += nInMul + nIn; |
| 6596 pNew->nOut += nInMul + nIn; |
| 6597 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize); |
| 6598 rc = whereLoopInsert(pBuilder, pNew); |
| 6599 |
| 6600 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 6601 pNew->nOut = saved_nOut; |
| 6602 }else{ |
| 6603 pNew->nOut = nOutUnadjusted; |
| 6604 } |
| 6605 |
| 6606 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
| 6607 && pNew->u.btree.nEq<pProbe->nColumn |
| 6608 ){ |
| 6609 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
| 6610 } |
| 6611 pNew->nOut = saved_nOut; |
| 6612 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 6613 pBuilder->nRecValid = nRecValid; |
| 6614 #endif |
| 6615 } |
| 6616 pNew->prereq = saved_prereq; |
| 6617 pNew->u.btree.nEq = saved_nEq; |
| 6618 pNew->u.btree.nBtm = saved_nBtm; |
| 6619 pNew->u.btree.nTop = saved_nTop; |
| 6620 pNew->nSkip = saved_nSkip; |
| 6621 pNew->wsFlags = saved_wsFlags; |
| 6622 pNew->nOut = saved_nOut; |
| 6623 pNew->nLTerm = saved_nLTerm; |
| 6624 |
| 6625 /* Consider using a skip-scan if there are no WHERE clause constraints |
| 6626 ** available for the left-most terms of the index, and if the average |
| 6627 ** number of repeats in the left-most terms is at least 18. |
| 6628 ** |
| 6629 ** The magic number 18 is selected on the basis that scanning 17 rows |
| 6630 ** is almost always quicker than an index seek (even though if the index |
| 6631 ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 6632 ** the code). And, even if it is not, it should not be too much slower. |
| 6633 ** On the other hand, the extra seeks could end up being significantly |
| 6634 ** more expensive. */ |
| 6635 assert( 42==sqlite3LogEst(18) ); |
| 6636 if( saved_nEq==saved_nSkip |
| 6637 && saved_nEq+1<pProbe->nKeyCol |
| 6638 && pProbe->noSkipScan==0 |
| 6639 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 6640 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 6641 ){ |
| 6642 LogEst nIter; |
| 6643 pNew->u.btree.nEq++; |
| 6644 pNew->nSkip++; |
| 6645 pNew->aLTerm[pNew->nLTerm++] = 0; |
| 6646 pNew->wsFlags |= WHERE_SKIPSCAN; |
| 6647 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
| 6648 pNew->nOut -= nIter; |
| 6649 /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 6650 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 6651 nIter += 5; |
| 6652 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 6653 pNew->nOut = saved_nOut; |
| 6654 pNew->u.btree.nEq = saved_nEq; |
| 6655 pNew->nSkip = saved_nSkip; |
| 6656 pNew->wsFlags = saved_wsFlags; |
| 6657 } |
| 6658 |
| 6659 WHERETRACE(0x800, ("END addBtreeIdx(%s), nEq=%d, rc=%d\n", |
| 6660 pProbe->zName, saved_nEq, rc)); |
| 6661 return rc; |
| 6662 } |
| 6663 |
| 6664 /* |
| 6665 ** Return True if it is possible that pIndex might be useful in |
| 6666 ** implementing the ORDER BY clause in pBuilder. |
| 6667 ** |
| 6668 ** Return False if pBuilder does not contain an ORDER BY clause or |
| 6669 ** if there is no way for pIndex to be useful in implementing that |
| 6670 ** ORDER BY clause. |
| 6671 */ |
| 6672 static int indexMightHelpWithOrderBy( |
| 6673 WhereLoopBuilder *pBuilder, |
| 6674 Index *pIndex, |
| 6675 int iCursor |
| 6676 ){ |
| 6677 ExprList *pOB; |
| 6678 ExprList *aColExpr; |
| 6679 int ii, jj; |
| 6680 |
| 6681 if( pIndex->bUnordered ) return 0; |
| 6682 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
| 6683 for(ii=0; ii<pOB->nExpr; ii++){ |
| 6684 Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
| 6685 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){ |
| 6686 if( pExpr->iColumn<0 ) return 1; |
| 6687 for(jj=0; jj<pIndex->nKeyCol; jj++){ |
| 6688 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 6689 } |
| 6690 }else if( (aColExpr = pIndex->aColExpr)!=0 ){ |
| 6691 for(jj=0; jj<pIndex->nKeyCol; jj++){ |
| 6692 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue; |
| 6693 if( sqlite3ExprCompare(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){ |
| 6694 return 1; |
| 6695 } |
| 6696 } |
| 6697 } |
| 6698 } |
| 6699 return 0; |
| 6700 } |
| 6701 |
| 6702 /* |
| 6703 ** Return a bitmask where 1s indicate that the corresponding column of |
| 6704 ** the table is used by an index. Only the first 63 columns are considered. |
| 6705 */ |
| 6706 static Bitmask columnsInIndex(Index *pIdx){ |
| 6707 Bitmask m = 0; |
| 6708 int j; |
| 6709 for(j=pIdx->nColumn-1; j>=0; j--){ |
| 6710 int x = pIdx->aiColumn[j]; |
| 6711 if( x>=0 ){ |
| 6712 testcase( x==BMS-1 ); |
| 6713 testcase( x==BMS-2 ); |
| 6714 if( x<BMS-1 ) m |= MASKBIT(x); |
| 6715 } |
| 6716 } |
| 6717 return m; |
| 6718 } |
| 6719 |
| 6720 /* Check to see if a partial index with pPartIndexWhere can be used |
| 6721 ** in the current query. Return true if it can be and false if not. |
| 6722 */ |
| 6723 static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 6724 int i; |
| 6725 WhereTerm *pTerm; |
| 6726 while( pWhere->op==TK_AND ){ |
| 6727 if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0; |
| 6728 pWhere = pWhere->pRight; |
| 6729 } |
| 6730 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 6731 Expr *pExpr = pTerm->pExpr; |
| 6732 if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab) |
| 6733 && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) |
| 6734 ){ |
| 6735 return 1; |
| 6736 } |
| 6737 } |
| 6738 return 0; |
| 6739 } |
| 6740 |
| 6741 /* |
| 6742 ** Add all WhereLoop objects for a single table of the join where the table |
| 6743 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be |
| 6744 ** a b-tree table, not a virtual table. |
| 6745 ** |
| 6746 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function |
| 6747 ** are calculated as follows: |
| 6748 ** |
| 6749 ** For a full scan, assuming the table (or index) contains nRow rows: |
| 6750 ** |
| 6751 ** cost = nRow * 3.0 // full-table scan |
| 6752 ** cost = nRow * K // scan of covering index |
| 6753 ** cost = nRow * (K+3.0) // scan of non-covering index |
| 6754 ** |
| 6755 ** where K is a value between 1.1 and 3.0 set based on the relative |
| 6756 ** estimated average size of the index and table records. |
| 6757 ** |
| 6758 ** For an index scan, where nVisit is the number of index rows visited |
| 6759 ** by the scan, and nSeek is the number of seek operations required on |
| 6760 ** the index b-tree: |
| 6761 ** |
| 6762 ** cost = nSeek * (log(nRow) + K * nVisit) // covering index |
| 6763 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index |
| 6764 ** |
| 6765 ** Normally, nSeek is 1. nSeek values greater than 1 come about if the |
| 6766 ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when |
| 6767 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. |
| 6768 ** |
| 6769 ** The estimated values (nRow, nVisit, nSeek) often contain a large amount |
| 6770 ** of uncertainty. For this reason, scoring is designed to pick plans that |
| 6771 ** "do the least harm" if the estimates are inaccurate. For example, a |
| 6772 ** log(nRow) factor is omitted from a non-covering index scan in order to |
| 6773 ** bias the scoring in favor of using an index, since the worst-case |
| 6774 ** performance of using an index is far better than the worst-case performance |
| 6775 ** of a full table scan. |
| 6776 */ |
| 6777 static int whereLoopAddBtree( |
| 6778 WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 6779 Bitmask mPrereq /* Extra prerequesites for using this table */ |
| 6780 ){ |
| 6781 WhereInfo *pWInfo; /* WHERE analysis context */ |
| 6782 Index *pProbe; /* An index we are evaluating */ |
| 6783 Index sPk; /* A fake index object for the primary key */ |
| 6784 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ |
| 6785 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
| 6786 SrcList *pTabList; /* The FROM clause */ |
| 6787 struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
| 6788 WhereLoop *pNew; /* Template WhereLoop object */ |
| 6789 int rc = SQLITE_OK; /* Return code */ |
| 6790 int iSortIdx = 1; /* Index number */ |
| 6791 int b; /* A boolean value */ |
| 6792 LogEst rSize; /* number of rows in the table */ |
| 6793 LogEst rLogSize; /* Logarithm of the number of rows in the table */ |
| 6794 WhereClause *pWC; /* The parsed WHERE clause */ |
| 6795 Table *pTab; /* Table being queried */ |
| 6796 |
| 6797 pNew = pBuilder->pNew; |
| 6798 pWInfo = pBuilder->pWInfo; |
| 6799 pTabList = pWInfo->pTabList; |
| 6800 pSrc = pTabList->a + pNew->iTab; |
| 6801 pTab = pSrc->pTab; |
| 6802 pWC = pBuilder->pWC; |
| 6803 assert( !IsVirtual(pSrc->pTab) ); |
| 6804 |
| 6805 if( pSrc->pIBIndex ){ |
| 6806 /* An INDEXED BY clause specifies a particular index to use */ |
| 6807 pProbe = pSrc->pIBIndex; |
| 6808 }else if( !HasRowid(pTab) ){ |
| 6809 pProbe = pTab->pIndex; |
| 6810 }else{ |
| 6811 /* There is no INDEXED BY clause. Create a fake Index object in local |
| 6812 ** variable sPk to represent the rowid primary key index. Make this |
| 6813 ** fake index the first in a chain of Index objects with all of the real |
| 6814 ** indices to follow */ |
| 6815 Index *pFirst; /* First of real indices on the table */ |
| 6816 memset(&sPk, 0, sizeof(Index)); |
| 6817 sPk.nKeyCol = 1; |
| 6818 sPk.nColumn = 1; |
| 6819 sPk.aiColumn = &aiColumnPk; |
| 6820 sPk.aiRowLogEst = aiRowEstPk; |
| 6821 sPk.onError = OE_Replace; |
| 6822 sPk.pTable = pTab; |
| 6823 sPk.szIdxRow = pTab->szTabRow; |
| 6824 aiRowEstPk[0] = pTab->nRowLogEst; |
| 6825 aiRowEstPk[1] = 0; |
| 6826 pFirst = pSrc->pTab->pIndex; |
| 6827 if( pSrc->fg.notIndexed==0 ){ |
| 6828 /* The real indices of the table are only considered if the |
| 6829 ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 6830 sPk.pNext = pFirst; |
| 6831 } |
| 6832 pProbe = &sPk; |
| 6833 } |
| 6834 rSize = pTab->nRowLogEst; |
| 6835 rLogSize = estLog(rSize); |
| 6836 |
| 6837 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 6838 /* Automatic indexes */ |
| 6839 if( !pBuilder->pOrSet /* Not part of an OR optimization */ |
| 6840 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 6841 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 6842 && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */ |
| 6843 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */ |
| 6844 && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */ |
| 6845 && !pSrc->fg.isCorrelated /* Not a correlated subquery */ |
| 6846 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */ |
| 6847 ){ |
| 6848 /* Generate auto-index WhereLoops */ |
| 6849 WhereTerm *pTerm; |
| 6850 WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 6851 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
| 6852 if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 6853 if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 6854 pNew->u.btree.nEq = 1; |
| 6855 pNew->nSkip = 0; |
| 6856 pNew->u.btree.pIndex = 0; |
| 6857 pNew->nLTerm = 1; |
| 6858 pNew->aLTerm[0] = pTerm; |
| 6859 /* TUNING: One-time cost for computing the automatic index is |
| 6860 ** estimated to be X*N*log2(N) where N is the number of rows in |
| 6861 ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 6862 ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 6863 ** of X is smaller for views and subqueries so that the query planner |
| 6864 ** will be more aggressive about generating automatic indexes for |
| 6865 ** those objects, since there is no opportunity to add schema |
| 6866 ** indexes on subqueries and views. */ |
| 6867 pNew->rSetup = rLogSize + rSize + 4; |
| 6868 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 6869 pNew->rSetup += 24; |
| 6870 } |
| 6871 ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
| 6872 if( pNew->rSetup<0 ) pNew->rSetup = 0; |
| 6873 /* TUNING: Each index lookup yields 20 rows in the table. This |
| 6874 ** is more than the usual guess of 10 rows, since we have no way |
| 6875 ** of knowing how selective the index will ultimately be. It would |
| 6876 ** not be unreasonable to make this value much larger. */ |
| 6877 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
| 6878 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
| 6879 pNew->wsFlags = WHERE_AUTO_INDEX; |
| 6880 pNew->prereq = mPrereq | pTerm->prereqRight; |
| 6881 rc = whereLoopInsert(pBuilder, pNew); |
| 6882 } |
| 6883 } |
| 6884 } |
| 6885 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
| 6886 |
| 6887 /* Loop over all indices |
| 6888 */ |
| 6889 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
| 6890 if( pProbe->pPartIdxWhere!=0 |
| 6891 && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ |
| 6892 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
| 6893 continue; /* Partial index inappropriate for this query */ |
| 6894 } |
| 6895 rSize = pProbe->aiRowLogEst[0]; |
| 6896 pNew->u.btree.nEq = 0; |
| 6897 pNew->u.btree.nBtm = 0; |
| 6898 pNew->u.btree.nTop = 0; |
| 6899 pNew->nSkip = 0; |
| 6900 pNew->nLTerm = 0; |
| 6901 pNew->iSortIdx = 0; |
| 6902 pNew->rSetup = 0; |
| 6903 pNew->prereq = mPrereq; |
| 6904 pNew->nOut = rSize; |
| 6905 pNew->u.btree.pIndex = pProbe; |
| 6906 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
| 6907 /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 6908 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
| 6909 if( pProbe->tnum<=0 ){ |
| 6910 /* Integer primary key index */ |
| 6911 pNew->wsFlags = WHERE_IPK; |
| 6912 |
| 6913 /* Full table scan */ |
| 6914 pNew->iSortIdx = b ? iSortIdx : 0; |
| 6915 /* TUNING: Cost of full table scan is (N*3.0). */ |
| 6916 pNew->rRun = rSize + 16; |
| 6917 ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
| 6918 whereLoopOutputAdjust(pWC, pNew, rSize); |
| 6919 rc = whereLoopInsert(pBuilder, pNew); |
| 6920 pNew->nOut = rSize; |
| 6921 if( rc ) break; |
| 6922 }else{ |
| 6923 Bitmask m; |
| 6924 if( pProbe->isCovering ){ |
| 6925 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 6926 m = 0; |
| 6927 }else{ |
| 6928 m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 6929 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 6930 } |
| 6931 |
| 6932 /* Full scan via index */ |
| 6933 if( b |
| 6934 || !HasRowid(pTab) |
| 6935 || pProbe->pPartIdxWhere!=0 |
| 6936 || ( m==0 |
| 6937 && pProbe->bUnordered==0 |
| 6938 && (pProbe->szIdxRow<pTab->szTabRow) |
| 6939 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 6940 && sqlite3GlobalConfig.bUseCis |
| 6941 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 6942 ) |
| 6943 ){ |
| 6944 pNew->iSortIdx = b ? iSortIdx : 0; |
| 6945 |
| 6946 /* The cost of visiting the index rows is N*K, where K is |
| 6947 ** between 1.1 and 3.0, depending on the relative sizes of the |
| 6948 ** index and table rows. */ |
| 6949 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; |
| 6950 if( m!=0 ){ |
| 6951 /* If this is a non-covering index scan, add in the cost of |
| 6952 ** doing table lookups. The cost will be 3x the number of |
| 6953 ** lookups. Take into account WHERE clause terms that can be |
| 6954 ** satisfied using just the index, and that do not require a |
| 6955 ** table lookup. */ |
| 6956 LogEst nLookup = rSize + 16; /* Base cost: N*3 */ |
| 6957 int ii; |
| 6958 int iCur = pSrc->iCursor; |
| 6959 WhereClause *pWC2 = &pWInfo->sWC; |
| 6960 for(ii=0; ii<pWC2->nTerm; ii++){ |
| 6961 WhereTerm *pTerm = &pWC2->a[ii]; |
| 6962 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){ |
| 6963 break; |
| 6964 } |
| 6965 /* pTerm can be evaluated using just the index. So reduce |
| 6966 ** the expected number of table lookups accordingly */ |
| 6967 if( pTerm->truthProb<=0 ){ |
| 6968 nLookup += pTerm->truthProb; |
| 6969 }else{ |
| 6970 nLookup--; |
| 6971 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19; |
| 6972 } |
| 6973 } |
| 6974 |
| 6975 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup); |
| 6976 } |
| 6977 ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
| 6978 whereLoopOutputAdjust(pWC, pNew, rSize); |
| 6979 rc = whereLoopInsert(pBuilder, pNew); |
| 6980 pNew->nOut = rSize; |
| 6981 if( rc ) break; |
| 6982 } |
| 6983 } |
| 6984 |
| 6985 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
| 6986 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 6987 sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 6988 pBuilder->nRecValid = 0; |
| 6989 pBuilder->pRec = 0; |
| 6990 #endif |
| 6991 |
| 6992 /* If there was an INDEXED BY clause, then only that one index is |
| 6993 ** considered. */ |
| 6994 if( pSrc->pIBIndex ) break; |
| 6995 } |
| 6996 return rc; |
| 6997 } |
| 6998 |
| 6999 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 7000 |
| 7001 /* |
| 7002 ** Argument pIdxInfo is already populated with all constraints that may |
| 7003 ** be used by the virtual table identified by pBuilder->pNew->iTab. This |
| 7004 ** function marks a subset of those constraints usable, invokes the |
| 7005 ** xBestIndex method and adds the returned plan to pBuilder. |
| 7006 ** |
| 7007 ** A constraint is marked usable if: |
| 7008 ** |
| 7009 ** * Argument mUsable indicates that its prerequisites are available, and |
| 7010 ** |
| 7011 ** * It is not one of the operators specified in the mExclude mask passed |
| 7012 ** as the fourth argument (which in practice is either WO_IN or 0). |
| 7013 ** |
| 7014 ** Argument mPrereq is a mask of tables that must be scanned before the |
| 7015 ** virtual table in question. These are added to the plans prerequisites |
| 7016 ** before it is added to pBuilder. |
| 7017 ** |
| 7018 ** Output parameter *pbIn is set to true if the plan added to pBuilder |
| 7019 ** uses one or more WO_IN terms, or false otherwise. |
| 7020 */ |
| 7021 static int whereLoopAddVirtualOne( |
| 7022 WhereLoopBuilder *pBuilder, |
| 7023 Bitmask mPrereq, /* Mask of tables that must be used. */ |
| 7024 Bitmask mUsable, /* Mask of usable tables */ |
| 7025 u16 mExclude, /* Exclude terms using these operators */ |
| 7026 sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ |
| 7027 u16 mNoOmit, /* Do not omit these constraints */ |
| 7028 int *pbIn /* OUT: True if plan uses an IN(...) op */ |
| 7029 ){ |
| 7030 WhereClause *pWC = pBuilder->pWC; |
| 7031 struct sqlite3_index_constraint *pIdxCons; |
| 7032 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; |
| 7033 int i; |
| 7034 int mxTerm; |
| 7035 int rc = SQLITE_OK; |
| 7036 WhereLoop *pNew = pBuilder->pNew; |
| 7037 Parse *pParse = pBuilder->pWInfo->pParse; |
| 7038 struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab]; |
| 7039 int nConstraint = pIdxInfo->nConstraint; |
| 7040 |
| 7041 assert( (mUsable & mPrereq)==mPrereq ); |
| 7042 *pbIn = 0; |
| 7043 pNew->prereq = mPrereq; |
| 7044 |
| 7045 /* Set the usable flag on the subset of constraints identified by |
| 7046 ** arguments mUsable and mExclude. */ |
| 7047 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 7048 for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 7049 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset]; |
| 7050 pIdxCons->usable = 0; |
| 7051 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight |
| 7052 && (pTerm->eOperator & mExclude)==0 |
| 7053 ){ |
| 7054 pIdxCons->usable = 1; |
| 7055 } |
| 7056 } |
| 7057 |
| 7058 /* Initialize the output fields of the sqlite3_index_info structure */ |
| 7059 memset(pUsage, 0, sizeof(pUsage[0])*nConstraint); |
| 7060 assert( pIdxInfo->needToFreeIdxStr==0 ); |
| 7061 pIdxInfo->idxStr = 0; |
| 7062 pIdxInfo->idxNum = 0; |
| 7063 pIdxInfo->orderByConsumed = 0; |
| 7064 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
| 7065 pIdxInfo->estimatedRows = 25; |
| 7066 pIdxInfo->idxFlags = 0; |
| 7067 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; |
| 7068 |
| 7069 /* Invoke the virtual table xBestIndex() method */ |
| 7070 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); |
| 7071 if( rc ) return rc; |
| 7072 |
| 7073 mxTerm = -1; |
| 7074 assert( pNew->nLSlot>=nConstraint ); |
| 7075 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
| 7076 pNew->u.vtab.omitMask = 0; |
| 7077 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 7078 for(i=0; i<nConstraint; i++, pIdxCons++){ |
| 7079 int iTerm; |
| 7080 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 7081 WhereTerm *pTerm; |
| 7082 int j = pIdxCons->iTermOffset; |
| 7083 if( iTerm>=nConstraint |
| 7084 || j<0 |
| 7085 || j>=pWC->nTerm |
| 7086 || pNew->aLTerm[iTerm]!=0 |
| 7087 || pIdxCons->usable==0 |
| 7088 ){ |
| 7089 rc = SQLITE_ERROR; |
| 7090 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); |
| 7091 return rc; |
| 7092 } |
| 7093 testcase( iTerm==nConstraint-1 ); |
| 7094 testcase( j==0 ); |
| 7095 testcase( j==pWC->nTerm-1 ); |
| 7096 pTerm = &pWC->a[j]; |
| 7097 pNew->prereq |= pTerm->prereqRight; |
| 7098 assert( iTerm<pNew->nLSlot ); |
| 7099 pNew->aLTerm[iTerm] = pTerm; |
| 7100 if( iTerm>mxTerm ) mxTerm = iTerm; |
| 7101 testcase( iTerm==15 ); |
| 7102 testcase( iTerm==16 ); |
| 7103 if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
| 7104 if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 7105 /* A virtual table that is constrained by an IN clause may not |
| 7106 ** consume the ORDER BY clause because (1) the order of IN terms |
| 7107 ** is not necessarily related to the order of output terms and |
| 7108 ** (2) Multiple outputs from a single IN value will not merge |
| 7109 ** together. */ |
| 7110 pIdxInfo->orderByConsumed = 0; |
| 7111 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; |
| 7112 *pbIn = 1; assert( (mExclude & WO_IN)==0 ); |
| 7113 } |
| 7114 } |
| 7115 } |
| 7116 pNew->u.vtab.omitMask &= ~mNoOmit; |
| 7117 |
| 7118 pNew->nLTerm = mxTerm+1; |
| 7119 assert( pNew->nLTerm<=pNew->nLSlot ); |
| 7120 pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 7121 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 7122 pIdxInfo->needToFreeIdxStr = 0; |
| 7123 pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
| 7124 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? |
| 7125 pIdxInfo->nOrderBy : 0); |
| 7126 pNew->rSetup = 0; |
| 7127 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
| 7128 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); |
| 7129 |
| 7130 /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated |
| 7131 ** that the scan will visit at most one row. Clear it otherwise. */ |
| 7132 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){ |
| 7133 pNew->wsFlags |= WHERE_ONEROW; |
| 7134 }else{ |
| 7135 pNew->wsFlags &= ~WHERE_ONEROW; |
| 7136 } |
| 7137 rc = whereLoopInsert(pBuilder, pNew); |
| 7138 if( pNew->u.vtab.needFree ){ |
| 7139 sqlite3_free(pNew->u.vtab.idxStr); |
| 7140 pNew->u.vtab.needFree = 0; |
| 7141 } |
| 7142 WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n", |
| 7143 *pbIn, (sqlite3_uint64)mPrereq, |
| 7144 (sqlite3_uint64)(pNew->prereq & ~mPrereq))); |
| 7145 |
| 7146 return rc; |
| 7147 } |
| 7148 |
| 7149 |
| 7150 /* |
| 7151 ** Add all WhereLoop objects for a table of the join identified by |
| 7152 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
| 7153 ** |
| 7154 ** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and |
| 7155 ** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause |
| 7156 ** entries that occur before the virtual table in the FROM clause and are |
| 7157 ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the |
| 7158 ** mUnusable mask contains all FROM clause entries that occur after the |
| 7159 ** virtual table and are separated from it by at least one LEFT or |
| 7160 ** CROSS JOIN. |
| 7161 ** |
| 7162 ** For example, if the query were: |
| 7163 ** |
| 7164 ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; |
| 7165 ** |
| 7166 ** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6). |
| 7167 ** |
| 7168 ** All the tables in mPrereq must be scanned before the current virtual |
| 7169 ** table. So any terms for which all prerequisites are satisfied by |
| 7170 ** mPrereq may be specified as "usable" in all calls to xBestIndex. |
| 7171 ** Conversely, all tables in mUnusable must be scanned after the current |
| 7172 ** virtual table, so any terms for which the prerequisites overlap with |
| 7173 ** mUnusable should always be configured as "not-usable" for xBestIndex. |
| 7174 */ |
| 7175 static int whereLoopAddVirtual( |
| 7176 WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 7177 Bitmask mPrereq, /* Tables that must be scanned before this one */ |
| 7178 Bitmask mUnusable /* Tables that must be scanned after this one */ |
| 7179 ){ |
| 7180 int rc = SQLITE_OK; /* Return code */ |
| 7181 WhereInfo *pWInfo; /* WHERE analysis context */ |
| 7182 Parse *pParse; /* The parsing context */ |
| 7183 WhereClause *pWC; /* The WHERE clause */ |
| 7184 struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 7185 sqlite3_index_info *p; /* Object to pass to xBestIndex() */ |
| 7186 int nConstraint; /* Number of constraints in p */ |
| 7187 int bIn; /* True if plan uses IN(...) operator */ |
| 7188 WhereLoop *pNew; |
| 7189 Bitmask mBest; /* Tables used by best possible plan */ |
| 7190 u16 mNoOmit; |
| 7191 |
| 7192 assert( (mPrereq & mUnusable)==0 ); |
| 7193 pWInfo = pBuilder->pWInfo; |
| 7194 pParse = pWInfo->pParse; |
| 7195 pWC = pBuilder->pWC; |
| 7196 pNew = pBuilder->pNew; |
| 7197 pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
| 7198 assert( IsVirtual(pSrc->pTab) ); |
| 7199 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy, |
| 7200 &mNoOmit); |
| 7201 if( p==0 ) return SQLITE_NOMEM_BKPT; |
| 7202 pNew->rSetup = 0; |
| 7203 pNew->wsFlags = WHERE_VIRTUALTABLE; |
| 7204 pNew->nLTerm = 0; |
| 7205 pNew->u.vtab.needFree = 0; |
| 7206 nConstraint = p->nConstraint; |
| 7207 if( whereLoopResize(pParse->db, pNew, nConstraint) ){ |
| 7208 sqlite3DbFree(pParse->db, p); |
| 7209 return SQLITE_NOMEM_BKPT; |
| 7210 } |
| 7211 |
| 7212 /* First call xBestIndex() with all constraints usable. */ |
| 7213 WHERETRACE(0x40, (" VirtualOne: all usable\n")); |
| 7214 rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn); |
| 7215 |
| 7216 /* If the call to xBestIndex() with all terms enabled produced a plan |
| 7217 ** that does not require any source tables (IOW: a plan with mBest==0), |
| 7218 ** then there is no point in making any further calls to xBestIndex() |
| 7219 ** since they will all return the same result (if the xBestIndex() |
| 7220 ** implementation is sane). */ |
| 7221 if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){ |
| 7222 int seenZero = 0; /* True if a plan with no prereqs seen */ |
| 7223 int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */ |
| 7224 Bitmask mPrev = 0; |
| 7225 Bitmask mBestNoIn = 0; |
| 7226 |
| 7227 /* If the plan produced by the earlier call uses an IN(...) term, call |
| 7228 ** xBestIndex again, this time with IN(...) terms disabled. */ |
| 7229 if( bIn ){ |
| 7230 WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); |
| 7231 rc = whereLoopAddVirtualOne( |
| 7232 pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn); |
| 7233 assert( bIn==0 ); |
| 7234 mBestNoIn = pNew->prereq & ~mPrereq; |
| 7235 if( mBestNoIn==0 ){ |
| 7236 seenZero = 1; |
| 7237 seenZeroNoIN = 1; |
| 7238 } |
| 7239 } |
| 7240 |
| 7241 /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) |
| 7242 ** in the set of terms that apply to the current virtual table. */ |
| 7243 while( rc==SQLITE_OK ){ |
| 7244 int i; |
| 7245 Bitmask mNext = ALLBITS; |
| 7246 assert( mNext>0 ); |
| 7247 for(i=0; i<nConstraint; i++){ |
| 7248 Bitmask mThis = ( |
| 7249 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq |
| 7250 ); |
| 7251 if( mThis>mPrev && mThis<mNext ) mNext = mThis; |
| 7252 } |
| 7253 mPrev = mNext; |
| 7254 if( mNext==ALLBITS ) break; |
| 7255 if( mNext==mBest || mNext==mBestNoIn ) continue; |
| 7256 WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", |
| 7257 (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); |
| 7258 rc = whereLoopAddVirtualOne( |
| 7259 pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn); |
| 7260 if( pNew->prereq==mPrereq ){ |
| 7261 seenZero = 1; |
| 7262 if( bIn==0 ) seenZeroNoIN = 1; |
| 7263 } |
| 7264 } |
| 7265 |
| 7266 /* If the calls to xBestIndex() in the above loop did not find a plan |
| 7267 ** that requires no source tables at all (i.e. one guaranteed to be |
| 7268 ** usable), make a call here with all source tables disabled */ |
| 7269 if( rc==SQLITE_OK && seenZero==0 ){ |
| 7270 WHERETRACE(0x40, (" VirtualOne: all disabled\n")); |
| 7271 rc = whereLoopAddVirtualOne( |
| 7272 pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn); |
| 7273 if( bIn==0 ) seenZeroNoIN = 1; |
| 7274 } |
| 7275 |
| 7276 /* If the calls to xBestIndex() have so far failed to find a plan |
| 7277 ** that requires no source tables at all and does not use an IN(...) |
| 7278 ** operator, make a final call to obtain one here. */ |
| 7279 if( rc==SQLITE_OK && seenZeroNoIN==0 ){ |
| 7280 WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); |
| 7281 rc = whereLoopAddVirtualOne( |
| 7282 pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn); |
| 7283 } |
| 7284 } |
| 7285 |
| 7286 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); |
| 7287 sqlite3DbFree(pParse->db, p); |
| 7288 return rc; |
| 7289 } |
| 7290 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7291 |
| 7292 /* |
| 7293 ** Add WhereLoop entries to handle OR terms. This works for either |
| 7294 ** btrees or virtual tables. |
| 7295 */ |
| 7296 static int whereLoopAddOr( |
| 7297 WhereLoopBuilder *pBuilder, |
| 7298 Bitmask mPrereq, |
| 7299 Bitmask mUnusable |
| 7300 ){ |
| 7301 WhereInfo *pWInfo = pBuilder->pWInfo; |
| 7302 WhereClause *pWC; |
| 7303 WhereLoop *pNew; |
| 7304 WhereTerm *pTerm, *pWCEnd; |
| 7305 int rc = SQLITE_OK; |
| 7306 int iCur; |
| 7307 WhereClause tempWC; |
| 7308 WhereLoopBuilder sSubBuild; |
| 7309 WhereOrSet sSum, sCur; |
| 7310 struct SrcList_item *pItem; |
| 7311 |
| 7312 pWC = pBuilder->pWC; |
| 7313 pWCEnd = pWC->a + pWC->nTerm; |
| 7314 pNew = pBuilder->pNew; |
| 7315 memset(&sSum, 0, sizeof(sSum)); |
| 7316 pItem = pWInfo->pTabList->a + pNew->iTab; |
| 7317 iCur = pItem->iCursor; |
| 7318 |
| 7319 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 7320 if( (pTerm->eOperator & WO_OR)!=0 |
| 7321 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 7322 ){ |
| 7323 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 7324 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 7325 WhereTerm *pOrTerm; |
| 7326 int once = 1; |
| 7327 int i, j; |
| 7328 |
| 7329 sSubBuild = *pBuilder; |
| 7330 sSubBuild.pOrderBy = 0; |
| 7331 sSubBuild.pOrSet = &sCur; |
| 7332 |
| 7333 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
| 7334 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
| 7335 if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 7336 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 7337 }else if( pOrTerm->leftCursor==iCur ){ |
| 7338 tempWC.pWInfo = pWC->pWInfo; |
| 7339 tempWC.pOuter = pWC; |
| 7340 tempWC.op = TK_AND; |
| 7341 tempWC.nTerm = 1; |
| 7342 tempWC.a = pOrTerm; |
| 7343 sSubBuild.pWC = &tempWC; |
| 7344 }else{ |
| 7345 continue; |
| 7346 } |
| 7347 sCur.n = 0; |
| 7348 #ifdef WHERETRACE_ENABLED |
| 7349 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 7350 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 7351 if( sqlite3WhereTrace & 0x400 ){ |
| 7352 sqlite3WhereClausePrint(sSubBuild.pWC); |
| 7353 } |
| 7354 #endif |
| 7355 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 7356 if( IsVirtual(pItem->pTab) ){ |
| 7357 rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable); |
| 7358 }else |
| 7359 #endif |
| 7360 { |
| 7361 rc = whereLoopAddBtree(&sSubBuild, mPrereq); |
| 7362 } |
| 7363 if( rc==SQLITE_OK ){ |
| 7364 rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable); |
| 7365 } |
| 7366 assert( rc==SQLITE_OK || sCur.n==0 ); |
| 7367 if( sCur.n==0 ){ |
| 7368 sSum.n = 0; |
| 7369 break; |
| 7370 }else if( once ){ |
| 7371 whereOrMove(&sSum, &sCur); |
| 7372 once = 0; |
| 7373 }else{ |
| 7374 WhereOrSet sPrev; |
| 7375 whereOrMove(&sPrev, &sSum); |
| 7376 sSum.n = 0; |
| 7377 for(i=0; i<sPrev.n; i++){ |
| 7378 for(j=0; j<sCur.n; j++){ |
| 7379 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
| 7380 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 7381 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
| 7382 } |
| 7383 } |
| 7384 } |
| 7385 } |
| 7386 pNew->nLTerm = 1; |
| 7387 pNew->aLTerm[0] = pTerm; |
| 7388 pNew->wsFlags = WHERE_MULTI_OR; |
| 7389 pNew->rSetup = 0; |
| 7390 pNew->iSortIdx = 0; |
| 7391 memset(&pNew->u, 0, sizeof(pNew->u)); |
| 7392 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
| 7393 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs |
| 7394 ** of all sub-scans required by the OR-scan. However, due to rounding |
| 7395 ** errors, it may be that the cost of the OR-scan is equal to its |
| 7396 ** most expensive sub-scan. Add the smallest possible penalty |
| 7397 ** (equivalent to multiplying the cost by 1.07) to ensure that |
| 7398 ** this does not happen. Otherwise, for WHERE clauses such as the |
| 7399 ** following where there is an index on "y": |
| 7400 ** |
| 7401 ** WHERE likelihood(x=?, 0.99) OR y=? |
| 7402 ** |
| 7403 ** the planner may elect to "OR" together a full-table scan and an |
| 7404 ** index lookup. And other similarly odd results. */ |
| 7405 pNew->rRun = sSum.a[i].rRun + 1; |
| 7406 pNew->nOut = sSum.a[i].nOut; |
| 7407 pNew->prereq = sSum.a[i].prereq; |
| 7408 rc = whereLoopInsert(pBuilder, pNew); |
| 7409 } |
| 7410 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); |
| 7411 } |
| 7412 } |
| 7413 return rc; |
| 7414 } |
| 7415 |
| 7416 /* |
| 7417 ** Add all WhereLoop objects for all tables |
| 7418 */ |
| 7419 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
| 7420 WhereInfo *pWInfo = pBuilder->pWInfo; |
| 7421 Bitmask mPrereq = 0; |
| 7422 Bitmask mPrior = 0; |
| 7423 int iTab; |
| 7424 SrcList *pTabList = pWInfo->pTabList; |
| 7425 struct SrcList_item *pItem; |
| 7426 struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; |
| 7427 sqlite3 *db = pWInfo->pParse->db; |
| 7428 int rc = SQLITE_OK; |
| 7429 WhereLoop *pNew; |
| 7430 u8 priorJointype = 0; |
| 7431 |
| 7432 /* Loop over the tables in the join, from left to right */ |
| 7433 pNew = pBuilder->pNew; |
| 7434 whereLoopInit(pNew); |
| 7435 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){ |
| 7436 Bitmask mUnusable = 0; |
| 7437 pNew->iTab = iTab; |
| 7438 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); |
| 7439 if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ |
| 7440 /* This condition is true when pItem is the FROM clause term on the |
| 7441 ** right-hand-side of a LEFT or CROSS JOIN. */ |
| 7442 mPrereq = mPrior; |
| 7443 } |
| 7444 priorJointype = pItem->fg.jointype; |
| 7445 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 7446 if( IsVirtual(pItem->pTab) ){ |
| 7447 struct SrcList_item *p; |
| 7448 for(p=&pItem[1]; p<pEnd; p++){ |
| 7449 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){ |
| 7450 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); |
| 7451 } |
| 7452 } |
| 7453 rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable); |
| 7454 }else |
| 7455 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 7456 { |
| 7457 rc = whereLoopAddBtree(pBuilder, mPrereq); |
| 7458 } |
| 7459 if( rc==SQLITE_OK ){ |
| 7460 rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); |
| 7461 } |
| 7462 mPrior |= pNew->maskSelf; |
| 7463 if( rc || db->mallocFailed ) break; |
| 7464 } |
| 7465 |
| 7466 whereLoopClear(db, pNew); |
| 7467 return rc; |
| 7468 } |
| 7469 |
| 7470 /* |
| 7471 ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
| 7472 ** parameters) to see if it outputs rows in the requested ORDER BY |
| 7473 ** (or GROUP BY) without requiring a separate sort operation. Return N: |
| 7474 ** |
| 7475 ** N>0: N terms of the ORDER BY clause are satisfied |
| 7476 ** N==0: No terms of the ORDER BY clause are satisfied |
| 7477 ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. |
| 7478 ** |
| 7479 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 7480 ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 7481 ** equivalent rows appear immediately adjacent to one another. GROUP BY |
| 7482 ** and DISTINCT do not require rows to appear in any particular order as long |
| 7483 ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT |
| 7484 ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 7485 ** pOrderBy terms must be matched in strict left-to-right order. |
| 7486 */ |
| 7487 static i8 wherePathSatisfiesOrderBy( |
| 7488 WhereInfo *pWInfo, /* The WHERE clause */ |
| 7489 ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
| 7490 WherePath *pPath, /* The WherePath to check */ |
| 7491 u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */ |
| 7492 u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
| 7493 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
| 7494 Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
| 7495 ){ |
| 7496 u8 revSet; /* True if rev is known */ |
| 7497 u8 rev; /* Composite sort order */ |
| 7498 u8 revIdx; /* Index sort order */ |
| 7499 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 7500 u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 7501 u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
| 7502 u16 eqOpMask; /* Allowed equality operators */ |
| 7503 u16 nKeyCol; /* Number of key columns in pIndex */ |
| 7504 u16 nColumn; /* Total number of ordered columns in the index */ |
| 7505 u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 7506 int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 7507 int i, j; /* Loop counters */ |
| 7508 int iCur; /* Cursor number for current WhereLoop */ |
| 7509 int iColumn; /* A column number within table iCur */ |
| 7510 WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
| 7511 WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 7512 Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 7513 CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 7514 Index *pIndex; /* The index associated with pLoop */ |
| 7515 sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 7516 Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 7517 Bitmask obDone; /* Mask of all ORDER BY terms */ |
| 7518 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
| 7519 Bitmask ready; /* Mask of inner loops */ |
| 7520 |
| 7521 /* |
| 7522 ** We say the WhereLoop is "one-row" if it generates no more than one |
| 7523 ** row of output. A WhereLoop is one-row if all of the following are true: |
| 7524 ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 7525 ** (b) The index is unique |
| 7526 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 7527 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
| 7528 ** |
| 7529 ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 7530 ** that WhereLoop that are in the ORDER BY clause are different for every |
| 7531 ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 7532 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 7533 ** is not order-distinct. To be order-distinct is not quite the same as being |
| 7534 ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 7535 ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 7536 ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 7537 ** |
| 7538 ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 7539 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 7540 ** automatically order-distinct. |
| 7541 */ |
| 7542 |
| 7543 assert( pOrderBy!=0 ); |
| 7544 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
| 7545 |
| 7546 nOrderBy = pOrderBy->nExpr; |
| 7547 testcase( nOrderBy==BMS-1 ); |
| 7548 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 7549 isOrderDistinct = 1; |
| 7550 obDone = MASKBIT(nOrderBy)-1; |
| 7551 orderDistinctMask = 0; |
| 7552 ready = 0; |
| 7553 eqOpMask = WO_EQ | WO_IS | WO_ISNULL; |
| 7554 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN; |
| 7555 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
| 7556 if( iLoop>0 ) ready |= pLoop->maskSelf; |
| 7557 if( iLoop<nLoop ){ |
| 7558 pLoop = pPath->aLoop[iLoop]; |
| 7559 if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue; |
| 7560 }else{ |
| 7561 pLoop = pLast; |
| 7562 } |
| 7563 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 7564 if( pLoop->u.vtab.isOrdered ) obSat = obDone; |
| 7565 break; |
| 7566 } |
| 7567 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
| 7568 |
| 7569 /* Mark off any ORDER BY term X that is a column in the table of |
| 7570 ** the current loop for which there is term in the WHERE |
| 7571 ** clause of the form X IS NULL or X=? that reference only outer |
| 7572 ** loops. |
| 7573 */ |
| 7574 for(i=0; i<nOrderBy; i++){ |
| 7575 if( MASKBIT(i) & obSat ) continue; |
| 7576 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 7577 if( pOBExpr->op!=TK_COLUMN ) continue; |
| 7578 if( pOBExpr->iTable!=iCur ) continue; |
| 7579 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
| 7580 ~ready, eqOpMask, 0); |
| 7581 if( pTerm==0 ) continue; |
| 7582 if( pTerm->eOperator==WO_IN ){ |
| 7583 /* IN terms are only valid for sorting in the ORDER BY LIMIT |
| 7584 ** optimization, and then only if they are actually used |
| 7585 ** by the query plan */ |
| 7586 assert( wctrlFlags & WHERE_ORDERBY_LIMIT ); |
| 7587 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){} |
| 7588 if( j>=pLoop->nLTerm ) continue; |
| 7589 } |
| 7590 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
| 7591 const char *z1, *z2; |
| 7592 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 7593 if( !pColl ) pColl = db->pDfltColl; |
| 7594 z1 = pColl->zName; |
| 7595 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 7596 if( !pColl ) pColl = db->pDfltColl; |
| 7597 z2 = pColl->zName; |
| 7598 if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
| 7599 testcase( pTerm->pExpr->op==TK_IS ); |
| 7600 } |
| 7601 obSat |= MASKBIT(i); |
| 7602 } |
| 7603 |
| 7604 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 7605 if( pLoop->wsFlags & WHERE_IPK ){ |
| 7606 pIndex = 0; |
| 7607 nKeyCol = 0; |
| 7608 nColumn = 1; |
| 7609 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
| 7610 return 0; |
| 7611 }else{ |
| 7612 nKeyCol = pIndex->nKeyCol; |
| 7613 nColumn = pIndex->nColumn; |
| 7614 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); |
| 7615 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID |
| 7616 || !HasRowid(pIndex->pTable)); |
| 7617 isOrderDistinct = IsUniqueIndex(pIndex); |
| 7618 } |
| 7619 |
| 7620 /* Loop through all columns of the index and deal with the ones |
| 7621 ** that are not constrained by == or IN. |
| 7622 */ |
| 7623 rev = revSet = 0; |
| 7624 distinctColumns = 0; |
| 7625 for(j=0; j<nColumn; j++){ |
| 7626 u8 bOnce = 1; /* True to run the ORDER BY search loop */ |
| 7627 |
| 7628 assert( j>=pLoop->u.btree.nEq |
| 7629 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip) |
| 7630 ); |
| 7631 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){ |
| 7632 u16 eOp = pLoop->aLTerm[j]->eOperator; |
| 7633 |
| 7634 /* Skip over == and IS and ISNULL terms. (Also skip IN terms when |
| 7635 ** doing WHERE_ORDERBY_LIMIT processing). |
| 7636 ** |
| 7637 ** If the current term is a column of an ((?,?) IN (SELECT...)) |
| 7638 ** expression for which the SELECT returns more than one column, |
| 7639 ** check that it is the only column used by this loop. Otherwise, |
| 7640 ** if it is one of two or more, none of the columns can be |
| 7641 ** considered to match an ORDER BY term. */ |
| 7642 if( (eOp & eqOpMask)!=0 ){ |
| 7643 if( eOp & WO_ISNULL ){ |
| 7644 testcase( isOrderDistinct ); |
| 7645 isOrderDistinct = 0; |
| 7646 } |
| 7647 continue; |
| 7648 }else if( ALWAYS(eOp & WO_IN) ){ |
| 7649 /* ALWAYS() justification: eOp is an equality operator due to the |
| 7650 ** j<pLoop->u.btree.nEq constraint above. Any equality other |
| 7651 ** than WO_IN is captured by the previous "if". So this one |
| 7652 ** always has to be WO_IN. */ |
| 7653 Expr *pX = pLoop->aLTerm[j]->pExpr; |
| 7654 for(i=j+1; i<pLoop->u.btree.nEq; i++){ |
| 7655 if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 7656 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) ); |
| 7657 bOnce = 0; |
| 7658 break; |
| 7659 } |
| 7660 } |
| 7661 } |
| 7662 } |
| 7663 |
| 7664 /* Get the column number in the table (iColumn) and sort order |
| 7665 ** (revIdx) for the j-th column of the index. |
| 7666 */ |
| 7667 if( pIndex ){ |
| 7668 iColumn = pIndex->aiColumn[j]; |
| 7669 revIdx = pIndex->aSortOrder[j]; |
| 7670 if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
| 7671 }else{ |
| 7672 iColumn = XN_ROWID; |
| 7673 revIdx = 0; |
| 7674 } |
| 7675 |
| 7676 /* An unconstrained column that might be NULL means that this |
| 7677 ** WhereLoop is not well-ordered |
| 7678 */ |
| 7679 if( isOrderDistinct |
| 7680 && iColumn>=0 |
| 7681 && j>=pLoop->u.btree.nEq |
| 7682 && pIndex->pTable->aCol[iColumn].notNull==0 |
| 7683 ){ |
| 7684 isOrderDistinct = 0; |
| 7685 } |
| 7686 |
| 7687 /* Find the ORDER BY term that corresponds to the j-th column |
| 7688 ** of the index and mark that ORDER BY term off |
| 7689 */ |
| 7690 isMatch = 0; |
| 7691 for(i=0; bOnce && i<nOrderBy; i++){ |
| 7692 if( MASKBIT(i) & obSat ) continue; |
| 7693 pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 7694 testcase( wctrlFlags & WHERE_GROUPBY ); |
| 7695 testcase( wctrlFlags & WHERE_DISTINCTBY ); |
| 7696 if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
| 7697 if( iColumn>=(-1) ){ |
| 7698 if( pOBExpr->op!=TK_COLUMN ) continue; |
| 7699 if( pOBExpr->iTable!=iCur ) continue; |
| 7700 if( pOBExpr->iColumn!=iColumn ) continue; |
| 7701 }else{ |
| 7702 if( sqlite3ExprCompare(pOBExpr,pIndex->aColExpr->a[j].pExpr,iCur) ){ |
| 7703 continue; |
| 7704 } |
| 7705 } |
| 7706 if( iColumn>=0 ){ |
| 7707 pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 7708 if( !pColl ) pColl = db->pDfltColl; |
| 7709 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 7710 } |
| 7711 isMatch = 1; |
| 7712 break; |
| 7713 } |
| 7714 if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 7715 /* Make sure the sort order is compatible in an ORDER BY clause. |
| 7716 ** Sort order is irrelevant for a GROUP BY clause. */ |
| 7717 if( revSet ){ |
| 7718 if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 7719 }else{ |
| 7720 rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 7721 if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 7722 revSet = 1; |
| 7723 } |
| 7724 } |
| 7725 if( isMatch ){ |
| 7726 if( iColumn==XN_ROWID ){ |
| 7727 testcase( distinctColumns==0 ); |
| 7728 distinctColumns = 1; |
| 7729 } |
| 7730 obSat |= MASKBIT(i); |
| 7731 }else{ |
| 7732 /* No match found */ |
| 7733 if( j==0 || j<nKeyCol ){ |
| 7734 testcase( isOrderDistinct!=0 ); |
| 7735 isOrderDistinct = 0; |
| 7736 } |
| 7737 break; |
| 7738 } |
| 7739 } /* end Loop over all index columns */ |
| 7740 if( distinctColumns ){ |
| 7741 testcase( isOrderDistinct==0 ); |
| 7742 isOrderDistinct = 1; |
| 7743 } |
| 7744 } /* end-if not one-row */ |
| 7745 |
| 7746 /* Mark off any other ORDER BY terms that reference pLoop */ |
| 7747 if( isOrderDistinct ){ |
| 7748 orderDistinctMask |= pLoop->maskSelf; |
| 7749 for(i=0; i<nOrderBy; i++){ |
| 7750 Expr *p; |
| 7751 Bitmask mTerm; |
| 7752 if( MASKBIT(i) & obSat ) continue; |
| 7753 p = pOrderBy->a[i].pExpr; |
| 7754 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p); |
| 7755 if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; |
| 7756 if( (mTerm&~orderDistinctMask)==0 ){ |
| 7757 obSat |= MASKBIT(i); |
| 7758 } |
| 7759 } |
| 7760 } |
| 7761 } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
| 7762 if( obSat==obDone ) return (i8)nOrderBy; |
| 7763 if( !isOrderDistinct ){ |
| 7764 for(i=nOrderBy-1; i>0; i--){ |
| 7765 Bitmask m = MASKBIT(i) - 1; |
| 7766 if( (obSat&m)==m ) return i; |
| 7767 } |
| 7768 return 0; |
| 7769 } |
| 7770 return -1; |
| 7771 } |
| 7772 |
| 7773 |
| 7774 /* |
| 7775 ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), |
| 7776 ** the planner assumes that the specified pOrderBy list is actually a GROUP |
| 7777 ** BY clause - and so any order that groups rows as required satisfies the |
| 7778 ** request. |
| 7779 ** |
| 7780 ** Normally, in this case it is not possible for the caller to determine |
| 7781 ** whether or not the rows are really being delivered in sorted order, or |
| 7782 ** just in some other order that provides the required grouping. However, |
| 7783 ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then |
| 7784 ** this function may be called on the returned WhereInfo object. It returns |
| 7785 ** true if the rows really will be sorted in the specified order, or false |
| 7786 ** otherwise. |
| 7787 ** |
| 7788 ** For example, assuming: |
| 7789 ** |
| 7790 ** CREATE INDEX i1 ON t1(x, Y); |
| 7791 ** |
| 7792 ** then |
| 7793 ** |
| 7794 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 |
| 7795 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 |
| 7796 */ |
| 7797 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){ |
| 7798 assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); |
| 7799 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); |
| 7800 return pWInfo->sorted; |
| 7801 } |
| 7802 |
| 7803 #ifdef WHERETRACE_ENABLED |
| 7804 /* For debugging use only: */ |
| 7805 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 7806 static char zName[65]; |
| 7807 int i; |
| 7808 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 7809 if( pLast ) zName[i++] = pLast->cId; |
| 7810 zName[i] = 0; |
| 7811 return zName; |
| 7812 } |
| 7813 #endif |
| 7814 |
| 7815 /* |
| 7816 ** Return the cost of sorting nRow rows, assuming that the keys have |
| 7817 ** nOrderby columns and that the first nSorted columns are already in |
| 7818 ** order. |
| 7819 */ |
| 7820 static LogEst whereSortingCost( |
| 7821 WhereInfo *pWInfo, |
| 7822 LogEst nRow, |
| 7823 int nOrderBy, |
| 7824 int nSorted |
| 7825 ){ |
| 7826 /* TUNING: Estimated cost of a full external sort, where N is |
| 7827 ** the number of rows to sort is: |
| 7828 ** |
| 7829 ** cost = (3.0 * N * log(N)). |
| 7830 ** |
| 7831 ** Or, if the order-by clause has X terms but only the last Y |
| 7832 ** terms are out of order, then block-sorting will reduce the |
| 7833 ** sorting cost to: |
| 7834 ** |
| 7835 ** cost = (3.0 * N * log(N)) * (Y/X) |
| 7836 ** |
| 7837 ** The (Y/X) term is implemented using stack variable rScale |
| 7838 ** below. */ |
| 7839 LogEst rScale, rSortCost; |
| 7840 assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); |
| 7841 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; |
| 7842 rSortCost = nRow + rScale + 16; |
| 7843 |
| 7844 /* Multiple by log(M) where M is the number of output rows. |
| 7845 ** Use the LIMIT for M if it is smaller */ |
| 7846 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){ |
| 7847 nRow = pWInfo->iLimit; |
| 7848 } |
| 7849 rSortCost += estLog(nRow); |
| 7850 return rSortCost; |
| 7851 } |
| 7852 |
| 7853 /* |
| 7854 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
| 7855 ** attempts to find the lowest cost path that visits each WhereLoop |
| 7856 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 7857 ** |
| 7858 ** Assume that the total number of output rows that will need to be sorted |
| 7859 ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 7860 ** costs if nRowEst==0. |
| 7861 ** |
| 7862 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 7863 ** error occurs. |
| 7864 */ |
| 7865 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
| 7866 int mxChoice; /* Maximum number of simultaneous paths tracked */ |
| 7867 int nLoop; /* Number of terms in the join */ |
| 7868 Parse *pParse; /* Parsing context */ |
| 7869 sqlite3 *db; /* The database connection */ |
| 7870 int iLoop; /* Loop counter over the terms of the join */ |
| 7871 int ii, jj; /* Loop counters */ |
| 7872 int mxI = 0; /* Index of next entry to replace */ |
| 7873 int nOrderBy; /* Number of ORDER BY clause terms */ |
| 7874 LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
| 7875 LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ |
| 7876 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 7877 WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 7878 WherePath *aTo; /* The nTo best paths at the current level */ |
| 7879 WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 7880 WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 7881 WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 7882 WhereLoop **pX; /* Used to divy up the pSpace memory */ |
| 7883 LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ |
| 7884 char *pSpace; /* Temporary memory used by this routine */ |
| 7885 int nSpace; /* Bytes of space allocated at pSpace */ |
| 7886 |
| 7887 pParse = pWInfo->pParse; |
| 7888 db = pParse->db; |
| 7889 nLoop = pWInfo->nLevel; |
| 7890 /* TUNING: For simple queries, only the best path is tracked. |
| 7891 ** For 2-way joins, the 5 best paths are followed. |
| 7892 ** For joins of 3 or more tables, track the 10 best paths */ |
| 7893 mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); |
| 7894 assert( nLoop<=pWInfo->pTabList->nSrc ); |
| 7895 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); |
| 7896 |
| 7897 /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this |
| 7898 ** case the purpose of this call is to estimate the number of rows returned |
| 7899 ** by the overall query. Once this estimate has been obtained, the caller |
| 7900 ** will invoke this function a second time, passing the estimate as the |
| 7901 ** nRowEst parameter. */ |
| 7902 if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 7903 nOrderBy = 0; |
| 7904 }else{ |
| 7905 nOrderBy = pWInfo->pOrderBy->nExpr; |
| 7906 } |
| 7907 |
| 7908 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ |
| 7909 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 7910 nSpace += sizeof(LogEst) * nOrderBy; |
| 7911 pSpace = sqlite3DbMallocRawNN(db, nSpace); |
| 7912 if( pSpace==0 ) return SQLITE_NOMEM_BKPT; |
| 7913 aTo = (WherePath*)pSpace; |
| 7914 aFrom = aTo+mxChoice; |
| 7915 memset(aFrom, 0, sizeof(aFrom[0])); |
| 7916 pX = (WhereLoop**)(aFrom+mxChoice); |
| 7917 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
| 7918 pFrom->aLoop = pX; |
| 7919 } |
| 7920 if( nOrderBy ){ |
| 7921 /* If there is an ORDER BY clause and it is not being ignored, set up |
| 7922 ** space for the aSortCost[] array. Each element of the aSortCost array |
| 7923 ** is either zero - meaning it has not yet been initialized - or the |
| 7924 ** cost of sorting nRowEst rows of data where the first X terms of |
| 7925 ** the ORDER BY clause are already in order, where X is the array |
| 7926 ** index. */ |
| 7927 aSortCost = (LogEst*)pX; |
| 7928 memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); |
| 7929 } |
| 7930 assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); |
| 7931 assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); |
| 7932 |
| 7933 /* Seed the search with a single WherePath containing zero WhereLoops. |
| 7934 ** |
| 7935 ** TUNING: Do not let the number of iterations go above 28. If the cost |
| 7936 ** of computing an automatic index is not paid back within the first 28 |
| 7937 ** rows, then do not use the automatic index. */ |
| 7938 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) ); |
| 7939 nFrom = 1; |
| 7940 assert( aFrom[0].isOrdered==0 ); |
| 7941 if( nOrderBy ){ |
| 7942 /* If nLoop is zero, then there are no FROM terms in the query. Since |
| 7943 ** in this case the query may return a maximum of one row, the results |
| 7944 ** are already in the requested order. Set isOrdered to nOrderBy to |
| 7945 ** indicate this. Or, if nLoop is greater than zero, set isOrdered to |
| 7946 ** -1, indicating that the result set may or may not be ordered, |
| 7947 ** depending on the loops added to the current plan. */ |
| 7948 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy; |
| 7949 } |
| 7950 |
| 7951 /* Compute successively longer WherePaths using the previous generation |
| 7952 ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 7953 ** best paths at each generation */ |
| 7954 for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 7955 nTo = 0; |
| 7956 for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 7957 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
| 7958 LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ |
| 7959 LogEst rCost; /* Cost of path (pFrom+pWLoop) */ |
| 7960 LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ |
| 7961 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 7962 Bitmask maskNew; /* Mask of src visited by (..) */ |
| 7963 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 7964 |
| 7965 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 7966 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
| 7967 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<10 ){ |
| 7968 /* Do not use an automatic index if the this loop is expected |
| 7969 ** to run less than 2 times. */ |
| 7970 assert( 10==sqlite3LogEst(2) ); |
| 7971 continue; |
| 7972 } |
| 7973 /* At this point, pWLoop is a candidate to be the next loop. |
| 7974 ** Compute its cost */ |
| 7975 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 7976 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
| 7977 nOut = pFrom->nRow + pWLoop->nOut; |
| 7978 maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
| 7979 if( isOrdered<0 ){ |
| 7980 isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
| 7981 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
| 7982 iLoop, pWLoop, &revMask); |
| 7983 }else{ |
| 7984 revMask = pFrom->revLoop; |
| 7985 } |
| 7986 if( isOrdered>=0 && isOrdered<nOrderBy ){ |
| 7987 if( aSortCost[isOrdered]==0 ){ |
| 7988 aSortCost[isOrdered] = whereSortingCost( |
| 7989 pWInfo, nRowEst, nOrderBy, isOrdered |
| 7990 ); |
| 7991 } |
| 7992 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]); |
| 7993 |
| 7994 WHERETRACE(0x002, |
| 7995 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", |
| 7996 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, |
| 7997 rUnsorted, rCost)); |
| 7998 }else{ |
| 7999 rCost = rUnsorted; |
| 8000 } |
| 8001 |
| 8002 /* Check to see if pWLoop should be added to the set of |
| 8003 ** mxChoice best-so-far paths. |
| 8004 ** |
| 8005 ** First look for an existing path among best-so-far paths |
| 8006 ** that covers the same set of loops and has the same isOrdered |
| 8007 ** setting as the current path candidate. |
| 8008 ** |
| 8009 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent |
| 8010 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range |
| 8011 ** of legal values for isOrdered, -1..64. |
| 8012 */ |
| 8013 for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
| 8014 if( pTo->maskLoop==maskNew |
| 8015 && ((pTo->isOrdered^isOrdered)&0x80)==0 |
| 8016 ){ |
| 8017 testcase( jj==nTo-1 ); |
| 8018 break; |
| 8019 } |
| 8020 } |
| 8021 if( jj>=nTo ){ |
| 8022 /* None of the existing best-so-far paths match the candidate. */ |
| 8023 if( nTo>=mxChoice |
| 8024 && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted)) |
| 8025 ){ |
| 8026 /* The current candidate is no better than any of the mxChoice |
| 8027 ** paths currently in the best-so-far buffer. So discard |
| 8028 ** this candidate as not viable. */ |
| 8029 #ifdef WHERETRACE_ENABLED /* 0x4 */ |
| 8030 if( sqlite3WhereTrace&0x4 ){ |
| 8031 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", |
| 8032 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
| 8033 isOrdered>=0 ? isOrdered+'0' : '?'); |
| 8034 } |
| 8035 #endif |
| 8036 continue; |
| 8037 } |
| 8038 /* If we reach this points it means that the new candidate path |
| 8039 ** needs to be added to the set of best-so-far paths. */ |
| 8040 if( nTo<mxChoice ){ |
| 8041 /* Increase the size of the aTo set by one */ |
| 8042 jj = nTo++; |
| 8043 }else{ |
| 8044 /* New path replaces the prior worst to keep count below mxChoice */ |
| 8045 jj = mxI; |
| 8046 } |
| 8047 pTo = &aTo[jj]; |
| 8048 #ifdef WHERETRACE_ENABLED /* 0x4 */ |
| 8049 if( sqlite3WhereTrace&0x4 ){ |
| 8050 sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", |
| 8051 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
| 8052 isOrdered>=0 ? isOrdered+'0' : '?'); |
| 8053 } |
| 8054 #endif |
| 8055 }else{ |
| 8056 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the |
| 8057 ** same set of loops and has the sam isOrdered setting as the |
| 8058 ** candidate path. Check to see if the candidate should replace |
| 8059 ** pTo or if the candidate should be skipped */ |
| 8060 if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){ |
| 8061 #ifdef WHERETRACE_ENABLED /* 0x4 */ |
| 8062 if( sqlite3WhereTrace&0x4 ){ |
| 8063 sqlite3DebugPrintf( |
| 8064 "Skip %s cost=%-3d,%3d order=%c", |
| 8065 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
| 8066 isOrdered>=0 ? isOrdered+'0' : '?'); |
| 8067 sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", |
| 8068 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 8069 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
| 8070 } |
| 8071 #endif |
| 8072 /* Discard the candidate path from further consideration */ |
| 8073 testcase( pTo->rCost==rCost ); |
| 8074 continue; |
| 8075 } |
| 8076 testcase( pTo->rCost==rCost+1 ); |
| 8077 /* Control reaches here if the candidate path is better than the |
| 8078 ** pTo path. Replace pTo with the candidate. */ |
| 8079 #ifdef WHERETRACE_ENABLED /* 0x4 */ |
| 8080 if( sqlite3WhereTrace&0x4 ){ |
| 8081 sqlite3DebugPrintf( |
| 8082 "Update %s cost=%-3d,%3d order=%c", |
| 8083 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
| 8084 isOrdered>=0 ? isOrdered+'0' : '?'); |
| 8085 sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", |
| 8086 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 8087 pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
| 8088 } |
| 8089 #endif |
| 8090 } |
| 8091 /* pWLoop is a winner. Add it to the set of best so far */ |
| 8092 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
| 8093 pTo->revLoop = revMask; |
| 8094 pTo->nRow = nOut; |
| 8095 pTo->rCost = rCost; |
| 8096 pTo->rUnsorted = rUnsorted; |
| 8097 pTo->isOrdered = isOrdered; |
| 8098 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 8099 pTo->aLoop[iLoop] = pWLoop; |
| 8100 if( nTo>=mxChoice ){ |
| 8101 mxI = 0; |
| 8102 mxCost = aTo[0].rCost; |
| 8103 mxUnsorted = aTo[0].nRow; |
| 8104 for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
| 8105 if( pTo->rCost>mxCost |
| 8106 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted) |
| 8107 ){ |
| 8108 mxCost = pTo->rCost; |
| 8109 mxUnsorted = pTo->rUnsorted; |
| 8110 mxI = jj; |
| 8111 } |
| 8112 } |
| 8113 } |
| 8114 } |
| 8115 } |
| 8116 |
| 8117 #ifdef WHERETRACE_ENABLED /* >=2 */ |
| 8118 if( sqlite3WhereTrace & 0x02 ){ |
| 8119 sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
| 8120 for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
| 8121 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
| 8122 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
| 8123 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| 8124 if( pTo->isOrdered>0 ){ |
| 8125 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 8126 }else{ |
| 8127 sqlite3DebugPrintf("\n"); |
| 8128 } |
| 8129 } |
| 8130 } |
| 8131 #endif |
| 8132 |
| 8133 /* Swap the roles of aFrom and aTo for the next generation */ |
| 8134 pFrom = aTo; |
| 8135 aTo = aFrom; |
| 8136 aFrom = pFrom; |
| 8137 nFrom = nTo; |
| 8138 } |
| 8139 |
| 8140 if( nFrom==0 ){ |
| 8141 sqlite3ErrorMsg(pParse, "no query solution"); |
| 8142 sqlite3DbFree(db, pSpace); |
| 8143 return SQLITE_ERROR; |
| 8144 } |
| 8145 |
| 8146 /* Find the lowest cost path. pFrom will be left pointing to that path */ |
| 8147 pFrom = aFrom; |
| 8148 for(ii=1; ii<nFrom; ii++){ |
| 8149 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 8150 } |
| 8151 assert( pWInfo->nLevel==nLoop ); |
| 8152 /* Load the lowest cost path into pWInfo */ |
| 8153 for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 8154 WhereLevel *pLevel = pWInfo->a + iLoop; |
| 8155 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
| 8156 pLevel->iFrom = pWLoop->iTab; |
| 8157 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
| 8158 } |
| 8159 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 8160 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 8161 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
| 8162 && nRowEst |
| 8163 ){ |
| 8164 Bitmask notUsed; |
| 8165 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinctSet, pFrom, |
| 8166 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
| 8167 if( rc==pWInfo->pDistinctSet->nExpr ){ |
| 8168 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 8169 } |
| 8170 } |
| 8171 if( pWInfo->pOrderBy ){ |
| 8172 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
| 8173 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 8174 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 8175 } |
| 8176 }else{ |
| 8177 pWInfo->nOBSat = pFrom->isOrdered; |
| 8178 pWInfo->revMask = pFrom->revLoop; |
| 8179 if( pWInfo->nOBSat<=0 ){ |
| 8180 pWInfo->nOBSat = 0; |
| 8181 if( nLoop>0 ){ |
| 8182 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags; |
| 8183 if( (wsFlags & WHERE_ONEROW)==0 |
| 8184 && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN) |
| 8185 ){ |
| 8186 Bitmask m = 0; |
| 8187 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom, |
| 8188 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m); |
| 8189 testcase( wsFlags & WHERE_IPK ); |
| 8190 testcase( wsFlags & WHERE_COLUMN_IN ); |
| 8191 if( rc==pWInfo->pOrderBy->nExpr ){ |
| 8192 pWInfo->bOrderedInnerLoop = 1; |
| 8193 pWInfo->revMask = m; |
| 8194 } |
| 8195 } |
| 8196 } |
| 8197 } |
| 8198 } |
| 8199 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 8200 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 |
| 8201 ){ |
| 8202 Bitmask revMask = 0; |
| 8203 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
| 8204 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
| 8205 ); |
| 8206 assert( pWInfo->sorted==0 ); |
| 8207 if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| 8208 pWInfo->sorted = 1; |
| 8209 pWInfo->revMask = revMask; |
| 8210 } |
| 8211 } |
| 8212 } |
| 8213 |
| 8214 |
| 8215 pWInfo->nRowOut = pFrom->nRow; |
| 8216 |
| 8217 /* Free temporary memory and return success */ |
| 8218 sqlite3DbFree(db, pSpace); |
| 8219 return SQLITE_OK; |
| 8220 } |
| 8221 |
| 8222 /* |
| 8223 ** Most queries use only a single table (they are not joins) and have |
| 8224 ** simple == constraints against indexed fields. This routine attempts |
| 8225 ** to plan those simple cases using much less ceremony than the |
| 8226 ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 8227 ** times for the common case. |
| 8228 ** |
| 8229 ** Return non-zero on success, if this query can be handled by this |
| 8230 ** no-frills query planner. Return zero if this query needs the |
| 8231 ** general-purpose query planner. |
| 8232 */ |
| 8233 static int whereShortCut(WhereLoopBuilder *pBuilder){ |
| 8234 WhereInfo *pWInfo; |
| 8235 struct SrcList_item *pItem; |
| 8236 WhereClause *pWC; |
| 8237 WhereTerm *pTerm; |
| 8238 WhereLoop *pLoop; |
| 8239 int iCur; |
| 8240 int j; |
| 8241 Table *pTab; |
| 8242 Index *pIdx; |
| 8243 |
| 8244 pWInfo = pBuilder->pWInfo; |
| 8245 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0; |
| 8246 assert( pWInfo->pTabList->nSrc>=1 ); |
| 8247 pItem = pWInfo->pTabList->a; |
| 8248 pTab = pItem->pTab; |
| 8249 if( IsVirtual(pTab) ) return 0; |
| 8250 if( pItem->fg.isIndexedBy ) return 0; |
| 8251 iCur = pItem->iCursor; |
| 8252 pWC = &pWInfo->sWC; |
| 8253 pLoop = pBuilder->pNew; |
| 8254 pLoop->wsFlags = 0; |
| 8255 pLoop->nSkip = 0; |
| 8256 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); |
| 8257 if( pTerm ){ |
| 8258 testcase( pTerm->eOperator & WO_IS ); |
| 8259 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 8260 pLoop->aLTerm[0] = pTerm; |
| 8261 pLoop->nLTerm = 1; |
| 8262 pLoop->u.btree.nEq = 1; |
| 8263 /* TUNING: Cost of a rowid lookup is 10 */ |
| 8264 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
| 8265 }else{ |
| 8266 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 8267 int opMask; |
| 8268 assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
| 8269 if( !IsUniqueIndex(pIdx) |
| 8270 || pIdx->pPartIdxWhere!=0 |
| 8271 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
| 8272 ) continue; |
| 8273 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; |
| 8274 for(j=0; j<pIdx->nKeyCol; j++){ |
| 8275 pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx); |
| 8276 if( pTerm==0 ) break; |
| 8277 testcase( pTerm->eOperator & WO_IS ); |
| 8278 pLoop->aLTerm[j] = pTerm; |
| 8279 } |
| 8280 if( j!=pIdx->nKeyCol ) continue; |
| 8281 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
| 8282 if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
| 8283 pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 8284 } |
| 8285 pLoop->nLTerm = j; |
| 8286 pLoop->u.btree.nEq = j; |
| 8287 pLoop->u.btree.pIndex = pIdx; |
| 8288 /* TUNING: Cost of a unique index lookup is 15 */ |
| 8289 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ |
| 8290 break; |
| 8291 } |
| 8292 } |
| 8293 if( pLoop->wsFlags ){ |
| 8294 pLoop->nOut = (LogEst)1; |
| 8295 pWInfo->a[0].pWLoop = pLoop; |
| 8296 pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); |
| 8297 pWInfo->a[0].iTabCur = iCur; |
| 8298 pWInfo->nRowOut = 1; |
| 8299 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
| 8300 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 8301 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 8302 } |
| 8303 #ifdef SQLITE_DEBUG |
| 8304 pLoop->cId = '0'; |
| 8305 #endif |
| 8306 return 1; |
| 8307 } |
| 8308 return 0; |
| 8309 } |
| 8310 |
| 8311 /* |
| 8312 ** Generate the beginning of the loop used for WHERE clause processing. |
| 8313 ** The return value is a pointer to an opaque structure that contains |
| 8314 ** information needed to terminate the loop. Later, the calling routine |
| 8315 ** should invoke sqlite3WhereEnd() with the return value of this function |
| 8316 ** in order to complete the WHERE clause processing. |
| 8317 ** |
| 8318 ** If an error occurs, this routine returns NULL. |
| 8319 ** |
| 8320 ** The basic idea is to do a nested loop, one loop for each table in |
| 8321 ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 8322 ** same as a SELECT with only a single table in the FROM clause.) For |
| 8323 ** example, if the SQL is this: |
| 8324 ** |
| 8325 ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 8326 ** |
| 8327 ** Then the code generated is conceptually like the following: |
| 8328 ** |
| 8329 ** foreach row1 in t1 do \ Code generated |
| 8330 ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
| 8331 ** foreach row3 in t3 do / |
| 8332 ** ... |
| 8333 ** end \ Code generated |
| 8334 ** end |-- by sqlite3WhereEnd() |
| 8335 ** end / |
| 8336 ** |
| 8337 ** Note that the loops might not be nested in the order in which they |
| 8338 ** appear in the FROM clause if a different order is better able to make |
| 8339 ** use of indices. Note also that when the IN operator appears in |
| 8340 ** the WHERE clause, it might result in additional nested loops for |
| 8341 ** scanning through all values on the right-hand side of the IN. |
| 8342 ** |
| 8343 ** There are Btree cursors associated with each table. t1 uses cursor |
| 8344 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 8345 ** And so forth. This routine generates code to open those VDBE cursors |
| 8346 ** and sqlite3WhereEnd() generates the code to close them. |
| 8347 ** |
| 8348 ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 8349 ** in pTabList pointing at their appropriate entries. The [...] code |
| 8350 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
| 8351 ** data from the various tables of the loop. |
| 8352 ** |
| 8353 ** If the WHERE clause is empty, the foreach loops must each scan their |
| 8354 ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 8355 ** the tables have indices and there are terms in the WHERE clause that |
| 8356 ** refer to those indices, a complete table scan can be avoided and the |
| 8357 ** code will run much faster. Most of the work of this routine is checking |
| 8358 ** to see if there are indices that can be used to speed up the loop. |
| 8359 ** |
| 8360 ** Terms of the WHERE clause are also used to limit which rows actually |
| 8361 ** make it to the "..." in the middle of the loop. After each "foreach", |
| 8362 ** terms of the WHERE clause that use only terms in that loop and outer |
| 8363 ** loops are evaluated and if false a jump is made around all subsequent |
| 8364 ** inner loops (or around the "..." if the test occurs within the inner- |
| 8365 ** most loop) |
| 8366 ** |
| 8367 ** OUTER JOINS |
| 8368 ** |
| 8369 ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 8370 ** |
| 8371 ** foreach row1 in t1 do |
| 8372 ** flag = 0 |
| 8373 ** foreach row2 in t2 do |
| 8374 ** start: |
| 8375 ** ... |
| 8376 ** flag = 1 |
| 8377 ** end |
| 8378 ** if flag==0 then |
| 8379 ** move the row2 cursor to a null row |
| 8380 ** goto start |
| 8381 ** fi |
| 8382 ** end |
| 8383 ** |
| 8384 ** ORDER BY CLAUSE PROCESSING |
| 8385 ** |
| 8386 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 8387 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
| 8388 ** if there is one. If there is no ORDER BY clause or if this routine |
| 8389 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
| 8390 ** |
| 8391 ** The iIdxCur parameter is the cursor number of an index. If |
| 8392 ** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index |
| 8393 ** to use for OR clause processing. The WHERE clause should use this |
| 8394 ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 8395 ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 8396 ** be used to compute the appropriate cursor depending on which index is |
| 8397 ** used. |
| 8398 */ |
| 8399 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( |
| 8400 Parse *pParse, /* The parser context */ |
| 8401 SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
| 8402 Expr *pWhere, /* The WHERE clause */ |
| 8403 ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
| 8404 ExprList *pDistinctSet, /* Try not to output two rows that duplicate these */ |
| 8405 u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ |
| 8406 int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number |
| 8407 ** If WHERE_USE_LIMIT, then the limit amount */ |
| 8408 ){ |
| 8409 int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
| 8410 int nTabList; /* Number of elements in pTabList */ |
| 8411 WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 8412 Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
| 8413 Bitmask notReady; /* Cursors that are not yet positioned */ |
| 8414 WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
| 8415 WhereMaskSet *pMaskSet; /* The expression mask set */ |
| 8416 WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
| 8417 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
| 8418 int ii; /* Loop counter */ |
| 8419 sqlite3 *db; /* Database connection */ |
| 8420 int rc; /* Return code */ |
| 8421 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */ |
| 8422 |
| 8423 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || ( |
| 8424 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 8425 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 8426 )); |
| 8427 |
| 8428 /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */ |
| 8429 assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 8430 || (wctrlFlags & WHERE_USE_LIMIT)==0 ); |
| 8431 |
| 8432 /* Variable initialization */ |
| 8433 db = pParse->db; |
| 8434 memset(&sWLB, 0, sizeof(sWLB)); |
| 8435 |
| 8436 /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 8437 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 8438 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
| 8439 sWLB.pOrderBy = pOrderBy; |
| 8440 |
| 8441 /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 8442 ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 8443 if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 8444 wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 8445 } |
| 8446 |
| 8447 /* The number of tables in the FROM clause is limited by the number of |
| 8448 ** bits in a Bitmask |
| 8449 */ |
| 8450 testcase( pTabList->nSrc==BMS ); |
| 8451 if( pTabList->nSrc>BMS ){ |
| 8452 sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
| 8453 return 0; |
| 8454 } |
| 8455 |
| 8456 /* This function normally generates a nested loop for all tables in |
| 8457 ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should |
| 8458 ** only generate code for the first table in pTabList and assume that |
| 8459 ** any cursors associated with subsequent tables are uninitialized. |
| 8460 */ |
| 8461 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc; |
| 8462 |
| 8463 /* Allocate and initialize the WhereInfo structure that will become the |
| 8464 ** return value. A single allocation is used to store the WhereInfo |
| 8465 ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 8466 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 8467 ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 8468 ** some architectures. Hence the ROUND8() below. |
| 8469 */ |
| 8470 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
| 8471 pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop)); |
| 8472 if( db->mallocFailed ){ |
| 8473 sqlite3DbFree(db, pWInfo); |
| 8474 pWInfo = 0; |
| 8475 goto whereBeginError; |
| 8476 } |
| 8477 pWInfo->pParse = pParse; |
| 8478 pWInfo->pTabList = pTabList; |
| 8479 pWInfo->pOrderBy = pOrderBy; |
| 8480 pWInfo->pDistinctSet = pDistinctSet; |
| 8481 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
| 8482 pWInfo->nLevel = nTabList; |
| 8483 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); |
| 8484 pWInfo->wctrlFlags = wctrlFlags; |
| 8485 pWInfo->iLimit = iAuxArg; |
| 8486 pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
| 8487 memset(&pWInfo->nOBSat, 0, |
| 8488 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); |
| 8489 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); |
| 8490 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ |
| 8491 pMaskSet = &pWInfo->sMaskSet; |
| 8492 sWLB.pWInfo = pWInfo; |
| 8493 sWLB.pWC = &pWInfo->sWC; |
| 8494 sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 8495 assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
| 8496 whereLoopInit(sWLB.pNew); |
| 8497 #ifdef SQLITE_DEBUG |
| 8498 sWLB.pNew->cId = '*'; |
| 8499 #endif |
| 8500 |
| 8501 /* Split the WHERE clause into separate subexpressions where each |
| 8502 ** subexpression is separated by an AND operator. |
| 8503 */ |
| 8504 initMaskSet(pMaskSet); |
| 8505 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo); |
| 8506 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND); |
| 8507 |
| 8508 /* Special case: a WHERE clause that is constant. Evaluate the |
| 8509 ** expression and either jump over all of the code or fall thru. |
| 8510 */ |
| 8511 for(ii=0; ii<sWLB.pWC->nTerm; ii++){ |
| 8512 if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ |
| 8513 sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, |
| 8514 SQLITE_JUMPIFNULL); |
| 8515 sWLB.pWC->a[ii].wtFlags |= TERM_CODED; |
| 8516 } |
| 8517 } |
| 8518 |
| 8519 /* Special case: No FROM clause |
| 8520 */ |
| 8521 if( nTabList==0 ){ |
| 8522 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; |
| 8523 if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 8524 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 8525 } |
| 8526 } |
| 8527 |
| 8528 /* Assign a bit from the bitmask to every term in the FROM clause. |
| 8529 ** |
| 8530 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. |
| 8531 ** |
| 8532 ** The rule of the previous sentence ensures thta if X is the bitmask for |
| 8533 ** a table T, then X-1 is the bitmask for all other tables to the left of T. |
| 8534 ** Knowing the bitmask for all tables to the left of a left join is |
| 8535 ** important. Ticket #3015. |
| 8536 ** |
| 8537 ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 8538 ** pTabList, not just the first nTabList tables. nTabList is normally |
| 8539 ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 8540 ** WHERE_OR_SUBCLAUSE flag is set. |
| 8541 */ |
| 8542 for(ii=0; ii<pTabList->nSrc; ii++){ |
| 8543 createMask(pMaskSet, pTabList->a[ii].iCursor); |
| 8544 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); |
| 8545 } |
| 8546 #ifdef SQLITE_DEBUG |
| 8547 for(ii=0; ii<pTabList->nSrc; ii++){ |
| 8548 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); |
| 8549 assert( m==MASKBIT(ii) ); |
| 8550 } |
| 8551 #endif |
| 8552 |
| 8553 /* Analyze all of the subexpressions. */ |
| 8554 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); |
| 8555 if( db->mallocFailed ) goto whereBeginError; |
| 8556 |
| 8557 if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 8558 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pDistinctSet) ){ |
| 8559 /* The DISTINCT marking is pointless. Ignore it. */ |
| 8560 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 8561 }else if( pOrderBy==0 ){ |
| 8562 /* Try to ORDER BY the result set to make distinct processing easier */ |
| 8563 pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
| 8564 pWInfo->pOrderBy = pDistinctSet; |
| 8565 } |
| 8566 } |
| 8567 |
| 8568 /* Construct the WhereLoop objects */ |
| 8569 #if defined(WHERETRACE_ENABLED) |
| 8570 if( sqlite3WhereTrace & 0xffff ){ |
| 8571 sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags); |
| 8572 if( wctrlFlags & WHERE_USE_LIMIT ){ |
| 8573 sqlite3DebugPrintf(", limit: %d", iAuxArg); |
| 8574 } |
| 8575 sqlite3DebugPrintf(")\n"); |
| 8576 } |
| 8577 if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ |
| 8578 sqlite3WhereClausePrint(sWLB.pWC); |
| 8579 } |
| 8580 #endif |
| 8581 |
| 8582 if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
| 8583 rc = whereLoopAddAll(&sWLB); |
| 8584 if( rc ) goto whereBeginError; |
| 8585 |
| 8586 #ifdef WHERETRACE_ENABLED |
| 8587 if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ |
| 8588 WhereLoop *p; |
| 8589 int i; |
| 8590 static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 8591 "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
| 8592 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 8593 p->cId = zLabel[i%sizeof(zLabel)]; |
| 8594 whereLoopPrint(p, sWLB.pWC); |
| 8595 } |
| 8596 } |
| 8597 #endif |
| 8598 |
| 8599 wherePathSolver(pWInfo, 0); |
| 8600 if( db->mallocFailed ) goto whereBeginError; |
| 8601 if( pWInfo->pOrderBy ){ |
| 8602 wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
| 8603 if( db->mallocFailed ) goto whereBeginError; |
| 8604 } |
| 8605 } |
| 8606 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
| 8607 pWInfo->revMask = ALLBITS; |
| 8608 } |
| 8609 if( pParse->nErr || NEVER(db->mallocFailed) ){ |
| 8610 goto whereBeginError; |
| 8611 } |
| 8612 #ifdef WHERETRACE_ENABLED |
| 8613 if( sqlite3WhereTrace ){ |
| 8614 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 8615 if( pWInfo->nOBSat>0 ){ |
| 8616 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
| 8617 } |
| 8618 switch( pWInfo->eDistinct ){ |
| 8619 case WHERE_DISTINCT_UNIQUE: { |
| 8620 sqlite3DebugPrintf(" DISTINCT=unique"); |
| 8621 break; |
| 8622 } |
| 8623 case WHERE_DISTINCT_ORDERED: { |
| 8624 sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 8625 break; |
| 8626 } |
| 8627 case WHERE_DISTINCT_UNORDERED: { |
| 8628 sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 8629 break; |
| 8630 } |
| 8631 } |
| 8632 sqlite3DebugPrintf("\n"); |
| 8633 for(ii=0; ii<pWInfo->nLevel; ii++){ |
| 8634 whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
| 8635 } |
| 8636 } |
| 8637 #endif |
| 8638 /* Attempt to omit tables from the join that do not effect the result */ |
| 8639 if( pWInfo->nLevel>=2 |
| 8640 && pDistinctSet!=0 |
| 8641 && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 8642 ){ |
| 8643 Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pDistinctSet); |
| 8644 if( sWLB.pOrderBy ){ |
| 8645 tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); |
| 8646 } |
| 8647 while( pWInfo->nLevel>=2 ){ |
| 8648 WhereTerm *pTerm, *pEnd; |
| 8649 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
| 8650 if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break; |
| 8651 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 8652 && (pLoop->wsFlags & WHERE_ONEROW)==0 |
| 8653 ){ |
| 8654 break; |
| 8655 } |
| 8656 if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
| 8657 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 8658 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 8659 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 8660 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 8661 ){ |
| 8662 break; |
| 8663 } |
| 8664 } |
| 8665 if( pTerm<pEnd ) break; |
| 8666 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 8667 pWInfo->nLevel--; |
| 8668 nTabList--; |
| 8669 } |
| 8670 } |
| 8671 WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
| 8672 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
| 8673 |
| 8674 /* If the caller is an UPDATE or DELETE statement that is requesting |
| 8675 ** to use a one-pass algorithm, determine if this is appropriate. |
| 8676 */ |
| 8677 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
| 8678 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){ |
| 8679 int wsFlags = pWInfo->a[0].pWLoop->wsFlags; |
| 8680 int bOnerow = (wsFlags & WHERE_ONEROW)!=0; |
| 8681 if( bOnerow |
| 8682 || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0 |
| 8683 && 0==(wsFlags & WHERE_VIRTUALTABLE)) |
| 8684 ){ |
| 8685 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI; |
| 8686 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){ |
| 8687 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){ |
| 8688 bFordelete = OPFLAG_FORDELETE; |
| 8689 } |
| 8690 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY); |
| 8691 } |
| 8692 } |
| 8693 } |
| 8694 |
| 8695 /* Open all tables in the pTabList and any indices selected for |
| 8696 ** searching those tables. |
| 8697 */ |
| 8698 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
| 8699 Table *pTab; /* Table to open */ |
| 8700 int iDb; /* Index of database containing table/index */ |
| 8701 struct SrcList_item *pTabItem; |
| 8702 |
| 8703 pTabItem = &pTabList->a[pLevel->iFrom]; |
| 8704 pTab = pTabItem->pTab; |
| 8705 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 8706 pLoop = pLevel->pWLoop; |
| 8707 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
| 8708 /* Do nothing */ |
| 8709 }else |
| 8710 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 8711 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 8712 const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
| 8713 int iCur = pTabItem->iCursor; |
| 8714 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
| 8715 }else if( IsVirtual(pTab) ){ |
| 8716 /* noop */ |
| 8717 }else |
| 8718 #endif |
| 8719 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 8720 && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){ |
| 8721 int op = OP_OpenRead; |
| 8722 if( pWInfo->eOnePass!=ONEPASS_OFF ){ |
| 8723 op = OP_OpenWrite; |
| 8724 pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 8725 }; |
| 8726 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
| 8727 assert( pTabItem->iCursor==pLevel->iTabCur ); |
| 8728 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); |
| 8729 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); |
| 8730 if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){ |
| 8731 Bitmask b = pTabItem->colUsed; |
| 8732 int n = 0; |
| 8733 for(; b; b=b>>1, n++){} |
| 8734 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32); |
| 8735 assert( n<=pTab->nCol ); |
| 8736 } |
| 8737 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 8738 if( pLoop->u.btree.pIndex!=0 ){ |
| 8739 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete); |
| 8740 }else |
| 8741 #endif |
| 8742 { |
| 8743 sqlite3VdbeChangeP5(v, bFordelete); |
| 8744 } |
| 8745 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 8746 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, |
| 8747 (const u8*)&pTabItem->colUsed, P4_INT64); |
| 8748 #endif |
| 8749 }else{ |
| 8750 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 8751 } |
| 8752 if( pLoop->wsFlags & WHERE_INDEXED ){ |
| 8753 Index *pIx = pLoop->u.btree.pIndex; |
| 8754 int iIndexCur; |
| 8755 int op = OP_OpenRead; |
| 8756 /* iAuxArg is always set if to a positive value if ONEPASS is possible */ |
| 8757 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); |
| 8758 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) |
| 8759 && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 |
| 8760 ){ |
| 8761 /* This is one term of an OR-optimization using the PRIMARY KEY of a |
| 8762 ** WITHOUT ROWID table. No need for a separate index */ |
| 8763 iIndexCur = pLevel->iTabCur; |
| 8764 op = 0; |
| 8765 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){ |
| 8766 Index *pJ = pTabItem->pTab->pIndex; |
| 8767 iIndexCur = iAuxArg; |
| 8768 assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 8769 while( ALWAYS(pJ) && pJ!=pIx ){ |
| 8770 iIndexCur++; |
| 8771 pJ = pJ->pNext; |
| 8772 } |
| 8773 op = OP_OpenWrite; |
| 8774 pWInfo->aiCurOnePass[1] = iIndexCur; |
| 8775 }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){ |
| 8776 iIndexCur = iAuxArg; |
| 8777 op = OP_ReopenIdx; |
| 8778 }else{ |
| 8779 iIndexCur = pParse->nTab++; |
| 8780 } |
| 8781 pLevel->iIdxCur = iIndexCur; |
| 8782 assert( pIx->pSchema==pTab->pSchema ); |
| 8783 assert( iIndexCur>=0 ); |
| 8784 if( op ){ |
| 8785 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 8786 sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
| 8787 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 |
| 8788 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0 |
| 8789 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 |
| 8790 ){ |
| 8791 sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ |
| 8792 } |
| 8793 VdbeComment((v, "%s", pIx->zName)); |
| 8794 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
| 8795 { |
| 8796 u64 colUsed = 0; |
| 8797 int ii, jj; |
| 8798 for(ii=0; ii<pIx->nColumn; ii++){ |
| 8799 jj = pIx->aiColumn[ii]; |
| 8800 if( jj<0 ) continue; |
| 8801 if( jj>63 ) jj = 63; |
| 8802 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue; |
| 8803 colUsed |= ((u64)1)<<(ii<63 ? ii : 63); |
| 8804 } |
| 8805 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0, |
| 8806 (u8*)&colUsed, P4_INT64); |
| 8807 } |
| 8808 #endif /* SQLITE_ENABLE_COLUMN_USED_MASK */ |
| 8809 } |
| 8810 } |
| 8811 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
| 8812 } |
| 8813 pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
| 8814 if( db->mallocFailed ) goto whereBeginError; |
| 8815 |
| 8816 /* Generate the code to do the search. Each iteration of the for |
| 8817 ** loop below generates code for a single nested loop of the VM |
| 8818 ** program. |
| 8819 */ |
| 8820 notReady = ~(Bitmask)0; |
| 8821 for(ii=0; ii<nTabList; ii++){ |
| 8822 int addrExplain; |
| 8823 int wsFlags; |
| 8824 pLevel = &pWInfo->a[ii]; |
| 8825 wsFlags = pLevel->pWLoop->wsFlags; |
| 8826 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 8827 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 8828 constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 8829 &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 8830 if( db->mallocFailed ) goto whereBeginError; |
| 8831 } |
| 8832 #endif |
| 8833 addrExplain = sqlite3WhereExplainOneScan( |
| 8834 pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 8835 ); |
| 8836 pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
| 8837 notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); |
| 8838 pWInfo->iContinue = pLevel->addrCont; |
| 8839 if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){ |
| 8840 sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); |
| 8841 } |
| 8842 } |
| 8843 |
| 8844 /* Done. */ |
| 8845 VdbeModuleComment((v, "Begin WHERE-core")); |
| 8846 return pWInfo; |
| 8847 |
| 8848 /* Jump here if malloc fails */ |
| 8849 whereBeginError: |
| 8850 if( pWInfo ){ |
| 8851 pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 8852 whereInfoFree(db, pWInfo); |
| 8853 } |
| 8854 return 0; |
| 8855 } |
| 8856 |
| 8857 /* |
| 8858 ** Generate the end of the WHERE loop. See comments on |
| 8859 ** sqlite3WhereBegin() for additional information. |
| 8860 */ |
| 8861 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ |
| 8862 Parse *pParse = pWInfo->pParse; |
| 8863 Vdbe *v = pParse->pVdbe; |
| 8864 int i; |
| 8865 WhereLevel *pLevel; |
| 8866 WhereLoop *pLoop; |
| 8867 SrcList *pTabList = pWInfo->pTabList; |
| 8868 sqlite3 *db = pParse->db; |
| 8869 |
| 8870 /* Generate loop termination code. |
| 8871 */ |
| 8872 VdbeModuleComment((v, "End WHERE-core")); |
| 8873 sqlite3ExprCacheClear(pParse); |
| 8874 for(i=pWInfo->nLevel-1; i>=0; i--){ |
| 8875 int addr; |
| 8876 pLevel = &pWInfo->a[i]; |
| 8877 pLoop = pLevel->pWLoop; |
| 8878 sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
| 8879 if( pLevel->op!=OP_Noop ){ |
| 8880 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); |
| 8881 sqlite3VdbeChangeP5(v, pLevel->p5); |
| 8882 VdbeCoverage(v); |
| 8883 VdbeCoverageIf(v, pLevel->op==OP_Next); |
| 8884 VdbeCoverageIf(v, pLevel->op==OP_Prev); |
| 8885 VdbeCoverageIf(v, pLevel->op==OP_VNext); |
| 8886 } |
| 8887 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
| 8888 struct InLoop *pIn; |
| 8889 int j; |
| 8890 sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
| 8891 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ |
| 8892 sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
| 8893 if( pIn->eEndLoopOp!=OP_Noop ){ |
| 8894 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
| 8895 VdbeCoverage(v); |
| 8896 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 8897 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
| 8898 } |
| 8899 sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
| 8900 } |
| 8901 } |
| 8902 sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 8903 if( pLevel->addrSkip ){ |
| 8904 sqlite3VdbeGoto(v, pLevel->addrSkip); |
| 8905 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
| 8906 sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 8907 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
| 8908 } |
| 8909 #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
| 8910 if( pLevel->addrLikeRep ){ |
| 8911 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1), |
| 8912 pLevel->addrLikeRep); |
| 8913 VdbeCoverage(v); |
| 8914 } |
| 8915 #endif |
| 8916 if( pLevel->iLeftJoin ){ |
| 8917 int ws = pLoop->wsFlags; |
| 8918 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
| 8919 assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 ); |
| 8920 if( (ws & WHERE_IDX_ONLY)==0 ){ |
| 8921 sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 8922 } |
| 8923 if( (ws & WHERE_INDEXED) |
| 8924 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx) |
| 8925 ){ |
| 8926 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
| 8927 } |
| 8928 if( pLevel->op==OP_Return ){ |
| 8929 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 8930 }else{ |
| 8931 sqlite3VdbeGoto(v, pLevel->addrFirst); |
| 8932 } |
| 8933 sqlite3VdbeJumpHere(v, addr); |
| 8934 } |
| 8935 VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
| 8936 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
| 8937 } |
| 8938 |
| 8939 /* The "break" point is here, just past the end of the outer loop. |
| 8940 ** Set it. |
| 8941 */ |
| 8942 sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
| 8943 |
| 8944 assert( pWInfo->nLevel<=pTabList->nSrc ); |
| 8945 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
| 8946 int k, last; |
| 8947 VdbeOp *pOp; |
| 8948 Index *pIdx = 0; |
| 8949 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
| 8950 Table *pTab = pTabItem->pTab; |
| 8951 assert( pTab!=0 ); |
| 8952 pLoop = pLevel->pWLoop; |
| 8953 |
| 8954 /* For a co-routine, change all OP_Column references to the table of |
| 8955 ** the co-routine into OP_Copy of result contained in a register. |
| 8956 ** OP_Rowid becomes OP_Null. |
| 8957 */ |
| 8958 if( pTabItem->fg.viaCoroutine && !db->mallocFailed ){ |
| 8959 translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, |
| 8960 pTabItem->regResult, 0); |
| 8961 continue; |
| 8962 } |
| 8963 |
| 8964 /* If this scan uses an index, make VDBE code substitutions to read data |
| 8965 ** from the index instead of from the table where possible. In some cases |
| 8966 ** this optimization prevents the table from ever being read, which can |
| 8967 ** yield a significant performance boost. |
| 8968 ** |
| 8969 ** Calls to the code generator in between sqlite3WhereBegin and |
| 8970 ** sqlite3WhereEnd will have created code that references the table |
| 8971 ** directly. This loop scans all that code looking for opcodes |
| 8972 ** that reference the table and converts them into opcodes that |
| 8973 ** reference the index. |
| 8974 */ |
| 8975 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 8976 pIdx = pLoop->u.btree.pIndex; |
| 8977 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 8978 pIdx = pLevel->u.pCovidx; |
| 8979 } |
| 8980 if( pIdx |
| 8981 && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable)) |
| 8982 && !db->mallocFailed |
| 8983 ){ |
| 8984 last = sqlite3VdbeCurrentAddr(v); |
| 8985 k = pLevel->addrBody; |
| 8986 pOp = sqlite3VdbeGetOp(v, k); |
| 8987 for(; k<last; k++, pOp++){ |
| 8988 if( pOp->p1!=pLevel->iTabCur ) continue; |
| 8989 if( pOp->opcode==OP_Column ){ |
| 8990 int x = pOp->p2; |
| 8991 assert( pIdx->pTable==pTab ); |
| 8992 if( !HasRowid(pTab) ){ |
| 8993 Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 8994 x = pPk->aiColumn[x]; |
| 8995 assert( x>=0 ); |
| 8996 } |
| 8997 x = sqlite3ColumnOfIndex(pIdx, x); |
| 8998 if( x>=0 ){ |
| 8999 pOp->p2 = x; |
| 9000 pOp->p1 = pLevel->iIdxCur; |
| 9001 } |
| 9002 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 |
| 9003 || pWInfo->eOnePass ); |
| 9004 }else if( pOp->opcode==OP_Rowid ){ |
| 9005 pOp->p1 = pLevel->iIdxCur; |
| 9006 pOp->opcode = OP_IdxRowid; |
| 9007 } |
| 9008 } |
| 9009 } |
| 9010 } |
| 9011 |
| 9012 /* Final cleanup |
| 9013 */ |
| 9014 pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 9015 whereInfoFree(db, pWInfo); |
| 9016 return; |
| 9017 } |
| 9018 |
| 9019 /************** End of where.c ***********************************************/ |
| 9020 /************** Begin file parse.c *******************************************/ |
| 9021 /* |
| 9022 ** 2000-05-29 |
| 9023 ** |
| 9024 ** The author disclaims copyright to this source code. In place of |
| 9025 ** a legal notice, here is a blessing: |
| 9026 ** |
| 9027 ** May you do good and not evil. |
| 9028 ** May you find forgiveness for yourself and forgive others. |
| 9029 ** May you share freely, never taking more than you give. |
| 9030 ** |
| 9031 ************************************************************************* |
| 9032 ** Driver template for the LEMON parser generator. |
| 9033 ** |
| 9034 ** The "lemon" program processes an LALR(1) input grammar file, then uses |
| 9035 ** this template to construct a parser. The "lemon" program inserts text |
| 9036 ** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the |
| 9037 ** interstitial "-" characters) contained in this template is changed into |
| 9038 ** the value of the %name directive from the grammar. Otherwise, the content |
| 9039 ** of this template is copied straight through into the generate parser |
| 9040 ** source file. |
| 9041 ** |
| 9042 ** The following is the concatenation of all %include directives from the |
| 9043 ** input grammar file: |
| 9044 */ |
| 9045 /* #include <stdio.h> */ |
| 9046 /************ Begin %include sections from the grammar ************************/ |
| 9047 |
| 9048 /* #include "sqliteInt.h" */ |
| 9049 |
| 9050 /* |
| 9051 ** Disable all error recovery processing in the parser push-down |
| 9052 ** automaton. |
| 9053 */ |
| 9054 #define YYNOERRORRECOVERY 1 |
| 9055 |
| 9056 /* |
| 9057 ** Make yytestcase() the same as testcase() |
| 9058 */ |
| 9059 #define yytestcase(X) testcase(X) |
| 9060 |
| 9061 /* |
| 9062 ** Indicate that sqlite3ParserFree() will never be called with a null |
| 9063 ** pointer. |
| 9064 */ |
| 9065 #define YYPARSEFREENEVERNULL 1 |
| 9066 |
| 9067 /* |
| 9068 ** In the amalgamation, the parse.c file generated by lemon and the |
| 9069 ** tokenize.c file are concatenated. In that case, sqlite3RunParser() |
| 9070 ** has access to the the size of the yyParser object and so the parser |
| 9071 ** engine can be allocated from stack. In that case, only the |
| 9072 ** sqlite3ParserInit() and sqlite3ParserFinalize() routines are invoked |
| 9073 ** and the sqlite3ParserAlloc() and sqlite3ParserFree() routines can be |
| 9074 ** omitted. |
| 9075 */ |
| 9076 #ifdef SQLITE_AMALGAMATION |
| 9077 # define sqlite3Parser_ENGINEALWAYSONSTACK 1 |
| 9078 #endif |
| 9079 |
| 9080 /* |
| 9081 ** Alternative datatype for the argument to the malloc() routine passed |
| 9082 ** into sqlite3ParserAlloc(). The default is size_t. |
| 9083 */ |
| 9084 #define YYMALLOCARGTYPE u64 |
| 9085 |
| 9086 /* |
| 9087 ** An instance of this structure holds information about the |
| 9088 ** LIMIT clause of a SELECT statement. |
| 9089 */ |
| 9090 struct LimitVal { |
| 9091 Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ |
| 9092 Expr *pOffset; /* The OFFSET expression. NULL if there is none */ |
| 9093 }; |
| 9094 |
| 9095 /* |
| 9096 ** An instance of the following structure describes the event of a |
| 9097 ** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, |
| 9098 ** TK_DELETE, or TK_INSTEAD. If the event is of the form |
| 9099 ** |
| 9100 ** UPDATE ON (a,b,c) |
| 9101 ** |
| 9102 ** Then the "b" IdList records the list "a,b,c". |
| 9103 */ |
| 9104 struct TrigEvent { int a; IdList * b; }; |
| 9105 |
| 9106 /* |
| 9107 ** Disable lookaside memory allocation for objects that might be |
| 9108 ** shared across database connections. |
| 9109 */ |
| 9110 static void disableLookaside(Parse *pParse){ |
| 9111 pParse->disableLookaside++; |
| 9112 pParse->db->lookaside.bDisable++; |
| 9113 } |
| 9114 |
| 9115 |
| 9116 /* |
| 9117 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for |
| 9118 ** all elements in the list. And make sure list length does not exceed |
| 9119 ** SQLITE_LIMIT_COMPOUND_SELECT. |
| 9120 */ |
| 9121 static void parserDoubleLinkSelect(Parse *pParse, Select *p){ |
| 9122 if( p->pPrior ){ |
| 9123 Select *pNext = 0, *pLoop; |
| 9124 int mxSelect, cnt = 0; |
| 9125 for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){ |
| 9126 pLoop->pNext = pNext; |
| 9127 pLoop->selFlags |= SF_Compound; |
| 9128 } |
| 9129 if( (p->selFlags & SF_MultiValue)==0 && |
| 9130 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && |
| 9131 cnt>mxSelect |
| 9132 ){ |
| 9133 sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); |
| 9134 } |
| 9135 } |
| 9136 } |
| 9137 |
| 9138 /* This is a utility routine used to set the ExprSpan.zStart and |
| 9139 ** ExprSpan.zEnd values of pOut so that the span covers the complete |
| 9140 ** range of text beginning with pStart and going to the end of pEnd. |
| 9141 */ |
| 9142 static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){ |
| 9143 pOut->zStart = pStart->z; |
| 9144 pOut->zEnd = &pEnd->z[pEnd->n]; |
| 9145 } |
| 9146 |
| 9147 /* Construct a new Expr object from a single identifier. Use the |
| 9148 ** new Expr to populate pOut. Set the span of pOut to be the identifier |
| 9149 ** that created the expression. |
| 9150 */ |
| 9151 static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token t){ |
| 9152 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1); |
| 9153 if( p ){ |
| 9154 memset(p, 0, sizeof(Expr)); |
| 9155 p->op = (u8)op; |
| 9156 p->flags = EP_Leaf; |
| 9157 p->iAgg = -1; |
| 9158 p->u.zToken = (char*)&p[1]; |
| 9159 memcpy(p->u.zToken, t.z, t.n); |
| 9160 p->u.zToken[t.n] = 0; |
| 9161 if( sqlite3Isquote(p->u.zToken[0]) ){ |
| 9162 if( p->u.zToken[0]=='"' ) p->flags |= EP_DblQuoted; |
| 9163 sqlite3Dequote(p->u.zToken); |
| 9164 } |
| 9165 #if SQLITE_MAX_EXPR_DEPTH>0 |
| 9166 p->nHeight = 1; |
| 9167 #endif |
| 9168 } |
| 9169 pOut->pExpr = p; |
| 9170 pOut->zStart = t.z; |
| 9171 pOut->zEnd = &t.z[t.n]; |
| 9172 } |
| 9173 |
| 9174 /* This routine constructs a binary expression node out of two ExprSpan |
| 9175 ** objects and uses the result to populate a new ExprSpan object. |
| 9176 */ |
| 9177 static void spanBinaryExpr( |
| 9178 Parse *pParse, /* The parsing context. Errors accumulate here */ |
| 9179 int op, /* The binary operation */ |
| 9180 ExprSpan *pLeft, /* The left operand, and output */ |
| 9181 ExprSpan *pRight /* The right operand */ |
| 9182 ){ |
| 9183 pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr); |
| 9184 pLeft->zEnd = pRight->zEnd; |
| 9185 } |
| 9186 |
| 9187 /* If doNot is true, then add a TK_NOT Expr-node wrapper around the |
| 9188 ** outside of *ppExpr. |
| 9189 */ |
| 9190 static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){ |
| 9191 if( doNot ){ |
| 9192 pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0); |
| 9193 } |
| 9194 } |
| 9195 |
| 9196 /* Construct an expression node for a unary postfix operator |
| 9197 */ |
| 9198 static void spanUnaryPostfix( |
| 9199 Parse *pParse, /* Parsing context to record errors */ |
| 9200 int op, /* The operator */ |
| 9201 ExprSpan *pOperand, /* The operand, and output */ |
| 9202 Token *pPostOp /* The operand token for setting the span */ |
| 9203 ){ |
| 9204 pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0); |
| 9205 pOperand->zEnd = &pPostOp->z[pPostOp->n]; |
| 9206 } |
| 9207 |
| 9208 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a |
| 9209 ** unary TK_ISNULL or TK_NOTNULL expression. */ |
| 9210 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ |
| 9211 sqlite3 *db = pParse->db; |
| 9212 if( pA && pY && pY->op==TK_NULL ){ |
| 9213 pA->op = (u8)op; |
| 9214 sqlite3ExprDelete(db, pA->pRight); |
| 9215 pA->pRight = 0; |
| 9216 } |
| 9217 } |
| 9218 |
| 9219 /* Construct an expression node for a unary prefix operator |
| 9220 */ |
| 9221 static void spanUnaryPrefix( |
| 9222 ExprSpan *pOut, /* Write the new expression node here */ |
| 9223 Parse *pParse, /* Parsing context to record errors */ |
| 9224 int op, /* The operator */ |
| 9225 ExprSpan *pOperand, /* The operand */ |
| 9226 Token *pPreOp /* The operand token for setting the span */ |
| 9227 ){ |
| 9228 pOut->zStart = pPreOp->z; |
| 9229 pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0); |
| 9230 pOut->zEnd = pOperand->zEnd; |
| 9231 } |
| 9232 |
| 9233 /* Add a single new term to an ExprList that is used to store a |
| 9234 ** list of identifiers. Report an error if the ID list contains |
| 9235 ** a COLLATE clause or an ASC or DESC keyword, except ignore the |
| 9236 ** error while parsing a legacy schema. |
| 9237 */ |
| 9238 static ExprList *parserAddExprIdListTerm( |
| 9239 Parse *pParse, |
| 9240 ExprList *pPrior, |
| 9241 Token *pIdToken, |
| 9242 int hasCollate, |
| 9243 int sortOrder |
| 9244 ){ |
| 9245 ExprList *p = sqlite3ExprListAppend(pParse, pPrior, 0); |
| 9246 if( (hasCollate || sortOrder!=SQLITE_SO_UNDEFINED) |
| 9247 && pParse->db->init.busy==0 |
| 9248 ){ |
| 9249 sqlite3ErrorMsg(pParse, "syntax error after column name \"%.*s\"", |
| 9250 pIdToken->n, pIdToken->z); |
| 9251 } |
| 9252 sqlite3ExprListSetName(pParse, p, pIdToken, 1); |
| 9253 return p; |
| 9254 } |
| 9255 /**************** End of %include directives **********************************/ |
| 9256 /* These constants specify the various numeric values for terminal symbols |
| 9257 ** in a format understandable to "makeheaders". This section is blank unless |
| 9258 ** "lemon" is run with the "-m" command-line option. |
| 9259 ***************** Begin makeheaders token definitions *************************/ |
| 9260 /**************** End makeheaders token definitions ***************************/ |
| 9261 |
| 9262 /* The next sections is a series of control #defines. |
| 9263 ** various aspects of the generated parser. |
| 9264 ** YYCODETYPE is the data type used to store the integer codes |
| 9265 ** that represent terminal and non-terminal symbols. |
| 9266 ** "unsigned char" is used if there are fewer than |
| 9267 ** 256 symbols. Larger types otherwise. |
| 9268 ** YYNOCODE is a number of type YYCODETYPE that is not used for |
| 9269 ** any terminal or nonterminal symbol. |
| 9270 ** YYFALLBACK If defined, this indicates that one or more tokens |
| 9271 ** (also known as: "terminal symbols") have fall-back |
| 9272 ** values which should be used if the original symbol |
| 9273 ** would not parse. This permits keywords to sometimes |
| 9274 ** be used as identifiers, for example. |
| 9275 ** YYACTIONTYPE is the data type used for "action codes" - numbers |
| 9276 ** that indicate what to do in response to the next |
| 9277 ** token. |
| 9278 ** sqlite3ParserTOKENTYPE is the data type used for minor type for termin
al |
| 9279 ** symbols. Background: A "minor type" is a semantic |
| 9280 ** value associated with a terminal or non-terminal |
| 9281 ** symbols. For example, for an "ID" terminal symbol, |
| 9282 ** the minor type might be the name of the identifier. |
| 9283 ** Each non-terminal can have a different minor type. |
| 9284 ** Terminal symbols all have the same minor type, though. |
| 9285 ** This macros defines the minor type for terminal |
| 9286 ** symbols. |
| 9287 ** YYMINORTYPE is the data type used for all minor types. |
| 9288 ** This is typically a union of many types, one of |
| 9289 ** which is sqlite3ParserTOKENTYPE. The entry in the unio
n |
| 9290 ** for terminal symbols is called "yy0". |
| 9291 ** YYSTACKDEPTH is the maximum depth of the parser's stack. If |
| 9292 ** zero the stack is dynamically sized using realloc() |
| 9293 ** sqlite3ParserARG_SDECL A static variable declaration for the %extra_ar
gument |
| 9294 ** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument |
| 9295 ** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser |
| 9296 ** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser |
| 9297 ** YYERRORSYMBOL is the code number of the error symbol. If not |
| 9298 ** defined, then do no error processing. |
| 9299 ** YYNSTATE the combined number of states. |
| 9300 ** YYNRULE the number of rules in the grammar |
| 9301 ** YY_MAX_SHIFT Maximum value for shift actions |
| 9302 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions |
| 9303 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions |
| 9304 ** YY_MIN_REDUCE Maximum value for reduce actions |
| 9305 ** YY_ERROR_ACTION The yy_action[] code for syntax error |
| 9306 ** YY_ACCEPT_ACTION The yy_action[] code for accept |
| 9307 ** YY_NO_ACTION The yy_action[] code for no-op |
| 9308 */ |
| 9309 #ifndef INTERFACE |
| 9310 # define INTERFACE 1 |
| 9311 #endif |
| 9312 /************* Begin control #defines *****************************************/ |
| 9313 #define YYCODETYPE unsigned char |
| 9314 #define YYNOCODE 252 |
| 9315 #define YYACTIONTYPE unsigned short int |
| 9316 #define YYWILDCARD 96 |
| 9317 #define sqlite3ParserTOKENTYPE Token |
| 9318 typedef union { |
| 9319 int yyinit; |
| 9320 sqlite3ParserTOKENTYPE yy0; |
| 9321 Expr* yy72; |
| 9322 TriggerStep* yy145; |
| 9323 ExprList* yy148; |
| 9324 SrcList* yy185; |
| 9325 ExprSpan yy190; |
| 9326 int yy194; |
| 9327 Select* yy243; |
| 9328 IdList* yy254; |
| 9329 With* yy285; |
| 9330 struct TrigEvent yy332; |
| 9331 struct LimitVal yy354; |
| 9332 struct {int value; int mask;} yy497; |
| 9333 } YYMINORTYPE; |
| 9334 #ifndef YYSTACKDEPTH |
| 9335 #define YYSTACKDEPTH 100 |
| 9336 #endif |
| 9337 #define sqlite3ParserARG_SDECL Parse *pParse; |
| 9338 #define sqlite3ParserARG_PDECL ,Parse *pParse |
| 9339 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse |
| 9340 #define sqlite3ParserARG_STORE yypParser->pParse = pParse |
| 9341 #define YYFALLBACK 1 |
| 9342 #define YYNSTATE 456 |
| 9343 #define YYNRULE 332 |
| 9344 #define YY_MAX_SHIFT 455 |
| 9345 #define YY_MIN_SHIFTREDUCE 668 |
| 9346 #define YY_MAX_SHIFTREDUCE 999 |
| 9347 #define YY_MIN_REDUCE 1000 |
| 9348 #define YY_MAX_REDUCE 1331 |
| 9349 #define YY_ERROR_ACTION 1332 |
| 9350 #define YY_ACCEPT_ACTION 1333 |
| 9351 #define YY_NO_ACTION 1334 |
| 9352 /************* End control #defines *******************************************/ |
| 9353 |
| 9354 /* Define the yytestcase() macro to be a no-op if is not already defined |
| 9355 ** otherwise. |
| 9356 ** |
| 9357 ** Applications can choose to define yytestcase() in the %include section |
| 9358 ** to a macro that can assist in verifying code coverage. For production |
| 9359 ** code the yytestcase() macro should be turned off. But it is useful |
| 9360 ** for testing. |
| 9361 */ |
| 9362 #ifndef yytestcase |
| 9363 # define yytestcase(X) |
| 9364 #endif |
| 9365 |
| 9366 |
| 9367 /* Next are the tables used to determine what action to take based on the |
| 9368 ** current state and lookahead token. These tables are used to implement |
| 9369 ** functions that take a state number and lookahead value and return an |
| 9370 ** action integer. |
| 9371 ** |
| 9372 ** Suppose the action integer is N. Then the action is determined as |
| 9373 ** follows |
| 9374 ** |
| 9375 ** 0 <= N <= YY_MAX_SHIFT Shift N. That is, push the lookahead |
| 9376 ** token onto the stack and goto state N. |
| 9377 ** |
| 9378 ** N between YY_MIN_SHIFTREDUCE Shift to an arbitrary state then |
| 9379 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE. |
| 9380 ** |
| 9381 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE |
| 9382 ** and YY_MAX_REDUCE |
| 9383 ** |
| 9384 ** N == YY_ERROR_ACTION A syntax error has occurred. |
| 9385 ** |
| 9386 ** N == YY_ACCEPT_ACTION The parser accepts its input. |
| 9387 ** |
| 9388 ** N == YY_NO_ACTION No such action. Denotes unused |
| 9389 ** slots in the yy_action[] table. |
| 9390 ** |
| 9391 ** The action table is constructed as a single large table named yy_action[]. |
| 9392 ** Given state S and lookahead X, the action is computed as either: |
| 9393 ** |
| 9394 ** (A) N = yy_action[ yy_shift_ofst[S] + X ] |
| 9395 ** (B) N = yy_default[S] |
| 9396 ** |
| 9397 ** The (A) formula is preferred. The B formula is used instead if: |
| 9398 ** (1) The yy_shift_ofst[S]+X value is out of range, or |
| 9399 ** (2) yy_lookahead[yy_shift_ofst[S]+X] is not equal to X, or |
| 9400 ** (3) yy_shift_ofst[S] equal YY_SHIFT_USE_DFLT. |
| 9401 ** (Implementation note: YY_SHIFT_USE_DFLT is chosen so that |
| 9402 ** YY_SHIFT_USE_DFLT+X will be out of range for all possible lookaheads X. |
| 9403 ** Hence only tests (1) and (2) need to be evaluated.) |
| 9404 ** |
| 9405 ** The formulas above are for computing the action when the lookahead is |
| 9406 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after |
| 9407 ** a reduce action) then the yy_reduce_ofst[] array is used in place of |
| 9408 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of |
| 9409 ** YY_SHIFT_USE_DFLT. |
| 9410 ** |
| 9411 ** The following are the tables generated in this section: |
| 9412 ** |
| 9413 ** yy_action[] A single table containing all actions. |
| 9414 ** yy_lookahead[] A table containing the lookahead for each entry in |
| 9415 ** yy_action. Used to detect hash collisions. |
| 9416 ** yy_shift_ofst[] For each state, the offset into yy_action for |
| 9417 ** shifting terminals. |
| 9418 ** yy_reduce_ofst[] For each state, the offset into yy_action for |
| 9419 ** shifting non-terminals after a reduce. |
| 9420 ** yy_default[] Default action for each state. |
| 9421 ** |
| 9422 *********** Begin parsing tables **********************************************/ |
| 9423 #define YY_ACTTAB_COUNT (1567) |
| 9424 static const YYACTIONTYPE yy_action[] = { |
| 9425 /* 0 */ 325, 832, 351, 825, 5, 203, 203, 819, 99, 100, |
| 9426 /* 10 */ 90, 842, 842, 854, 857, 846, 846, 97, 97, 98, |
| 9427 /* 20 */ 98, 98, 98, 301, 96, 96, 96, 96, 95, 95, |
| 9428 /* 30 */ 94, 94, 94, 93, 351, 325, 977, 977, 824, 824, |
| 9429 /* 40 */ 826, 947, 354, 99, 100, 90, 842, 842, 854, 857, |
| 9430 /* 50 */ 846, 846, 97, 97, 98, 98, 98, 98, 338, 96, |
| 9431 /* 60 */ 96, 96, 96, 95, 95, 94, 94, 94, 93, 351, |
| 9432 /* 70 */ 95, 95, 94, 94, 94, 93, 351, 791, 977, 977, |
| 9433 /* 80 */ 325, 94, 94, 94, 93, 351, 792, 75, 99, 100, |
| 9434 /* 90 */ 90, 842, 842, 854, 857, 846, 846, 97, 97, 98, |
| 9435 /* 100 */ 98, 98, 98, 450, 96, 96, 96, 96, 95, 95, |
| 9436 /* 110 */ 94, 94, 94, 93, 351, 1333, 155, 155, 2, 325, |
| 9437 /* 120 */ 275, 146, 132, 52, 52, 93, 351, 99, 100, 90, |
| 9438 /* 130 */ 842, 842, 854, 857, 846, 846, 97, 97, 98, 98, |
| 9439 /* 140 */ 98, 98, 101, 96, 96, 96, 96, 95, 95, 94, |
| 9440 /* 150 */ 94, 94, 93, 351, 958, 958, 325, 268, 428, 413, |
| 9441 /* 160 */ 411, 61, 752, 752, 99, 100, 90, 842, 842, 854, |
| 9442 /* 170 */ 857, 846, 846, 97, 97, 98, 98, 98, 98, 60, |
| 9443 /* 180 */ 96, 96, 96, 96, 95, 95, 94, 94, 94, 93, |
| 9444 /* 190 */ 351, 325, 270, 329, 273, 277, 959, 960, 250, 99, |
| 9445 /* 200 */ 100, 90, 842, 842, 854, 857, 846, 846, 97, 97, |
| 9446 /* 210 */ 98, 98, 98, 98, 301, 96, 96, 96, 96, 95, |
| 9447 /* 220 */ 95, 94, 94, 94, 93, 351, 325, 938, 1326, 698, |
| 9448 /* 230 */ 706, 1326, 242, 412, 99, 100, 90, 842, 842, 854, |
| 9449 /* 240 */ 857, 846, 846, 97, 97, 98, 98, 98, 98, 347, |
| 9450 /* 250 */ 96, 96, 96, 96, 95, 95, 94, 94, 94, 93, |
| 9451 /* 260 */ 351, 325, 938, 1327, 384, 699, 1327, 381, 379, 99, |
| 9452 /* 270 */ 100, 90, 842, 842, 854, 857, 846, 846, 97, 97, |
| 9453 /* 280 */ 98, 98, 98, 98, 701, 96, 96, 96, 96, 95, |
| 9454 /* 290 */ 95, 94, 94, 94, 93, 351, 325, 92, 89, 178, |
| 9455 /* 300 */ 833, 936, 373, 700, 99, 100, 90, 842, 842, 854, |
| 9456 /* 310 */ 857, 846, 846, 97, 97, 98, 98, 98, 98, 375, |
| 9457 /* 320 */ 96, 96, 96, 96, 95, 95, 94, 94, 94, 93, |
| 9458 /* 330 */ 351, 325, 1276, 947, 354, 818, 936, 739, 739, 99, |
| 9459 /* 340 */ 100, 90, 842, 842, 854, 857, 846, 846, 97, 97, |
| 9460 /* 350 */ 98, 98, 98, 98, 230, 96, 96, 96, 96, 95, |
| 9461 /* 360 */ 95, 94, 94, 94, 93, 351, 325, 969, 227, 92, |
| 9462 /* 370 */ 89, 178, 373, 300, 99, 100, 90, 842, 842, 854, |
| 9463 /* 380 */ 857, 846, 846, 97, 97, 98, 98, 98, 98, 921, |
| 9464 /* 390 */ 96, 96, 96, 96, 95, 95, 94, 94, 94, 93, |
| 9465 /* 400 */ 351, 325, 449, 447, 447, 447, 147, 737, 737, 99, |
| 9466 /* 410 */ 100, 90, 842, 842, 854, 857, 846, 846, 97, 97, |
| 9467 /* 420 */ 98, 98, 98, 98, 296, 96, 96, 96, 96, 95, |
| 9468 /* 430 */ 95, 94, 94, 94, 93, 351, 325, 419, 231, 958, |
| 9469 /* 440 */ 958, 158, 25, 422, 99, 100, 90, 842, 842, 854, |
| 9470 /* 450 */ 857, 846, 846, 97, 97, 98, 98, 98, 98, 450, |
| 9471 /* 460 */ 96, 96, 96, 96, 95, 95, 94, 94, 94, 93, |
| 9472 /* 470 */ 351, 443, 224, 224, 420, 958, 958, 962, 325, 52, |
| 9473 /* 480 */ 52, 959, 960, 176, 415, 78, 99, 100, 90, 842, |
| 9474 /* 490 */ 842, 854, 857, 846, 846, 97, 97, 98, 98, 98, |
| 9475 /* 500 */ 98, 379, 96, 96, 96, 96, 95, 95, 94, 94, |
| 9476 /* 510 */ 94, 93, 351, 325, 428, 418, 298, 959, 960, 962, |
| 9477 /* 520 */ 81, 99, 88, 90, 842, 842, 854, 857, 846, 846, |
| 9478 /* 530 */ 97, 97, 98, 98, 98, 98, 717, 96, 96, 96, |
| 9479 /* 540 */ 96, 95, 95, 94, 94, 94, 93, 351, 325, 843, |
| 9480 /* 550 */ 843, 855, 858, 996, 318, 343, 379, 100, 90, 842, |
| 9481 /* 560 */ 842, 854, 857, 846, 846, 97, 97, 98, 98, 98, |
| 9482 /* 570 */ 98, 450, 96, 96, 96, 96, 95, 95, 94, 94, |
| 9483 /* 580 */ 94, 93, 351, 325, 350, 350, 350, 260, 377, 340, |
| 9484 /* 590 */ 929, 52, 52, 90, 842, 842, 854, 857, 846, 846, |
| 9485 /* 600 */ 97, 97, 98, 98, 98, 98, 361, 96, 96, 96, |
| 9486 /* 610 */ 96, 95, 95, 94, 94, 94, 93, 351, 86, 445, |
| 9487 /* 620 */ 847, 3, 1203, 361, 360, 378, 344, 813, 958, 958, |
| 9488 /* 630 */ 1300, 86, 445, 729, 3, 212, 169, 287, 405, 282, |
| 9489 /* 640 */ 404, 199, 232, 450, 300, 760, 83, 84, 280, 245, |
| 9490 /* 650 */ 262, 365, 251, 85, 352, 352, 92, 89, 178, 83, |
| 9491 /* 660 */ 84, 242, 412, 52, 52, 448, 85, 352, 352, 246, |
| 9492 /* 670 */ 959, 960, 194, 455, 670, 402, 399, 398, 448, 243, |
| 9493 /* 680 */ 221, 114, 434, 776, 361, 450, 397, 268, 747, 224, |
| 9494 /* 690 */ 224, 132, 132, 198, 832, 434, 452, 451, 428, 427, |
| 9495 /* 700 */ 819, 415, 734, 713, 132, 52, 52, 832, 268, 452, |
| 9496 /* 710 */ 451, 734, 194, 819, 363, 402, 399, 398, 450, 1271, |
| 9497 /* 720 */ 1271, 23, 958, 958, 86, 445, 397, 3, 228, 429, |
| 9498 /* 730 */ 895, 824, 824, 826, 827, 19, 203, 720, 52, 52, |
| 9499 /* 740 */ 428, 408, 439, 249, 824, 824, 826, 827, 19, 229, |
| 9500 /* 750 */ 403, 153, 83, 84, 761, 177, 241, 450, 721, 85, |
| 9501 /* 760 */ 352, 352, 120, 157, 959, 960, 58, 977, 409, 355, |
| 9502 /* 770 */ 330, 448, 268, 428, 430, 320, 790, 32, 32, 86, |
| 9503 /* 780 */ 445, 776, 3, 341, 98, 98, 98, 98, 434, 96, |
| 9504 /* 790 */ 96, 96, 96, 95, 95, 94, 94, 94, 93, 351, |
| 9505 /* 800 */ 832, 120, 452, 451, 813, 887, 819, 83, 84, 977, |
| 9506 /* 810 */ 813, 132, 410, 920, 85, 352, 352, 132, 407, 789, |
| 9507 /* 820 */ 958, 958, 92, 89, 178, 917, 448, 262, 370, 261, |
| 9508 /* 830 */ 82, 914, 80, 262, 370, 261, 776, 824, 824, 826, |
| 9509 /* 840 */ 827, 19, 934, 434, 96, 96, 96, 96, 95, 95, |
| 9510 /* 850 */ 94, 94, 94, 93, 351, 832, 74, 452, 451, 958, |
| 9511 /* 860 */ 958, 819, 959, 960, 120, 92, 89, 178, 945, 2, |
| 9512 /* 870 */ 918, 965, 268, 1, 976, 76, 445, 762, 3, 708, |
| 9513 /* 880 */ 901, 901, 387, 958, 958, 757, 919, 371, 740, 778, |
| 9514 /* 890 */ 756, 257, 824, 824, 826, 827, 19, 417, 741, 450, |
| 9515 /* 900 */ 24, 959, 960, 83, 84, 369, 958, 958, 177, 226, |
| 9516 /* 910 */ 85, 352, 352, 885, 315, 314, 313, 215, 311, 10, |
| 9517 /* 920 */ 10, 683, 448, 349, 348, 959, 960, 909, 777, 157, |
| 9518 /* 930 */ 120, 958, 958, 337, 776, 416, 711, 310, 450, 434, |
| 9519 /* 940 */ 450, 321, 450, 791, 103, 200, 175, 450, 959, 960, |
| 9520 /* 950 */ 908, 832, 792, 452, 451, 9, 9, 819, 10, 10, |
| 9521 /* 960 */ 52, 52, 51, 51, 180, 716, 248, 10, 10, 171, |
| 9522 /* 970 */ 170, 167, 339, 959, 960, 247, 984, 702, 702, 450, |
| 9523 /* 980 */ 715, 233, 686, 982, 889, 983, 182, 914, 824, 824, |
| 9524 /* 990 */ 826, 827, 19, 183, 256, 423, 132, 181, 394, 10, |
| 9525 /* 1000 */ 10, 889, 891, 749, 958, 958, 917, 268, 985, 198, |
| 9526 /* 1010 */ 985, 349, 348, 425, 415, 299, 817, 832, 326, 825, |
| 9527 /* 1020 */ 120, 332, 133, 819, 268, 98, 98, 98, 98, 91, |
| 9528 /* 1030 */ 96, 96, 96, 96, 95, 95, 94, 94, 94, 93, |
| 9529 /* 1040 */ 351, 157, 810, 371, 382, 359, 959, 960, 358, 268, |
| 9530 /* 1050 */ 450, 918, 368, 324, 824, 824, 826, 450, 709, 450, |
| 9531 /* 1060 */ 264, 380, 889, 450, 877, 746, 253, 919, 255, 433, |
| 9532 /* 1070 */ 36, 36, 234, 450, 234, 120, 269, 37, 37, 12, |
| 9533 /* 1080 */ 12, 334, 272, 27, 27, 450, 330, 118, 450, 162, |
| 9534 /* 1090 */ 742, 280, 450, 38, 38, 450, 985, 356, 985, 450, |
| 9535 /* 1100 */ 709, 1210, 450, 132, 450, 39, 39, 450, 40, 40, |
| 9536 /* 1110 */ 450, 362, 41, 41, 450, 42, 42, 450, 254, 28, |
| 9537 /* 1120 */ 28, 450, 29, 29, 31, 31, 450, 43, 43, 450, |
| 9538 /* 1130 */ 44, 44, 450, 714, 45, 45, 450, 11, 11, 767, |
| 9539 /* 1140 */ 450, 46, 46, 450, 268, 450, 105, 105, 450, 47, |
| 9540 /* 1150 */ 47, 450, 48, 48, 450, 237, 33, 33, 450, 172, |
| 9541 /* 1160 */ 49, 49, 450, 50, 50, 34, 34, 274, 122, 122, |
| 9542 /* 1170 */ 450, 123, 123, 450, 124, 124, 450, 898, 56, 56, |
| 9543 /* 1180 */ 450, 897, 35, 35, 450, 267, 450, 817, 450, 817, |
| 9544 /* 1190 */ 106, 106, 450, 53, 53, 385, 107, 107, 450, 817, |
| 9545 /* 1200 */ 108, 108, 817, 450, 104, 104, 121, 121, 119, 119, |
| 9546 /* 1210 */ 450, 117, 112, 112, 450, 276, 450, 225, 111, 111, |
| 9547 /* 1220 */ 450, 730, 450, 109, 109, 450, 673, 674, 675, 912, |
| 9548 /* 1230 */ 110, 110, 317, 998, 55, 55, 57, 57, 692, 331, |
| 9549 /* 1240 */ 54, 54, 26, 26, 696, 30, 30, 317, 937, 197, |
| 9550 /* 1250 */ 196, 195, 335, 281, 336, 446, 331, 745, 689, 436, |
| 9551 /* 1260 */ 440, 444, 120, 72, 386, 223, 175, 345, 757, 933, |
| 9552 /* 1270 */ 20, 286, 319, 756, 815, 372, 374, 202, 202, 202, |
| 9553 /* 1280 */ 263, 395, 285, 74, 208, 21, 696, 719, 718, 884, |
| 9554 /* 1290 */ 120, 120, 120, 120, 120, 754, 278, 828, 77, 74, |
| 9555 /* 1300 */ 726, 727, 785, 783, 880, 202, 999, 208, 894, 893, |
| 9556 /* 1310 */ 894, 893, 694, 816, 763, 116, 774, 1290, 431, 432, |
| 9557 /* 1320 */ 302, 999, 390, 303, 823, 697, 691, 680, 159, 289, |
| 9558 /* 1330 */ 679, 884, 681, 952, 291, 218, 293, 7, 316, 828, |
| 9559 /* 1340 */ 173, 805, 259, 364, 252, 911, 376, 713, 295, 435, |
| 9560 /* 1350 */ 308, 168, 955, 993, 135, 400, 990, 284, 882, 881, |
| 9561 /* 1360 */ 205, 928, 926, 59, 333, 62, 144, 156, 130, 72, |
| 9562 /* 1370 */ 802, 366, 367, 393, 137, 185, 189, 160, 139, 383, |
| 9563 /* 1380 */ 67, 896, 140, 141, 142, 148, 389, 812, 775, 266, |
| 9564 /* 1390 */ 219, 190, 154, 391, 913, 876, 271, 406, 191, 322, |
| 9565 /* 1400 */ 682, 733, 192, 342, 732, 724, 731, 711, 723, 421, |
| 9566 /* 1410 */ 705, 71, 323, 6, 204, 771, 288, 79, 297, 346, |
| 9567 /* 1420 */ 772, 704, 290, 283, 703, 770, 292, 294, 967, 239, |
| 9568 /* 1430 */ 769, 102, 862, 438, 426, 240, 424, 442, 73, 213, |
| 9569 /* 1440 */ 688, 238, 22, 453, 953, 214, 217, 216, 454, 677, |
| 9570 /* 1450 */ 676, 671, 753, 125, 115, 235, 126, 669, 353, 166, |
| 9571 /* 1460 */ 127, 244, 179, 357, 306, 304, 305, 307, 113, 892, |
| 9572 /* 1470 */ 327, 890, 811, 328, 134, 128, 136, 138, 743, 258, |
| 9573 /* 1480 */ 907, 184, 143, 129, 910, 186, 63, 64, 145, 187, |
| 9574 /* 1490 */ 906, 65, 8, 66, 13, 188, 202, 899, 265, 149, |
| 9575 /* 1500 */ 987, 388, 150, 685, 161, 392, 285, 193, 279, 396, |
| 9576 /* 1510 */ 151, 401, 68, 14, 15, 722, 69, 236, 831, 131, |
| 9577 /* 1520 */ 830, 860, 70, 751, 16, 414, 755, 4, 174, 220, |
| 9578 /* 1530 */ 222, 784, 201, 152, 779, 77, 74, 17, 18, 875, |
| 9579 /* 1540 */ 861, 859, 916, 864, 915, 207, 206, 942, 163, 437, |
| 9580 /* 1550 */ 948, 943, 164, 209, 1002, 441, 863, 165, 210, 829, |
| 9581 /* 1560 */ 695, 87, 312, 211, 1292, 1291, 309, |
| 9582 }; |
| 9583 static const YYCODETYPE yy_lookahead[] = { |
| 9584 /* 0 */ 19, 95, 53, 97, 22, 24, 24, 101, 27, 28, |
| 9585 /* 10 */ 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, |
| 9586 /* 20 */ 39, 40, 41, 152, 43, 44, 45, 46, 47, 48, |
| 9587 /* 30 */ 49, 50, 51, 52, 53, 19, 55, 55, 132, 133, |
| 9588 /* 40 */ 134, 1, 2, 27, 28, 29, 30, 31, 32, 33, |
| 9589 /* 50 */ 34, 35, 36, 37, 38, 39, 40, 41, 187, 43, |
| 9590 /* 60 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 9591 /* 70 */ 47, 48, 49, 50, 51, 52, 53, 61, 97, 97, |
| 9592 /* 80 */ 19, 49, 50, 51, 52, 53, 70, 26, 27, 28, |
| 9593 /* 90 */ 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, |
| 9594 /* 100 */ 39, 40, 41, 152, 43, 44, 45, 46, 47, 48, |
| 9595 /* 110 */ 49, 50, 51, 52, 53, 144, 145, 146, 147, 19, |
| 9596 /* 120 */ 16, 22, 92, 172, 173, 52, 53, 27, 28, 29, |
| 9597 /* 130 */ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, |
| 9598 /* 140 */ 40, 41, 81, 43, 44, 45, 46, 47, 48, 49, |
| 9599 /* 150 */ 50, 51, 52, 53, 55, 56, 19, 152, 207, 208, |
| 9600 /* 160 */ 115, 24, 117, 118, 27, 28, 29, 30, 31, 32, |
| 9601 /* 170 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 79, |
| 9602 /* 180 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 9603 /* 190 */ 53, 19, 88, 157, 90, 23, 97, 98, 193, 27, |
| 9604 /* 200 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 9605 /* 210 */ 38, 39, 40, 41, 152, 43, 44, 45, 46, 47, |
| 9606 /* 220 */ 48, 49, 50, 51, 52, 53, 19, 22, 23, 172, |
| 9607 /* 230 */ 23, 26, 119, 120, 27, 28, 29, 30, 31, 32, |
| 9608 /* 240 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 187, |
| 9609 /* 250 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 9610 /* 260 */ 53, 19, 22, 23, 228, 23, 26, 231, 152, 27, |
| 9611 /* 270 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 9612 /* 280 */ 38, 39, 40, 41, 172, 43, 44, 45, 46, 47, |
| 9613 /* 290 */ 48, 49, 50, 51, 52, 53, 19, 221, 222, 223, |
| 9614 /* 300 */ 23, 96, 152, 172, 27, 28, 29, 30, 31, 32, |
| 9615 /* 310 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 152, |
| 9616 /* 320 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 9617 /* 330 */ 53, 19, 0, 1, 2, 23, 96, 190, 191, 27, |
| 9618 /* 340 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 9619 /* 350 */ 38, 39, 40, 41, 238, 43, 44, 45, 46, 47, |
| 9620 /* 360 */ 48, 49, 50, 51, 52, 53, 19, 185, 218, 221, |
| 9621 /* 370 */ 222, 223, 152, 152, 27, 28, 29, 30, 31, 32, |
| 9622 /* 380 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 241, |
| 9623 /* 390 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 9624 /* 400 */ 53, 19, 152, 168, 169, 170, 22, 190, 191, 27, |
| 9625 /* 410 */ 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, |
| 9626 /* 420 */ 38, 39, 40, 41, 152, 43, 44, 45, 46, 47, |
| 9627 /* 430 */ 48, 49, 50, 51, 52, 53, 19, 19, 218, 55, |
| 9628 /* 440 */ 56, 24, 22, 152, 27, 28, 29, 30, 31, 32, |
| 9629 /* 450 */ 33, 34, 35, 36, 37, 38, 39, 40, 41, 152, |
| 9630 /* 460 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 9631 /* 470 */ 53, 250, 194, 195, 56, 55, 56, 55, 19, 172, |
| 9632 /* 480 */ 173, 97, 98, 152, 206, 138, 27, 28, 29, 30, |
| 9633 /* 490 */ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 9634 /* 500 */ 41, 152, 43, 44, 45, 46, 47, 48, 49, 50, |
| 9635 /* 510 */ 51, 52, 53, 19, 207, 208, 152, 97, 98, 97, |
| 9636 /* 520 */ 138, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
| 9637 /* 530 */ 36, 37, 38, 39, 40, 41, 181, 43, 44, 45, |
| 9638 /* 540 */ 46, 47, 48, 49, 50, 51, 52, 53, 19, 30, |
| 9639 /* 550 */ 31, 32, 33, 247, 248, 19, 152, 28, 29, 30, |
| 9640 /* 560 */ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 9641 /* 570 */ 41, 152, 43, 44, 45, 46, 47, 48, 49, 50, |
| 9642 /* 580 */ 51, 52, 53, 19, 168, 169, 170, 238, 19, 53, |
| 9643 /* 590 */ 152, 172, 173, 29, 30, 31, 32, 33, 34, 35, |
| 9644 /* 600 */ 36, 37, 38, 39, 40, 41, 152, 43, 44, 45, |
| 9645 /* 610 */ 46, 47, 48, 49, 50, 51, 52, 53, 19, 20, |
| 9646 /* 620 */ 101, 22, 23, 169, 170, 56, 207, 85, 55, 56, |
| 9647 /* 630 */ 23, 19, 20, 26, 22, 99, 100, 101, 102, 103, |
| 9648 /* 640 */ 104, 105, 238, 152, 152, 210, 47, 48, 112, 152, |
| 9649 /* 650 */ 108, 109, 110, 54, 55, 56, 221, 222, 223, 47, |
| 9650 /* 660 */ 48, 119, 120, 172, 173, 66, 54, 55, 56, 152, |
| 9651 /* 670 */ 97, 98, 99, 148, 149, 102, 103, 104, 66, 154, |
| 9652 /* 680 */ 23, 156, 83, 26, 230, 152, 113, 152, 163, 194, |
| 9653 /* 690 */ 195, 92, 92, 30, 95, 83, 97, 98, 207, 208, |
| 9654 /* 700 */ 101, 206, 179, 180, 92, 172, 173, 95, 152, 97, |
| 9655 /* 710 */ 98, 188, 99, 101, 219, 102, 103, 104, 152, 119, |
| 9656 /* 720 */ 120, 196, 55, 56, 19, 20, 113, 22, 193, 163, |
| 9657 /* 730 */ 11, 132, 133, 134, 135, 136, 24, 65, 172, 173, |
| 9658 /* 740 */ 207, 208, 250, 152, 132, 133, 134, 135, 136, 193, |
| 9659 /* 750 */ 78, 84, 47, 48, 49, 98, 199, 152, 86, 54, |
| 9660 /* 760 */ 55, 56, 196, 152, 97, 98, 209, 55, 163, 244, |
| 9661 /* 770 */ 107, 66, 152, 207, 208, 164, 175, 172, 173, 19, |
| 9662 /* 780 */ 20, 124, 22, 111, 38, 39, 40, 41, 83, 43, |
| 9663 /* 790 */ 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, |
| 9664 /* 800 */ 95, 196, 97, 98, 85, 152, 101, 47, 48, 97, |
| 9665 /* 810 */ 85, 92, 207, 193, 54, 55, 56, 92, 49, 175, |
| 9666 /* 820 */ 55, 56, 221, 222, 223, 12, 66, 108, 109, 110, |
| 9667 /* 830 */ 137, 163, 139, 108, 109, 110, 26, 132, 133, 134, |
| 9668 /* 840 */ 135, 136, 152, 83, 43, 44, 45, 46, 47, 48, |
| 9669 /* 850 */ 49, 50, 51, 52, 53, 95, 26, 97, 98, 55, |
| 9670 /* 860 */ 56, 101, 97, 98, 196, 221, 222, 223, 146, 147, |
| 9671 /* 870 */ 57, 171, 152, 22, 26, 19, 20, 49, 22, 179, |
| 9672 /* 880 */ 108, 109, 110, 55, 56, 116, 73, 219, 75, 124, |
| 9673 /* 890 */ 121, 152, 132, 133, 134, 135, 136, 163, 85, 152, |
| 9674 /* 900 */ 232, 97, 98, 47, 48, 237, 55, 56, 98, 5, |
| 9675 /* 910 */ 54, 55, 56, 193, 10, 11, 12, 13, 14, 172, |
| 9676 /* 920 */ 173, 17, 66, 47, 48, 97, 98, 152, 124, 152, |
| 9677 /* 930 */ 196, 55, 56, 186, 124, 152, 106, 160, 152, 83, |
| 9678 /* 940 */ 152, 164, 152, 61, 22, 211, 212, 152, 97, 98, |
| 9679 /* 950 */ 152, 95, 70, 97, 98, 172, 173, 101, 172, 173, |
| 9680 /* 960 */ 172, 173, 172, 173, 60, 181, 62, 172, 173, 47, |
| 9681 /* 970 */ 48, 123, 186, 97, 98, 71, 100, 55, 56, 152, |
| 9682 /* 980 */ 181, 186, 21, 107, 152, 109, 82, 163, 132, 133, |
| 9683 /* 990 */ 134, 135, 136, 89, 16, 207, 92, 93, 19, 172, |
| 9684 /* 1000 */ 173, 169, 170, 195, 55, 56, 12, 152, 132, 30, |
| 9685 /* 1010 */ 134, 47, 48, 186, 206, 225, 152, 95, 114, 97, |
| 9686 /* 1020 */ 196, 245, 246, 101, 152, 38, 39, 40, 41, 42, |
| 9687 /* 1030 */ 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, |
| 9688 /* 1040 */ 53, 152, 163, 219, 152, 141, 97, 98, 193, 152, |
| 9689 /* 1050 */ 152, 57, 91, 164, 132, 133, 134, 152, 55, 152, |
| 9690 /* 1060 */ 152, 237, 230, 152, 103, 193, 88, 73, 90, 75, |
| 9691 /* 1070 */ 172, 173, 183, 152, 185, 196, 152, 172, 173, 172, |
| 9692 /* 1080 */ 173, 217, 152, 172, 173, 152, 107, 22, 152, 24, |
| 9693 /* 1090 */ 193, 112, 152, 172, 173, 152, 132, 242, 134, 152, |
| 9694 /* 1100 */ 97, 140, 152, 92, 152, 172, 173, 152, 172, 173, |
| 9695 /* 1110 */ 152, 100, 172, 173, 152, 172, 173, 152, 140, 172, |
| 9696 /* 1120 */ 173, 152, 172, 173, 172, 173, 152, 172, 173, 152, |
| 9697 /* 1130 */ 172, 173, 152, 152, 172, 173, 152, 172, 173, 213, |
| 9698 /* 1140 */ 152, 172, 173, 152, 152, 152, 172, 173, 152, 172, |
| 9699 /* 1150 */ 173, 152, 172, 173, 152, 210, 172, 173, 152, 26, |
| 9700 /* 1160 */ 172, 173, 152, 172, 173, 172, 173, 152, 172, 173, |
| 9701 /* 1170 */ 152, 172, 173, 152, 172, 173, 152, 59, 172, 173, |
| 9702 /* 1180 */ 152, 63, 172, 173, 152, 193, 152, 152, 152, 152, |
| 9703 /* 1190 */ 172, 173, 152, 172, 173, 77, 172, 173, 152, 152, |
| 9704 /* 1200 */ 172, 173, 152, 152, 172, 173, 172, 173, 172, 173, |
| 9705 /* 1210 */ 152, 22, 172, 173, 152, 152, 152, 22, 172, 173, |
| 9706 /* 1220 */ 152, 152, 152, 172, 173, 152, 7, 8, 9, 163, |
| 9707 /* 1230 */ 172, 173, 22, 23, 172, 173, 172, 173, 166, 167, |
| 9708 /* 1240 */ 172, 173, 172, 173, 55, 172, 173, 22, 23, 108, |
| 9709 /* 1250 */ 109, 110, 217, 152, 217, 166, 167, 163, 163, 163, |
| 9710 /* 1260 */ 163, 163, 196, 130, 217, 211, 212, 217, 116, 23, |
| 9711 /* 1270 */ 22, 101, 26, 121, 23, 23, 23, 26, 26, 26, |
| 9712 /* 1280 */ 23, 23, 112, 26, 26, 37, 97, 100, 101, 55, |
| 9713 /* 1290 */ 196, 196, 196, 196, 196, 23, 23, 55, 26, 26, |
| 9714 /* 1300 */ 7, 8, 23, 152, 23, 26, 96, 26, 132, 132, |
| 9715 /* 1310 */ 134, 134, 23, 152, 152, 26, 152, 122, 152, 191, |
| 9716 /* 1320 */ 152, 96, 234, 152, 152, 152, 152, 152, 197, 210, |
| 9717 /* 1330 */ 152, 97, 152, 152, 210, 233, 210, 198, 150, 97, |
| 9718 /* 1340 */ 184, 201, 239, 214, 214, 201, 239, 180, 214, 227, |
| 9719 /* 1350 */ 200, 198, 155, 67, 243, 176, 69, 175, 175, 175, |
| 9720 /* 1360 */ 122, 159, 159, 240, 159, 240, 22, 220, 27, 130, |
| 9721 /* 1370 */ 201, 18, 159, 18, 189, 158, 158, 220, 192, 159, |
| 9722 /* 1380 */ 137, 236, 192, 192, 192, 189, 74, 189, 159, 235, |
| 9723 /* 1390 */ 159, 158, 22, 177, 201, 201, 159, 107, 158, 177, |
| 9724 /* 1400 */ 159, 174, 158, 76, 174, 182, 174, 106, 182, 125, |
| 9725 /* 1410 */ 174, 107, 177, 22, 159, 216, 215, 137, 159, 53, |
| 9726 /* 1420 */ 216, 176, 215, 174, 174, 216, 215, 215, 174, 229, |
| 9727 /* 1430 */ 216, 129, 224, 177, 126, 229, 127, 177, 128, 25, |
| 9728 /* 1440 */ 162, 226, 26, 161, 13, 153, 6, 153, 151, 151, |
| 9729 /* 1450 */ 151, 151, 205, 165, 178, 178, 165, 4, 3, 22, |
| 9730 /* 1460 */ 165, 142, 15, 94, 202, 204, 203, 201, 16, 23, |
| 9731 /* 1470 */ 249, 23, 120, 249, 246, 111, 131, 123, 20, 16, |
| 9732 /* 1480 */ 1, 125, 123, 111, 56, 64, 37, 37, 131, 122, |
| 9733 /* 1490 */ 1, 37, 5, 37, 22, 107, 26, 80, 140, 80, |
| 9734 /* 1500 */ 87, 72, 107, 20, 24, 19, 112, 105, 23, 79, |
| 9735 /* 1510 */ 22, 79, 22, 22, 22, 58, 22, 79, 23, 68, |
| 9736 /* 1520 */ 23, 23, 26, 116, 22, 26, 23, 22, 122, 23, |
| 9737 /* 1530 */ 23, 56, 64, 22, 124, 26, 26, 64, 64, 23, |
| 9738 /* 1540 */ 23, 23, 23, 11, 23, 22, 26, 23, 22, 24, |
| 9739 /* 1550 */ 1, 23, 22, 26, 251, 24, 23, 22, 122, 23, |
| 9740 /* 1560 */ 23, 22, 15, 122, 122, 122, 23, |
| 9741 }; |
| 9742 #define YY_SHIFT_USE_DFLT (1567) |
| 9743 #define YY_SHIFT_COUNT (455) |
| 9744 #define YY_SHIFT_MIN (-94) |
| 9745 #define YY_SHIFT_MAX (1549) |
| 9746 static const short yy_shift_ofst[] = { |
| 9747 /* 0 */ 40, 599, 904, 612, 760, 760, 760, 760, 725, -19, |
| 9748 /* 10 */ 16, 16, 100, 760, 760, 760, 760, 760, 760, 760, |
| 9749 /* 20 */ 876, 876, 573, 542, 719, 600, 61, 137, 172, 207, |
| 9750 /* 30 */ 242, 277, 312, 347, 382, 417, 459, 459, 459, 459, |
| 9751 /* 40 */ 459, 459, 459, 459, 459, 459, 459, 459, 459, 459, |
| 9752 /* 50 */ 459, 459, 459, 494, 459, 529, 564, 564, 705, 760, |
| 9753 /* 60 */ 760, 760, 760, 760, 760, 760, 760, 760, 760, 760, |
| 9754 /* 70 */ 760, 760, 760, 760, 760, 760, 760, 760, 760, 760, |
| 9755 /* 80 */ 760, 760, 760, 760, 760, 760, 760, 760, 760, 760, |
| 9756 /* 90 */ 856, 760, 760, 760, 760, 760, 760, 760, 760, 760, |
| 9757 /* 100 */ 760, 760, 760, 760, 987, 746, 746, 746, 746, 746, |
| 9758 /* 110 */ 801, 23, 32, 949, 961, 979, 964, 964, 949, 73, |
| 9759 /* 120 */ 113, -51, 1567, 1567, 1567, 536, 536, 536, 99, 99, |
| 9760 /* 130 */ 813, 813, 667, 205, 240, 949, 949, 949, 949, 949, |
| 9761 /* 140 */ 949, 949, 949, 949, 949, 949, 949, 949, 949, 949, |
| 9762 /* 150 */ 949, 949, 949, 949, 949, 332, 1011, 422, 422, 113, |
| 9763 /* 160 */ 30, 30, 30, 30, 30, 30, 1567, 1567, 1567, 922, |
| 9764 /* 170 */ -94, -94, 384, 613, 828, 420, 765, 804, 851, 949, |
| 9765 /* 180 */ 949, 949, 949, 949, 949, 949, 949, 949, 949, 949, |
| 9766 /* 190 */ 949, 949, 949, 949, 949, 672, 672, 672, 949, 949, |
| 9767 /* 200 */ 657, 949, 949, 949, -18, 949, 949, 994, 949, 949, |
| 9768 /* 210 */ 949, 949, 949, 949, 949, 949, 949, 949, 772, 1118, |
| 9769 /* 220 */ 712, 712, 712, 810, 45, 769, 1219, 1133, 418, 418, |
| 9770 /* 230 */ 569, 1133, 569, 830, 607, 663, 882, 418, 693, 882, |
| 9771 /* 240 */ 882, 848, 1152, 1065, 1286, 1238, 1238, 1287, 1287, 1238, |
| 9772 /* 250 */ 1344, 1341, 1239, 1353, 1353, 1353, 1353, 1238, 1355, 1239, |
| 9773 /* 260 */ 1344, 1341, 1341, 1239, 1238, 1355, 1243, 1312, 1238, 1238, |
| 9774 /* 270 */ 1355, 1370, 1238, 1355, 1238, 1355, 1370, 1290, 1290, 1290, |
| 9775 /* 280 */ 1327, 1370, 1290, 1301, 1290, 1327, 1290, 1290, 1284, 1304, |
| 9776 /* 290 */ 1284, 1304, 1284, 1304, 1284, 1304, 1238, 1391, 1238, 1280, |
| 9777 /* 300 */ 1370, 1366, 1366, 1370, 1302, 1308, 1310, 1309, 1239, 1414, |
| 9778 /* 310 */ 1416, 1431, 1431, 1440, 1440, 1440, 1440, 1567, 1567, 1567, |
| 9779 /* 320 */ 1567, 1567, 1567, 1567, 1567, 519, 978, 1210, 1225, 104, |
| 9780 /* 330 */ 1141, 1189, 1246, 1248, 1251, 1252, 1253, 1257, 1258, 1273, |
| 9781 /* 340 */ 1003, 1187, 1293, 1170, 1272, 1279, 1234, 1281, 1176, 1177, |
| 9782 /* 350 */ 1289, 1242, 1195, 1453, 1455, 1437, 1319, 1447, 1369, 1452, |
| 9783 /* 360 */ 1446, 1448, 1352, 1345, 1364, 1354, 1458, 1356, 1463, 1479, |
| 9784 /* 370 */ 1359, 1357, 1449, 1450, 1454, 1456, 1372, 1428, 1421, 1367, |
| 9785 /* 380 */ 1489, 1487, 1472, 1388, 1358, 1417, 1470, 1419, 1413, 1429, |
| 9786 /* 390 */ 1395, 1480, 1483, 1486, 1394, 1402, 1488, 1430, 1490, 1491, |
| 9787 /* 400 */ 1485, 1492, 1432, 1457, 1494, 1438, 1451, 1495, 1497, 1498, |
| 9788 /* 410 */ 1496, 1407, 1502, 1503, 1505, 1499, 1406, 1506, 1507, 1475, |
| 9789 /* 420 */ 1468, 1511, 1410, 1509, 1473, 1510, 1474, 1516, 1509, 1517, |
| 9790 /* 430 */ 1518, 1519, 1520, 1521, 1523, 1532, 1524, 1526, 1525, 1527, |
| 9791 /* 440 */ 1528, 1530, 1531, 1527, 1533, 1535, 1536, 1537, 1539, 1436, |
| 9792 /* 450 */ 1441, 1442, 1443, 1543, 1547, 1549, |
| 9793 }; |
| 9794 #define YY_REDUCE_USE_DFLT (-130) |
| 9795 #define YY_REDUCE_COUNT (324) |
| 9796 #define YY_REDUCE_MIN (-129) |
| 9797 #define YY_REDUCE_MAX (1300) |
| 9798 static const short yy_reduce_ofst[] = { |
| 9799 /* 0 */ -29, 566, 525, 605, -49, 307, 491, 533, 668, 435, |
| 9800 /* 10 */ 601, 644, 148, 747, 786, 795, 419, 788, 827, 790, |
| 9801 /* 20 */ 454, 832, 889, 495, 824, 734, 76, 76, 76, 76, |
| 9802 /* 30 */ 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, |
| 9803 /* 40 */ 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, |
| 9804 /* 50 */ 76, 76, 76, 76, 76, 76, 76, 76, 783, 898, |
| 9805 /* 60 */ 905, 907, 911, 921, 933, 936, 940, 943, 947, 950, |
| 9806 /* 70 */ 952, 955, 958, 962, 965, 969, 974, 977, 980, 984, |
| 9807 /* 80 */ 988, 991, 993, 996, 999, 1002, 1006, 1010, 1018, 1021, |
| 9808 /* 90 */ 1024, 1028, 1032, 1034, 1036, 1040, 1046, 1051, 1058, 1062, |
| 9809 /* 100 */ 1064, 1068, 1070, 1073, 76, 76, 76, 76, 76, 76, |
| 9810 /* 110 */ 76, 76, 76, 855, 36, 523, 235, 416, 777, 76, |
| 9811 /* 120 */ 278, 76, 76, 76, 76, 700, 700, 700, 150, 220, |
| 9812 /* 130 */ 147, 217, 221, 306, 306, 611, 5, 535, 556, 620, |
| 9813 /* 140 */ 720, 872, 897, 116, 864, 349, 1035, 1037, 404, 1047, |
| 9814 /* 150 */ 992, -129, 1050, 492, 62, 722, 879, 1072, 1089, 808, |
| 9815 /* 160 */ 1066, 1094, 1095, 1096, 1097, 1098, 776, 1054, 557, 57, |
| 9816 /* 170 */ 112, 131, 167, 182, 250, 272, 291, 331, 364, 438, |
| 9817 /* 180 */ 497, 517, 591, 653, 690, 739, 775, 798, 892, 908, |
| 9818 /* 190 */ 924, 930, 1015, 1063, 1069, 355, 784, 799, 981, 1101, |
| 9819 /* 200 */ 926, 1151, 1161, 1162, 945, 1164, 1166, 1128, 1168, 1171, |
| 9820 /* 210 */ 1172, 250, 1173, 1174, 1175, 1178, 1180, 1181, 1088, 1102, |
| 9821 /* 220 */ 1119, 1124, 1126, 926, 1131, 1139, 1188, 1140, 1129, 1130, |
| 9822 /* 230 */ 1103, 1144, 1107, 1179, 1156, 1167, 1182, 1134, 1122, 1183, |
| 9823 /* 240 */ 1184, 1150, 1153, 1197, 1111, 1202, 1203, 1123, 1125, 1205, |
| 9824 /* 250 */ 1147, 1185, 1169, 1186, 1190, 1191, 1192, 1213, 1217, 1193, |
| 9825 /* 260 */ 1157, 1196, 1198, 1194, 1220, 1218, 1145, 1154, 1229, 1231, |
| 9826 /* 270 */ 1233, 1216, 1237, 1240, 1241, 1244, 1222, 1227, 1230, 1232, |
| 9827 /* 280 */ 1223, 1235, 1236, 1245, 1249, 1226, 1250, 1254, 1199, 1201, |
| 9828 /* 290 */ 1204, 1207, 1209, 1211, 1214, 1212, 1255, 1208, 1259, 1215, |
| 9829 /* 300 */ 1256, 1200, 1206, 1260, 1247, 1261, 1263, 1262, 1266, 1278, |
| 9830 /* 310 */ 1282, 1292, 1294, 1297, 1298, 1299, 1300, 1221, 1224, 1228, |
| 9831 /* 320 */ 1288, 1291, 1276, 1277, 1295, |
| 9832 }; |
| 9833 static const YYACTIONTYPE yy_default[] = { |
| 9834 /* 0 */ 1281, 1271, 1271, 1271, 1203, 1203, 1203, 1203, 1271, 1096, |
| 9835 /* 10 */ 1125, 1125, 1255, 1332, 1332, 1332, 1332, 1332, 1332, 1202, |
| 9836 /* 20 */ 1332, 1332, 1332, 1332, 1271, 1100, 1131, 1332, 1332, 1332, |
| 9837 /* 30 */ 1332, 1204, 1205, 1332, 1332, 1332, 1254, 1256, 1141, 1140, |
| 9838 /* 40 */ 1139, 1138, 1237, 1112, 1136, 1129, 1133, 1204, 1198, 1199, |
| 9839 /* 50 */ 1197, 1201, 1205, 1332, 1132, 1167, 1182, 1166, 1332, 1332, |
| 9840 /* 60 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9841 /* 70 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9842 /* 80 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9843 /* 90 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9844 /* 100 */ 1332, 1332, 1332, 1332, 1176, 1181, 1188, 1180, 1177, 1169, |
| 9845 /* 110 */ 1168, 1170, 1171, 1332, 1019, 1067, 1332, 1332, 1332, 1172, |
| 9846 /* 120 */ 1332, 1173, 1185, 1184, 1183, 1262, 1289, 1288, 1332, 1332, |
| 9847 /* 130 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9848 /* 140 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9849 /* 150 */ 1332, 1332, 1332, 1332, 1332, 1281, 1271, 1025, 1025, 1332, |
| 9850 /* 160 */ 1271, 1271, 1271, 1271, 1271, 1271, 1267, 1100, 1091, 1332, |
| 9851 /* 170 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9852 /* 180 */ 1259, 1257, 1332, 1218, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9853 /* 190 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9854 /* 200 */ 1332, 1332, 1332, 1332, 1096, 1332, 1332, 1332, 1332, 1332, |
| 9855 /* 210 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1283, 1332, 1232, |
| 9856 /* 220 */ 1096, 1096, 1096, 1098, 1080, 1090, 1004, 1135, 1114, 1114, |
| 9857 /* 230 */ 1321, 1135, 1321, 1042, 1303, 1039, 1125, 1114, 1200, 1125, |
| 9858 /* 240 */ 1125, 1097, 1090, 1332, 1324, 1105, 1105, 1323, 1323, 1105, |
| 9859 /* 250 */ 1146, 1070, 1135, 1076, 1076, 1076, 1076, 1105, 1016, 1135, |
| 9860 /* 260 */ 1146, 1070, 1070, 1135, 1105, 1016, 1236, 1318, 1105, 1105, |
| 9861 /* 270 */ 1016, 1211, 1105, 1016, 1105, 1016, 1211, 1068, 1068, 1068, |
| 9862 /* 280 */ 1057, 1211, 1068, 1042, 1068, 1057, 1068, 1068, 1118, 1113, |
| 9863 /* 290 */ 1118, 1113, 1118, 1113, 1118, 1113, 1105, 1206, 1105, 1332, |
| 9864 /* 300 */ 1211, 1215, 1215, 1211, 1130, 1119, 1128, 1126, 1135, 1022, |
| 9865 /* 310 */ 1060, 1286, 1286, 1282, 1282, 1282, 1282, 1329, 1329, 1267, |
| 9866 /* 320 */ 1298, 1298, 1044, 1044, 1298, 1332, 1332, 1332, 1332, 1332, |
| 9867 /* 330 */ 1332, 1293, 1332, 1220, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9868 /* 340 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9869 /* 350 */ 1332, 1332, 1152, 1332, 1000, 1264, 1332, 1332, 1263, 1332, |
| 9870 /* 360 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9871 /* 370 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1320, |
| 9872 /* 380 */ 1332, 1332, 1332, 1332, 1332, 1332, 1235, 1234, 1332, 1332, |
| 9873 /* 390 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9874 /* 400 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, |
| 9875 /* 410 */ 1332, 1082, 1332, 1332, 1332, 1307, 1332, 1332, 1332, 1332, |
| 9876 /* 420 */ 1332, 1332, 1332, 1127, 1332, 1120, 1332, 1332, 1311, 1332, |
| 9877 /* 430 */ 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1332, 1273, |
| 9878 /* 440 */ 1332, 1332, 1332, 1272, 1332, 1332, 1332, 1332, 1332, 1154, |
| 9879 /* 450 */ 1332, 1153, 1157, 1332, 1010, 1332, |
| 9880 }; |
| 9881 /********** End of lemon-generated parsing tables *****************************/ |
| 9882 |
| 9883 /* The next table maps tokens (terminal symbols) into fallback tokens. |
| 9884 ** If a construct like the following: |
| 9885 ** |
| 9886 ** %fallback ID X Y Z. |
| 9887 ** |
| 9888 ** appears in the grammar, then ID becomes a fallback token for X, Y, |
| 9889 ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser |
| 9890 ** but it does not parse, the type of the token is changed to ID and |
| 9891 ** the parse is retried before an error is thrown. |
| 9892 ** |
| 9893 ** This feature can be used, for example, to cause some keywords in a language |
| 9894 ** to revert to identifiers if they keyword does not apply in the context where |
| 9895 ** it appears. |
| 9896 */ |
| 9897 #ifdef YYFALLBACK |
| 9898 static const YYCODETYPE yyFallback[] = { |
| 9899 0, /* $ => nothing */ |
| 9900 0, /* SEMI => nothing */ |
| 9901 55, /* EXPLAIN => ID */ |
| 9902 55, /* QUERY => ID */ |
| 9903 55, /* PLAN => ID */ |
| 9904 55, /* BEGIN => ID */ |
| 9905 0, /* TRANSACTION => nothing */ |
| 9906 55, /* DEFERRED => ID */ |
| 9907 55, /* IMMEDIATE => ID */ |
| 9908 55, /* EXCLUSIVE => ID */ |
| 9909 0, /* COMMIT => nothing */ |
| 9910 55, /* END => ID */ |
| 9911 55, /* ROLLBACK => ID */ |
| 9912 55, /* SAVEPOINT => ID */ |
| 9913 55, /* RELEASE => ID */ |
| 9914 0, /* TO => nothing */ |
| 9915 0, /* TABLE => nothing */ |
| 9916 0, /* CREATE => nothing */ |
| 9917 55, /* IF => ID */ |
| 9918 0, /* NOT => nothing */ |
| 9919 0, /* EXISTS => nothing */ |
| 9920 55, /* TEMP => ID */ |
| 9921 0, /* LP => nothing */ |
| 9922 0, /* RP => nothing */ |
| 9923 0, /* AS => nothing */ |
| 9924 55, /* WITHOUT => ID */ |
| 9925 0, /* COMMA => nothing */ |
| 9926 0, /* OR => nothing */ |
| 9927 0, /* AND => nothing */ |
| 9928 0, /* IS => nothing */ |
| 9929 55, /* MATCH => ID */ |
| 9930 55, /* LIKE_KW => ID */ |
| 9931 0, /* BETWEEN => nothing */ |
| 9932 0, /* IN => nothing */ |
| 9933 0, /* ISNULL => nothing */ |
| 9934 0, /* NOTNULL => nothing */ |
| 9935 0, /* NE => nothing */ |
| 9936 0, /* EQ => nothing */ |
| 9937 0, /* GT => nothing */ |
| 9938 0, /* LE => nothing */ |
| 9939 0, /* LT => nothing */ |
| 9940 0, /* GE => nothing */ |
| 9941 0, /* ESCAPE => nothing */ |
| 9942 0, /* BITAND => nothing */ |
| 9943 0, /* BITOR => nothing */ |
| 9944 0, /* LSHIFT => nothing */ |
| 9945 0, /* RSHIFT => nothing */ |
| 9946 0, /* PLUS => nothing */ |
| 9947 0, /* MINUS => nothing */ |
| 9948 0, /* STAR => nothing */ |
| 9949 0, /* SLASH => nothing */ |
| 9950 0, /* REM => nothing */ |
| 9951 0, /* CONCAT => nothing */ |
| 9952 0, /* COLLATE => nothing */ |
| 9953 0, /* BITNOT => nothing */ |
| 9954 0, /* ID => nothing */ |
| 9955 0, /* INDEXED => nothing */ |
| 9956 55, /* ABORT => ID */ |
| 9957 55, /* ACTION => ID */ |
| 9958 55, /* AFTER => ID */ |
| 9959 55, /* ANALYZE => ID */ |
| 9960 55, /* ASC => ID */ |
| 9961 55, /* ATTACH => ID */ |
| 9962 55, /* BEFORE => ID */ |
| 9963 55, /* BY => ID */ |
| 9964 55, /* CASCADE => ID */ |
| 9965 55, /* CAST => ID */ |
| 9966 55, /* COLUMNKW => ID */ |
| 9967 55, /* CONFLICT => ID */ |
| 9968 55, /* DATABASE => ID */ |
| 9969 55, /* DESC => ID */ |
| 9970 55, /* DETACH => ID */ |
| 9971 55, /* EACH => ID */ |
| 9972 55, /* FAIL => ID */ |
| 9973 55, /* FOR => ID */ |
| 9974 55, /* IGNORE => ID */ |
| 9975 55, /* INITIALLY => ID */ |
| 9976 55, /* INSTEAD => ID */ |
| 9977 55, /* NO => ID */ |
| 9978 55, /* KEY => ID */ |
| 9979 55, /* OF => ID */ |
| 9980 55, /* OFFSET => ID */ |
| 9981 55, /* PRAGMA => ID */ |
| 9982 55, /* RAISE => ID */ |
| 9983 55, /* RECURSIVE => ID */ |
| 9984 55, /* REPLACE => ID */ |
| 9985 55, /* RESTRICT => ID */ |
| 9986 55, /* ROW => ID */ |
| 9987 55, /* TRIGGER => ID */ |
| 9988 55, /* VACUUM => ID */ |
| 9989 55, /* VIEW => ID */ |
| 9990 55, /* VIRTUAL => ID */ |
| 9991 55, /* WITH => ID */ |
| 9992 55, /* REINDEX => ID */ |
| 9993 55, /* RENAME => ID */ |
| 9994 55, /* CTIME_KW => ID */ |
| 9995 }; |
| 9996 #endif /* YYFALLBACK */ |
| 9997 |
| 9998 /* The following structure represents a single element of the |
| 9999 ** parser's stack. Information stored includes: |
| 10000 ** |
| 10001 ** + The state number for the parser at this level of the stack. |
| 10002 ** |
| 10003 ** + The value of the token stored at this level of the stack. |
| 10004 ** (In other words, the "major" token.) |
| 10005 ** |
| 10006 ** + The semantic value stored at this level of the stack. This is |
| 10007 ** the information used by the action routines in the grammar. |
| 10008 ** It is sometimes called the "minor" token. |
| 10009 ** |
| 10010 ** After the "shift" half of a SHIFTREDUCE action, the stateno field |
| 10011 ** actually contains the reduce action for the second half of the |
| 10012 ** SHIFTREDUCE. |
| 10013 */ |
| 10014 struct yyStackEntry { |
| 10015 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */ |
| 10016 YYCODETYPE major; /* The major token value. This is the code |
| 10017 ** number for the token at this stack level */ |
| 10018 YYMINORTYPE minor; /* The user-supplied minor token value. This |
| 10019 ** is the value of the token */ |
| 10020 }; |
| 10021 typedef struct yyStackEntry yyStackEntry; |
| 10022 |
| 10023 /* The state of the parser is completely contained in an instance of |
| 10024 ** the following structure */ |
| 10025 struct yyParser { |
| 10026 yyStackEntry *yytos; /* Pointer to top element of the stack */ |
| 10027 #ifdef YYTRACKMAXSTACKDEPTH |
| 10028 int yyhwm; /* High-water mark of the stack */ |
| 10029 #endif |
| 10030 #ifndef YYNOERRORRECOVERY |
| 10031 int yyerrcnt; /* Shifts left before out of the error */ |
| 10032 #endif |
| 10033 sqlite3ParserARG_SDECL /* A place to hold %extra_argument */ |
| 10034 #if YYSTACKDEPTH<=0 |
| 10035 int yystksz; /* Current side of the stack */ |
| 10036 yyStackEntry *yystack; /* The parser's stack */ |
| 10037 yyStackEntry yystk0; /* First stack entry */ |
| 10038 #else |
| 10039 yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ |
| 10040 #endif |
| 10041 }; |
| 10042 typedef struct yyParser yyParser; |
| 10043 |
| 10044 #ifndef NDEBUG |
| 10045 /* #include <stdio.h> */ |
| 10046 static FILE *yyTraceFILE = 0; |
| 10047 static char *yyTracePrompt = 0; |
| 10048 #endif /* NDEBUG */ |
| 10049 |
| 10050 #ifndef NDEBUG |
| 10051 /* |
| 10052 ** Turn parser tracing on by giving a stream to which to write the trace |
| 10053 ** and a prompt to preface each trace message. Tracing is turned off |
| 10054 ** by making either argument NULL |
| 10055 ** |
| 10056 ** Inputs: |
| 10057 ** <ul> |
| 10058 ** <li> A FILE* to which trace output should be written. |
| 10059 ** If NULL, then tracing is turned off. |
| 10060 ** <li> A prefix string written at the beginning of every |
| 10061 ** line of trace output. If NULL, then tracing is |
| 10062 ** turned off. |
| 10063 ** </ul> |
| 10064 ** |
| 10065 ** Outputs: |
| 10066 ** None. |
| 10067 */ |
| 10068 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){ |
| 10069 yyTraceFILE = TraceFILE; |
| 10070 yyTracePrompt = zTracePrompt; |
| 10071 if( yyTraceFILE==0 ) yyTracePrompt = 0; |
| 10072 else if( yyTracePrompt==0 ) yyTraceFILE = 0; |
| 10073 } |
| 10074 #endif /* NDEBUG */ |
| 10075 |
| 10076 #ifndef NDEBUG |
| 10077 /* For tracing shifts, the names of all terminals and nonterminals |
| 10078 ** are required. The following table supplies these names */ |
| 10079 static const char *const yyTokenName[] = { |
| 10080 "$", "SEMI", "EXPLAIN", "QUERY", |
| 10081 "PLAN", "BEGIN", "TRANSACTION", "DEFERRED", |
| 10082 "IMMEDIATE", "EXCLUSIVE", "COMMIT", "END", |
| 10083 "ROLLBACK", "SAVEPOINT", "RELEASE", "TO", |
| 10084 "TABLE", "CREATE", "IF", "NOT", |
| 10085 "EXISTS", "TEMP", "LP", "RP", |
| 10086 "AS", "WITHOUT", "COMMA", "OR", |
| 10087 "AND", "IS", "MATCH", "LIKE_KW", |
| 10088 "BETWEEN", "IN", "ISNULL", "NOTNULL", |
| 10089 "NE", "EQ", "GT", "LE", |
| 10090 "LT", "GE", "ESCAPE", "BITAND", |
| 10091 "BITOR", "LSHIFT", "RSHIFT", "PLUS", |
| 10092 "MINUS", "STAR", "SLASH", "REM", |
| 10093 "CONCAT", "COLLATE", "BITNOT", "ID", |
| 10094 "INDEXED", "ABORT", "ACTION", "AFTER", |
| 10095 "ANALYZE", "ASC", "ATTACH", "BEFORE", |
| 10096 "BY", "CASCADE", "CAST", "COLUMNKW", |
| 10097 "CONFLICT", "DATABASE", "DESC", "DETACH", |
| 10098 "EACH", "FAIL", "FOR", "IGNORE", |
| 10099 "INITIALLY", "INSTEAD", "NO", "KEY", |
| 10100 "OF", "OFFSET", "PRAGMA", "RAISE", |
| 10101 "RECURSIVE", "REPLACE", "RESTRICT", "ROW", |
| 10102 "TRIGGER", "VACUUM", "VIEW", "VIRTUAL", |
| 10103 "WITH", "REINDEX", "RENAME", "CTIME_KW", |
| 10104 "ANY", "STRING", "JOIN_KW", "CONSTRAINT", |
| 10105 "DEFAULT", "NULL", "PRIMARY", "UNIQUE", |
| 10106 "CHECK", "REFERENCES", "AUTOINCR", "ON", |
| 10107 "INSERT", "DELETE", "UPDATE", "SET", |
| 10108 "DEFERRABLE", "FOREIGN", "DROP", "UNION", |
| 10109 "ALL", "EXCEPT", "INTERSECT", "SELECT", |
| 10110 "VALUES", "DISTINCT", "DOT", "FROM", |
| 10111 "JOIN", "USING", "ORDER", "GROUP", |
| 10112 "HAVING", "LIMIT", "WHERE", "INTO", |
| 10113 "FLOAT", "BLOB", "INTEGER", "VARIABLE", |
| 10114 "CASE", "WHEN", "THEN", "ELSE", |
| 10115 "INDEX", "ALTER", "ADD", "error", |
| 10116 "input", "cmdlist", "ecmd", "explain", |
| 10117 "cmdx", "cmd", "transtype", "trans_opt", |
| 10118 "nm", "savepoint_opt", "create_table", "create_table_args", |
| 10119 "createkw", "temp", "ifnotexists", "dbnm", |
| 10120 "columnlist", "conslist_opt", "table_options", "select", |
| 10121 "columnname", "carglist", "typetoken", "typename", |
| 10122 "signed", "plus_num", "minus_num", "ccons", |
| 10123 "term", "expr", "onconf", "sortorder", |
| 10124 "autoinc", "eidlist_opt", "refargs", "defer_subclause", |
| 10125 "refarg", "refact", "init_deferred_pred_opt", "conslist", |
| 10126 "tconscomma", "tcons", "sortlist", "eidlist", |
| 10127 "defer_subclause_opt", "orconf", "resolvetype", "raisetype", |
| 10128 "ifexists", "fullname", "selectnowith", "oneselect", |
| 10129 "with", "multiselect_op", "distinct", "selcollist", |
| 10130 "from", "where_opt", "groupby_opt", "having_opt", |
| 10131 "orderby_opt", "limit_opt", "values", "nexprlist", |
| 10132 "exprlist", "sclp", "as", "seltablist", |
| 10133 "stl_prefix", "joinop", "indexed_opt", "on_opt", |
| 10134 "using_opt", "idlist", "setlist", "insert_cmd", |
| 10135 "idlist_opt", "likeop", "between_op", "in_op", |
| 10136 "paren_exprlist", "case_operand", "case_exprlist", "case_else", |
| 10137 "uniqueflag", "collate", "nmnum", "trigger_decl", |
| 10138 "trigger_cmd_list", "trigger_time", "trigger_event", "foreach_clause", |
| 10139 "when_clause", "trigger_cmd", "trnm", "tridxby", |
| 10140 "database_kw_opt", "key_opt", "add_column_fullname", "kwcolumn_opt", |
| 10141 "create_vtab", "vtabarglist", "vtabarg", "vtabargtoken", |
| 10142 "lp", "anylist", "wqlist", |
| 10143 }; |
| 10144 #endif /* NDEBUG */ |
| 10145 |
| 10146 #ifndef NDEBUG |
| 10147 /* For tracing reduce actions, the names of all rules are required. |
| 10148 */ |
| 10149 static const char *const yyRuleName[] = { |
| 10150 /* 0 */ "explain ::= EXPLAIN", |
| 10151 /* 1 */ "explain ::= EXPLAIN QUERY PLAN", |
| 10152 /* 2 */ "cmdx ::= cmd", |
| 10153 /* 3 */ "cmd ::= BEGIN transtype trans_opt", |
| 10154 /* 4 */ "transtype ::=", |
| 10155 /* 5 */ "transtype ::= DEFERRED", |
| 10156 /* 6 */ "transtype ::= IMMEDIATE", |
| 10157 /* 7 */ "transtype ::= EXCLUSIVE", |
| 10158 /* 8 */ "cmd ::= COMMIT trans_opt", |
| 10159 /* 9 */ "cmd ::= END trans_opt", |
| 10160 /* 10 */ "cmd ::= ROLLBACK trans_opt", |
| 10161 /* 11 */ "cmd ::= SAVEPOINT nm", |
| 10162 /* 12 */ "cmd ::= RELEASE savepoint_opt nm", |
| 10163 /* 13 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm", |
| 10164 /* 14 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm", |
| 10165 /* 15 */ "createkw ::= CREATE", |
| 10166 /* 16 */ "ifnotexists ::=", |
| 10167 /* 17 */ "ifnotexists ::= IF NOT EXISTS", |
| 10168 /* 18 */ "temp ::= TEMP", |
| 10169 /* 19 */ "temp ::=", |
| 10170 /* 20 */ "create_table_args ::= LP columnlist conslist_opt RP table_options", |
| 10171 /* 21 */ "create_table_args ::= AS select", |
| 10172 /* 22 */ "table_options ::=", |
| 10173 /* 23 */ "table_options ::= WITHOUT nm", |
| 10174 /* 24 */ "columnname ::= nm typetoken", |
| 10175 /* 25 */ "typetoken ::=", |
| 10176 /* 26 */ "typetoken ::= typename LP signed RP", |
| 10177 /* 27 */ "typetoken ::= typename LP signed COMMA signed RP", |
| 10178 /* 28 */ "typename ::= typename ID|STRING", |
| 10179 /* 29 */ "ccons ::= CONSTRAINT nm", |
| 10180 /* 30 */ "ccons ::= DEFAULT term", |
| 10181 /* 31 */ "ccons ::= DEFAULT LP expr RP", |
| 10182 /* 32 */ "ccons ::= DEFAULT PLUS term", |
| 10183 /* 33 */ "ccons ::= DEFAULT MINUS term", |
| 10184 /* 34 */ "ccons ::= DEFAULT ID|INDEXED", |
| 10185 /* 35 */ "ccons ::= NOT NULL onconf", |
| 10186 /* 36 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc", |
| 10187 /* 37 */ "ccons ::= UNIQUE onconf", |
| 10188 /* 38 */ "ccons ::= CHECK LP expr RP", |
| 10189 /* 39 */ "ccons ::= REFERENCES nm eidlist_opt refargs", |
| 10190 /* 40 */ "ccons ::= defer_subclause", |
| 10191 /* 41 */ "ccons ::= COLLATE ID|STRING", |
| 10192 /* 42 */ "autoinc ::=", |
| 10193 /* 43 */ "autoinc ::= AUTOINCR", |
| 10194 /* 44 */ "refargs ::=", |
| 10195 /* 45 */ "refargs ::= refargs refarg", |
| 10196 /* 46 */ "refarg ::= MATCH nm", |
| 10197 /* 47 */ "refarg ::= ON INSERT refact", |
| 10198 /* 48 */ "refarg ::= ON DELETE refact", |
| 10199 /* 49 */ "refarg ::= ON UPDATE refact", |
| 10200 /* 50 */ "refact ::= SET NULL", |
| 10201 /* 51 */ "refact ::= SET DEFAULT", |
| 10202 /* 52 */ "refact ::= CASCADE", |
| 10203 /* 53 */ "refact ::= RESTRICT", |
| 10204 /* 54 */ "refact ::= NO ACTION", |
| 10205 /* 55 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt", |
| 10206 /* 56 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt", |
| 10207 /* 57 */ "init_deferred_pred_opt ::=", |
| 10208 /* 58 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED", |
| 10209 /* 59 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE", |
| 10210 /* 60 */ "conslist_opt ::=", |
| 10211 /* 61 */ "tconscomma ::= COMMA", |
| 10212 /* 62 */ "tcons ::= CONSTRAINT nm", |
| 10213 /* 63 */ "tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf", |
| 10214 /* 64 */ "tcons ::= UNIQUE LP sortlist RP onconf", |
| 10215 /* 65 */ "tcons ::= CHECK LP expr RP onconf", |
| 10216 /* 66 */ "tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refarg
s defer_subclause_opt", |
| 10217 /* 67 */ "defer_subclause_opt ::=", |
| 10218 /* 68 */ "onconf ::=", |
| 10219 /* 69 */ "onconf ::= ON CONFLICT resolvetype", |
| 10220 /* 70 */ "orconf ::=", |
| 10221 /* 71 */ "orconf ::= OR resolvetype", |
| 10222 /* 72 */ "resolvetype ::= IGNORE", |
| 10223 /* 73 */ "resolvetype ::= REPLACE", |
| 10224 /* 74 */ "cmd ::= DROP TABLE ifexists fullname", |
| 10225 /* 75 */ "ifexists ::= IF EXISTS", |
| 10226 /* 76 */ "ifexists ::=", |
| 10227 /* 77 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select
", |
| 10228 /* 78 */ "cmd ::= DROP VIEW ifexists fullname", |
| 10229 /* 79 */ "cmd ::= select", |
| 10230 /* 80 */ "select ::= with selectnowith", |
| 10231 /* 81 */ "selectnowith ::= selectnowith multiselect_op oneselect", |
| 10232 /* 82 */ "multiselect_op ::= UNION", |
| 10233 /* 83 */ "multiselect_op ::= UNION ALL", |
| 10234 /* 84 */ "multiselect_op ::= EXCEPT|INTERSECT", |
| 10235 /* 85 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt
having_opt orderby_opt limit_opt", |
| 10236 /* 86 */ "values ::= VALUES LP nexprlist RP", |
| 10237 /* 87 */ "values ::= values COMMA LP exprlist RP", |
| 10238 /* 88 */ "distinct ::= DISTINCT", |
| 10239 /* 89 */ "distinct ::= ALL", |
| 10240 /* 90 */ "distinct ::=", |
| 10241 /* 91 */ "sclp ::=", |
| 10242 /* 92 */ "selcollist ::= sclp expr as", |
| 10243 /* 93 */ "selcollist ::= sclp STAR", |
| 10244 /* 94 */ "selcollist ::= sclp nm DOT STAR", |
| 10245 /* 95 */ "as ::= AS nm", |
| 10246 /* 96 */ "as ::=", |
| 10247 /* 97 */ "from ::=", |
| 10248 /* 98 */ "from ::= FROM seltablist", |
| 10249 /* 99 */ "stl_prefix ::= seltablist joinop", |
| 10250 /* 100 */ "stl_prefix ::=", |
| 10251 /* 101 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt", |
| 10252 /* 102 */ "seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt
", |
| 10253 /* 103 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt", |
| 10254 /* 104 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt", |
| 10255 /* 105 */ "dbnm ::=", |
| 10256 /* 106 */ "dbnm ::= DOT nm", |
| 10257 /* 107 */ "fullname ::= nm dbnm", |
| 10258 /* 108 */ "joinop ::= COMMA|JOIN", |
| 10259 /* 109 */ "joinop ::= JOIN_KW JOIN", |
| 10260 /* 110 */ "joinop ::= JOIN_KW nm JOIN", |
| 10261 /* 111 */ "joinop ::= JOIN_KW nm nm JOIN", |
| 10262 /* 112 */ "on_opt ::= ON expr", |
| 10263 /* 113 */ "on_opt ::=", |
| 10264 /* 114 */ "indexed_opt ::=", |
| 10265 /* 115 */ "indexed_opt ::= INDEXED BY nm", |
| 10266 /* 116 */ "indexed_opt ::= NOT INDEXED", |
| 10267 /* 117 */ "using_opt ::= USING LP idlist RP", |
| 10268 /* 118 */ "using_opt ::=", |
| 10269 /* 119 */ "orderby_opt ::=", |
| 10270 /* 120 */ "orderby_opt ::= ORDER BY sortlist", |
| 10271 /* 121 */ "sortlist ::= sortlist COMMA expr sortorder", |
| 10272 /* 122 */ "sortlist ::= expr sortorder", |
| 10273 /* 123 */ "sortorder ::= ASC", |
| 10274 /* 124 */ "sortorder ::= DESC", |
| 10275 /* 125 */ "sortorder ::=", |
| 10276 /* 126 */ "groupby_opt ::=", |
| 10277 /* 127 */ "groupby_opt ::= GROUP BY nexprlist", |
| 10278 /* 128 */ "having_opt ::=", |
| 10279 /* 129 */ "having_opt ::= HAVING expr", |
| 10280 /* 130 */ "limit_opt ::=", |
| 10281 /* 131 */ "limit_opt ::= LIMIT expr", |
| 10282 /* 132 */ "limit_opt ::= LIMIT expr OFFSET expr", |
| 10283 /* 133 */ "limit_opt ::= LIMIT expr COMMA expr", |
| 10284 /* 134 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt", |
| 10285 /* 135 */ "where_opt ::=", |
| 10286 /* 136 */ "where_opt ::= WHERE expr", |
| 10287 /* 137 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_op
t", |
| 10288 /* 138 */ "setlist ::= setlist COMMA nm EQ expr", |
| 10289 /* 139 */ "setlist ::= setlist COMMA LP idlist RP EQ expr", |
| 10290 /* 140 */ "setlist ::= nm EQ expr", |
| 10291 /* 141 */ "setlist ::= LP idlist RP EQ expr", |
| 10292 /* 142 */ "cmd ::= with insert_cmd INTO fullname idlist_opt select", |
| 10293 /* 143 */ "cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALUES", |
| 10294 /* 144 */ "insert_cmd ::= INSERT orconf", |
| 10295 /* 145 */ "insert_cmd ::= REPLACE", |
| 10296 /* 146 */ "idlist_opt ::=", |
| 10297 /* 147 */ "idlist_opt ::= LP idlist RP", |
| 10298 /* 148 */ "idlist ::= idlist COMMA nm", |
| 10299 /* 149 */ "idlist ::= nm", |
| 10300 /* 150 */ "expr ::= LP expr RP", |
| 10301 /* 151 */ "term ::= NULL", |
| 10302 /* 152 */ "expr ::= ID|INDEXED", |
| 10303 /* 153 */ "expr ::= JOIN_KW", |
| 10304 /* 154 */ "expr ::= nm DOT nm", |
| 10305 /* 155 */ "expr ::= nm DOT nm DOT nm", |
| 10306 /* 156 */ "term ::= FLOAT|BLOB", |
| 10307 /* 157 */ "term ::= STRING", |
| 10308 /* 158 */ "term ::= INTEGER", |
| 10309 /* 159 */ "expr ::= VARIABLE", |
| 10310 /* 160 */ "expr ::= expr COLLATE ID|STRING", |
| 10311 /* 161 */ "expr ::= CAST LP expr AS typetoken RP", |
| 10312 /* 162 */ "expr ::= ID|INDEXED LP distinct exprlist RP", |
| 10313 /* 163 */ "expr ::= ID|INDEXED LP STAR RP", |
| 10314 /* 164 */ "term ::= CTIME_KW", |
| 10315 /* 165 */ "expr ::= LP nexprlist COMMA expr RP", |
| 10316 /* 166 */ "expr ::= expr AND expr", |
| 10317 /* 167 */ "expr ::= expr OR expr", |
| 10318 /* 168 */ "expr ::= expr LT|GT|GE|LE expr", |
| 10319 /* 169 */ "expr ::= expr EQ|NE expr", |
| 10320 /* 170 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr", |
| 10321 /* 171 */ "expr ::= expr PLUS|MINUS expr", |
| 10322 /* 172 */ "expr ::= expr STAR|SLASH|REM expr", |
| 10323 /* 173 */ "expr ::= expr CONCAT expr", |
| 10324 /* 174 */ "likeop ::= LIKE_KW|MATCH", |
| 10325 /* 175 */ "likeop ::= NOT LIKE_KW|MATCH", |
| 10326 /* 176 */ "expr ::= expr likeop expr", |
| 10327 /* 177 */ "expr ::= expr likeop expr ESCAPE expr", |
| 10328 /* 178 */ "expr ::= expr ISNULL|NOTNULL", |
| 10329 /* 179 */ "expr ::= expr NOT NULL", |
| 10330 /* 180 */ "expr ::= expr IS expr", |
| 10331 /* 181 */ "expr ::= expr IS NOT expr", |
| 10332 /* 182 */ "expr ::= NOT expr", |
| 10333 /* 183 */ "expr ::= BITNOT expr", |
| 10334 /* 184 */ "expr ::= MINUS expr", |
| 10335 /* 185 */ "expr ::= PLUS expr", |
| 10336 /* 186 */ "between_op ::= BETWEEN", |
| 10337 /* 187 */ "between_op ::= NOT BETWEEN", |
| 10338 /* 188 */ "expr ::= expr between_op expr AND expr", |
| 10339 /* 189 */ "in_op ::= IN", |
| 10340 /* 190 */ "in_op ::= NOT IN", |
| 10341 /* 191 */ "expr ::= expr in_op LP exprlist RP", |
| 10342 /* 192 */ "expr ::= LP select RP", |
| 10343 /* 193 */ "expr ::= expr in_op LP select RP", |
| 10344 /* 194 */ "expr ::= expr in_op nm dbnm paren_exprlist", |
| 10345 /* 195 */ "expr ::= EXISTS LP select RP", |
| 10346 /* 196 */ "expr ::= CASE case_operand case_exprlist case_else END", |
| 10347 /* 197 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr", |
| 10348 /* 198 */ "case_exprlist ::= WHEN expr THEN expr", |
| 10349 /* 199 */ "case_else ::= ELSE expr", |
| 10350 /* 200 */ "case_else ::=", |
| 10351 /* 201 */ "case_operand ::= expr", |
| 10352 /* 202 */ "case_operand ::=", |
| 10353 /* 203 */ "exprlist ::=", |
| 10354 /* 204 */ "nexprlist ::= nexprlist COMMA expr", |
| 10355 /* 205 */ "nexprlist ::= expr", |
| 10356 /* 206 */ "paren_exprlist ::=", |
| 10357 /* 207 */ "paren_exprlist ::= LP exprlist RP", |
| 10358 /* 208 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortl
ist RP where_opt", |
| 10359 /* 209 */ "uniqueflag ::= UNIQUE", |
| 10360 /* 210 */ "uniqueflag ::=", |
| 10361 /* 211 */ "eidlist_opt ::=", |
| 10362 /* 212 */ "eidlist_opt ::= LP eidlist RP", |
| 10363 /* 213 */ "eidlist ::= eidlist COMMA nm collate sortorder", |
| 10364 /* 214 */ "eidlist ::= nm collate sortorder", |
| 10365 /* 215 */ "collate ::=", |
| 10366 /* 216 */ "collate ::= COLLATE ID|STRING", |
| 10367 /* 217 */ "cmd ::= DROP INDEX ifexists fullname", |
| 10368 /* 218 */ "cmd ::= VACUUM", |
| 10369 /* 219 */ "cmd ::= VACUUM nm", |
| 10370 /* 220 */ "cmd ::= PRAGMA nm dbnm", |
| 10371 /* 221 */ "cmd ::= PRAGMA nm dbnm EQ nmnum", |
| 10372 /* 222 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP", |
| 10373 /* 223 */ "cmd ::= PRAGMA nm dbnm EQ minus_num", |
| 10374 /* 224 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP", |
| 10375 /* 225 */ "plus_num ::= PLUS INTEGER|FLOAT", |
| 10376 /* 226 */ "minus_num ::= MINUS INTEGER|FLOAT", |
| 10377 /* 227 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END", |
| 10378 /* 228 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigg
er_event ON fullname foreach_clause when_clause", |
| 10379 /* 229 */ "trigger_time ::= BEFORE", |
| 10380 /* 230 */ "trigger_time ::= AFTER", |
| 10381 /* 231 */ "trigger_time ::= INSTEAD OF", |
| 10382 /* 232 */ "trigger_time ::=", |
| 10383 /* 233 */ "trigger_event ::= DELETE|INSERT", |
| 10384 /* 234 */ "trigger_event ::= UPDATE", |
| 10385 /* 235 */ "trigger_event ::= UPDATE OF idlist", |
| 10386 /* 236 */ "when_clause ::=", |
| 10387 /* 237 */ "when_clause ::= WHEN expr", |
| 10388 /* 238 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI", |
| 10389 /* 239 */ "trigger_cmd_list ::= trigger_cmd SEMI", |
| 10390 /* 240 */ "trnm ::= nm DOT nm", |
| 10391 /* 241 */ "tridxby ::= INDEXED BY nm", |
| 10392 /* 242 */ "tridxby ::= NOT INDEXED", |
| 10393 /* 243 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt", |
| 10394 /* 244 */ "trigger_cmd ::= insert_cmd INTO trnm idlist_opt select", |
| 10395 /* 245 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt", |
| 10396 /* 246 */ "trigger_cmd ::= select", |
| 10397 /* 247 */ "expr ::= RAISE LP IGNORE RP", |
| 10398 /* 248 */ "expr ::= RAISE LP raisetype COMMA nm RP", |
| 10399 /* 249 */ "raisetype ::= ROLLBACK", |
| 10400 /* 250 */ "raisetype ::= ABORT", |
| 10401 /* 251 */ "raisetype ::= FAIL", |
| 10402 /* 252 */ "cmd ::= DROP TRIGGER ifexists fullname", |
| 10403 /* 253 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt", |
| 10404 /* 254 */ "cmd ::= DETACH database_kw_opt expr", |
| 10405 /* 255 */ "key_opt ::=", |
| 10406 /* 256 */ "key_opt ::= KEY expr", |
| 10407 /* 257 */ "cmd ::= REINDEX", |
| 10408 /* 258 */ "cmd ::= REINDEX nm dbnm", |
| 10409 /* 259 */ "cmd ::= ANALYZE", |
| 10410 /* 260 */ "cmd ::= ANALYZE nm dbnm", |
| 10411 /* 261 */ "cmd ::= ALTER TABLE fullname RENAME TO nm", |
| 10412 /* 262 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname
carglist", |
| 10413 /* 263 */ "add_column_fullname ::= fullname", |
| 10414 /* 264 */ "cmd ::= create_vtab", |
| 10415 /* 265 */ "cmd ::= create_vtab LP vtabarglist RP", |
| 10416 /* 266 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm"
, |
| 10417 /* 267 */ "vtabarg ::=", |
| 10418 /* 268 */ "vtabargtoken ::= ANY", |
| 10419 /* 269 */ "vtabargtoken ::= lp anylist RP", |
| 10420 /* 270 */ "lp ::= LP", |
| 10421 /* 271 */ "with ::=", |
| 10422 /* 272 */ "with ::= WITH wqlist", |
| 10423 /* 273 */ "with ::= WITH RECURSIVE wqlist", |
| 10424 /* 274 */ "wqlist ::= nm eidlist_opt AS LP select RP", |
| 10425 /* 275 */ "wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP", |
| 10426 /* 276 */ "input ::= cmdlist", |
| 10427 /* 277 */ "cmdlist ::= cmdlist ecmd", |
| 10428 /* 278 */ "cmdlist ::= ecmd", |
| 10429 /* 279 */ "ecmd ::= SEMI", |
| 10430 /* 280 */ "ecmd ::= explain cmdx SEMI", |
| 10431 /* 281 */ "explain ::=", |
| 10432 /* 282 */ "trans_opt ::=", |
| 10433 /* 283 */ "trans_opt ::= TRANSACTION", |
| 10434 /* 284 */ "trans_opt ::= TRANSACTION nm", |
| 10435 /* 285 */ "savepoint_opt ::= SAVEPOINT", |
| 10436 /* 286 */ "savepoint_opt ::=", |
| 10437 /* 287 */ "cmd ::= create_table create_table_args", |
| 10438 /* 288 */ "columnlist ::= columnlist COMMA columnname carglist", |
| 10439 /* 289 */ "columnlist ::= columnname carglist", |
| 10440 /* 290 */ "nm ::= ID|INDEXED", |
| 10441 /* 291 */ "nm ::= STRING", |
| 10442 /* 292 */ "nm ::= JOIN_KW", |
| 10443 /* 293 */ "typetoken ::= typename", |
| 10444 /* 294 */ "typename ::= ID|STRING", |
| 10445 /* 295 */ "signed ::= plus_num", |
| 10446 /* 296 */ "signed ::= minus_num", |
| 10447 /* 297 */ "carglist ::= carglist ccons", |
| 10448 /* 298 */ "carglist ::=", |
| 10449 /* 299 */ "ccons ::= NULL onconf", |
| 10450 /* 300 */ "conslist_opt ::= COMMA conslist", |
| 10451 /* 301 */ "conslist ::= conslist tconscomma tcons", |
| 10452 /* 302 */ "conslist ::= tcons", |
| 10453 /* 303 */ "tconscomma ::=", |
| 10454 /* 304 */ "defer_subclause_opt ::= defer_subclause", |
| 10455 /* 305 */ "resolvetype ::= raisetype", |
| 10456 /* 306 */ "selectnowith ::= oneselect", |
| 10457 /* 307 */ "oneselect ::= values", |
| 10458 /* 308 */ "sclp ::= selcollist COMMA", |
| 10459 /* 309 */ "as ::= ID|STRING", |
| 10460 /* 310 */ "expr ::= term", |
| 10461 /* 311 */ "exprlist ::= nexprlist", |
| 10462 /* 312 */ "nmnum ::= plus_num", |
| 10463 /* 313 */ "nmnum ::= nm", |
| 10464 /* 314 */ "nmnum ::= ON", |
| 10465 /* 315 */ "nmnum ::= DELETE", |
| 10466 /* 316 */ "nmnum ::= DEFAULT", |
| 10467 /* 317 */ "plus_num ::= INTEGER|FLOAT", |
| 10468 /* 318 */ "foreach_clause ::=", |
| 10469 /* 319 */ "foreach_clause ::= FOR EACH ROW", |
| 10470 /* 320 */ "trnm ::= nm", |
| 10471 /* 321 */ "tridxby ::=", |
| 10472 /* 322 */ "database_kw_opt ::= DATABASE", |
| 10473 /* 323 */ "database_kw_opt ::=", |
| 10474 /* 324 */ "kwcolumn_opt ::=", |
| 10475 /* 325 */ "kwcolumn_opt ::= COLUMNKW", |
| 10476 /* 326 */ "vtabarglist ::= vtabarg", |
| 10477 /* 327 */ "vtabarglist ::= vtabarglist COMMA vtabarg", |
| 10478 /* 328 */ "vtabarg ::= vtabarg vtabargtoken", |
| 10479 /* 329 */ "anylist ::=", |
| 10480 /* 330 */ "anylist ::= anylist LP anylist RP", |
| 10481 /* 331 */ "anylist ::= anylist ANY", |
| 10482 }; |
| 10483 #endif /* NDEBUG */ |
| 10484 |
| 10485 |
| 10486 #if YYSTACKDEPTH<=0 |
| 10487 /* |
| 10488 ** Try to increase the size of the parser stack. Return the number |
| 10489 ** of errors. Return 0 on success. |
| 10490 */ |
| 10491 static int yyGrowStack(yyParser *p){ |
| 10492 int newSize; |
| 10493 int idx; |
| 10494 yyStackEntry *pNew; |
| 10495 |
| 10496 newSize = p->yystksz*2 + 100; |
| 10497 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0; |
| 10498 if( p->yystack==&p->yystk0 ){ |
| 10499 pNew = malloc(newSize*sizeof(pNew[0])); |
| 10500 if( pNew ) pNew[0] = p->yystk0; |
| 10501 }else{ |
| 10502 pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); |
| 10503 } |
| 10504 if( pNew ){ |
| 10505 p->yystack = pNew; |
| 10506 p->yytos = &p->yystack[idx]; |
| 10507 #ifndef NDEBUG |
| 10508 if( yyTraceFILE ){ |
| 10509 fprintf(yyTraceFILE,"%sStack grows from %d to %d entries.\n", |
| 10510 yyTracePrompt, p->yystksz, newSize); |
| 10511 } |
| 10512 #endif |
| 10513 p->yystksz = newSize; |
| 10514 } |
| 10515 return pNew==0; |
| 10516 } |
| 10517 #endif |
| 10518 |
| 10519 /* Datatype of the argument to the memory allocated passed as the |
| 10520 ** second argument to sqlite3ParserAlloc() below. This can be changed by |
| 10521 ** putting an appropriate #define in the %include section of the input |
| 10522 ** grammar. |
| 10523 */ |
| 10524 #ifndef YYMALLOCARGTYPE |
| 10525 # define YYMALLOCARGTYPE size_t |
| 10526 #endif |
| 10527 |
| 10528 /* Initialize a new parser that has already been allocated. |
| 10529 */ |
| 10530 SQLITE_PRIVATE void sqlite3ParserInit(void *yypParser){ |
| 10531 yyParser *pParser = (yyParser*)yypParser; |
| 10532 #ifdef YYTRACKMAXSTACKDEPTH |
| 10533 pParser->yyhwm = 0; |
| 10534 #endif |
| 10535 #if YYSTACKDEPTH<=0 |
| 10536 pParser->yytos = NULL; |
| 10537 pParser->yystack = NULL; |
| 10538 pParser->yystksz = 0; |
| 10539 if( yyGrowStack(pParser) ){ |
| 10540 pParser->yystack = &pParser->yystk0; |
| 10541 pParser->yystksz = 1; |
| 10542 } |
| 10543 #endif |
| 10544 #ifndef YYNOERRORRECOVERY |
| 10545 pParser->yyerrcnt = -1; |
| 10546 #endif |
| 10547 pParser->yytos = pParser->yystack; |
| 10548 pParser->yystack[0].stateno = 0; |
| 10549 pParser->yystack[0].major = 0; |
| 10550 } |
| 10551 |
| 10552 #ifndef sqlite3Parser_ENGINEALWAYSONSTACK |
| 10553 /* |
| 10554 ** This function allocates a new parser. |
| 10555 ** The only argument is a pointer to a function which works like |
| 10556 ** malloc. |
| 10557 ** |
| 10558 ** Inputs: |
| 10559 ** A pointer to the function used to allocate memory. |
| 10560 ** |
| 10561 ** Outputs: |
| 10562 ** A pointer to a parser. This pointer is used in subsequent calls |
| 10563 ** to sqlite3Parser and sqlite3ParserFree. |
| 10564 */ |
| 10565 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(YYMALLOCARGTYPE)){ |
| 10566 yyParser *pParser; |
| 10567 pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) ); |
| 10568 if( pParser ) sqlite3ParserInit(pParser); |
| 10569 return pParser; |
| 10570 } |
| 10571 #endif /* sqlite3Parser_ENGINEALWAYSONSTACK */ |
| 10572 |
| 10573 |
| 10574 /* The following function deletes the "minor type" or semantic value |
| 10575 ** associated with a symbol. The symbol can be either a terminal |
| 10576 ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is |
| 10577 ** a pointer to the value to be deleted. The code used to do the |
| 10578 ** deletions is derived from the %destructor and/or %token_destructor |
| 10579 ** directives of the input grammar. |
| 10580 */ |
| 10581 static void yy_destructor( |
| 10582 yyParser *yypParser, /* The parser */ |
| 10583 YYCODETYPE yymajor, /* Type code for object to destroy */ |
| 10584 YYMINORTYPE *yypminor /* The object to be destroyed */ |
| 10585 ){ |
| 10586 sqlite3ParserARG_FETCH; |
| 10587 switch( yymajor ){ |
| 10588 /* Here is inserted the actions which take place when a |
| 10589 ** terminal or non-terminal is destroyed. This can happen |
| 10590 ** when the symbol is popped from the stack during a |
| 10591 ** reduce or during error processing or when a parser is |
| 10592 ** being destroyed before it is finished parsing. |
| 10593 ** |
| 10594 ** Note: during a reduce, the only symbols destroyed are those |
| 10595 ** which appear on the RHS of the rule, but which are *not* used |
| 10596 ** inside the C code. |
| 10597 */ |
| 10598 /********* Begin destructor definitions ***************************************/ |
| 10599 case 163: /* select */ |
| 10600 case 194: /* selectnowith */ |
| 10601 case 195: /* oneselect */ |
| 10602 case 206: /* values */ |
| 10603 { |
| 10604 sqlite3SelectDelete(pParse->db, (yypminor->yy243)); |
| 10605 } |
| 10606 break; |
| 10607 case 172: /* term */ |
| 10608 case 173: /* expr */ |
| 10609 { |
| 10610 sqlite3ExprDelete(pParse->db, (yypminor->yy190).pExpr); |
| 10611 } |
| 10612 break; |
| 10613 case 177: /* eidlist_opt */ |
| 10614 case 186: /* sortlist */ |
| 10615 case 187: /* eidlist */ |
| 10616 case 199: /* selcollist */ |
| 10617 case 202: /* groupby_opt */ |
| 10618 case 204: /* orderby_opt */ |
| 10619 case 207: /* nexprlist */ |
| 10620 case 208: /* exprlist */ |
| 10621 case 209: /* sclp */ |
| 10622 case 218: /* setlist */ |
| 10623 case 224: /* paren_exprlist */ |
| 10624 case 226: /* case_exprlist */ |
| 10625 { |
| 10626 sqlite3ExprListDelete(pParse->db, (yypminor->yy148)); |
| 10627 } |
| 10628 break; |
| 10629 case 193: /* fullname */ |
| 10630 case 200: /* from */ |
| 10631 case 211: /* seltablist */ |
| 10632 case 212: /* stl_prefix */ |
| 10633 { |
| 10634 sqlite3SrcListDelete(pParse->db, (yypminor->yy185)); |
| 10635 } |
| 10636 break; |
| 10637 case 196: /* with */ |
| 10638 case 250: /* wqlist */ |
| 10639 { |
| 10640 sqlite3WithDelete(pParse->db, (yypminor->yy285)); |
| 10641 } |
| 10642 break; |
| 10643 case 201: /* where_opt */ |
| 10644 case 203: /* having_opt */ |
| 10645 case 215: /* on_opt */ |
| 10646 case 225: /* case_operand */ |
| 10647 case 227: /* case_else */ |
| 10648 case 236: /* when_clause */ |
| 10649 case 241: /* key_opt */ |
| 10650 { |
| 10651 sqlite3ExprDelete(pParse->db, (yypminor->yy72)); |
| 10652 } |
| 10653 break; |
| 10654 case 216: /* using_opt */ |
| 10655 case 217: /* idlist */ |
| 10656 case 220: /* idlist_opt */ |
| 10657 { |
| 10658 sqlite3IdListDelete(pParse->db, (yypminor->yy254)); |
| 10659 } |
| 10660 break; |
| 10661 case 232: /* trigger_cmd_list */ |
| 10662 case 237: /* trigger_cmd */ |
| 10663 { |
| 10664 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy145)); |
| 10665 } |
| 10666 break; |
| 10667 case 234: /* trigger_event */ |
| 10668 { |
| 10669 sqlite3IdListDelete(pParse->db, (yypminor->yy332).b); |
| 10670 } |
| 10671 break; |
| 10672 /********* End destructor definitions *****************************************/ |
| 10673 default: break; /* If no destructor action specified: do nothing */ |
| 10674 } |
| 10675 } |
| 10676 |
| 10677 /* |
| 10678 ** Pop the parser's stack once. |
| 10679 ** |
| 10680 ** If there is a destructor routine associated with the token which |
| 10681 ** is popped from the stack, then call it. |
| 10682 */ |
| 10683 static void yy_pop_parser_stack(yyParser *pParser){ |
| 10684 yyStackEntry *yytos; |
| 10685 assert( pParser->yytos!=0 ); |
| 10686 assert( pParser->yytos > pParser->yystack ); |
| 10687 yytos = pParser->yytos--; |
| 10688 #ifndef NDEBUG |
| 10689 if( yyTraceFILE ){ |
| 10690 fprintf(yyTraceFILE,"%sPopping %s\n", |
| 10691 yyTracePrompt, |
| 10692 yyTokenName[yytos->major]); |
| 10693 } |
| 10694 #endif |
| 10695 yy_destructor(pParser, yytos->major, &yytos->minor); |
| 10696 } |
| 10697 |
| 10698 /* |
| 10699 ** Clear all secondary memory allocations from the parser |
| 10700 */ |
| 10701 SQLITE_PRIVATE void sqlite3ParserFinalize(void *p){ |
| 10702 yyParser *pParser = (yyParser*)p; |
| 10703 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser); |
| 10704 #if YYSTACKDEPTH<=0 |
| 10705 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack); |
| 10706 #endif |
| 10707 } |
| 10708 |
| 10709 #ifndef sqlite3Parser_ENGINEALWAYSONSTACK |
| 10710 /* |
| 10711 ** Deallocate and destroy a parser. Destructors are called for |
| 10712 ** all stack elements before shutting the parser down. |
| 10713 ** |
| 10714 ** If the YYPARSEFREENEVERNULL macro exists (for example because it |
| 10715 ** is defined in a %include section of the input grammar) then it is |
| 10716 ** assumed that the input pointer is never NULL. |
| 10717 */ |
| 10718 SQLITE_PRIVATE void sqlite3ParserFree( |
| 10719 void *p, /* The parser to be deleted */ |
| 10720 void (*freeProc)(void*) /* Function used to reclaim memory */ |
| 10721 ){ |
| 10722 #ifndef YYPARSEFREENEVERNULL |
| 10723 if( p==0 ) return; |
| 10724 #endif |
| 10725 sqlite3ParserFinalize(p); |
| 10726 (*freeProc)(p); |
| 10727 } |
| 10728 #endif /* sqlite3Parser_ENGINEALWAYSONSTACK */ |
| 10729 |
| 10730 /* |
| 10731 ** Return the peak depth of the stack for a parser. |
| 10732 */ |
| 10733 #ifdef YYTRACKMAXSTACKDEPTH |
| 10734 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){ |
| 10735 yyParser *pParser = (yyParser*)p; |
| 10736 return pParser->yyhwm; |
| 10737 } |
| 10738 #endif |
| 10739 |
| 10740 /* |
| 10741 ** Find the appropriate action for a parser given the terminal |
| 10742 ** look-ahead token iLookAhead. |
| 10743 */ |
| 10744 static unsigned int yy_find_shift_action( |
| 10745 yyParser *pParser, /* The parser */ |
| 10746 YYCODETYPE iLookAhead /* The look-ahead token */ |
| 10747 ){ |
| 10748 int i; |
| 10749 int stateno = pParser->yytos->stateno; |
| 10750 |
| 10751 if( stateno>=YY_MIN_REDUCE ) return stateno; |
| 10752 assert( stateno <= YY_SHIFT_COUNT ); |
| 10753 do{ |
| 10754 i = yy_shift_ofst[stateno]; |
| 10755 assert( iLookAhead!=YYNOCODE ); |
| 10756 i += iLookAhead; |
| 10757 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ |
| 10758 #ifdef YYFALLBACK |
| 10759 YYCODETYPE iFallback; /* Fallback token */ |
| 10760 if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0]) |
| 10761 && (iFallback = yyFallback[iLookAhead])!=0 ){ |
| 10762 #ifndef NDEBUG |
| 10763 if( yyTraceFILE ){ |
| 10764 fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n", |
| 10765 yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); |
| 10766 } |
| 10767 #endif |
| 10768 assert( yyFallback[iFallback]==0 ); /* Fallback loop must terminate */ |
| 10769 iLookAhead = iFallback; |
| 10770 continue; |
| 10771 } |
| 10772 #endif |
| 10773 #ifdef YYWILDCARD |
| 10774 { |
| 10775 int j = i - iLookAhead + YYWILDCARD; |
| 10776 if( |
| 10777 #if YY_SHIFT_MIN+YYWILDCARD<0 |
| 10778 j>=0 && |
| 10779 #endif |
| 10780 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT |
| 10781 j<YY_ACTTAB_COUNT && |
| 10782 #endif |
| 10783 yy_lookahead[j]==YYWILDCARD && iLookAhead>0 |
| 10784 ){ |
| 10785 #ifndef NDEBUG |
| 10786 if( yyTraceFILE ){ |
| 10787 fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n", |
| 10788 yyTracePrompt, yyTokenName[iLookAhead], |
| 10789 yyTokenName[YYWILDCARD]); |
| 10790 } |
| 10791 #endif /* NDEBUG */ |
| 10792 return yy_action[j]; |
| 10793 } |
| 10794 } |
| 10795 #endif /* YYWILDCARD */ |
| 10796 return yy_default[stateno]; |
| 10797 }else{ |
| 10798 return yy_action[i]; |
| 10799 } |
| 10800 }while(1); |
| 10801 } |
| 10802 |
| 10803 /* |
| 10804 ** Find the appropriate action for a parser given the non-terminal |
| 10805 ** look-ahead token iLookAhead. |
| 10806 */ |
| 10807 static int yy_find_reduce_action( |
| 10808 int stateno, /* Current state number */ |
| 10809 YYCODETYPE iLookAhead /* The look-ahead token */ |
| 10810 ){ |
| 10811 int i; |
| 10812 #ifdef YYERRORSYMBOL |
| 10813 if( stateno>YY_REDUCE_COUNT ){ |
| 10814 return yy_default[stateno]; |
| 10815 } |
| 10816 #else |
| 10817 assert( stateno<=YY_REDUCE_COUNT ); |
| 10818 #endif |
| 10819 i = yy_reduce_ofst[stateno]; |
| 10820 assert( i!=YY_REDUCE_USE_DFLT ); |
| 10821 assert( iLookAhead!=YYNOCODE ); |
| 10822 i += iLookAhead; |
| 10823 #ifdef YYERRORSYMBOL |
| 10824 if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ |
| 10825 return yy_default[stateno]; |
| 10826 } |
| 10827 #else |
| 10828 assert( i>=0 && i<YY_ACTTAB_COUNT ); |
| 10829 assert( yy_lookahead[i]==iLookAhead ); |
| 10830 #endif |
| 10831 return yy_action[i]; |
| 10832 } |
| 10833 |
| 10834 /* |
| 10835 ** The following routine is called if the stack overflows. |
| 10836 */ |
| 10837 static void yyStackOverflow(yyParser *yypParser){ |
| 10838 sqlite3ParserARG_FETCH; |
| 10839 #ifndef NDEBUG |
| 10840 if( yyTraceFILE ){ |
| 10841 fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); |
| 10842 } |
| 10843 #endif |
| 10844 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); |
| 10845 /* Here code is inserted which will execute if the parser |
| 10846 ** stack every overflows */ |
| 10847 /******** Begin %stack_overflow code ******************************************/ |
| 10848 |
| 10849 sqlite3ErrorMsg(pParse, "parser stack overflow"); |
| 10850 /******** End %stack_overflow code ********************************************/ |
| 10851 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var
*/ |
| 10852 } |
| 10853 |
| 10854 /* |
| 10855 ** Print tracing information for a SHIFT action |
| 10856 */ |
| 10857 #ifndef NDEBUG |
| 10858 static void yyTraceShift(yyParser *yypParser, int yyNewState){ |
| 10859 if( yyTraceFILE ){ |
| 10860 if( yyNewState<YYNSTATE ){ |
| 10861 fprintf(yyTraceFILE,"%sShift '%s', go to state %d\n", |
| 10862 yyTracePrompt,yyTokenName[yypParser->yytos->major], |
| 10863 yyNewState); |
| 10864 }else{ |
| 10865 fprintf(yyTraceFILE,"%sShift '%s'\n", |
| 10866 yyTracePrompt,yyTokenName[yypParser->yytos->major]); |
| 10867 } |
| 10868 } |
| 10869 } |
| 10870 #else |
| 10871 # define yyTraceShift(X,Y) |
| 10872 #endif |
| 10873 |
| 10874 /* |
| 10875 ** Perform a shift action. |
| 10876 */ |
| 10877 static void yy_shift( |
| 10878 yyParser *yypParser, /* The parser to be shifted */ |
| 10879 int yyNewState, /* The new state to shift in */ |
| 10880 int yyMajor, /* The major token to shift in */ |
| 10881 sqlite3ParserTOKENTYPE yyMinor /* The minor token to shift in */ |
| 10882 ){ |
| 10883 yyStackEntry *yytos; |
| 10884 yypParser->yytos++; |
| 10885 #ifdef YYTRACKMAXSTACKDEPTH |
| 10886 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ |
| 10887 yypParser->yyhwm++; |
| 10888 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) ); |
| 10889 } |
| 10890 #endif |
| 10891 #if YYSTACKDEPTH>0 |
| 10892 if( yypParser->yytos>=&yypParser->yystack[YYSTACKDEPTH] ){ |
| 10893 yypParser->yytos--; |
| 10894 yyStackOverflow(yypParser); |
| 10895 return; |
| 10896 } |
| 10897 #else |
| 10898 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){ |
| 10899 if( yyGrowStack(yypParser) ){ |
| 10900 yypParser->yytos--; |
| 10901 yyStackOverflow(yypParser); |
| 10902 return; |
| 10903 } |
| 10904 } |
| 10905 #endif |
| 10906 if( yyNewState > YY_MAX_SHIFT ){ |
| 10907 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; |
| 10908 } |
| 10909 yytos = yypParser->yytos; |
| 10910 yytos->stateno = (YYACTIONTYPE)yyNewState; |
| 10911 yytos->major = (YYCODETYPE)yyMajor; |
| 10912 yytos->minor.yy0 = yyMinor; |
| 10913 yyTraceShift(yypParser, yyNewState); |
| 10914 } |
| 10915 |
| 10916 /* The following table contains information about every rule that |
| 10917 ** is used during the reduce. |
| 10918 */ |
| 10919 static const struct { |
| 10920 YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ |
| 10921 unsigned char nrhs; /* Number of right-hand side symbols in the rule */ |
| 10922 } yyRuleInfo[] = { |
| 10923 { 147, 1 }, |
| 10924 { 147, 3 }, |
| 10925 { 148, 1 }, |
| 10926 { 149, 3 }, |
| 10927 { 150, 0 }, |
| 10928 { 150, 1 }, |
| 10929 { 150, 1 }, |
| 10930 { 150, 1 }, |
| 10931 { 149, 2 }, |
| 10932 { 149, 2 }, |
| 10933 { 149, 2 }, |
| 10934 { 149, 2 }, |
| 10935 { 149, 3 }, |
| 10936 { 149, 5 }, |
| 10937 { 154, 6 }, |
| 10938 { 156, 1 }, |
| 10939 { 158, 0 }, |
| 10940 { 158, 3 }, |
| 10941 { 157, 1 }, |
| 10942 { 157, 0 }, |
| 10943 { 155, 5 }, |
| 10944 { 155, 2 }, |
| 10945 { 162, 0 }, |
| 10946 { 162, 2 }, |
| 10947 { 164, 2 }, |
| 10948 { 166, 0 }, |
| 10949 { 166, 4 }, |
| 10950 { 166, 6 }, |
| 10951 { 167, 2 }, |
| 10952 { 171, 2 }, |
| 10953 { 171, 2 }, |
| 10954 { 171, 4 }, |
| 10955 { 171, 3 }, |
| 10956 { 171, 3 }, |
| 10957 { 171, 2 }, |
| 10958 { 171, 3 }, |
| 10959 { 171, 5 }, |
| 10960 { 171, 2 }, |
| 10961 { 171, 4 }, |
| 10962 { 171, 4 }, |
| 10963 { 171, 1 }, |
| 10964 { 171, 2 }, |
| 10965 { 176, 0 }, |
| 10966 { 176, 1 }, |
| 10967 { 178, 0 }, |
| 10968 { 178, 2 }, |
| 10969 { 180, 2 }, |
| 10970 { 180, 3 }, |
| 10971 { 180, 3 }, |
| 10972 { 180, 3 }, |
| 10973 { 181, 2 }, |
| 10974 { 181, 2 }, |
| 10975 { 181, 1 }, |
| 10976 { 181, 1 }, |
| 10977 { 181, 2 }, |
| 10978 { 179, 3 }, |
| 10979 { 179, 2 }, |
| 10980 { 182, 0 }, |
| 10981 { 182, 2 }, |
| 10982 { 182, 2 }, |
| 10983 { 161, 0 }, |
| 10984 { 184, 1 }, |
| 10985 { 185, 2 }, |
| 10986 { 185, 7 }, |
| 10987 { 185, 5 }, |
| 10988 { 185, 5 }, |
| 10989 { 185, 10 }, |
| 10990 { 188, 0 }, |
| 10991 { 174, 0 }, |
| 10992 { 174, 3 }, |
| 10993 { 189, 0 }, |
| 10994 { 189, 2 }, |
| 10995 { 190, 1 }, |
| 10996 { 190, 1 }, |
| 10997 { 149, 4 }, |
| 10998 { 192, 2 }, |
| 10999 { 192, 0 }, |
| 11000 { 149, 9 }, |
| 11001 { 149, 4 }, |
| 11002 { 149, 1 }, |
| 11003 { 163, 2 }, |
| 11004 { 194, 3 }, |
| 11005 { 197, 1 }, |
| 11006 { 197, 2 }, |
| 11007 { 197, 1 }, |
| 11008 { 195, 9 }, |
| 11009 { 206, 4 }, |
| 11010 { 206, 5 }, |
| 11011 { 198, 1 }, |
| 11012 { 198, 1 }, |
| 11013 { 198, 0 }, |
| 11014 { 209, 0 }, |
| 11015 { 199, 3 }, |
| 11016 { 199, 2 }, |
| 11017 { 199, 4 }, |
| 11018 { 210, 2 }, |
| 11019 { 210, 0 }, |
| 11020 { 200, 0 }, |
| 11021 { 200, 2 }, |
| 11022 { 212, 2 }, |
| 11023 { 212, 0 }, |
| 11024 { 211, 7 }, |
| 11025 { 211, 9 }, |
| 11026 { 211, 7 }, |
| 11027 { 211, 7 }, |
| 11028 { 159, 0 }, |
| 11029 { 159, 2 }, |
| 11030 { 193, 2 }, |
| 11031 { 213, 1 }, |
| 11032 { 213, 2 }, |
| 11033 { 213, 3 }, |
| 11034 { 213, 4 }, |
| 11035 { 215, 2 }, |
| 11036 { 215, 0 }, |
| 11037 { 214, 0 }, |
| 11038 { 214, 3 }, |
| 11039 { 214, 2 }, |
| 11040 { 216, 4 }, |
| 11041 { 216, 0 }, |
| 11042 { 204, 0 }, |
| 11043 { 204, 3 }, |
| 11044 { 186, 4 }, |
| 11045 { 186, 2 }, |
| 11046 { 175, 1 }, |
| 11047 { 175, 1 }, |
| 11048 { 175, 0 }, |
| 11049 { 202, 0 }, |
| 11050 { 202, 3 }, |
| 11051 { 203, 0 }, |
| 11052 { 203, 2 }, |
| 11053 { 205, 0 }, |
| 11054 { 205, 2 }, |
| 11055 { 205, 4 }, |
| 11056 { 205, 4 }, |
| 11057 { 149, 6 }, |
| 11058 { 201, 0 }, |
| 11059 { 201, 2 }, |
| 11060 { 149, 8 }, |
| 11061 { 218, 5 }, |
| 11062 { 218, 7 }, |
| 11063 { 218, 3 }, |
| 11064 { 218, 5 }, |
| 11065 { 149, 6 }, |
| 11066 { 149, 7 }, |
| 11067 { 219, 2 }, |
| 11068 { 219, 1 }, |
| 11069 { 220, 0 }, |
| 11070 { 220, 3 }, |
| 11071 { 217, 3 }, |
| 11072 { 217, 1 }, |
| 11073 { 173, 3 }, |
| 11074 { 172, 1 }, |
| 11075 { 173, 1 }, |
| 11076 { 173, 1 }, |
| 11077 { 173, 3 }, |
| 11078 { 173, 5 }, |
| 11079 { 172, 1 }, |
| 11080 { 172, 1 }, |
| 11081 { 172, 1 }, |
| 11082 { 173, 1 }, |
| 11083 { 173, 3 }, |
| 11084 { 173, 6 }, |
| 11085 { 173, 5 }, |
| 11086 { 173, 4 }, |
| 11087 { 172, 1 }, |
| 11088 { 173, 5 }, |
| 11089 { 173, 3 }, |
| 11090 { 173, 3 }, |
| 11091 { 173, 3 }, |
| 11092 { 173, 3 }, |
| 11093 { 173, 3 }, |
| 11094 { 173, 3 }, |
| 11095 { 173, 3 }, |
| 11096 { 173, 3 }, |
| 11097 { 221, 1 }, |
| 11098 { 221, 2 }, |
| 11099 { 173, 3 }, |
| 11100 { 173, 5 }, |
| 11101 { 173, 2 }, |
| 11102 { 173, 3 }, |
| 11103 { 173, 3 }, |
| 11104 { 173, 4 }, |
| 11105 { 173, 2 }, |
| 11106 { 173, 2 }, |
| 11107 { 173, 2 }, |
| 11108 { 173, 2 }, |
| 11109 { 222, 1 }, |
| 11110 { 222, 2 }, |
| 11111 { 173, 5 }, |
| 11112 { 223, 1 }, |
| 11113 { 223, 2 }, |
| 11114 { 173, 5 }, |
| 11115 { 173, 3 }, |
| 11116 { 173, 5 }, |
| 11117 { 173, 5 }, |
| 11118 { 173, 4 }, |
| 11119 { 173, 5 }, |
| 11120 { 226, 5 }, |
| 11121 { 226, 4 }, |
| 11122 { 227, 2 }, |
| 11123 { 227, 0 }, |
| 11124 { 225, 1 }, |
| 11125 { 225, 0 }, |
| 11126 { 208, 0 }, |
| 11127 { 207, 3 }, |
| 11128 { 207, 1 }, |
| 11129 { 224, 0 }, |
| 11130 { 224, 3 }, |
| 11131 { 149, 12 }, |
| 11132 { 228, 1 }, |
| 11133 { 228, 0 }, |
| 11134 { 177, 0 }, |
| 11135 { 177, 3 }, |
| 11136 { 187, 5 }, |
| 11137 { 187, 3 }, |
| 11138 { 229, 0 }, |
| 11139 { 229, 2 }, |
| 11140 { 149, 4 }, |
| 11141 { 149, 1 }, |
| 11142 { 149, 2 }, |
| 11143 { 149, 3 }, |
| 11144 { 149, 5 }, |
| 11145 { 149, 6 }, |
| 11146 { 149, 5 }, |
| 11147 { 149, 6 }, |
| 11148 { 169, 2 }, |
| 11149 { 170, 2 }, |
| 11150 { 149, 5 }, |
| 11151 { 231, 11 }, |
| 11152 { 233, 1 }, |
| 11153 { 233, 1 }, |
| 11154 { 233, 2 }, |
| 11155 { 233, 0 }, |
| 11156 { 234, 1 }, |
| 11157 { 234, 1 }, |
| 11158 { 234, 3 }, |
| 11159 { 236, 0 }, |
| 11160 { 236, 2 }, |
| 11161 { 232, 3 }, |
| 11162 { 232, 2 }, |
| 11163 { 238, 3 }, |
| 11164 { 239, 3 }, |
| 11165 { 239, 2 }, |
| 11166 { 237, 7 }, |
| 11167 { 237, 5 }, |
| 11168 { 237, 5 }, |
| 11169 { 237, 1 }, |
| 11170 { 173, 4 }, |
| 11171 { 173, 6 }, |
| 11172 { 191, 1 }, |
| 11173 { 191, 1 }, |
| 11174 { 191, 1 }, |
| 11175 { 149, 4 }, |
| 11176 { 149, 6 }, |
| 11177 { 149, 3 }, |
| 11178 { 241, 0 }, |
| 11179 { 241, 2 }, |
| 11180 { 149, 1 }, |
| 11181 { 149, 3 }, |
| 11182 { 149, 1 }, |
| 11183 { 149, 3 }, |
| 11184 { 149, 6 }, |
| 11185 { 149, 7 }, |
| 11186 { 242, 1 }, |
| 11187 { 149, 1 }, |
| 11188 { 149, 4 }, |
| 11189 { 244, 8 }, |
| 11190 { 246, 0 }, |
| 11191 { 247, 1 }, |
| 11192 { 247, 3 }, |
| 11193 { 248, 1 }, |
| 11194 { 196, 0 }, |
| 11195 { 196, 2 }, |
| 11196 { 196, 3 }, |
| 11197 { 250, 6 }, |
| 11198 { 250, 8 }, |
| 11199 { 144, 1 }, |
| 11200 { 145, 2 }, |
| 11201 { 145, 1 }, |
| 11202 { 146, 1 }, |
| 11203 { 146, 3 }, |
| 11204 { 147, 0 }, |
| 11205 { 151, 0 }, |
| 11206 { 151, 1 }, |
| 11207 { 151, 2 }, |
| 11208 { 153, 1 }, |
| 11209 { 153, 0 }, |
| 11210 { 149, 2 }, |
| 11211 { 160, 4 }, |
| 11212 { 160, 2 }, |
| 11213 { 152, 1 }, |
| 11214 { 152, 1 }, |
| 11215 { 152, 1 }, |
| 11216 { 166, 1 }, |
| 11217 { 167, 1 }, |
| 11218 { 168, 1 }, |
| 11219 { 168, 1 }, |
| 11220 { 165, 2 }, |
| 11221 { 165, 0 }, |
| 11222 { 171, 2 }, |
| 11223 { 161, 2 }, |
| 11224 { 183, 3 }, |
| 11225 { 183, 1 }, |
| 11226 { 184, 0 }, |
| 11227 { 188, 1 }, |
| 11228 { 190, 1 }, |
| 11229 { 194, 1 }, |
| 11230 { 195, 1 }, |
| 11231 { 209, 2 }, |
| 11232 { 210, 1 }, |
| 11233 { 173, 1 }, |
| 11234 { 208, 1 }, |
| 11235 { 230, 1 }, |
| 11236 { 230, 1 }, |
| 11237 { 230, 1 }, |
| 11238 { 230, 1 }, |
| 11239 { 230, 1 }, |
| 11240 { 169, 1 }, |
| 11241 { 235, 0 }, |
| 11242 { 235, 3 }, |
| 11243 { 238, 1 }, |
| 11244 { 239, 0 }, |
| 11245 { 240, 1 }, |
| 11246 { 240, 0 }, |
| 11247 { 243, 0 }, |
| 11248 { 243, 1 }, |
| 11249 { 245, 1 }, |
| 11250 { 245, 3 }, |
| 11251 { 246, 2 }, |
| 11252 { 249, 0 }, |
| 11253 { 249, 4 }, |
| 11254 { 249, 2 }, |
| 11255 }; |
| 11256 |
| 11257 static void yy_accept(yyParser*); /* Forward Declaration */ |
| 11258 |
| 11259 /* |
| 11260 ** Perform a reduce action and the shift that must immediately |
| 11261 ** follow the reduce. |
| 11262 */ |
| 11263 static void yy_reduce( |
| 11264 yyParser *yypParser, /* The parser */ |
| 11265 unsigned int yyruleno /* Number of the rule by which to reduce */ |
| 11266 ){ |
| 11267 int yygoto; /* The next state */ |
| 11268 int yyact; /* The next action */ |
| 11269 yyStackEntry *yymsp; /* The top of the parser's stack */ |
| 11270 int yysize; /* Amount to pop the stack */ |
| 11271 sqlite3ParserARG_FETCH; |
| 11272 yymsp = yypParser->yytos; |
| 11273 #ifndef NDEBUG |
| 11274 if( yyTraceFILE && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ |
| 11275 yysize = yyRuleInfo[yyruleno].nrhs; |
| 11276 fprintf(yyTraceFILE, "%sReduce [%s], go to state %d.\n", yyTracePrompt, |
| 11277 yyRuleName[yyruleno], yymsp[-yysize].stateno); |
| 11278 } |
| 11279 #endif /* NDEBUG */ |
| 11280 |
| 11281 /* Check that the stack is large enough to grow by a single entry |
| 11282 ** if the RHS of the rule is empty. This ensures that there is room |
| 11283 ** enough on the stack to push the LHS value */ |
| 11284 if( yyRuleInfo[yyruleno].nrhs==0 ){ |
| 11285 #ifdef YYTRACKMAXSTACKDEPTH |
| 11286 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){ |
| 11287 yypParser->yyhwm++; |
| 11288 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack)); |
| 11289 } |
| 11290 #endif |
| 11291 #if YYSTACKDEPTH>0 |
| 11292 if( yypParser->yytos>=&yypParser->yystack[YYSTACKDEPTH-1] ){ |
| 11293 yyStackOverflow(yypParser); |
| 11294 return; |
| 11295 } |
| 11296 #else |
| 11297 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){ |
| 11298 if( yyGrowStack(yypParser) ){ |
| 11299 yyStackOverflow(yypParser); |
| 11300 return; |
| 11301 } |
| 11302 yymsp = yypParser->yytos; |
| 11303 } |
| 11304 #endif |
| 11305 } |
| 11306 |
| 11307 switch( yyruleno ){ |
| 11308 /* Beginning here are the reduction cases. A typical example |
| 11309 ** follows: |
| 11310 ** case 0: |
| 11311 ** #line <lineno> <grammarfile> |
| 11312 ** { ... } // User supplied code |
| 11313 ** #line <lineno> <thisfile> |
| 11314 ** break; |
| 11315 */ |
| 11316 /********** Begin reduce actions **********************************************/ |
| 11317 YYMINORTYPE yylhsminor; |
| 11318 case 0: /* explain ::= EXPLAIN */ |
| 11319 { pParse->explain = 1; } |
| 11320 break; |
| 11321 case 1: /* explain ::= EXPLAIN QUERY PLAN */ |
| 11322 { pParse->explain = 2; } |
| 11323 break; |
| 11324 case 2: /* cmdx ::= cmd */ |
| 11325 { sqlite3FinishCoding(pParse); } |
| 11326 break; |
| 11327 case 3: /* cmd ::= BEGIN transtype trans_opt */ |
| 11328 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy194);} |
| 11329 break; |
| 11330 case 4: /* transtype ::= */ |
| 11331 {yymsp[1].minor.yy194 = TK_DEFERRED;} |
| 11332 break; |
| 11333 case 5: /* transtype ::= DEFERRED */ |
| 11334 case 6: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==6); |
| 11335 case 7: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==7); |
| 11336 {yymsp[0].minor.yy194 = yymsp[0].major; /*A-overwrites-X*/} |
| 11337 break; |
| 11338 case 8: /* cmd ::= COMMIT trans_opt */ |
| 11339 case 9: /* cmd ::= END trans_opt */ yytestcase(yyruleno==9); |
| 11340 {sqlite3CommitTransaction(pParse);} |
| 11341 break; |
| 11342 case 10: /* cmd ::= ROLLBACK trans_opt */ |
| 11343 {sqlite3RollbackTransaction(pParse);} |
| 11344 break; |
| 11345 case 11: /* cmd ::= SAVEPOINT nm */ |
| 11346 { |
| 11347 sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0); |
| 11348 } |
| 11349 break; |
| 11350 case 12: /* cmd ::= RELEASE savepoint_opt nm */ |
| 11351 { |
| 11352 sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0); |
| 11353 } |
| 11354 break; |
| 11355 case 13: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */ |
| 11356 { |
| 11357 sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0); |
| 11358 } |
| 11359 break; |
| 11360 case 14: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */ |
| 11361 { |
| 11362 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].m
inor.yy194,0,0,yymsp[-2].minor.yy194); |
| 11363 } |
| 11364 break; |
| 11365 case 15: /* createkw ::= CREATE */ |
| 11366 {disableLookaside(pParse);} |
| 11367 break; |
| 11368 case 16: /* ifnotexists ::= */ |
| 11369 case 19: /* temp ::= */ yytestcase(yyruleno==19); |
| 11370 case 22: /* table_options ::= */ yytestcase(yyruleno==22); |
| 11371 case 42: /* autoinc ::= */ yytestcase(yyruleno==42); |
| 11372 case 57: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==57); |
| 11373 case 67: /* defer_subclause_opt ::= */ yytestcase(yyruleno==67); |
| 11374 case 76: /* ifexists ::= */ yytestcase(yyruleno==76); |
| 11375 case 90: /* distinct ::= */ yytestcase(yyruleno==90); |
| 11376 case 215: /* collate ::= */ yytestcase(yyruleno==215); |
| 11377 {yymsp[1].minor.yy194 = 0;} |
| 11378 break; |
| 11379 case 17: /* ifnotexists ::= IF NOT EXISTS */ |
| 11380 {yymsp[-2].minor.yy194 = 1;} |
| 11381 break; |
| 11382 case 18: /* temp ::= TEMP */ |
| 11383 case 43: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==43); |
| 11384 {yymsp[0].minor.yy194 = 1;} |
| 11385 break; |
| 11386 case 20: /* create_table_args ::= LP columnlist conslist_opt RP table_opti
ons */ |
| 11387 { |
| 11388 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].mino
r.yy194,0); |
| 11389 } |
| 11390 break; |
| 11391 case 21: /* create_table_args ::= AS select */ |
| 11392 { |
| 11393 sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy243); |
| 11394 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy243); |
| 11395 } |
| 11396 break; |
| 11397 case 23: /* table_options ::= WITHOUT nm */ |
| 11398 { |
| 11399 if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5
)==0 ){ |
| 11400 yymsp[-1].minor.yy194 = TF_WithoutRowid | TF_NoVisibleRowid; |
| 11401 }else{ |
| 11402 yymsp[-1].minor.yy194 = 0; |
| 11403 sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n,
yymsp[0].minor.yy0.z); |
| 11404 } |
| 11405 } |
| 11406 break; |
| 11407 case 24: /* columnname ::= nm typetoken */ |
| 11408 {sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);} |
| 11409 break; |
| 11410 case 25: /* typetoken ::= */ |
| 11411 case 60: /* conslist_opt ::= */ yytestcase(yyruleno==60); |
| 11412 case 96: /* as ::= */ yytestcase(yyruleno==96); |
| 11413 {yymsp[1].minor.yy0.n = 0; yymsp[1].minor.yy0.z = 0;} |
| 11414 break; |
| 11415 case 26: /* typetoken ::= typename LP signed RP */ |
| 11416 { |
| 11417 yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yy
msp[-3].minor.yy0.z); |
| 11418 } |
| 11419 break; |
| 11420 case 27: /* typetoken ::= typename LP signed COMMA signed RP */ |
| 11421 { |
| 11422 yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yy
msp[-5].minor.yy0.z); |
| 11423 } |
| 11424 break; |
| 11425 case 28: /* typename ::= typename ID|STRING */ |
| 11426 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1]
.minor.yy0.z);} |
| 11427 break; |
| 11428 case 29: /* ccons ::= CONSTRAINT nm */ |
| 11429 case 62: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==62); |
| 11430 {pParse->constraintName = yymsp[0].minor.yy0;} |
| 11431 break; |
| 11432 case 30: /* ccons ::= DEFAULT term */ |
| 11433 case 32: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==32); |
| 11434 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy190);} |
| 11435 break; |
| 11436 case 31: /* ccons ::= DEFAULT LP expr RP */ |
| 11437 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy190);} |
| 11438 break; |
| 11439 case 33: /* ccons ::= DEFAULT MINUS term */ |
| 11440 { |
| 11441 ExprSpan v; |
| 11442 v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy190.pExpr, 0); |
| 11443 v.zStart = yymsp[-1].minor.yy0.z; |
| 11444 v.zEnd = yymsp[0].minor.yy190.zEnd; |
| 11445 sqlite3AddDefaultValue(pParse,&v); |
| 11446 } |
| 11447 break; |
| 11448 case 34: /* ccons ::= DEFAULT ID|INDEXED */ |
| 11449 { |
| 11450 ExprSpan v; |
| 11451 spanExpr(&v, pParse, TK_STRING, yymsp[0].minor.yy0); |
| 11452 sqlite3AddDefaultValue(pParse,&v); |
| 11453 } |
| 11454 break; |
| 11455 case 35: /* ccons ::= NOT NULL onconf */ |
| 11456 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy194);} |
| 11457 break; |
| 11458 case 36: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */ |
| 11459 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy194,yymsp[0].minor.yy194,yymsp[
-2].minor.yy194);} |
| 11460 break; |
| 11461 case 37: /* ccons ::= UNIQUE onconf */ |
| 11462 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy194,0,0,0,0, |
| 11463 SQLITE_IDXTYPE_UNIQUE);} |
| 11464 break; |
| 11465 case 38: /* ccons ::= CHECK LP expr RP */ |
| 11466 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy190.pExpr);} |
| 11467 break; |
| 11468 case 39: /* ccons ::= REFERENCES nm eidlist_opt refargs */ |
| 11469 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy148,yym
sp[0].minor.yy194);} |
| 11470 break; |
| 11471 case 40: /* ccons ::= defer_subclause */ |
| 11472 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy194);} |
| 11473 break; |
| 11474 case 41: /* ccons ::= COLLATE ID|STRING */ |
| 11475 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);} |
| 11476 break; |
| 11477 case 44: /* refargs ::= */ |
| 11478 { yymsp[1].minor.yy194 = OE_None*0x0101; /* EV: R-19803-45884 */} |
| 11479 break; |
| 11480 case 45: /* refargs ::= refargs refarg */ |
| 11481 { yymsp[-1].minor.yy194 = (yymsp[-1].minor.yy194 & ~yymsp[0].minor.yy497.mask) |
yymsp[0].minor.yy497.value; } |
| 11482 break; |
| 11483 case 46: /* refarg ::= MATCH nm */ |
| 11484 { yymsp[-1].minor.yy497.value = 0; yymsp[-1].minor.yy497.mask = 0x000000; } |
| 11485 break; |
| 11486 case 47: /* refarg ::= ON INSERT refact */ |
| 11487 { yymsp[-2].minor.yy497.value = 0; yymsp[-2].minor.yy497.mask = 0x000000; } |
| 11488 break; |
| 11489 case 48: /* refarg ::= ON DELETE refact */ |
| 11490 { yymsp[-2].minor.yy497.value = yymsp[0].minor.yy194; yymsp[-2].minor.yy497.
mask = 0x0000ff; } |
| 11491 break; |
| 11492 case 49: /* refarg ::= ON UPDATE refact */ |
| 11493 { yymsp[-2].minor.yy497.value = yymsp[0].minor.yy194<<8; yymsp[-2].minor.yy497.
mask = 0x00ff00; } |
| 11494 break; |
| 11495 case 50: /* refact ::= SET NULL */ |
| 11496 { yymsp[-1].minor.yy194 = OE_SetNull; /* EV: R-33326-45252 */} |
| 11497 break; |
| 11498 case 51: /* refact ::= SET DEFAULT */ |
| 11499 { yymsp[-1].minor.yy194 = OE_SetDflt; /* EV: R-33326-45252 */} |
| 11500 break; |
| 11501 case 52: /* refact ::= CASCADE */ |
| 11502 { yymsp[0].minor.yy194 = OE_Cascade; /* EV: R-33326-45252 */} |
| 11503 break; |
| 11504 case 53: /* refact ::= RESTRICT */ |
| 11505 { yymsp[0].minor.yy194 = OE_Restrict; /* EV: R-33326-45252 */} |
| 11506 break; |
| 11507 case 54: /* refact ::= NO ACTION */ |
| 11508 { yymsp[-1].minor.yy194 = OE_None; /* EV: R-33326-45252 */} |
| 11509 break; |
| 11510 case 55: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ |
| 11511 {yymsp[-2].minor.yy194 = 0;} |
| 11512 break; |
| 11513 case 56: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ |
| 11514 case 71: /* orconf ::= OR resolvetype */ yytestcase(yyruleno==71); |
| 11515 case 144: /* insert_cmd ::= INSERT orconf */ yytestcase(yyruleno==144); |
| 11516 {yymsp[-1].minor.yy194 = yymsp[0].minor.yy194;} |
| 11517 break; |
| 11518 case 58: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ |
| 11519 case 75: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==75); |
| 11520 case 187: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==187); |
| 11521 case 190: /* in_op ::= NOT IN */ yytestcase(yyruleno==190); |
| 11522 case 216: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==216); |
| 11523 {yymsp[-1].minor.yy194 = 1;} |
| 11524 break; |
| 11525 case 59: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ |
| 11526 {yymsp[-1].minor.yy194 = 0;} |
| 11527 break; |
| 11528 case 61: /* tconscomma ::= COMMA */ |
| 11529 {pParse->constraintName.n = 0;} |
| 11530 break; |
| 11531 case 63: /* tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */ |
| 11532 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy148,yymsp[0].minor.yy194,yymsp[-2
].minor.yy194,0);} |
| 11533 break; |
| 11534 case 64: /* tcons ::= UNIQUE LP sortlist RP onconf */ |
| 11535 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy148,yymsp[0].minor.yy194,0,0,
0,0, |
| 11536 SQLITE_IDXTYPE_UNIQUE);} |
| 11537 break; |
| 11538 case 65: /* tcons ::= CHECK LP expr RP onconf */ |
| 11539 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy190.pExpr);} |
| 11540 break; |
| 11541 case 66: /* tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt
refargs defer_subclause_opt */ |
| 11542 { |
| 11543 sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy148, &yymsp[-3].minor.yy0,
yymsp[-2].minor.yy148, yymsp[-1].minor.yy194); |
| 11544 sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy194); |
| 11545 } |
| 11546 break; |
| 11547 case 68: /* onconf ::= */ |
| 11548 case 70: /* orconf ::= */ yytestcase(yyruleno==70); |
| 11549 {yymsp[1].minor.yy194 = OE_Default;} |
| 11550 break; |
| 11551 case 69: /* onconf ::= ON CONFLICT resolvetype */ |
| 11552 {yymsp[-2].minor.yy194 = yymsp[0].minor.yy194;} |
| 11553 break; |
| 11554 case 72: /* resolvetype ::= IGNORE */ |
| 11555 {yymsp[0].minor.yy194 = OE_Ignore;} |
| 11556 break; |
| 11557 case 73: /* resolvetype ::= REPLACE */ |
| 11558 case 145: /* insert_cmd ::= REPLACE */ yytestcase(yyruleno==145); |
| 11559 {yymsp[0].minor.yy194 = OE_Replace;} |
| 11560 break; |
| 11561 case 74: /* cmd ::= DROP TABLE ifexists fullname */ |
| 11562 { |
| 11563 sqlite3DropTable(pParse, yymsp[0].minor.yy185, 0, yymsp[-1].minor.yy194); |
| 11564 } |
| 11565 break; |
| 11566 case 77: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS
select */ |
| 11567 { |
| 11568 sqlite3CreateView(pParse, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-
3].minor.yy0, yymsp[-2].minor.yy148, yymsp[0].minor.yy243, yymsp[-7].minor.yy194
, yymsp[-5].minor.yy194); |
| 11569 } |
| 11570 break; |
| 11571 case 78: /* cmd ::= DROP VIEW ifexists fullname */ |
| 11572 { |
| 11573 sqlite3DropTable(pParse, yymsp[0].minor.yy185, 1, yymsp[-1].minor.yy194); |
| 11574 } |
| 11575 break; |
| 11576 case 79: /* cmd ::= select */ |
| 11577 { |
| 11578 SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0}; |
| 11579 sqlite3Select(pParse, yymsp[0].minor.yy243, &dest); |
| 11580 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy243); |
| 11581 } |
| 11582 break; |
| 11583 case 80: /* select ::= with selectnowith */ |
| 11584 { |
| 11585 Select *p = yymsp[0].minor.yy243; |
| 11586 if( p ){ |
| 11587 p->pWith = yymsp[-1].minor.yy285; |
| 11588 parserDoubleLinkSelect(pParse, p); |
| 11589 }else{ |
| 11590 sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy285); |
| 11591 } |
| 11592 yymsp[-1].minor.yy243 = p; /*A-overwrites-W*/ |
| 11593 } |
| 11594 break; |
| 11595 case 81: /* selectnowith ::= selectnowith multiselect_op oneselect */ |
| 11596 { |
| 11597 Select *pRhs = yymsp[0].minor.yy243; |
| 11598 Select *pLhs = yymsp[-2].minor.yy243; |
| 11599 if( pRhs && pRhs->pPrior ){ |
| 11600 SrcList *pFrom; |
| 11601 Token x; |
| 11602 x.n = 0; |
| 11603 parserDoubleLinkSelect(pParse, pRhs); |
| 11604 pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); |
| 11605 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0); |
| 11606 } |
| 11607 if( pRhs ){ |
| 11608 pRhs->op = (u8)yymsp[-1].minor.yy194; |
| 11609 pRhs->pPrior = pLhs; |
| 11610 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue; |
| 11611 pRhs->selFlags &= ~SF_MultiValue; |
| 11612 if( yymsp[-1].minor.yy194!=TK_ALL ) pParse->hasCompound = 1; |
| 11613 }else{ |
| 11614 sqlite3SelectDelete(pParse->db, pLhs); |
| 11615 } |
| 11616 yymsp[-2].minor.yy243 = pRhs; |
| 11617 } |
| 11618 break; |
| 11619 case 82: /* multiselect_op ::= UNION */ |
| 11620 case 84: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==84
); |
| 11621 {yymsp[0].minor.yy194 = yymsp[0].major; /*A-overwrites-OP*/} |
| 11622 break; |
| 11623 case 83: /* multiselect_op ::= UNION ALL */ |
| 11624 {yymsp[-1].minor.yy194 = TK_ALL;} |
| 11625 break; |
| 11626 case 85: /* oneselect ::= SELECT distinct selcollist from where_opt groupb
y_opt having_opt orderby_opt limit_opt */ |
| 11627 { |
| 11628 #if SELECTTRACE_ENABLED |
| 11629 Token s = yymsp[-8].minor.yy0; /*A-overwrites-S*/ |
| 11630 #endif |
| 11631 yymsp[-8].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5
].minor.yy185,yymsp[-4].minor.yy72,yymsp[-3].minor.yy148,yymsp[-2].minor.yy72,yy
msp[-1].minor.yy148,yymsp[-7].minor.yy194,yymsp[0].minor.yy354.pLimit,yymsp[0].m
inor.yy354.pOffset); |
| 11632 #if SELECTTRACE_ENABLED |
| 11633 /* Populate the Select.zSelName[] string that is used to help with |
| 11634 ** query planner debugging, to differentiate between multiple Select |
| 11635 ** objects in a complex query. |
| 11636 ** |
| 11637 ** If the SELECT keyword is immediately followed by a C-style comment |
| 11638 ** then extract the first few alphanumeric characters from within that |
| 11639 ** comment to be the zSelName value. Otherwise, the label is #N where |
| 11640 ** is an integer that is incremented with each SELECT statement seen. |
| 11641 */ |
| 11642 if( yymsp[-8].minor.yy243!=0 ){ |
| 11643 const char *z = s.z+6; |
| 11644 int i; |
| 11645 sqlite3_snprintf(sizeof(yymsp[-8].minor.yy243->zSelName), yymsp[-8].minor.yy
243->zSelName, "#%d", |
| 11646 ++pParse->nSelect); |
| 11647 while( z[0]==' ' ) z++; |
| 11648 if( z[0]=='/' && z[1]=='*' ){ |
| 11649 z += 2; |
| 11650 while( z[0]==' ' ) z++; |
| 11651 for(i=0; sqlite3Isalnum(z[i]); i++){} |
| 11652 sqlite3_snprintf(sizeof(yymsp[-8].minor.yy243->zSelName), yymsp[-8].minor.
yy243->zSelName, "%.*s", i, z); |
| 11653 } |
| 11654 } |
| 11655 #endif /* SELECTRACE_ENABLED */ |
| 11656 } |
| 11657 break; |
| 11658 case 86: /* values ::= VALUES LP nexprlist RP */ |
| 11659 { |
| 11660 yymsp[-3].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,
0,SF_Values,0,0); |
| 11661 } |
| 11662 break; |
| 11663 case 87: /* values ::= values COMMA LP exprlist RP */ |
| 11664 { |
| 11665 Select *pRight, *pLeft = yymsp[-4].minor.yy243; |
| 11666 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values|SF_
MultiValue,0,0); |
| 11667 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; |
| 11668 if( pRight ){ |
| 11669 pRight->op = TK_ALL; |
| 11670 pRight->pPrior = pLeft; |
| 11671 yymsp[-4].minor.yy243 = pRight; |
| 11672 }else{ |
| 11673 yymsp[-4].minor.yy243 = pLeft; |
| 11674 } |
| 11675 } |
| 11676 break; |
| 11677 case 88: /* distinct ::= DISTINCT */ |
| 11678 {yymsp[0].minor.yy194 = SF_Distinct;} |
| 11679 break; |
| 11680 case 89: /* distinct ::= ALL */ |
| 11681 {yymsp[0].minor.yy194 = SF_All;} |
| 11682 break; |
| 11683 case 91: /* sclp ::= */ |
| 11684 case 119: /* orderby_opt ::= */ yytestcase(yyruleno==119); |
| 11685 case 126: /* groupby_opt ::= */ yytestcase(yyruleno==126); |
| 11686 case 203: /* exprlist ::= */ yytestcase(yyruleno==203); |
| 11687 case 206: /* paren_exprlist ::= */ yytestcase(yyruleno==206); |
| 11688 case 211: /* eidlist_opt ::= */ yytestcase(yyruleno==211); |
| 11689 {yymsp[1].minor.yy148 = 0;} |
| 11690 break; |
| 11691 case 92: /* selcollist ::= sclp expr as */ |
| 11692 { |
| 11693 yymsp[-2].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy148,
yymsp[-1].minor.yy190.pExpr); |
| 11694 if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-2].minor.y
y148, &yymsp[0].minor.yy0, 1); |
| 11695 sqlite3ExprListSetSpan(pParse,yymsp[-2].minor.yy148,&yymsp[-1].minor.yy190); |
| 11696 } |
| 11697 break; |
| 11698 case 93: /* selcollist ::= sclp STAR */ |
| 11699 { |
| 11700 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0); |
| 11701 yymsp[-1].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy148, p
); |
| 11702 } |
| 11703 break; |
| 11704 case 94: /* selcollist ::= sclp nm DOT STAR */ |
| 11705 { |
| 11706 Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0); |
| 11707 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1); |
| 11708 Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); |
| 11709 yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148, pD
ot); |
| 11710 } |
| 11711 break; |
| 11712 case 95: /* as ::= AS nm */ |
| 11713 case 106: /* dbnm ::= DOT nm */ yytestcase(yyruleno==106); |
| 11714 case 225: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==225); |
| 11715 case 226: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==226
); |
| 11716 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;} |
| 11717 break; |
| 11718 case 97: /* from ::= */ |
| 11719 {yymsp[1].minor.yy185 = sqlite3DbMallocZero(pParse->db, sizeof(*yymsp[1].minor.y
y185));} |
| 11720 break; |
| 11721 case 98: /* from ::= FROM seltablist */ |
| 11722 { |
| 11723 yymsp[-1].minor.yy185 = yymsp[0].minor.yy185; |
| 11724 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy185); |
| 11725 } |
| 11726 break; |
| 11727 case 99: /* stl_prefix ::= seltablist joinop */ |
| 11728 { |
| 11729 if( ALWAYS(yymsp[-1].minor.yy185 && yymsp[-1].minor.yy185->nSrc>0) ) yymsp[-1
].minor.yy185->a[yymsp[-1].minor.yy185->nSrc-1].fg.jointype = (u8)yymsp[0].minor
.yy194; |
| 11730 } |
| 11731 break; |
| 11732 case 100: /* stl_prefix ::= */ |
| 11733 {yymsp[1].minor.yy185 = 0;} |
| 11734 break; |
| 11735 case 101: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using
_opt */ |
| 11736 { |
| 11737 yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy
185,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].m
inor.yy72,yymsp[0].minor.yy254); |
| 11738 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy185, &yymsp[-2].minor.yy0); |
| 11739 } |
| 11740 break; |
| 11741 case 102: /* seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt us
ing_opt */ |
| 11742 { |
| 11743 yymsp[-8].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy
185,&yymsp[-7].minor.yy0,&yymsp[-6].minor.yy0,&yymsp[-2].minor.yy0,0,yymsp[-1].m
inor.yy72,yymsp[0].minor.yy254); |
| 11744 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy185, yymsp[-4].minor.yy148); |
| 11745 } |
| 11746 break; |
| 11747 case 103: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */ |
| 11748 { |
| 11749 yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.
yy185,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy243,yymsp[-1].minor.yy72,yymsp[
0].minor.yy254); |
| 11750 } |
| 11751 break; |
| 11752 case 104: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_op
t */ |
| 11753 { |
| 11754 if( yymsp[-6].minor.yy185==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.
yy72==0 && yymsp[0].minor.yy254==0 ){ |
| 11755 yymsp[-6].minor.yy185 = yymsp[-4].minor.yy185; |
| 11756 }else if( yymsp[-4].minor.yy185->nSrc==1 ){ |
| 11757 yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].mino
r.yy185,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy72,yymsp[0].minor.yy254); |
| 11758 if( yymsp[-6].minor.yy185 ){ |
| 11759 struct SrcList_item *pNew = &yymsp[-6].minor.yy185->a[yymsp[-6].minor.yy
185->nSrc-1]; |
| 11760 struct SrcList_item *pOld = yymsp[-4].minor.yy185->a; |
| 11761 pNew->zName = pOld->zName; |
| 11762 pNew->zDatabase = pOld->zDatabase; |
| 11763 pNew->pSelect = pOld->pSelect; |
| 11764 pOld->zName = pOld->zDatabase = 0; |
| 11765 pOld->pSelect = 0; |
| 11766 } |
| 11767 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy185); |
| 11768 }else{ |
| 11769 Select *pSubquery; |
| 11770 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy185); |
| 11771 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,SF_Nes
tedFrom,0,0); |
| 11772 yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].mino
r.yy185,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy72,yymsp[0].minor.y
y254); |
| 11773 } |
| 11774 } |
| 11775 break; |
| 11776 case 105: /* dbnm ::= */ |
| 11777 case 114: /* indexed_opt ::= */ yytestcase(yyruleno==114); |
| 11778 {yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;} |
| 11779 break; |
| 11780 case 107: /* fullname ::= nm dbnm */ |
| 11781 {yymsp[-1].minor.yy185 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,
&yymsp[0].minor.yy0); /*A-overwrites-X*/} |
| 11782 break; |
| 11783 case 108: /* joinop ::= COMMA|JOIN */ |
| 11784 { yymsp[0].minor.yy194 = JT_INNER; } |
| 11785 break; |
| 11786 case 109: /* joinop ::= JOIN_KW JOIN */ |
| 11787 {yymsp[-1].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-
overwrites-A*/} |
| 11788 break; |
| 11789 case 110: /* joinop ::= JOIN_KW nm JOIN */ |
| 11790 {yymsp[-2].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].
minor.yy0,0); /*X-overwrites-A*/} |
| 11791 break; |
| 11792 case 111: /* joinop ::= JOIN_KW nm nm JOIN */ |
| 11793 {yymsp[-3].minor.yy194 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].
minor.yy0,&yymsp[-1].minor.yy0);/*X-overwrites-A*/} |
| 11794 break; |
| 11795 case 112: /* on_opt ::= ON expr */ |
| 11796 case 129: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==129); |
| 11797 case 136: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==136); |
| 11798 case 199: /* case_else ::= ELSE expr */ yytestcase(yyruleno==199); |
| 11799 {yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr;} |
| 11800 break; |
| 11801 case 113: /* on_opt ::= */ |
| 11802 case 128: /* having_opt ::= */ yytestcase(yyruleno==128); |
| 11803 case 135: /* where_opt ::= */ yytestcase(yyruleno==135); |
| 11804 case 200: /* case_else ::= */ yytestcase(yyruleno==200); |
| 11805 case 202: /* case_operand ::= */ yytestcase(yyruleno==202); |
| 11806 {yymsp[1].minor.yy72 = 0;} |
| 11807 break; |
| 11808 case 115: /* indexed_opt ::= INDEXED BY nm */ |
| 11809 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;} |
| 11810 break; |
| 11811 case 116: /* indexed_opt ::= NOT INDEXED */ |
| 11812 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;} |
| 11813 break; |
| 11814 case 117: /* using_opt ::= USING LP idlist RP */ |
| 11815 {yymsp[-3].minor.yy254 = yymsp[-1].minor.yy254;} |
| 11816 break; |
| 11817 case 118: /* using_opt ::= */ |
| 11818 case 146: /* idlist_opt ::= */ yytestcase(yyruleno==146); |
| 11819 {yymsp[1].minor.yy254 = 0;} |
| 11820 break; |
| 11821 case 120: /* orderby_opt ::= ORDER BY sortlist */ |
| 11822 case 127: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==12
7); |
| 11823 {yymsp[-2].minor.yy148 = yymsp[0].minor.yy148;} |
| 11824 break; |
| 11825 case 121: /* sortlist ::= sortlist COMMA expr sortorder */ |
| 11826 { |
| 11827 yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148,yym
sp[-1].minor.yy190.pExpr); |
| 11828 sqlite3ExprListSetSortOrder(yymsp[-3].minor.yy148,yymsp[0].minor.yy194); |
| 11829 } |
| 11830 break; |
| 11831 case 122: /* sortlist ::= expr sortorder */ |
| 11832 { |
| 11833 yymsp[-1].minor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy190.p
Expr); /*A-overwrites-Y*/ |
| 11834 sqlite3ExprListSetSortOrder(yymsp[-1].minor.yy148,yymsp[0].minor.yy194); |
| 11835 } |
| 11836 break; |
| 11837 case 123: /* sortorder ::= ASC */ |
| 11838 {yymsp[0].minor.yy194 = SQLITE_SO_ASC;} |
| 11839 break; |
| 11840 case 124: /* sortorder ::= DESC */ |
| 11841 {yymsp[0].minor.yy194 = SQLITE_SO_DESC;} |
| 11842 break; |
| 11843 case 125: /* sortorder ::= */ |
| 11844 {yymsp[1].minor.yy194 = SQLITE_SO_UNDEFINED;} |
| 11845 break; |
| 11846 case 130: /* limit_opt ::= */ |
| 11847 {yymsp[1].minor.yy354.pLimit = 0; yymsp[1].minor.yy354.pOffset = 0;} |
| 11848 break; |
| 11849 case 131: /* limit_opt ::= LIMIT expr */ |
| 11850 {yymsp[-1].minor.yy354.pLimit = yymsp[0].minor.yy190.pExpr; yymsp[-1].minor.yy35
4.pOffset = 0;} |
| 11851 break; |
| 11852 case 132: /* limit_opt ::= LIMIT expr OFFSET expr */ |
| 11853 {yymsp[-3].minor.yy354.pLimit = yymsp[-2].minor.yy190.pExpr; yymsp[-3].minor.yy3
54.pOffset = yymsp[0].minor.yy190.pExpr;} |
| 11854 break; |
| 11855 case 133: /* limit_opt ::= LIMIT expr COMMA expr */ |
| 11856 {yymsp[-3].minor.yy354.pOffset = yymsp[-2].minor.yy190.pExpr; yymsp[-3].minor.yy
354.pLimit = yymsp[0].minor.yy190.pExpr;} |
| 11857 break; |
| 11858 case 134: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */ |
| 11859 { |
| 11860 sqlite3WithPush(pParse, yymsp[-5].minor.yy285, 1); |
| 11861 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy185, &yymsp[-1].minor.yy0); |
| 11862 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy185,yymsp[0].minor.yy72); |
| 11863 } |
| 11864 break; |
| 11865 case 137: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist w
here_opt */ |
| 11866 { |
| 11867 sqlite3WithPush(pParse, yymsp[-7].minor.yy285, 1); |
| 11868 sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy185, &yymsp[-3].minor.yy0); |
| 11869 sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy148,"set list"); |
| 11870 sqlite3Update(pParse,yymsp[-4].minor.yy185,yymsp[-1].minor.yy148,yymsp[0].mino
r.yy72,yymsp[-5].minor.yy194); |
| 11871 } |
| 11872 break; |
| 11873 case 138: /* setlist ::= setlist COMMA nm EQ expr */ |
| 11874 { |
| 11875 yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy148, y
ymsp[0].minor.yy190.pExpr); |
| 11876 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy148, &yymsp[-2].minor.yy0, 1)
; |
| 11877 } |
| 11878 break; |
| 11879 case 139: /* setlist ::= setlist COMMA LP idlist RP EQ expr */ |
| 11880 { |
| 11881 yymsp[-6].minor.yy148 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy
148, yymsp[-3].minor.yy254, yymsp[0].minor.yy190.pExpr); |
| 11882 } |
| 11883 break; |
| 11884 case 140: /* setlist ::= nm EQ expr */ |
| 11885 { |
| 11886 yylhsminor.yy148 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy190.pExpr
); |
| 11887 sqlite3ExprListSetName(pParse, yylhsminor.yy148, &yymsp[-2].minor.yy0, 1); |
| 11888 } |
| 11889 yymsp[-2].minor.yy148 = yylhsminor.yy148; |
| 11890 break; |
| 11891 case 141: /* setlist ::= LP idlist RP EQ expr */ |
| 11892 { |
| 11893 yymsp[-4].minor.yy148 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor
.yy254, yymsp[0].minor.yy190.pExpr); |
| 11894 } |
| 11895 break; |
| 11896 case 142: /* cmd ::= with insert_cmd INTO fullname idlist_opt select */ |
| 11897 { |
| 11898 sqlite3WithPush(pParse, yymsp[-5].minor.yy285, 1); |
| 11899 sqlite3Insert(pParse, yymsp[-2].minor.yy185, yymsp[0].minor.yy243, yymsp[-1].m
inor.yy254, yymsp[-4].minor.yy194); |
| 11900 } |
| 11901 break; |
| 11902 case 143: /* cmd ::= with insert_cmd INTO fullname idlist_opt DEFAULT VALU
ES */ |
| 11903 { |
| 11904 sqlite3WithPush(pParse, yymsp[-6].minor.yy285, 1); |
| 11905 sqlite3Insert(pParse, yymsp[-3].minor.yy185, 0, yymsp[-2].minor.yy254, yymsp[-
5].minor.yy194); |
| 11906 } |
| 11907 break; |
| 11908 case 147: /* idlist_opt ::= LP idlist RP */ |
| 11909 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;} |
| 11910 break; |
| 11911 case 148: /* idlist ::= idlist COMMA nm */ |
| 11912 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy254,&y
ymsp[0].minor.yy0);} |
| 11913 break; |
| 11914 case 149: /* idlist ::= nm */ |
| 11915 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0); /
*A-overwrites-Y*/} |
| 11916 break; |
| 11917 case 150: /* expr ::= LP expr RP */ |
| 11918 {spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-o
verwrites-B*/ yymsp[-2].minor.yy190.pExpr = yymsp[-1].minor.yy190.pExpr;} |
| 11919 break; |
| 11920 case 151: /* term ::= NULL */ |
| 11921 case 156: /* term ::= FLOAT|BLOB */ yytestcase(yyruleno==156); |
| 11922 case 157: /* term ::= STRING */ yytestcase(yyruleno==157); |
| 11923 {spanExpr(&yymsp[0].minor.yy190,pParse,yymsp[0].major,yymsp[0].minor.yy0);/*A-ov
erwrites-X*/} |
| 11924 break; |
| 11925 case 152: /* expr ::= ID|INDEXED */ |
| 11926 case 153: /* expr ::= JOIN_KW */ yytestcase(yyruleno==153); |
| 11927 {spanExpr(&yymsp[0].minor.yy190,pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites
-X*/} |
| 11928 break; |
| 11929 case 154: /* expr ::= nm DOT nm */ |
| 11930 { |
| 11931 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1); |
| 11932 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1); |
| 11933 spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*A-
overwrites-X*/ |
| 11934 yymsp[-2].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2); |
| 11935 } |
| 11936 break; |
| 11937 case 155: /* expr ::= nm DOT nm DOT nm */ |
| 11938 { |
| 11939 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1); |
| 11940 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1); |
| 11941 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1); |
| 11942 Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3); |
| 11943 spanSet(&yymsp[-4].minor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); /*A-
overwrites-X*/ |
| 11944 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4); |
| 11945 } |
| 11946 break; |
| 11947 case 158: /* term ::= INTEGER */ |
| 11948 { |
| 11949 yylhsminor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].mi
nor.yy0, 1); |
| 11950 yylhsminor.yy190.zStart = yymsp[0].minor.yy0.z; |
| 11951 yylhsminor.yy190.zEnd = yymsp[0].minor.yy0.z + yymsp[0].minor.yy0.n; |
| 11952 if( yylhsminor.yy190.pExpr ) yylhsminor.yy190.pExpr->flags |= EP_Leaf; |
| 11953 } |
| 11954 yymsp[0].minor.yy190 = yylhsminor.yy190; |
| 11955 break; |
| 11956 case 159: /* expr ::= VARIABLE */ |
| 11957 { |
| 11958 if( !(yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1]))
){ |
| 11959 u32 n = yymsp[0].minor.yy0.n; |
| 11960 spanExpr(&yymsp[0].minor.yy190, pParse, TK_VARIABLE, yymsp[0].minor.yy0); |
| 11961 sqlite3ExprAssignVarNumber(pParse, yymsp[0].minor.yy190.pExpr, n); |
| 11962 }else{ |
| 11963 /* When doing a nested parse, one can include terms in an expression |
| 11964 ** that look like this: #1 #2 ... These terms refer to registers |
| 11965 ** in the virtual machine. #N is the N-th register. */ |
| 11966 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/ |
| 11967 assert( t.n>=2 ); |
| 11968 spanSet(&yymsp[0].minor.yy190, &t, &t); |
| 11969 if( pParse->nested==0 ){ |
| 11970 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); |
| 11971 yymsp[0].minor.yy190.pExpr = 0; |
| 11972 }else{ |
| 11973 yymsp[0].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); |
| 11974 if( yymsp[0].minor.yy190.pExpr ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.
yy190.pExpr->iTable); |
| 11975 } |
| 11976 } |
| 11977 } |
| 11978 break; |
| 11979 case 160: /* expr ::= expr COLLATE ID|STRING */ |
| 11980 { |
| 11981 yymsp[-2].minor.yy190.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].min
or.yy190.pExpr, &yymsp[0].minor.yy0, 1); |
| 11982 yymsp[-2].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; |
| 11983 } |
| 11984 break; |
| 11985 case 161: /* expr ::= CAST LP expr AS typetoken RP */ |
| 11986 { |
| 11987 spanSet(&yymsp[-5].minor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); /*A-
overwrites-X*/ |
| 11988 yymsp[-5].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1]
.minor.yy0, 1); |
| 11989 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy190.pExpr, yymsp[-3].m
inor.yy190.pExpr, 0); |
| 11990 } |
| 11991 break; |
| 11992 case 162: /* expr ::= ID|INDEXED LP distinct exprlist RP */ |
| 11993 { |
| 11994 if( yymsp[-1].minor.yy148 && yymsp[-1].minor.yy148->nExpr>pParse->db->aLimit[S
QLITE_LIMIT_FUNCTION_ARG] ){ |
| 11995 sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].mino
r.yy0); |
| 11996 } |
| 11997 yylhsminor.yy190.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy148, &y
ymsp[-4].minor.yy0); |
| 11998 spanSet(&yylhsminor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); |
| 11999 if( yymsp[-2].minor.yy194==SF_Distinct && yylhsminor.yy190.pExpr ){ |
| 12000 yylhsminor.yy190.pExpr->flags |= EP_Distinct; |
| 12001 } |
| 12002 } |
| 12003 yymsp[-4].minor.yy190 = yylhsminor.yy190; |
| 12004 break; |
| 12005 case 163: /* expr ::= ID|INDEXED LP STAR RP */ |
| 12006 { |
| 12007 yylhsminor.yy190.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0); |
| 12008 spanSet(&yylhsminor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); |
| 12009 } |
| 12010 yymsp[-3].minor.yy190 = yylhsminor.yy190; |
| 12011 break; |
| 12012 case 164: /* term ::= CTIME_KW */ |
| 12013 { |
| 12014 yylhsminor.yy190.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0); |
| 12015 spanSet(&yylhsminor.yy190, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); |
| 12016 } |
| 12017 yymsp[0].minor.yy190 = yylhsminor.yy190; |
| 12018 break; |
| 12019 case 165: /* expr ::= LP nexprlist COMMA expr RP */ |
| 12020 { |
| 12021 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy148, yymsp[-
1].minor.yy190.pExpr); |
| 12022 yylhsminor.yy190.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0); |
| 12023 if( yylhsminor.yy190.pExpr ){ |
| 12024 yylhsminor.yy190.pExpr->x.pList = pList; |
| 12025 spanSet(&yylhsminor.yy190, &yymsp[-4].minor.yy0, &yymsp[0].minor.yy0); |
| 12026 }else{ |
| 12027 sqlite3ExprListDelete(pParse->db, pList); |
| 12028 } |
| 12029 } |
| 12030 yymsp[-4].minor.yy190 = yylhsminor.yy190; |
| 12031 break; |
| 12032 case 166: /* expr ::= expr AND expr */ |
| 12033 case 167: /* expr ::= expr OR expr */ yytestcase(yyruleno==167); |
| 12034 case 168: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==168); |
| 12035 case 169: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==169); |
| 12036 case 170: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(y
yruleno==170); |
| 12037 case 171: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==171); |
| 12038 case 172: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==172
); |
| 12039 case 173: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==173); |
| 12040 {spanBinaryExpr(pParse,yymsp[-1].major,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy
190);} |
| 12041 break; |
| 12042 case 174: /* likeop ::= LIKE_KW|MATCH */ |
| 12043 {yymsp[0].minor.yy0=yymsp[0].minor.yy0;/*A-overwrites-X*/} |
| 12044 break; |
| 12045 case 175: /* likeop ::= NOT LIKE_KW|MATCH */ |
| 12046 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yy
msp[-1].minor.yy0-overwrite-yymsp[0].minor.yy0*/} |
| 12047 break; |
| 12048 case 176: /* expr ::= expr likeop expr */ |
| 12049 { |
| 12050 ExprList *pList; |
| 12051 int bNot = yymsp[-1].minor.yy0.n & 0x80000000; |
| 12052 yymsp[-1].minor.yy0.n &= 0x7fffffff; |
| 12053 pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy190.pExpr); |
| 12054 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy190.pExpr); |
| 12055 yymsp[-2].minor.yy190.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].mi
nor.yy0); |
| 12056 exprNot(pParse, bNot, &yymsp[-2].minor.yy190); |
| 12057 yymsp[-2].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd; |
| 12058 if( yymsp[-2].minor.yy190.pExpr ) yymsp[-2].minor.yy190.pExpr->flags |= EP_Inf
ixFunc; |
| 12059 } |
| 12060 break; |
| 12061 case 177: /* expr ::= expr likeop expr ESCAPE expr */ |
| 12062 { |
| 12063 ExprList *pList; |
| 12064 int bNot = yymsp[-3].minor.yy0.n & 0x80000000; |
| 12065 yymsp[-3].minor.yy0.n &= 0x7fffffff; |
| 12066 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr); |
| 12067 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy190.pExpr); |
| 12068 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy190.pExpr); |
| 12069 yymsp[-4].minor.yy190.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].mi
nor.yy0); |
| 12070 exprNot(pParse, bNot, &yymsp[-4].minor.yy190); |
| 12071 yymsp[-4].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd; |
| 12072 if( yymsp[-4].minor.yy190.pExpr ) yymsp[-4].minor.yy190.pExpr->flags |= EP_Inf
ixFunc; |
| 12073 } |
| 12074 break; |
| 12075 case 178: /* expr ::= expr ISNULL|NOTNULL */ |
| 12076 {spanUnaryPostfix(pParse,yymsp[0].major,&yymsp[-1].minor.yy190,&yymsp[0].minor.y
y0);} |
| 12077 break; |
| 12078 case 179: /* expr ::= expr NOT NULL */ |
| 12079 {spanUnaryPostfix(pParse,TK_NOTNULL,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy0);
} |
| 12080 break; |
| 12081 case 180: /* expr ::= expr IS expr */ |
| 12082 { |
| 12083 spanBinaryExpr(pParse,TK_IS,&yymsp[-2].minor.yy190,&yymsp[0].minor.yy190); |
| 12084 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy190.pExpr, yymsp[-2].minor.yy190.
pExpr, TK_ISNULL); |
| 12085 } |
| 12086 break; |
| 12087 case 181: /* expr ::= expr IS NOT expr */ |
| 12088 { |
| 12089 spanBinaryExpr(pParse,TK_ISNOT,&yymsp[-3].minor.yy190,&yymsp[0].minor.yy190); |
| 12090 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy190.pExpr, yymsp[-3].minor.yy190.
pExpr, TK_NOTNULL); |
| 12091 } |
| 12092 break; |
| 12093 case 182: /* expr ::= NOT expr */ |
| 12094 case 183: /* expr ::= BITNOT expr */ yytestcase(yyruleno==183); |
| 12095 {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,yymsp[-1].major,&yymsp[0].minor.y
y190,&yymsp[-1].minor.yy0);/*A-overwrites-B*/} |
| 12096 break; |
| 12097 case 184: /* expr ::= MINUS expr */ |
| 12098 {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,TK_UMINUS,&yymsp[0].minor.yy190,&
yymsp[-1].minor.yy0);/*A-overwrites-B*/} |
| 12099 break; |
| 12100 case 185: /* expr ::= PLUS expr */ |
| 12101 {spanUnaryPrefix(&yymsp[-1].minor.yy190,pParse,TK_UPLUS,&yymsp[0].minor.yy190,&y
ymsp[-1].minor.yy0);/*A-overwrites-B*/} |
| 12102 break; |
| 12103 case 186: /* between_op ::= BETWEEN */ |
| 12104 case 189: /* in_op ::= IN */ yytestcase(yyruleno==189); |
| 12105 {yymsp[0].minor.yy194 = 0;} |
| 12106 break; |
| 12107 case 188: /* expr ::= expr between_op expr AND expr */ |
| 12108 { |
| 12109 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.pExpr)
; |
| 12110 pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy190.pExpr); |
| 12111 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor
.yy190.pExpr, 0); |
| 12112 if( yymsp[-4].minor.yy190.pExpr ){ |
| 12113 yymsp[-4].minor.yy190.pExpr->x.pList = pList; |
| 12114 }else{ |
| 12115 sqlite3ExprListDelete(pParse->db, pList); |
| 12116 } |
| 12117 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190); |
| 12118 yymsp[-4].minor.yy190.zEnd = yymsp[0].minor.yy190.zEnd; |
| 12119 } |
| 12120 break; |
| 12121 case 191: /* expr ::= expr in_op LP exprlist RP */ |
| 12122 { |
| 12123 if( yymsp[-1].minor.yy148==0 ){ |
| 12124 /* Expressions of the form |
| 12125 ** |
| 12126 ** expr1 IN () |
| 12127 ** expr1 NOT IN () |
| 12128 ** |
| 12129 ** simplify to constants 0 (false) and 1 (true), respectively, |
| 12130 ** regardless of the value of expr1. |
| 12131 */ |
| 12132 sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy190.pExpr); |
| 12133 yymsp[-4].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sql
ite3IntTokens[yymsp[-3].minor.yy194],1); |
| 12134 }else if( yymsp[-1].minor.yy148->nExpr==1 ){ |
| 12135 /* Expressions of the form: |
| 12136 ** |
| 12137 ** expr1 IN (?1) |
| 12138 ** expr1 NOT IN (?2) |
| 12139 ** |
| 12140 ** with exactly one value on the RHS can be simplified to something |
| 12141 ** like this: |
| 12142 ** |
| 12143 ** expr1 == ?1 |
| 12144 ** expr1 <> ?2 |
| 12145 ** |
| 12146 ** But, the RHS of the == or <> is marked with the EP_Generic flag |
| 12147 ** so that it may not contribute to the computation of comparison |
| 12148 ** affinity or the collating sequence to use for comparison. Otherwise, |
| 12149 ** the semantics would be subtly different from IN or NOT IN. |
| 12150 */ |
| 12151 Expr *pRHS = yymsp[-1].minor.yy148->a[0].pExpr; |
| 12152 yymsp[-1].minor.yy148->a[0].pExpr = 0; |
| 12153 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy148); |
| 12154 /* pRHS cannot be NULL because a malloc error would have been detected |
| 12155 ** before now and control would have never reached this point */ |
| 12156 if( ALWAYS(pRHS) ){ |
| 12157 pRHS->flags &= ~EP_Collate; |
| 12158 pRHS->flags |= EP_Generic; |
| 12159 } |
| 12160 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, yymsp[-3].minor.yy194 ?
TK_NE : TK_EQ, yymsp[-4].minor.yy190.pExpr, pRHS); |
| 12161 }else{ |
| 12162 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.
yy190.pExpr, 0); |
| 12163 if( yymsp[-4].minor.yy190.pExpr ){ |
| 12164 yymsp[-4].minor.yy190.pExpr->x.pList = yymsp[-1].minor.yy148; |
| 12165 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy190.pExpr); |
| 12166 }else{ |
| 12167 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy148); |
| 12168 } |
| 12169 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190); |
| 12170 } |
| 12171 yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; |
| 12172 } |
| 12173 break; |
| 12174 case 192: /* expr ::= LP select RP */ |
| 12175 { |
| 12176 spanSet(&yymsp[-2].minor.yy190,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /*
A-overwrites-B*/ |
| 12177 yymsp[-2].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0); |
| 12178 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy190.pExpr, yymsp[-1].minor.y
y243); |
| 12179 } |
| 12180 break; |
| 12181 case 193: /* expr ::= expr in_op LP select RP */ |
| 12182 { |
| 12183 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy
190.pExpr, 0); |
| 12184 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, yymsp[-1].minor.y
y243); |
| 12185 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190); |
| 12186 yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; |
| 12187 } |
| 12188 break; |
| 12189 case 194: /* expr ::= expr in_op nm dbnm paren_exprlist */ |
| 12190 { |
| 12191 SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yym
sp[-1].minor.yy0); |
| 12192 Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); |
| 12193 if( yymsp[0].minor.yy148 ) sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc :
0, yymsp[0].minor.yy148); |
| 12194 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy
190.pExpr, 0); |
| 12195 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, pSelect); |
| 12196 exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190); |
| 12197 yymsp[-4].minor.yy190.zEnd = yymsp[-1].minor.yy0.z ? &yymsp[-1].minor.yy0.z[
yymsp[-1].minor.yy0.n] : &yymsp[-2].minor.yy0.z[yymsp[-2].minor.yy0.n]; |
| 12198 } |
| 12199 break; |
| 12200 case 195: /* expr ::= EXISTS LP select RP */ |
| 12201 { |
| 12202 Expr *p; |
| 12203 spanSet(&yymsp[-3].minor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*
A-overwrites-B*/ |
| 12204 p = yymsp[-3].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0); |
| 12205 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy243); |
| 12206 } |
| 12207 break; |
| 12208 case 196: /* expr ::= CASE case_operand case_exprlist case_else END */ |
| 12209 { |
| 12210 spanSet(&yymsp[-4].minor.yy190,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); /*A
-overwrites-C*/ |
| 12211 yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy
72, 0); |
| 12212 if( yymsp[-4].minor.yy190.pExpr ){ |
| 12213 yymsp[-4].minor.yy190.pExpr->x.pList = yymsp[-1].minor.yy72 ? sqlite3ExprLis
tAppend(pParse,yymsp[-2].minor.yy148,yymsp[-1].minor.yy72) : yymsp[-2].minor.yy1
48; |
| 12214 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy190.pExpr); |
| 12215 }else{ |
| 12216 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy148); |
| 12217 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy72); |
| 12218 } |
| 12219 } |
| 12220 break; |
| 12221 case 197: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */ |
| 12222 { |
| 12223 yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy148, yy
msp[-2].minor.yy190.pExpr); |
| 12224 yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy148, yy
msp[0].minor.yy190.pExpr); |
| 12225 } |
| 12226 break; |
| 12227 case 198: /* case_exprlist ::= WHEN expr THEN expr */ |
| 12228 { |
| 12229 yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy190.
pExpr); |
| 12230 yymsp[-3].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy148, yy
msp[0].minor.yy190.pExpr); |
| 12231 } |
| 12232 break; |
| 12233 case 201: /* case_operand ::= expr */ |
| 12234 {yymsp[0].minor.yy72 = yymsp[0].minor.yy190.pExpr; /*A-overwrites-X*/} |
| 12235 break; |
| 12236 case 204: /* nexprlist ::= nexprlist COMMA expr */ |
| 12237 {yymsp[-2].minor.yy148 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy148,yyms
p[0].minor.yy190.pExpr);} |
| 12238 break; |
| 12239 case 205: /* nexprlist ::= expr */ |
| 12240 {yymsp[0].minor.yy148 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy190.pExp
r); /*A-overwrites-Y*/} |
| 12241 break; |
| 12242 case 207: /* paren_exprlist ::= LP exprlist RP */ |
| 12243 case 212: /* eidlist_opt ::= LP eidlist RP */ yytestcase(yyruleno==212); |
| 12244 {yymsp[-2].minor.yy148 = yymsp[-1].minor.yy148;} |
| 12245 break; |
| 12246 case 208: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm L
P sortlist RP where_opt */ |
| 12247 { |
| 12248 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, |
| 12249 sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0),
yymsp[-2].minor.yy148, yymsp[-10].minor.yy194, |
| 12250 &yymsp[-11].minor.yy0, yymsp[0].minor.yy72, SQLITE_SO_ASC,
yymsp[-8].minor.yy194, SQLITE_IDXTYPE_APPDEF); |
| 12251 } |
| 12252 break; |
| 12253 case 209: /* uniqueflag ::= UNIQUE */ |
| 12254 case 250: /* raisetype ::= ABORT */ yytestcase(yyruleno==250); |
| 12255 {yymsp[0].minor.yy194 = OE_Abort;} |
| 12256 break; |
| 12257 case 210: /* uniqueflag ::= */ |
| 12258 {yymsp[1].minor.yy194 = OE_None;} |
| 12259 break; |
| 12260 case 213: /* eidlist ::= eidlist COMMA nm collate sortorder */ |
| 12261 { |
| 12262 yymsp[-4].minor.yy148 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy148,
&yymsp[-2].minor.yy0, yymsp[-1].minor.yy194, yymsp[0].minor.yy194); |
| 12263 } |
| 12264 break; |
| 12265 case 214: /* eidlist ::= nm collate sortorder */ |
| 12266 { |
| 12267 yymsp[-2].minor.yy148 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy
0, yymsp[-1].minor.yy194, yymsp[0].minor.yy194); /*A-overwrites-Y*/ |
| 12268 } |
| 12269 break; |
| 12270 case 217: /* cmd ::= DROP INDEX ifexists fullname */ |
| 12271 {sqlite3DropIndex(pParse, yymsp[0].minor.yy185, yymsp[-1].minor.yy194);} |
| 12272 break; |
| 12273 case 218: /* cmd ::= VACUUM */ |
| 12274 {sqlite3Vacuum(pParse,0);} |
| 12275 break; |
| 12276 case 219: /* cmd ::= VACUUM nm */ |
| 12277 {sqlite3Vacuum(pParse,&yymsp[0].minor.yy0);} |
| 12278 break; |
| 12279 case 220: /* cmd ::= PRAGMA nm dbnm */ |
| 12280 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);} |
| 12281 break; |
| 12282 case 221: /* cmd ::= PRAGMA nm dbnm EQ nmnum */ |
| 12283 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.
yy0,0);} |
| 12284 break; |
| 12285 case 222: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */ |
| 12286 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor
.yy0,0);} |
| 12287 break; |
| 12288 case 223: /* cmd ::= PRAGMA nm dbnm EQ minus_num */ |
| 12289 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.
yy0,1);} |
| 12290 break; |
| 12291 case 224: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */ |
| 12292 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor
.yy0,1);} |
| 12293 break; |
| 12294 case 227: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */ |
| 12295 { |
| 12296 Token all; |
| 12297 all.z = yymsp[-3].minor.yy0.z; |
| 12298 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.y
y0.n; |
| 12299 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy145, &all); |
| 12300 } |
| 12301 break; |
| 12302 case 228: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_tim
e trigger_event ON fullname foreach_clause when_clause */ |
| 12303 { |
| 12304 sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[
-5].minor.yy194, yymsp[-4].minor.yy332.a, yymsp[-4].minor.yy332.b, yymsp[-2].min
or.yy185, yymsp[0].minor.yy72, yymsp[-10].minor.yy194, yymsp[-8].minor.yy194); |
| 12305 yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6]
.minor.yy0); /*A-overwrites-T*/ |
| 12306 } |
| 12307 break; |
| 12308 case 229: /* trigger_time ::= BEFORE */ |
| 12309 { yymsp[0].minor.yy194 = TK_BEFORE; } |
| 12310 break; |
| 12311 case 230: /* trigger_time ::= AFTER */ |
| 12312 { yymsp[0].minor.yy194 = TK_AFTER; } |
| 12313 break; |
| 12314 case 231: /* trigger_time ::= INSTEAD OF */ |
| 12315 { yymsp[-1].minor.yy194 = TK_INSTEAD;} |
| 12316 break; |
| 12317 case 232: /* trigger_time ::= */ |
| 12318 { yymsp[1].minor.yy194 = TK_BEFORE; } |
| 12319 break; |
| 12320 case 233: /* trigger_event ::= DELETE|INSERT */ |
| 12321 case 234: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==234); |
| 12322 {yymsp[0].minor.yy332.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy33
2.b = 0;} |
| 12323 break; |
| 12324 case 235: /* trigger_event ::= UPDATE OF idlist */ |
| 12325 {yymsp[-2].minor.yy332.a = TK_UPDATE; yymsp[-2].minor.yy332.b = yymsp[0].minor.y
y254;} |
| 12326 break; |
| 12327 case 236: /* when_clause ::= */ |
| 12328 case 255: /* key_opt ::= */ yytestcase(yyruleno==255); |
| 12329 { yymsp[1].minor.yy72 = 0; } |
| 12330 break; |
| 12331 case 237: /* when_clause ::= WHEN expr */ |
| 12332 case 256: /* key_opt ::= KEY expr */ yytestcase(yyruleno==256); |
| 12333 { yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr; } |
| 12334 break; |
| 12335 case 238: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */ |
| 12336 { |
| 12337 assert( yymsp[-2].minor.yy145!=0 ); |
| 12338 yymsp[-2].minor.yy145->pLast->pNext = yymsp[-1].minor.yy145; |
| 12339 yymsp[-2].minor.yy145->pLast = yymsp[-1].minor.yy145; |
| 12340 } |
| 12341 break; |
| 12342 case 239: /* trigger_cmd_list ::= trigger_cmd SEMI */ |
| 12343 { |
| 12344 assert( yymsp[-1].minor.yy145!=0 ); |
| 12345 yymsp[-1].minor.yy145->pLast = yymsp[-1].minor.yy145; |
| 12346 } |
| 12347 break; |
| 12348 case 240: /* trnm ::= nm DOT nm */ |
| 12349 { |
| 12350 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0; |
| 12351 sqlite3ErrorMsg(pParse, |
| 12352 "qualified table names are not allowed on INSERT, UPDATE, and DELETE " |
| 12353 "statements within triggers"); |
| 12354 } |
| 12355 break; |
| 12356 case 241: /* tridxby ::= INDEXED BY nm */ |
| 12357 { |
| 12358 sqlite3ErrorMsg(pParse, |
| 12359 "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " |
| 12360 "within triggers"); |
| 12361 } |
| 12362 break; |
| 12363 case 242: /* tridxby ::= NOT INDEXED */ |
| 12364 { |
| 12365 sqlite3ErrorMsg(pParse, |
| 12366 "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " |
| 12367 "within triggers"); |
| 12368 } |
| 12369 break; |
| 12370 case 243: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_
opt */ |
| 12371 {yymsp[-6].minor.yy145 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.y
y0, yymsp[-1].minor.yy148, yymsp[0].minor.yy72, yymsp[-5].minor.yy194);} |
| 12372 break; |
| 12373 case 244: /* trigger_cmd ::= insert_cmd INTO trnm idlist_opt select */ |
| 12374 {yymsp[-4].minor.yy145 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.y
y0, yymsp[-1].minor.yy254, yymsp[0].minor.yy243, yymsp[-4].minor.yy194);/*A-over
writes-R*/} |
| 12375 break; |
| 12376 case 245: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */ |
| 12377 {yymsp[-4].minor.yy145 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.y
y0, yymsp[0].minor.yy72);} |
| 12378 break; |
| 12379 case 246: /* trigger_cmd ::= select */ |
| 12380 {yymsp[0].minor.yy145 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy24
3); /*A-overwrites-X*/} |
| 12381 break; |
| 12382 case 247: /* expr ::= RAISE LP IGNORE RP */ |
| 12383 { |
| 12384 spanSet(&yymsp[-3].minor.yy190,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); /*A
-overwrites-X*/ |
| 12385 yymsp[-3].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); |
| 12386 if( yymsp[-3].minor.yy190.pExpr ){ |
| 12387 yymsp[-3].minor.yy190.pExpr->affinity = OE_Ignore; |
| 12388 } |
| 12389 } |
| 12390 break; |
| 12391 case 248: /* expr ::= RAISE LP raisetype COMMA nm RP */ |
| 12392 { |
| 12393 spanSet(&yymsp[-5].minor.yy190,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); /*A
-overwrites-X*/ |
| 12394 yymsp[-5].minor.yy190.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1
].minor.yy0, 1); |
| 12395 if( yymsp[-5].minor.yy190.pExpr ) { |
| 12396 yymsp[-5].minor.yy190.pExpr->affinity = (char)yymsp[-3].minor.yy194; |
| 12397 } |
| 12398 } |
| 12399 break; |
| 12400 case 249: /* raisetype ::= ROLLBACK */ |
| 12401 {yymsp[0].minor.yy194 = OE_Rollback;} |
| 12402 break; |
| 12403 case 251: /* raisetype ::= FAIL */ |
| 12404 {yymsp[0].minor.yy194 = OE_Fail;} |
| 12405 break; |
| 12406 case 252: /* cmd ::= DROP TRIGGER ifexists fullname */ |
| 12407 { |
| 12408 sqlite3DropTrigger(pParse,yymsp[0].minor.yy185,yymsp[-1].minor.yy194); |
| 12409 } |
| 12410 break; |
| 12411 case 253: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */ |
| 12412 { |
| 12413 sqlite3Attach(pParse, yymsp[-3].minor.yy190.pExpr, yymsp[-1].minor.yy190.pExpr
, yymsp[0].minor.yy72); |
| 12414 } |
| 12415 break; |
| 12416 case 254: /* cmd ::= DETACH database_kw_opt expr */ |
| 12417 { |
| 12418 sqlite3Detach(pParse, yymsp[0].minor.yy190.pExpr); |
| 12419 } |
| 12420 break; |
| 12421 case 257: /* cmd ::= REINDEX */ |
| 12422 {sqlite3Reindex(pParse, 0, 0);} |
| 12423 break; |
| 12424 case 258: /* cmd ::= REINDEX nm dbnm */ |
| 12425 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);} |
| 12426 break; |
| 12427 case 259: /* cmd ::= ANALYZE */ |
| 12428 {sqlite3Analyze(pParse, 0, 0);} |
| 12429 break; |
| 12430 case 260: /* cmd ::= ANALYZE nm dbnm */ |
| 12431 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);} |
| 12432 break; |
| 12433 case 261: /* cmd ::= ALTER TABLE fullname RENAME TO nm */ |
| 12434 { |
| 12435 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy185,&yymsp[0].minor.yy0); |
| 12436 } |
| 12437 break; |
| 12438 case 262: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt colu
mnname carglist */ |
| 12439 { |
| 12440 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pP
arse->sLastToken.n; |
| 12441 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0); |
| 12442 } |
| 12443 break; |
| 12444 case 263: /* add_column_fullname ::= fullname */ |
| 12445 { |
| 12446 disableLookaside(pParse); |
| 12447 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy185); |
| 12448 } |
| 12449 break; |
| 12450 case 264: /* cmd ::= create_vtab */ |
| 12451 {sqlite3VtabFinishParse(pParse,0);} |
| 12452 break; |
| 12453 case 265: /* cmd ::= create_vtab LP vtabarglist RP */ |
| 12454 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);} |
| 12455 break; |
| 12456 case 266: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm US
ING nm */ |
| 12457 { |
| 12458 sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &y
ymsp[0].minor.yy0, yymsp[-4].minor.yy194); |
| 12459 } |
| 12460 break; |
| 12461 case 267: /* vtabarg ::= */ |
| 12462 {sqlite3VtabArgInit(pParse);} |
| 12463 break; |
| 12464 case 268: /* vtabargtoken ::= ANY */ |
| 12465 case 269: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==269); |
| 12466 case 270: /* lp ::= LP */ yytestcase(yyruleno==270); |
| 12467 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);} |
| 12468 break; |
| 12469 case 271: /* with ::= */ |
| 12470 {yymsp[1].minor.yy285 = 0;} |
| 12471 break; |
| 12472 case 272: /* with ::= WITH wqlist */ |
| 12473 { yymsp[-1].minor.yy285 = yymsp[0].minor.yy285; } |
| 12474 break; |
| 12475 case 273: /* with ::= WITH RECURSIVE wqlist */ |
| 12476 { yymsp[-2].minor.yy285 = yymsp[0].minor.yy285; } |
| 12477 break; |
| 12478 case 274: /* wqlist ::= nm eidlist_opt AS LP select RP */ |
| 12479 { |
| 12480 yymsp[-5].minor.yy285 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[
-4].minor.yy148, yymsp[-1].minor.yy243); /*A-overwrites-X*/ |
| 12481 } |
| 12482 break; |
| 12483 case 275: /* wqlist ::= wqlist COMMA nm eidlist_opt AS LP select RP */ |
| 12484 { |
| 12485 yymsp[-7].minor.yy285 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy285, &yymsp[-
5].minor.yy0, yymsp[-4].minor.yy148, yymsp[-1].minor.yy243); |
| 12486 } |
| 12487 break; |
| 12488 default: |
| 12489 /* (276) input ::= cmdlist */ yytestcase(yyruleno==276); |
| 12490 /* (277) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==277); |
| 12491 /* (278) cmdlist ::= ecmd (OPTIMIZED OUT) */ assert(yyruleno!=278); |
| 12492 /* (279) ecmd ::= SEMI */ yytestcase(yyruleno==279); |
| 12493 /* (280) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==280); |
| 12494 /* (281) explain ::= */ yytestcase(yyruleno==281); |
| 12495 /* (282) trans_opt ::= */ yytestcase(yyruleno==282); |
| 12496 /* (283) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==283); |
| 12497 /* (284) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==284); |
| 12498 /* (285) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==285); |
| 12499 /* (286) savepoint_opt ::= */ yytestcase(yyruleno==286); |
| 12500 /* (287) cmd ::= create_table create_table_args */ yytestcase(yyruleno==28
7); |
| 12501 /* (288) columnlist ::= columnlist COMMA columnname carglist */ yytestcase
(yyruleno==288); |
| 12502 /* (289) columnlist ::= columnname carglist */ yytestcase(yyruleno==289); |
| 12503 /* (290) nm ::= ID|INDEXED */ yytestcase(yyruleno==290); |
| 12504 /* (291) nm ::= STRING */ yytestcase(yyruleno==291); |
| 12505 /* (292) nm ::= JOIN_KW */ yytestcase(yyruleno==292); |
| 12506 /* (293) typetoken ::= typename */ yytestcase(yyruleno==293); |
| 12507 /* (294) typename ::= ID|STRING */ yytestcase(yyruleno==294); |
| 12508 /* (295) signed ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=295); |
| 12509 /* (296) signed ::= minus_num (OPTIMIZED OUT) */ assert(yyruleno!=296); |
| 12510 /* (297) carglist ::= carglist ccons */ yytestcase(yyruleno==297); |
| 12511 /* (298) carglist ::= */ yytestcase(yyruleno==298); |
| 12512 /* (299) ccons ::= NULL onconf */ yytestcase(yyruleno==299); |
| 12513 /* (300) conslist_opt ::= COMMA conslist */ yytestcase(yyruleno==300); |
| 12514 /* (301) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==30
1); |
| 12515 /* (302) conslist ::= tcons (OPTIMIZED OUT) */ assert(yyruleno!=302); |
| 12516 /* (303) tconscomma ::= */ yytestcase(yyruleno==303); |
| 12517 /* (304) defer_subclause_opt ::= defer_subclause (OPTIMIZED OUT) */ assert
(yyruleno!=304); |
| 12518 /* (305) resolvetype ::= raisetype (OPTIMIZED OUT) */ assert(yyruleno!=305
); |
| 12519 /* (306) selectnowith ::= oneselect (OPTIMIZED OUT) */ assert(yyruleno!=30
6); |
| 12520 /* (307) oneselect ::= values */ yytestcase(yyruleno==307); |
| 12521 /* (308) sclp ::= selcollist COMMA */ yytestcase(yyruleno==308); |
| 12522 /* (309) as ::= ID|STRING */ yytestcase(yyruleno==309); |
| 12523 /* (310) expr ::= term (OPTIMIZED OUT) */ assert(yyruleno!=310); |
| 12524 /* (311) exprlist ::= nexprlist */ yytestcase(yyruleno==311); |
| 12525 /* (312) nmnum ::= plus_num (OPTIMIZED OUT) */ assert(yyruleno!=312); |
| 12526 /* (313) nmnum ::= nm (OPTIMIZED OUT) */ assert(yyruleno!=313); |
| 12527 /* (314) nmnum ::= ON */ yytestcase(yyruleno==314); |
| 12528 /* (315) nmnum ::= DELETE */ yytestcase(yyruleno==315); |
| 12529 /* (316) nmnum ::= DEFAULT */ yytestcase(yyruleno==316); |
| 12530 /* (317) plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==317); |
| 12531 /* (318) foreach_clause ::= */ yytestcase(yyruleno==318); |
| 12532 /* (319) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==319); |
| 12533 /* (320) trnm ::= nm */ yytestcase(yyruleno==320); |
| 12534 /* (321) tridxby ::= */ yytestcase(yyruleno==321); |
| 12535 /* (322) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==322); |
| 12536 /* (323) database_kw_opt ::= */ yytestcase(yyruleno==323); |
| 12537 /* (324) kwcolumn_opt ::= */ yytestcase(yyruleno==324); |
| 12538 /* (325) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==325); |
| 12539 /* (326) vtabarglist ::= vtabarg */ yytestcase(yyruleno==326); |
| 12540 /* (327) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno=
=327); |
| 12541 /* (328) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==328); |
| 12542 /* (329) anylist ::= */ yytestcase(yyruleno==329); |
| 12543 /* (330) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==330); |
| 12544 /* (331) anylist ::= anylist ANY */ yytestcase(yyruleno==331); |
| 12545 break; |
| 12546 /********** End reduce actions ************************************************/ |
| 12547 }; |
| 12548 assert( yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) ); |
| 12549 yygoto = yyRuleInfo[yyruleno].lhs; |
| 12550 yysize = yyRuleInfo[yyruleno].nrhs; |
| 12551 yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); |
| 12552 if( yyact <= YY_MAX_SHIFTREDUCE ){ |
| 12553 if( yyact>YY_MAX_SHIFT ){ |
| 12554 yyact += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE; |
| 12555 } |
| 12556 yymsp -= yysize-1; |
| 12557 yypParser->yytos = yymsp; |
| 12558 yymsp->stateno = (YYACTIONTYPE)yyact; |
| 12559 yymsp->major = (YYCODETYPE)yygoto; |
| 12560 yyTraceShift(yypParser, yyact); |
| 12561 }else{ |
| 12562 assert( yyact == YY_ACCEPT_ACTION ); |
| 12563 yypParser->yytos -= yysize; |
| 12564 yy_accept(yypParser); |
| 12565 } |
| 12566 } |
| 12567 |
| 12568 /* |
| 12569 ** The following code executes when the parse fails |
| 12570 */ |
| 12571 #ifndef YYNOERRORRECOVERY |
| 12572 static void yy_parse_failed( |
| 12573 yyParser *yypParser /* The parser */ |
| 12574 ){ |
| 12575 sqlite3ParserARG_FETCH; |
| 12576 #ifndef NDEBUG |
| 12577 if( yyTraceFILE ){ |
| 12578 fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); |
| 12579 } |
| 12580 #endif |
| 12581 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser); |
| 12582 /* Here code is inserted which will be executed whenever the |
| 12583 ** parser fails */ |
| 12584 /************ Begin %parse_failure code ***************************************/ |
| 12585 /************ End %parse_failure code *****************************************/ |
| 12586 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument varia
ble */ |
| 12587 } |
| 12588 #endif /* YYNOERRORRECOVERY */ |
| 12589 |
| 12590 /* |
| 12591 ** The following code executes when a syntax error first occurs. |
| 12592 */ |
| 12593 static void yy_syntax_error( |
| 12594 yyParser *yypParser, /* The parser */ |
| 12595 int yymajor, /* The major type of the error token */ |
| 12596 sqlite3ParserTOKENTYPE yyminor /* The minor type of the error token */ |
| 12597 ){ |
| 12598 sqlite3ParserARG_FETCH; |
| 12599 #define TOKEN yyminor |
| 12600 /************ Begin %syntax_error code ****************************************/ |
| 12601 |
| 12602 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ |
| 12603 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */ |
| 12604 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); |
| 12605 /************ End %syntax_error code ******************************************/ |
| 12606 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument varia
ble */ |
| 12607 } |
| 12608 |
| 12609 /* |
| 12610 ** The following is executed when the parser accepts |
| 12611 */ |
| 12612 static void yy_accept( |
| 12613 yyParser *yypParser /* The parser */ |
| 12614 ){ |
| 12615 sqlite3ParserARG_FETCH; |
| 12616 #ifndef NDEBUG |
| 12617 if( yyTraceFILE ){ |
| 12618 fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); |
| 12619 } |
| 12620 #endif |
| 12621 #ifndef YYNOERRORRECOVERY |
| 12622 yypParser->yyerrcnt = -1; |
| 12623 #endif |
| 12624 assert( yypParser->yytos==yypParser->yystack ); |
| 12625 /* Here code is inserted which will be executed whenever the |
| 12626 ** parser accepts */ |
| 12627 /*********** Begin %parse_accept code *****************************************/ |
| 12628 /*********** End %parse_accept code *******************************************/ |
| 12629 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument varia
ble */ |
| 12630 } |
| 12631 |
| 12632 /* The main parser program. |
| 12633 ** The first argument is a pointer to a structure obtained from |
| 12634 ** "sqlite3ParserAlloc" which describes the current state of the parser. |
| 12635 ** The second argument is the major token number. The third is |
| 12636 ** the minor token. The fourth optional argument is whatever the |
| 12637 ** user wants (and specified in the grammar) and is available for |
| 12638 ** use by the action routines. |
| 12639 ** |
| 12640 ** Inputs: |
| 12641 ** <ul> |
| 12642 ** <li> A pointer to the parser (an opaque structure.) |
| 12643 ** <li> The major token number. |
| 12644 ** <li> The minor token number. |
| 12645 ** <li> An option argument of a grammar-specified type. |
| 12646 ** </ul> |
| 12647 ** |
| 12648 ** Outputs: |
| 12649 ** None. |
| 12650 */ |
| 12651 SQLITE_PRIVATE void sqlite3Parser( |
| 12652 void *yyp, /* The parser */ |
| 12653 int yymajor, /* The major token code number */ |
| 12654 sqlite3ParserTOKENTYPE yyminor /* The value for the token */ |
| 12655 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */ |
| 12656 ){ |
| 12657 YYMINORTYPE yyminorunion; |
| 12658 unsigned int yyact; /* The parser action. */ |
| 12659 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) |
| 12660 int yyendofinput; /* True if we are at the end of input */ |
| 12661 #endif |
| 12662 #ifdef YYERRORSYMBOL |
| 12663 int yyerrorhit = 0; /* True if yymajor has invoked an error */ |
| 12664 #endif |
| 12665 yyParser *yypParser; /* The parser */ |
| 12666 |
| 12667 yypParser = (yyParser*)yyp; |
| 12668 assert( yypParser->yytos!=0 ); |
| 12669 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY) |
| 12670 yyendofinput = (yymajor==0); |
| 12671 #endif |
| 12672 sqlite3ParserARG_STORE; |
| 12673 |
| 12674 #ifndef NDEBUG |
| 12675 if( yyTraceFILE ){ |
| 12676 fprintf(yyTraceFILE,"%sInput '%s'\n",yyTracePrompt,yyTokenName[yymajor]); |
| 12677 } |
| 12678 #endif |
| 12679 |
| 12680 do{ |
| 12681 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); |
| 12682 if( yyact <= YY_MAX_SHIFTREDUCE ){ |
| 12683 yy_shift(yypParser,yyact,yymajor,yyminor); |
| 12684 #ifndef YYNOERRORRECOVERY |
| 12685 yypParser->yyerrcnt--; |
| 12686 #endif |
| 12687 yymajor = YYNOCODE; |
| 12688 }else if( yyact <= YY_MAX_REDUCE ){ |
| 12689 yy_reduce(yypParser,yyact-YY_MIN_REDUCE); |
| 12690 }else{ |
| 12691 assert( yyact == YY_ERROR_ACTION ); |
| 12692 yyminorunion.yy0 = yyminor; |
| 12693 #ifdef YYERRORSYMBOL |
| 12694 int yymx; |
| 12695 #endif |
| 12696 #ifndef NDEBUG |
| 12697 if( yyTraceFILE ){ |
| 12698 fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); |
| 12699 } |
| 12700 #endif |
| 12701 #ifdef YYERRORSYMBOL |
| 12702 /* A syntax error has occurred. |
| 12703 ** The response to an error depends upon whether or not the |
| 12704 ** grammar defines an error token "ERROR". |
| 12705 ** |
| 12706 ** This is what we do if the grammar does define ERROR: |
| 12707 ** |
| 12708 ** * Call the %syntax_error function. |
| 12709 ** |
| 12710 ** * Begin popping the stack until we enter a state where |
| 12711 ** it is legal to shift the error symbol, then shift |
| 12712 ** the error symbol. |
| 12713 ** |
| 12714 ** * Set the error count to three. |
| 12715 ** |
| 12716 ** * Begin accepting and shifting new tokens. No new error |
| 12717 ** processing will occur until three tokens have been |
| 12718 ** shifted successfully. |
| 12719 ** |
| 12720 */ |
| 12721 if( yypParser->yyerrcnt<0 ){ |
| 12722 yy_syntax_error(yypParser,yymajor,yyminor); |
| 12723 } |
| 12724 yymx = yypParser->yytos->major; |
| 12725 if( yymx==YYERRORSYMBOL || yyerrorhit ){ |
| 12726 #ifndef NDEBUG |
| 12727 if( yyTraceFILE ){ |
| 12728 fprintf(yyTraceFILE,"%sDiscard input token %s\n", |
| 12729 yyTracePrompt,yyTokenName[yymajor]); |
| 12730 } |
| 12731 #endif |
| 12732 yy_destructor(yypParser, (YYCODETYPE)yymajor, &yyminorunion); |
| 12733 yymajor = YYNOCODE; |
| 12734 }else{ |
| 12735 while( yypParser->yytos >= yypParser->yystack |
| 12736 && yymx != YYERRORSYMBOL |
| 12737 && (yyact = yy_find_reduce_action( |
| 12738 yypParser->yytos->stateno, |
| 12739 YYERRORSYMBOL)) >= YY_MIN_REDUCE |
| 12740 ){ |
| 12741 yy_pop_parser_stack(yypParser); |
| 12742 } |
| 12743 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){ |
| 12744 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); |
| 12745 yy_parse_failed(yypParser); |
| 12746 #ifndef YYNOERRORRECOVERY |
| 12747 yypParser->yyerrcnt = -1; |
| 12748 #endif |
| 12749 yymajor = YYNOCODE; |
| 12750 }else if( yymx!=YYERRORSYMBOL ){ |
| 12751 yy_shift(yypParser,yyact,YYERRORSYMBOL,yyminor); |
| 12752 } |
| 12753 } |
| 12754 yypParser->yyerrcnt = 3; |
| 12755 yyerrorhit = 1; |
| 12756 #elif defined(YYNOERRORRECOVERY) |
| 12757 /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to |
| 12758 ** do any kind of error recovery. Instead, simply invoke the syntax |
| 12759 ** error routine and continue going as if nothing had happened. |
| 12760 ** |
| 12761 ** Applications can set this macro (for example inside %include) if |
| 12762 ** they intend to abandon the parse upon the first syntax error seen. |
| 12763 */ |
| 12764 yy_syntax_error(yypParser,yymajor, yyminor); |
| 12765 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); |
| 12766 yymajor = YYNOCODE; |
| 12767 |
| 12768 #else /* YYERRORSYMBOL is not defined */ |
| 12769 /* This is what we do if the grammar does not define ERROR: |
| 12770 ** |
| 12771 ** * Report an error message, and throw away the input token. |
| 12772 ** |
| 12773 ** * If the input token is $, then fail the parse. |
| 12774 ** |
| 12775 ** As before, subsequent error messages are suppressed until |
| 12776 ** three input tokens have been successfully shifted. |
| 12777 */ |
| 12778 if( yypParser->yyerrcnt<=0 ){ |
| 12779 yy_syntax_error(yypParser,yymajor, yyminor); |
| 12780 } |
| 12781 yypParser->yyerrcnt = 3; |
| 12782 yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); |
| 12783 if( yyendofinput ){ |
| 12784 yy_parse_failed(yypParser); |
| 12785 #ifndef YYNOERRORRECOVERY |
| 12786 yypParser->yyerrcnt = -1; |
| 12787 #endif |
| 12788 } |
| 12789 yymajor = YYNOCODE; |
| 12790 #endif |
| 12791 } |
| 12792 }while( yymajor!=YYNOCODE && yypParser->yytos>yypParser->yystack ); |
| 12793 #ifndef NDEBUG |
| 12794 if( yyTraceFILE ){ |
| 12795 yyStackEntry *i; |
| 12796 char cDiv = '['; |
| 12797 fprintf(yyTraceFILE,"%sReturn. Stack=",yyTracePrompt); |
| 12798 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){ |
| 12799 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]); |
| 12800 cDiv = ' '; |
| 12801 } |
| 12802 fprintf(yyTraceFILE,"]\n"); |
| 12803 } |
| 12804 #endif |
| 12805 return; |
| 12806 } |
| 12807 |
| 12808 /************** End of parse.c ***********************************************/ |
| 12809 /************** Begin file tokenize.c ****************************************/ |
| 12810 /* |
| 12811 ** 2001 September 15 |
| 12812 ** |
| 12813 ** The author disclaims copyright to this source code. In place of |
| 12814 ** a legal notice, here is a blessing: |
| 12815 ** |
| 12816 ** May you do good and not evil. |
| 12817 ** May you find forgiveness for yourself and forgive others. |
| 12818 ** May you share freely, never taking more than you give. |
| 12819 ** |
| 12820 ************************************************************************* |
| 12821 ** An tokenizer for SQL |
| 12822 ** |
| 12823 ** This file contains C code that splits an SQL input string up into |
| 12824 ** individual tokens and sends those tokens one-by-one over to the |
| 12825 ** parser for analysis. |
| 12826 */ |
| 12827 /* #include "sqliteInt.h" */ |
| 12828 /* #include <stdlib.h> */ |
| 12829 |
| 12830 /* Character classes for tokenizing |
| 12831 ** |
| 12832 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented |
| 12833 ** using a lookup table, whereas a switch() directly on c uses a binary search. |
| 12834 ** The lookup table is much faster. To maximize speed, and to ensure that |
| 12835 ** a lookup table is used, all of the classes need to be small integers and |
| 12836 ** all of them need to be used within the switch. |
| 12837 */ |
| 12838 #define CC_X 0 /* The letter 'x', or start of BLOB literal */ |
| 12839 #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ |
| 12840 #define CC_ID 2 /* unicode characters usable in IDs */ |
| 12841 #define CC_DIGIT 3 /* Digits */ |
| 12842 #define CC_DOLLAR 4 /* '$' */ |
| 12843 #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ |
| 12844 #define CC_VARNUM 6 /* '?'. Numeric SQL variables */ |
| 12845 #define CC_SPACE 7 /* Space characters */ |
| 12846 #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ |
| 12847 #define CC_QUOTE2 9 /* '['. [...] style quoted ids */ |
| 12848 #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ |
| 12849 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ |
| 12850 #define CC_LT 12 /* '<'. Part of < or <= or <> */ |
| 12851 #define CC_GT 13 /* '>'. Part of > or >= */ |
| 12852 #define CC_EQ 14 /* '='. Part of = or == */ |
| 12853 #define CC_BANG 15 /* '!'. Part of != */ |
| 12854 #define CC_SLASH 16 /* '/'. / or c-style comment */ |
| 12855 #define CC_LP 17 /* '(' */ |
| 12856 #define CC_RP 18 /* ')' */ |
| 12857 #define CC_SEMI 19 /* ';' */ |
| 12858 #define CC_PLUS 20 /* '+' */ |
| 12859 #define CC_STAR 21 /* '*' */ |
| 12860 #define CC_PERCENT 22 /* '%' */ |
| 12861 #define CC_COMMA 23 /* ',' */ |
| 12862 #define CC_AND 24 /* '&' */ |
| 12863 #define CC_TILDA 25 /* '~' */ |
| 12864 #define CC_DOT 26 /* '.' */ |
| 12865 #define CC_ILLEGAL 27 /* Illegal character */ |
| 12866 |
| 12867 static const unsigned char aiClass[] = { |
| 12868 #ifdef SQLITE_ASCII |
| 12869 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
| 12870 /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, |
| 12871 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 12872 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, |
| 12873 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, |
| 12874 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 12875 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, |
| 12876 /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 12877 /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, |
| 12878 /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12879 /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12880 /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12881 /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12882 /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12883 /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12884 /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 12885 /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 |
| 12886 #endif |
| 12887 #ifdef SQLITE_EBCDIC |
| 12888 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
| 12889 /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27, |
| 12890 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 12891 /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 12892 /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 12893 /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10, |
| 12894 /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27, |
| 12895 /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6, |
| 12896 /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8, |
| 12897 /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 12898 /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 12899 /* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, |
| 12900 /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27, |
| 12901 /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 12902 /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 12903 /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, |
| 12904 /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, |
| 12905 #endif |
| 12906 }; |
| 12907 |
| 12908 /* |
| 12909 ** The charMap() macro maps alphabetic characters (only) into their |
| 12910 ** lower-case ASCII equivalent. On ASCII machines, this is just |
| 12911 ** an upper-to-lower case map. On EBCDIC machines we also need |
| 12912 ** to adjust the encoding. The mapping is only valid for alphabetics |
| 12913 ** which are the only characters for which this feature is used. |
| 12914 ** |
| 12915 ** Used by keywordhash.h |
| 12916 */ |
| 12917 #ifdef SQLITE_ASCII |
| 12918 # define charMap(X) sqlite3UpperToLower[(unsigned char)X] |
| 12919 #endif |
| 12920 #ifdef SQLITE_EBCDIC |
| 12921 # define charMap(X) ebcdicToAscii[(unsigned char)X] |
| 12922 const unsigned char ebcdicToAscii[] = { |
| 12923 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 12924 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ |
| 12925 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ |
| 12926 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
| 12927 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ |
| 12928 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 12929 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ |
| 12930 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ |
| 12931 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 12932 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ |
| 12933 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ |
| 12934 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ |
| 12935 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 12936 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ |
| 12937 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ |
| 12938 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ |
| 12939 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ |
| 12940 }; |
| 12941 #endif |
| 12942 |
| 12943 /* |
| 12944 ** The sqlite3KeywordCode function looks up an identifier to determine if |
| 12945 ** it is a keyword. If it is a keyword, the token code of that keyword is |
| 12946 ** returned. If the input is not a keyword, TK_ID is returned. |
| 12947 ** |
| 12948 ** The implementation of this routine was generated by a program, |
| 12949 ** mkkeywordhash.c, located in the tool subdirectory of the distribution. |
| 12950 ** The output of the mkkeywordhash.c program is written into a file |
| 12951 ** named keywordhash.h and then included into this source file by |
| 12952 ** the #include below. |
| 12953 */ |
| 12954 /************** Include keywordhash.h in the middle of tokenize.c ************/ |
| 12955 /************** Begin file keywordhash.h *************************************/ |
| 12956 /***** This file contains automatically generated code ****** |
| 12957 ** |
| 12958 ** The code in this file has been automatically generated by |
| 12959 ** |
| 12960 ** sqlite/tool/mkkeywordhash.c |
| 12961 ** |
| 12962 ** The code in this file implements a function that determines whether |
| 12963 ** or not a given identifier is really an SQL keyword. The same thing |
| 12964 ** might be implemented more directly using a hand-written hash table. |
| 12965 ** But by using this automatically generated code, the size of the code |
| 12966 ** is substantially reduced. This is important for embedded applications |
| 12967 ** on platforms with limited memory. |
| 12968 */ |
| 12969 /* Hash score: 182 */ |
| 12970 static int keywordCode(const char *z, int n, int *pType){ |
| 12971 /* zText[] encodes 834 bytes of keywords in 554 bytes */ |
| 12972 /* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */ |
| 12973 /* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */ |
| 12974 /* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */ |
| 12975 /* UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE */ |
| 12976 /* BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH */ |
| 12977 /* IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN */ |
| 12978 /* WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT */ |
| 12979 /* CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL */ |
| 12980 /* FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING */ |
| 12981 /* VACUUMVIEWINITIALLY */ |
| 12982 static const char zText[553] = { |
| 12983 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H', |
| 12984 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G', |
| 12985 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A', |
| 12986 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F', |
| 12987 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N', |
| 12988 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I', |
| 12989 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E', |
| 12990 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E', |
| 12991 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T', |
| 12992 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q', |
| 12993 'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S', |
| 12994 'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A', |
| 12995 'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E', |
| 12996 'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A', |
| 12997 'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A', |
| 12998 'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A', |
| 12999 'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J', |
| 13000 'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L', |
| 13001 'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E', |
| 13002 'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H', |
| 13003 'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E', |
| 13004 'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E', |
| 13005 'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M', |
| 13006 'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R', |
| 13007 'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A', |
| 13008 'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D', |
| 13009 'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O', |
| 13010 'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T', |
| 13011 'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R', |
| 13012 'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M', |
| 13013 'V','I','E','W','I','N','I','T','I','A','L','L','Y', |
| 13014 }; |
| 13015 static const unsigned char aHash[127] = { |
| 13016 76, 105, 117, 74, 0, 45, 0, 0, 82, 0, 77, 0, 0, |
| 13017 42, 12, 78, 15, 0, 116, 85, 54, 112, 0, 19, 0, 0, |
| 13018 121, 0, 119, 115, 0, 22, 93, 0, 9, 0, 0, 70, 71, |
| 13019 0, 69, 6, 0, 48, 90, 102, 0, 118, 101, 0, 0, 44, |
| 13020 0, 103, 24, 0, 17, 0, 122, 53, 23, 0, 5, 110, 25, |
| 13021 96, 0, 0, 124, 106, 60, 123, 57, 28, 55, 0, 91, 0, |
| 13022 100, 26, 0, 99, 0, 0, 0, 95, 92, 97, 88, 109, 14, |
| 13023 39, 108, 0, 81, 0, 18, 89, 111, 32, 0, 120, 80, 113, |
| 13024 62, 46, 84, 0, 0, 94, 40, 59, 114, 0, 36, 0, 0, |
| 13025 29, 0, 86, 63, 64, 0, 20, 61, 0, 56, |
| 13026 }; |
| 13027 static const unsigned char aNext[124] = { |
| 13028 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, |
| 13029 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, |
| 13030 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 13031 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 0, 0, 50, |
| 13032 0, 43, 3, 47, 0, 0, 0, 0, 30, 0, 58, 0, 38, |
| 13033 0, 0, 0, 1, 66, 0, 0, 67, 0, 41, 0, 0, 0, |
| 13034 0, 0, 0, 49, 65, 0, 0, 0, 0, 31, 52, 16, 34, |
| 13035 10, 0, 0, 0, 0, 0, 0, 0, 11, 72, 79, 0, 8, |
| 13036 0, 104, 98, 0, 107, 0, 87, 0, 75, 51, 0, 27, 37, |
| 13037 73, 83, 0, 35, 68, 0, 0, |
| 13038 }; |
| 13039 static const unsigned char aLen[124] = { |
| 13040 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6, |
| 13041 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6, |
| 13042 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10, |
| 13043 4, 6, 2, 3, 9, 4, 2, 6, 5, 7, 4, 5, 7, |
| 13044 6, 6, 5, 6, 5, 5, 9, 7, 7, 3, 2, 4, 4, |
| 13045 7, 3, 6, 4, 7, 6, 12, 6, 9, 4, 6, 5, 4, |
| 13046 7, 6, 5, 6, 7, 5, 4, 5, 6, 5, 7, 3, 7, |
| 13047 13, 2, 2, 4, 6, 6, 8, 5, 17, 12, 7, 8, 8, |
| 13048 2, 4, 4, 4, 4, 4, 2, 2, 6, 5, 8, 5, 8, |
| 13049 3, 5, 5, 6, 4, 9, 3, |
| 13050 }; |
| 13051 static const unsigned short int aOffset[124] = { |
| 13052 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33, |
| 13053 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81, |
| 13054 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152, |
| 13055 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192, |
| 13056 199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246, |
| 13057 250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318, |
| 13058 320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380, |
| 13059 387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459, |
| 13060 460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513, |
| 13061 521, 524, 529, 534, 540, 544, 549, |
| 13062 }; |
| 13063 static const unsigned char aCode[124] = { |
| 13064 TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE, |
| 13065 TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN, |
| 13066 TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD, |
| 13067 TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE, |
| 13068 TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE, |
| 13069 TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW, |
| 13070 TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT, |
| 13071 TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO, |
| 13072 TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP, |
| 13073 TK_OR, TK_UNIQUE, TK_QUERY, TK_WITHOUT, TK_WITH, |
| 13074 TK_JOIN_KW, TK_RELEASE, TK_ATTACH, TK_HAVING, TK_GROUP, |
| 13075 TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RECURSIVE, TK_BETWEEN, |
| 13076 TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, TK_LIKE_KW, |
| 13077 TK_CASCADE, TK_ASC, TK_DELETE, TK_CASE, TK_COLLATE, |
| 13078 TK_CREATE, TK_CTIME_KW, TK_DETACH, TK_IMMEDIATE, TK_JOIN, |
| 13079 TK_INSERT, TK_MATCH, TK_PLAN, TK_ANALYZE, TK_PRAGMA, |
| 13080 TK_ABORT, TK_VALUES, TK_VIRTUAL, TK_LIMIT, TK_WHEN, |
| 13081 TK_WHERE, TK_RENAME, TK_AFTER, TK_REPLACE, TK_AND, |
| 13082 TK_DEFAULT, TK_AUTOINCR, TK_TO, TK_IN, TK_CAST, |
| 13083 TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, TK_JOIN_KW, TK_CTIME_KW, |
| 13084 TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED, TK_DISTINCT, TK_IS, |
| 13085 TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, TK_LIKE_KW, |
| 13086 TK_BY, TK_IF, TK_ISNULL, TK_ORDER, TK_RESTRICT, |
| 13087 TK_JOIN_KW, TK_ROLLBACK, TK_ROW, TK_UNION, TK_USING, |
| 13088 TK_VACUUM, TK_VIEW, TK_INITIALLY, TK_ALL, |
| 13089 }; |
| 13090 int i, j; |
| 13091 const char *zKW; |
| 13092 if( n>=2 ){ |
| 13093 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127; |
| 13094 for(i=((int)aHash[i])-1; i>=0; i=((int)aNext[i])-1){ |
| 13095 if( aLen[i]!=n ) continue; |
| 13096 j = 0; |
| 13097 zKW = &zText[aOffset[i]]; |
| 13098 #ifdef SQLITE_ASCII |
| 13099 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; } |
| 13100 #endif |
| 13101 #ifdef SQLITE_EBCDIC |
| 13102 while( j<n && toupper(z[j])==zKW[j] ){ j++; } |
| 13103 #endif |
| 13104 if( j<n ) continue; |
| 13105 testcase( i==0 ); /* REINDEX */ |
| 13106 testcase( i==1 ); /* INDEXED */ |
| 13107 testcase( i==2 ); /* INDEX */ |
| 13108 testcase( i==3 ); /* DESC */ |
| 13109 testcase( i==4 ); /* ESCAPE */ |
| 13110 testcase( i==5 ); /* EACH */ |
| 13111 testcase( i==6 ); /* CHECK */ |
| 13112 testcase( i==7 ); /* KEY */ |
| 13113 testcase( i==8 ); /* BEFORE */ |
| 13114 testcase( i==9 ); /* FOREIGN */ |
| 13115 testcase( i==10 ); /* FOR */ |
| 13116 testcase( i==11 ); /* IGNORE */ |
| 13117 testcase( i==12 ); /* REGEXP */ |
| 13118 testcase( i==13 ); /* EXPLAIN */ |
| 13119 testcase( i==14 ); /* INSTEAD */ |
| 13120 testcase( i==15 ); /* ADD */ |
| 13121 testcase( i==16 ); /* DATABASE */ |
| 13122 testcase( i==17 ); /* AS */ |
| 13123 testcase( i==18 ); /* SELECT */ |
| 13124 testcase( i==19 ); /* TABLE */ |
| 13125 testcase( i==20 ); /* LEFT */ |
| 13126 testcase( i==21 ); /* THEN */ |
| 13127 testcase( i==22 ); /* END */ |
| 13128 testcase( i==23 ); /* DEFERRABLE */ |
| 13129 testcase( i==24 ); /* ELSE */ |
| 13130 testcase( i==25 ); /* EXCEPT */ |
| 13131 testcase( i==26 ); /* TRANSACTION */ |
| 13132 testcase( i==27 ); /* ACTION */ |
| 13133 testcase( i==28 ); /* ON */ |
| 13134 testcase( i==29 ); /* NATURAL */ |
| 13135 testcase( i==30 ); /* ALTER */ |
| 13136 testcase( i==31 ); /* RAISE */ |
| 13137 testcase( i==32 ); /* EXCLUSIVE */ |
| 13138 testcase( i==33 ); /* EXISTS */ |
| 13139 testcase( i==34 ); /* SAVEPOINT */ |
| 13140 testcase( i==35 ); /* INTERSECT */ |
| 13141 testcase( i==36 ); /* TRIGGER */ |
| 13142 testcase( i==37 ); /* REFERENCES */ |
| 13143 testcase( i==38 ); /* CONSTRAINT */ |
| 13144 testcase( i==39 ); /* INTO */ |
| 13145 testcase( i==40 ); /* OFFSET */ |
| 13146 testcase( i==41 ); /* OF */ |
| 13147 testcase( i==42 ); /* SET */ |
| 13148 testcase( i==43 ); /* TEMPORARY */ |
| 13149 testcase( i==44 ); /* TEMP */ |
| 13150 testcase( i==45 ); /* OR */ |
| 13151 testcase( i==46 ); /* UNIQUE */ |
| 13152 testcase( i==47 ); /* QUERY */ |
| 13153 testcase( i==48 ); /* WITHOUT */ |
| 13154 testcase( i==49 ); /* WITH */ |
| 13155 testcase( i==50 ); /* OUTER */ |
| 13156 testcase( i==51 ); /* RELEASE */ |
| 13157 testcase( i==52 ); /* ATTACH */ |
| 13158 testcase( i==53 ); /* HAVING */ |
| 13159 testcase( i==54 ); /* GROUP */ |
| 13160 testcase( i==55 ); /* UPDATE */ |
| 13161 testcase( i==56 ); /* BEGIN */ |
| 13162 testcase( i==57 ); /* INNER */ |
| 13163 testcase( i==58 ); /* RECURSIVE */ |
| 13164 testcase( i==59 ); /* BETWEEN */ |
| 13165 testcase( i==60 ); /* NOTNULL */ |
| 13166 testcase( i==61 ); /* NOT */ |
| 13167 testcase( i==62 ); /* NO */ |
| 13168 testcase( i==63 ); /* NULL */ |
| 13169 testcase( i==64 ); /* LIKE */ |
| 13170 testcase( i==65 ); /* CASCADE */ |
| 13171 testcase( i==66 ); /* ASC */ |
| 13172 testcase( i==67 ); /* DELETE */ |
| 13173 testcase( i==68 ); /* CASE */ |
| 13174 testcase( i==69 ); /* COLLATE */ |
| 13175 testcase( i==70 ); /* CREATE */ |
| 13176 testcase( i==71 ); /* CURRENT_DATE */ |
| 13177 testcase( i==72 ); /* DETACH */ |
| 13178 testcase( i==73 ); /* IMMEDIATE */ |
| 13179 testcase( i==74 ); /* JOIN */ |
| 13180 testcase( i==75 ); /* INSERT */ |
| 13181 testcase( i==76 ); /* MATCH */ |
| 13182 testcase( i==77 ); /* PLAN */ |
| 13183 testcase( i==78 ); /* ANALYZE */ |
| 13184 testcase( i==79 ); /* PRAGMA */ |
| 13185 testcase( i==80 ); /* ABORT */ |
| 13186 testcase( i==81 ); /* VALUES */ |
| 13187 testcase( i==82 ); /* VIRTUAL */ |
| 13188 testcase( i==83 ); /* LIMIT */ |
| 13189 testcase( i==84 ); /* WHEN */ |
| 13190 testcase( i==85 ); /* WHERE */ |
| 13191 testcase( i==86 ); /* RENAME */ |
| 13192 testcase( i==87 ); /* AFTER */ |
| 13193 testcase( i==88 ); /* REPLACE */ |
| 13194 testcase( i==89 ); /* AND */ |
| 13195 testcase( i==90 ); /* DEFAULT */ |
| 13196 testcase( i==91 ); /* AUTOINCREMENT */ |
| 13197 testcase( i==92 ); /* TO */ |
| 13198 testcase( i==93 ); /* IN */ |
| 13199 testcase( i==94 ); /* CAST */ |
| 13200 testcase( i==95 ); /* COLUMN */ |
| 13201 testcase( i==96 ); /* COMMIT */ |
| 13202 testcase( i==97 ); /* CONFLICT */ |
| 13203 testcase( i==98 ); /* CROSS */ |
| 13204 testcase( i==99 ); /* CURRENT_TIMESTAMP */ |
| 13205 testcase( i==100 ); /* CURRENT_TIME */ |
| 13206 testcase( i==101 ); /* PRIMARY */ |
| 13207 testcase( i==102 ); /* DEFERRED */ |
| 13208 testcase( i==103 ); /* DISTINCT */ |
| 13209 testcase( i==104 ); /* IS */ |
| 13210 testcase( i==105 ); /* DROP */ |
| 13211 testcase( i==106 ); /* FAIL */ |
| 13212 testcase( i==107 ); /* FROM */ |
| 13213 testcase( i==108 ); /* FULL */ |
| 13214 testcase( i==109 ); /* GLOB */ |
| 13215 testcase( i==110 ); /* BY */ |
| 13216 testcase( i==111 ); /* IF */ |
| 13217 testcase( i==112 ); /* ISNULL */ |
| 13218 testcase( i==113 ); /* ORDER */ |
| 13219 testcase( i==114 ); /* RESTRICT */ |
| 13220 testcase( i==115 ); /* RIGHT */ |
| 13221 testcase( i==116 ); /* ROLLBACK */ |
| 13222 testcase( i==117 ); /* ROW */ |
| 13223 testcase( i==118 ); /* UNION */ |
| 13224 testcase( i==119 ); /* USING */ |
| 13225 testcase( i==120 ); /* VACUUM */ |
| 13226 testcase( i==121 ); /* VIEW */ |
| 13227 testcase( i==122 ); /* INITIALLY */ |
| 13228 testcase( i==123 ); /* ALL */ |
| 13229 *pType = aCode[i]; |
| 13230 break; |
| 13231 } |
| 13232 } |
| 13233 return n; |
| 13234 } |
| 13235 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){ |
| 13236 int id = TK_ID; |
| 13237 keywordCode((char*)z, n, &id); |
| 13238 return id; |
| 13239 } |
| 13240 #define SQLITE_N_KEYWORD 124 |
| 13241 |
| 13242 /************** End of keywordhash.h *****************************************/ |
| 13243 /************** Continuing where we left off in tokenize.c *******************/ |
| 13244 |
| 13245 |
| 13246 /* |
| 13247 ** If X is a character that can be used in an identifier then |
| 13248 ** IdChar(X) will be true. Otherwise it is false. |
| 13249 ** |
| 13250 ** For ASCII, any character with the high-order bit set is |
| 13251 ** allowed in an identifier. For 7-bit characters, |
| 13252 ** sqlite3IsIdChar[X] must be 1. |
| 13253 ** |
| 13254 ** For EBCDIC, the rules are more complex but have the same |
| 13255 ** end result. |
| 13256 ** |
| 13257 ** Ticket #1066. the SQL standard does not allow '$' in the |
| 13258 ** middle of identifiers. But many SQL implementations do. |
| 13259 ** SQLite will allow '$' in identifiers for compatibility. |
| 13260 ** But the feature is undocumented. |
| 13261 */ |
| 13262 #ifdef SQLITE_ASCII |
| 13263 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) |
| 13264 #endif |
| 13265 #ifdef SQLITE_EBCDIC |
| 13266 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = { |
| 13267 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
| 13268 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 13269 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ |
| 13270 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ |
| 13271 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 13272 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ |
| 13273 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ |
| 13274 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ |
| 13275 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 13276 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ |
| 13277 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ |
| 13278 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 13279 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 13280 }; |
| 13281 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
| 13282 #endif |
| 13283 |
| 13284 /* Make the IdChar function accessible from ctime.c */ |
| 13285 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 13286 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
| 13287 #endif |
| 13288 |
| 13289 |
| 13290 /* |
| 13291 ** Return the length (in bytes) of the token that begins at z[0]. |
| 13292 ** Store the token type in *tokenType before returning. |
| 13293 */ |
| 13294 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){ |
| 13295 int i, c; |
| 13296 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte |
| 13297 ** of the token. See the comment on the CC_ defines |
| 13298 ** above. */ |
| 13299 case CC_SPACE: { |
| 13300 testcase( z[0]==' ' ); |
| 13301 testcase( z[0]=='\t' ); |
| 13302 testcase( z[0]=='\n' ); |
| 13303 testcase( z[0]=='\f' ); |
| 13304 testcase( z[0]=='\r' ); |
| 13305 for(i=1; sqlite3Isspace(z[i]); i++){} |
| 13306 *tokenType = TK_SPACE; |
| 13307 return i; |
| 13308 } |
| 13309 case CC_MINUS: { |
| 13310 if( z[1]=='-' ){ |
| 13311 for(i=2; (c=z[i])!=0 && c!='\n'; i++){} |
| 13312 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
| 13313 return i; |
| 13314 } |
| 13315 *tokenType = TK_MINUS; |
| 13316 return 1; |
| 13317 } |
| 13318 case CC_LP: { |
| 13319 *tokenType = TK_LP; |
| 13320 return 1; |
| 13321 } |
| 13322 case CC_RP: { |
| 13323 *tokenType = TK_RP; |
| 13324 return 1; |
| 13325 } |
| 13326 case CC_SEMI: { |
| 13327 *tokenType = TK_SEMI; |
| 13328 return 1; |
| 13329 } |
| 13330 case CC_PLUS: { |
| 13331 *tokenType = TK_PLUS; |
| 13332 return 1; |
| 13333 } |
| 13334 case CC_STAR: { |
| 13335 *tokenType = TK_STAR; |
| 13336 return 1; |
| 13337 } |
| 13338 case CC_SLASH: { |
| 13339 if( z[1]!='*' || z[2]==0 ){ |
| 13340 *tokenType = TK_SLASH; |
| 13341 return 1; |
| 13342 } |
| 13343 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} |
| 13344 if( c ) i++; |
| 13345 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
| 13346 return i; |
| 13347 } |
| 13348 case CC_PERCENT: { |
| 13349 *tokenType = TK_REM; |
| 13350 return 1; |
| 13351 } |
| 13352 case CC_EQ: { |
| 13353 *tokenType = TK_EQ; |
| 13354 return 1 + (z[1]=='='); |
| 13355 } |
| 13356 case CC_LT: { |
| 13357 if( (c=z[1])=='=' ){ |
| 13358 *tokenType = TK_LE; |
| 13359 return 2; |
| 13360 }else if( c=='>' ){ |
| 13361 *tokenType = TK_NE; |
| 13362 return 2; |
| 13363 }else if( c=='<' ){ |
| 13364 *tokenType = TK_LSHIFT; |
| 13365 return 2; |
| 13366 }else{ |
| 13367 *tokenType = TK_LT; |
| 13368 return 1; |
| 13369 } |
| 13370 } |
| 13371 case CC_GT: { |
| 13372 if( (c=z[1])=='=' ){ |
| 13373 *tokenType = TK_GE; |
| 13374 return 2; |
| 13375 }else if( c=='>' ){ |
| 13376 *tokenType = TK_RSHIFT; |
| 13377 return 2; |
| 13378 }else{ |
| 13379 *tokenType = TK_GT; |
| 13380 return 1; |
| 13381 } |
| 13382 } |
| 13383 case CC_BANG: { |
| 13384 if( z[1]!='=' ){ |
| 13385 *tokenType = TK_ILLEGAL; |
| 13386 return 1; |
| 13387 }else{ |
| 13388 *tokenType = TK_NE; |
| 13389 return 2; |
| 13390 } |
| 13391 } |
| 13392 case CC_PIPE: { |
| 13393 if( z[1]!='|' ){ |
| 13394 *tokenType = TK_BITOR; |
| 13395 return 1; |
| 13396 }else{ |
| 13397 *tokenType = TK_CONCAT; |
| 13398 return 2; |
| 13399 } |
| 13400 } |
| 13401 case CC_COMMA: { |
| 13402 *tokenType = TK_COMMA; |
| 13403 return 1; |
| 13404 } |
| 13405 case CC_AND: { |
| 13406 *tokenType = TK_BITAND; |
| 13407 return 1; |
| 13408 } |
| 13409 case CC_TILDA: { |
| 13410 *tokenType = TK_BITNOT; |
| 13411 return 1; |
| 13412 } |
| 13413 case CC_QUOTE: { |
| 13414 int delim = z[0]; |
| 13415 testcase( delim=='`' ); |
| 13416 testcase( delim=='\'' ); |
| 13417 testcase( delim=='"' ); |
| 13418 for(i=1; (c=z[i])!=0; i++){ |
| 13419 if( c==delim ){ |
| 13420 if( z[i+1]==delim ){ |
| 13421 i++; |
| 13422 }else{ |
| 13423 break; |
| 13424 } |
| 13425 } |
| 13426 } |
| 13427 if( c=='\'' ){ |
| 13428 *tokenType = TK_STRING; |
| 13429 return i+1; |
| 13430 }else if( c!=0 ){ |
| 13431 *tokenType = TK_ID; |
| 13432 return i+1; |
| 13433 }else{ |
| 13434 *tokenType = TK_ILLEGAL; |
| 13435 return i; |
| 13436 } |
| 13437 } |
| 13438 case CC_DOT: { |
| 13439 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 13440 if( !sqlite3Isdigit(z[1]) ) |
| 13441 #endif |
| 13442 { |
| 13443 *tokenType = TK_DOT; |
| 13444 return 1; |
| 13445 } |
| 13446 /* If the next character is a digit, this is a floating point |
| 13447 ** number that begins with ".". Fall thru into the next case */ |
| 13448 } |
| 13449 case CC_DIGIT: { |
| 13450 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); |
| 13451 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); |
| 13452 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); |
| 13453 testcase( z[0]=='9' ); |
| 13454 *tokenType = TK_INTEGER; |
| 13455 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 13456 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ |
| 13457 for(i=3; sqlite3Isxdigit(z[i]); i++){} |
| 13458 return i; |
| 13459 } |
| 13460 #endif |
| 13461 for(i=0; sqlite3Isdigit(z[i]); i++){} |
| 13462 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 13463 if( z[i]=='.' ){ |
| 13464 i++; |
| 13465 while( sqlite3Isdigit(z[i]) ){ i++; } |
| 13466 *tokenType = TK_FLOAT; |
| 13467 } |
| 13468 if( (z[i]=='e' || z[i]=='E') && |
| 13469 ( sqlite3Isdigit(z[i+1]) |
| 13470 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) |
| 13471 ) |
| 13472 ){ |
| 13473 i += 2; |
| 13474 while( sqlite3Isdigit(z[i]) ){ i++; } |
| 13475 *tokenType = TK_FLOAT; |
| 13476 } |
| 13477 #endif |
| 13478 while( IdChar(z[i]) ){ |
| 13479 *tokenType = TK_ILLEGAL; |
| 13480 i++; |
| 13481 } |
| 13482 return i; |
| 13483 } |
| 13484 case CC_QUOTE2: { |
| 13485 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} |
| 13486 *tokenType = c==']' ? TK_ID : TK_ILLEGAL; |
| 13487 return i; |
| 13488 } |
| 13489 case CC_VARNUM: { |
| 13490 *tokenType = TK_VARIABLE; |
| 13491 for(i=1; sqlite3Isdigit(z[i]); i++){} |
| 13492 return i; |
| 13493 } |
| 13494 case CC_DOLLAR: |
| 13495 case CC_VARALPHA: { |
| 13496 int n = 0; |
| 13497 testcase( z[0]=='$' ); testcase( z[0]=='@' ); |
| 13498 testcase( z[0]==':' ); testcase( z[0]=='#' ); |
| 13499 *tokenType = TK_VARIABLE; |
| 13500 for(i=1; (c=z[i])!=0; i++){ |
| 13501 if( IdChar(c) ){ |
| 13502 n++; |
| 13503 #ifndef SQLITE_OMIT_TCL_VARIABLE |
| 13504 }else if( c=='(' && n>0 ){ |
| 13505 do{ |
| 13506 i++; |
| 13507 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); |
| 13508 if( c==')' ){ |
| 13509 i++; |
| 13510 }else{ |
| 13511 *tokenType = TK_ILLEGAL; |
| 13512 } |
| 13513 break; |
| 13514 }else if( c==':' && z[i+1]==':' ){ |
| 13515 i++; |
| 13516 #endif |
| 13517 }else{ |
| 13518 break; |
| 13519 } |
| 13520 } |
| 13521 if( n==0 ) *tokenType = TK_ILLEGAL; |
| 13522 return i; |
| 13523 } |
| 13524 case CC_KYWD: { |
| 13525 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} |
| 13526 if( IdChar(z[i]) ){ |
| 13527 /* This token started out using characters that can appear in keywords, |
| 13528 ** but z[i] is a character not allowed within keywords, so this must |
| 13529 ** be an identifier instead */ |
| 13530 i++; |
| 13531 break; |
| 13532 } |
| 13533 *tokenType = TK_ID; |
| 13534 return keywordCode((char*)z, i, tokenType); |
| 13535 } |
| 13536 case CC_X: { |
| 13537 #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 13538 testcase( z[0]=='x' ); testcase( z[0]=='X' ); |
| 13539 if( z[1]=='\'' ){ |
| 13540 *tokenType = TK_BLOB; |
| 13541 for(i=2; sqlite3Isxdigit(z[i]); i++){} |
| 13542 if( z[i]!='\'' || i%2 ){ |
| 13543 *tokenType = TK_ILLEGAL; |
| 13544 while( z[i] && z[i]!='\'' ){ i++; } |
| 13545 } |
| 13546 if( z[i] ) i++; |
| 13547 return i; |
| 13548 } |
| 13549 #endif |
| 13550 /* If it is not a BLOB literal, then it must be an ID, since no |
| 13551 ** SQL keywords start with the letter 'x'. Fall through */ |
| 13552 } |
| 13553 case CC_ID: { |
| 13554 i = 1; |
| 13555 break; |
| 13556 } |
| 13557 default: { |
| 13558 *tokenType = TK_ILLEGAL; |
| 13559 return 1; |
| 13560 } |
| 13561 } |
| 13562 while( IdChar(z[i]) ){ i++; } |
| 13563 *tokenType = TK_ID; |
| 13564 return i; |
| 13565 } |
| 13566 |
| 13567 /* |
| 13568 ** Run the parser on the given SQL string. The parser structure is |
| 13569 ** passed in. An SQLITE_ status code is returned. If an error occurs |
| 13570 ** then an and attempt is made to write an error message into |
| 13571 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that |
| 13572 ** error message. |
| 13573 */ |
| 13574 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzEr
rMsg){ |
| 13575 int nErr = 0; /* Number of errors encountered */ |
| 13576 int i; /* Loop counter */ |
| 13577 void *pEngine; /* The LEMON-generated LALR(1) parser */ |
| 13578 int tokenType; /* type of the next token */ |
| 13579 int lastTokenParsed = -1; /* type of the previous token */ |
| 13580 sqlite3 *db = pParse->db; /* The database connection */ |
| 13581 int mxSqlLen; /* Max length of an SQL string */ |
| 13582 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 13583 unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */ |
| 13584 #endif |
| 13585 |
| 13586 assert( zSql!=0 ); |
| 13587 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 13588 if( db->nVdbeActive==0 ){ |
| 13589 db->u1.isInterrupted = 0; |
| 13590 } |
| 13591 pParse->rc = SQLITE_OK; |
| 13592 pParse->zTail = zSql; |
| 13593 i = 0; |
| 13594 assert( pzErrMsg!=0 ); |
| 13595 /* sqlite3ParserTrace(stdout, "parser: "); */ |
| 13596 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 13597 pEngine = zSpace; |
| 13598 sqlite3ParserInit(pEngine); |
| 13599 #else |
| 13600 pEngine = sqlite3ParserAlloc(sqlite3Malloc); |
| 13601 if( pEngine==0 ){ |
| 13602 sqlite3OomFault(db); |
| 13603 return SQLITE_NOMEM_BKPT; |
| 13604 } |
| 13605 #endif |
| 13606 assert( pParse->pNewTable==0 ); |
| 13607 assert( pParse->pNewTrigger==0 ); |
| 13608 assert( pParse->nVar==0 ); |
| 13609 assert( pParse->pVList==0 ); |
| 13610 while( 1 ){ |
| 13611 assert( i>=0 ); |
| 13612 if( zSql[i]!=0 ){ |
| 13613 pParse->sLastToken.z = &zSql[i]; |
| 13614 pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType); |
| 13615 i += pParse->sLastToken.n; |
| 13616 if( i>mxSqlLen ){ |
| 13617 pParse->rc = SQLITE_TOOBIG; |
| 13618 break; |
| 13619 } |
| 13620 }else{ |
| 13621 /* Upon reaching the end of input, call the parser two more times |
| 13622 ** with tokens TK_SEMI and 0, in that order. */ |
| 13623 if( lastTokenParsed==TK_SEMI ){ |
| 13624 tokenType = 0; |
| 13625 }else if( lastTokenParsed==0 ){ |
| 13626 break; |
| 13627 }else{ |
| 13628 tokenType = TK_SEMI; |
| 13629 } |
| 13630 } |
| 13631 if( tokenType>=TK_SPACE ){ |
| 13632 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); |
| 13633 if( db->u1.isInterrupted ){ |
| 13634 pParse->rc = SQLITE_INTERRUPT; |
| 13635 break; |
| 13636 } |
| 13637 if( tokenType==TK_ILLEGAL ){ |
| 13638 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", |
| 13639 &pParse->sLastToken); |
| 13640 break; |
| 13641 } |
| 13642 }else{ |
| 13643 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); |
| 13644 lastTokenParsed = tokenType; |
| 13645 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; |
| 13646 } |
| 13647 } |
| 13648 assert( nErr==0 ); |
| 13649 pParse->zTail = &zSql[i]; |
| 13650 #ifdef YYTRACKMAXSTACKDEPTH |
| 13651 sqlite3_mutex_enter(sqlite3MallocMutex()); |
| 13652 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, |
| 13653 sqlite3ParserStackPeak(pEngine) |
| 13654 ); |
| 13655 sqlite3_mutex_leave(sqlite3MallocMutex()); |
| 13656 #endif /* YYDEBUG */ |
| 13657 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 13658 sqlite3ParserFinalize(pEngine); |
| 13659 #else |
| 13660 sqlite3ParserFree(pEngine, sqlite3_free); |
| 13661 #endif |
| 13662 if( db->mallocFailed ){ |
| 13663 pParse->rc = SQLITE_NOMEM_BKPT; |
| 13664 } |
| 13665 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
| 13666 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 13667 } |
| 13668 assert( pzErrMsg!=0 ); |
| 13669 if( pParse->zErrMsg ){ |
| 13670 *pzErrMsg = pParse->zErrMsg; |
| 13671 sqlite3_log(pParse->rc, "%s", *pzErrMsg); |
| 13672 pParse->zErrMsg = 0; |
| 13673 nErr++; |
| 13674 } |
| 13675 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ |
| 13676 sqlite3VdbeDelete(pParse->pVdbe); |
| 13677 pParse->pVdbe = 0; |
| 13678 } |
| 13679 #ifndef SQLITE_OMIT_SHARED_CACHE |
| 13680 if( pParse->nested==0 ){ |
| 13681 sqlite3DbFree(db, pParse->aTableLock); |
| 13682 pParse->aTableLock = 0; |
| 13683 pParse->nTableLock = 0; |
| 13684 } |
| 13685 #endif |
| 13686 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 13687 sqlite3_free(pParse->apVtabLock); |
| 13688 #endif |
| 13689 |
| 13690 if( !IN_DECLARE_VTAB ){ |
| 13691 /* If the pParse->declareVtab flag is set, do not delete any table |
| 13692 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) |
| 13693 ** will take responsibility for freeing the Table structure. |
| 13694 */ |
| 13695 sqlite3DeleteTable(db, pParse->pNewTable); |
| 13696 } |
| 13697 |
| 13698 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree); |
| 13699 sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 13700 sqlite3DbFree(db, pParse->pVList); |
| 13701 while( pParse->pAinc ){ |
| 13702 AutoincInfo *p = pParse->pAinc; |
| 13703 pParse->pAinc = p->pNext; |
| 13704 sqlite3DbFree(db, p); |
| 13705 } |
| 13706 while( pParse->pZombieTab ){ |
| 13707 Table *p = pParse->pZombieTab; |
| 13708 pParse->pZombieTab = p->pNextZombie; |
| 13709 sqlite3DeleteTable(db, p); |
| 13710 } |
| 13711 assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
| 13712 return nErr; |
| 13713 } |
| 13714 |
| 13715 /************** End of tokenize.c ********************************************/ |
| 13716 /************** Begin file complete.c ****************************************/ |
| 13717 /* |
| 13718 ** 2001 September 15 |
| 13719 ** |
| 13720 ** The author disclaims copyright to this source code. In place of |
| 13721 ** a legal notice, here is a blessing: |
| 13722 ** |
| 13723 ** May you do good and not evil. |
| 13724 ** May you find forgiveness for yourself and forgive others. |
| 13725 ** May you share freely, never taking more than you give. |
| 13726 ** |
| 13727 ************************************************************************* |
| 13728 ** An tokenizer for SQL |
| 13729 ** |
| 13730 ** This file contains C code that implements the sqlite3_complete() API. |
| 13731 ** This code used to be part of the tokenizer.c source file. But by |
| 13732 ** separating it out, the code will be automatically omitted from |
| 13733 ** static links that do not use it. |
| 13734 */ |
| 13735 /* #include "sqliteInt.h" */ |
| 13736 #ifndef SQLITE_OMIT_COMPLETE |
| 13737 |
| 13738 /* |
| 13739 ** This is defined in tokenize.c. We just have to import the definition. |
| 13740 */ |
| 13741 #ifndef SQLITE_AMALGAMATION |
| 13742 #ifdef SQLITE_ASCII |
| 13743 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) |
| 13744 #endif |
| 13745 #ifdef SQLITE_EBCDIC |
| 13746 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[]; |
| 13747 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
| 13748 #endif |
| 13749 #endif /* SQLITE_AMALGAMATION */ |
| 13750 |
| 13751 |
| 13752 /* |
| 13753 ** Token types used by the sqlite3_complete() routine. See the header |
| 13754 ** comments on that procedure for additional information. |
| 13755 */ |
| 13756 #define tkSEMI 0 |
| 13757 #define tkWS 1 |
| 13758 #define tkOTHER 2 |
| 13759 #ifndef SQLITE_OMIT_TRIGGER |
| 13760 #define tkEXPLAIN 3 |
| 13761 #define tkCREATE 4 |
| 13762 #define tkTEMP 5 |
| 13763 #define tkTRIGGER 6 |
| 13764 #define tkEND 7 |
| 13765 #endif |
| 13766 |
| 13767 /* |
| 13768 ** Return TRUE if the given SQL string ends in a semicolon. |
| 13769 ** |
| 13770 ** Special handling is require for CREATE TRIGGER statements. |
| 13771 ** Whenever the CREATE TRIGGER keywords are seen, the statement |
| 13772 ** must end with ";END;". |
| 13773 ** |
| 13774 ** This implementation uses a state machine with 8 states: |
| 13775 ** |
| 13776 ** (0) INVALID We have not yet seen a non-whitespace character. |
| 13777 ** |
| 13778 ** (1) START At the beginning or end of an SQL statement. This routine |
| 13779 ** returns 1 if it ends in the START state and 0 if it ends |
| 13780 ** in any other state. |
| 13781 ** |
| 13782 ** (2) NORMAL We are in the middle of statement which ends with a single |
| 13783 ** semicolon. |
| 13784 ** |
| 13785 ** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of |
| 13786 ** a statement. |
| 13787 ** |
| 13788 ** (4) CREATE The keyword CREATE has been seen at the beginning of a |
| 13789 ** statement, possibly preceded by EXPLAIN and/or followed by |
| 13790 ** TEMP or TEMPORARY |
| 13791 ** |
| 13792 ** (5) TRIGGER We are in the middle of a trigger definition that must be |
| 13793 ** ended by a semicolon, the keyword END, and another semicolon. |
| 13794 ** |
| 13795 ** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at |
| 13796 ** the end of a trigger definition. |
| 13797 ** |
| 13798 ** (7) END We've seen the ";END" of the ";END;" that occurs at the end |
| 13799 ** of a trigger definition. |
| 13800 ** |
| 13801 ** Transitions between states above are determined by tokens extracted |
| 13802 ** from the input. The following tokens are significant: |
| 13803 ** |
| 13804 ** (0) tkSEMI A semicolon. |
| 13805 ** (1) tkWS Whitespace. |
| 13806 ** (2) tkOTHER Any other SQL token. |
| 13807 ** (3) tkEXPLAIN The "explain" keyword. |
| 13808 ** (4) tkCREATE The "create" keyword. |
| 13809 ** (5) tkTEMP The "temp" or "temporary" keyword. |
| 13810 ** (6) tkTRIGGER The "trigger" keyword. |
| 13811 ** (7) tkEND The "end" keyword. |
| 13812 ** |
| 13813 ** Whitespace never causes a state transition and is always ignored. |
| 13814 ** This means that a SQL string of all whitespace is invalid. |
| 13815 ** |
| 13816 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed |
| 13817 ** to recognize the end of a trigger can be omitted. All we have to do |
| 13818 ** is look for a semicolon that is not part of an string or comment. |
| 13819 */ |
| 13820 SQLITE_API int sqlite3_complete(const char *zSql){ |
| 13821 u8 state = 0; /* Current state, using numbers defined in header comment */ |
| 13822 u8 token; /* Value of the next token */ |
| 13823 |
| 13824 #ifndef SQLITE_OMIT_TRIGGER |
| 13825 /* A complex statement machine used to detect the end of a CREATE TRIGGER |
| 13826 ** statement. This is the normal case. |
| 13827 */ |
| 13828 static const u8 trans[8][8] = { |
| 13829 /* Token: */ |
| 13830 /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ |
| 13831 /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, }, |
| 13832 /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, }, |
| 13833 /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, }, |
| 13834 /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, }, |
| 13835 /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, }, |
| 13836 /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, }, |
| 13837 /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, }, |
| 13838 /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, }, |
| 13839 }; |
| 13840 #else |
| 13841 /* If triggers are not supported by this compile then the statement machine |
| 13842 ** used to detect the end of a statement is much simpler |
| 13843 */ |
| 13844 static const u8 trans[3][3] = { |
| 13845 /* Token: */ |
| 13846 /* State: ** SEMI WS OTHER */ |
| 13847 /* 0 INVALID: */ { 1, 0, 2, }, |
| 13848 /* 1 START: */ { 1, 1, 2, }, |
| 13849 /* 2 NORMAL: */ { 1, 2, 2, }, |
| 13850 }; |
| 13851 #endif /* SQLITE_OMIT_TRIGGER */ |
| 13852 |
| 13853 #ifdef SQLITE_ENABLE_API_ARMOR |
| 13854 if( zSql==0 ){ |
| 13855 (void)SQLITE_MISUSE_BKPT; |
| 13856 return 0; |
| 13857 } |
| 13858 #endif |
| 13859 |
| 13860 while( *zSql ){ |
| 13861 switch( *zSql ){ |
| 13862 case ';': { /* A semicolon */ |
| 13863 token = tkSEMI; |
| 13864 break; |
| 13865 } |
| 13866 case ' ': |
| 13867 case '\r': |
| 13868 case '\t': |
| 13869 case '\n': |
| 13870 case '\f': { /* White space is ignored */ |
| 13871 token = tkWS; |
| 13872 break; |
| 13873 } |
| 13874 case '/': { /* C-style comments */ |
| 13875 if( zSql[1]!='*' ){ |
| 13876 token = tkOTHER; |
| 13877 break; |
| 13878 } |
| 13879 zSql += 2; |
| 13880 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } |
| 13881 if( zSql[0]==0 ) return 0; |
| 13882 zSql++; |
| 13883 token = tkWS; |
| 13884 break; |
| 13885 } |
| 13886 case '-': { /* SQL-style comments from "--" to end of line */ |
| 13887 if( zSql[1]!='-' ){ |
| 13888 token = tkOTHER; |
| 13889 break; |
| 13890 } |
| 13891 while( *zSql && *zSql!='\n' ){ zSql++; } |
| 13892 if( *zSql==0 ) return state==1; |
| 13893 token = tkWS; |
| 13894 break; |
| 13895 } |
| 13896 case '[': { /* Microsoft-style identifiers in [...] */ |
| 13897 zSql++; |
| 13898 while( *zSql && *zSql!=']' ){ zSql++; } |
| 13899 if( *zSql==0 ) return 0; |
| 13900 token = tkOTHER; |
| 13901 break; |
| 13902 } |
| 13903 case '`': /* Grave-accent quoted symbols used by MySQL */ |
| 13904 case '"': /* single- and double-quoted strings */ |
| 13905 case '\'': { |
| 13906 int c = *zSql; |
| 13907 zSql++; |
| 13908 while( *zSql && *zSql!=c ){ zSql++; } |
| 13909 if( *zSql==0 ) return 0; |
| 13910 token = tkOTHER; |
| 13911 break; |
| 13912 } |
| 13913 default: { |
| 13914 #ifdef SQLITE_EBCDIC |
| 13915 unsigned char c; |
| 13916 #endif |
| 13917 if( IdChar((u8)*zSql) ){ |
| 13918 /* Keywords and unquoted identifiers */ |
| 13919 int nId; |
| 13920 for(nId=1; IdChar(zSql[nId]); nId++){} |
| 13921 #ifdef SQLITE_OMIT_TRIGGER |
| 13922 token = tkOTHER; |
| 13923 #else |
| 13924 switch( *zSql ){ |
| 13925 case 'c': case 'C': { |
| 13926 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ |
| 13927 token = tkCREATE; |
| 13928 }else{ |
| 13929 token = tkOTHER; |
| 13930 } |
| 13931 break; |
| 13932 } |
| 13933 case 't': case 'T': { |
| 13934 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ |
| 13935 token = tkTRIGGER; |
| 13936 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ |
| 13937 token = tkTEMP; |
| 13938 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ |
| 13939 token = tkTEMP; |
| 13940 }else{ |
| 13941 token = tkOTHER; |
| 13942 } |
| 13943 break; |
| 13944 } |
| 13945 case 'e': case 'E': { |
| 13946 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ |
| 13947 token = tkEND; |
| 13948 }else |
| 13949 #ifndef SQLITE_OMIT_EXPLAIN |
| 13950 if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ |
| 13951 token = tkEXPLAIN; |
| 13952 }else |
| 13953 #endif |
| 13954 { |
| 13955 token = tkOTHER; |
| 13956 } |
| 13957 break; |
| 13958 } |
| 13959 default: { |
| 13960 token = tkOTHER; |
| 13961 break; |
| 13962 } |
| 13963 } |
| 13964 #endif /* SQLITE_OMIT_TRIGGER */ |
| 13965 zSql += nId-1; |
| 13966 }else{ |
| 13967 /* Operators and special symbols */ |
| 13968 token = tkOTHER; |
| 13969 } |
| 13970 break; |
| 13971 } |
| 13972 } |
| 13973 state = trans[state][token]; |
| 13974 zSql++; |
| 13975 } |
| 13976 return state==1; |
| 13977 } |
| 13978 |
| 13979 #ifndef SQLITE_OMIT_UTF16 |
| 13980 /* |
| 13981 ** This routine is the same as the sqlite3_complete() routine described |
| 13982 ** above, except that the parameter is required to be UTF-16 encoded, not |
| 13983 ** UTF-8. |
| 13984 */ |
| 13985 SQLITE_API int sqlite3_complete16(const void *zSql){ |
| 13986 sqlite3_value *pVal; |
| 13987 char const *zSql8; |
| 13988 int rc; |
| 13989 |
| 13990 #ifndef SQLITE_OMIT_AUTOINIT |
| 13991 rc = sqlite3_initialize(); |
| 13992 if( rc ) return rc; |
| 13993 #endif |
| 13994 pVal = sqlite3ValueNew(0); |
| 13995 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 13996 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 13997 if( zSql8 ){ |
| 13998 rc = sqlite3_complete(zSql8); |
| 13999 }else{ |
| 14000 rc = SQLITE_NOMEM_BKPT; |
| 14001 } |
| 14002 sqlite3ValueFree(pVal); |
| 14003 return rc & 0xff; |
| 14004 } |
| 14005 #endif /* SQLITE_OMIT_UTF16 */ |
| 14006 #endif /* SQLITE_OMIT_COMPLETE */ |
| 14007 |
| 14008 /************** End of complete.c ********************************************/ |
| 14009 /************** Begin file main.c ********************************************/ |
| 14010 /* |
| 14011 ** 2001 September 15 |
| 14012 ** |
| 14013 ** The author disclaims copyright to this source code. In place of |
| 14014 ** a legal notice, here is a blessing: |
| 14015 ** |
| 14016 ** May you do good and not evil. |
| 14017 ** May you find forgiveness for yourself and forgive others. |
| 14018 ** May you share freely, never taking more than you give. |
| 14019 ** |
| 14020 ************************************************************************* |
| 14021 ** Main file for the SQLite library. The routines in this file |
| 14022 ** implement the programmer interface to the library. Routines in |
| 14023 ** other files are for internal use by SQLite and should not be |
| 14024 ** accessed by users of the library. |
| 14025 */ |
| 14026 /* #include "sqliteInt.h" */ |
| 14027 |
| 14028 #ifdef SQLITE_ENABLE_FTS3 |
| 14029 /************** Include fts3.h in the middle of main.c ***********************/ |
| 14030 /************** Begin file fts3.h ********************************************/ |
| 14031 /* |
| 14032 ** 2006 Oct 10 |
| 14033 ** |
| 14034 ** The author disclaims copyright to this source code. In place of |
| 14035 ** a legal notice, here is a blessing: |
| 14036 ** |
| 14037 ** May you do good and not evil. |
| 14038 ** May you find forgiveness for yourself and forgive others. |
| 14039 ** May you share freely, never taking more than you give. |
| 14040 ** |
| 14041 ****************************************************************************** |
| 14042 ** |
| 14043 ** This header file is used by programs that want to link against the |
| 14044 ** FTS3 library. All it does is declare the sqlite3Fts3Init() interface. |
| 14045 */ |
| 14046 /* #include "sqlite3.h" */ |
| 14047 |
| 14048 #if 0 |
| 14049 extern "C" { |
| 14050 #endif /* __cplusplus */ |
| 14051 |
| 14052 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db); |
| 14053 |
| 14054 #if 0 |
| 14055 } /* extern "C" */ |
| 14056 #endif /* __cplusplus */ |
| 14057 |
| 14058 /************** End of fts3.h ************************************************/ |
| 14059 /************** Continuing where we left off in main.c ***********************/ |
| 14060 #endif |
| 14061 #ifdef SQLITE_ENABLE_RTREE |
| 14062 /************** Include rtree.h in the middle of main.c **********************/ |
| 14063 /************** Begin file rtree.h *******************************************/ |
| 14064 /* |
| 14065 ** 2008 May 26 |
| 14066 ** |
| 14067 ** The author disclaims copyright to this source code. In place of |
| 14068 ** a legal notice, here is a blessing: |
| 14069 ** |
| 14070 ** May you do good and not evil. |
| 14071 ** May you find forgiveness for yourself and forgive others. |
| 14072 ** May you share freely, never taking more than you give. |
| 14073 ** |
| 14074 ****************************************************************************** |
| 14075 ** |
| 14076 ** This header file is used by programs that want to link against the |
| 14077 ** RTREE library. All it does is declare the sqlite3RtreeInit() interface. |
| 14078 */ |
| 14079 /* #include "sqlite3.h" */ |
| 14080 |
| 14081 #if 0 |
| 14082 extern "C" { |
| 14083 #endif /* __cplusplus */ |
| 14084 |
| 14085 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db); |
| 14086 |
| 14087 #if 0 |
| 14088 } /* extern "C" */ |
| 14089 #endif /* __cplusplus */ |
| 14090 |
| 14091 /************** End of rtree.h ***********************************************/ |
| 14092 /************** Continuing where we left off in main.c ***********************/ |
| 14093 #endif |
| 14094 #ifdef SQLITE_ENABLE_ICU |
| 14095 /************** Include sqliteicu.h in the middle of main.c ******************/ |
| 14096 /************** Begin file sqliteicu.h ***************************************/ |
| 14097 /* |
| 14098 ** 2008 May 26 |
| 14099 ** |
| 14100 ** The author disclaims copyright to this source code. In place of |
| 14101 ** a legal notice, here is a blessing: |
| 14102 ** |
| 14103 ** May you do good and not evil. |
| 14104 ** May you find forgiveness for yourself and forgive others. |
| 14105 ** May you share freely, never taking more than you give. |
| 14106 ** |
| 14107 ****************************************************************************** |
| 14108 ** |
| 14109 ** This header file is used by programs that want to link against the |
| 14110 ** ICU extension. All it does is declare the sqlite3IcuInit() interface. |
| 14111 */ |
| 14112 /* #include "sqlite3.h" */ |
| 14113 |
| 14114 #if 0 |
| 14115 extern "C" { |
| 14116 #endif /* __cplusplus */ |
| 14117 |
| 14118 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db); |
| 14119 |
| 14120 #if 0 |
| 14121 } /* extern "C" */ |
| 14122 #endif /* __cplusplus */ |
| 14123 |
| 14124 |
| 14125 /************** End of sqliteicu.h *******************************************/ |
| 14126 /************** Continuing where we left off in main.c ***********************/ |
| 14127 #endif |
| 14128 #ifdef SQLITE_ENABLE_JSON1 |
| 14129 SQLITE_PRIVATE int sqlite3Json1Init(sqlite3*); |
| 14130 #endif |
| 14131 #ifdef SQLITE_ENABLE_FTS5 |
| 14132 SQLITE_PRIVATE int sqlite3Fts5Init(sqlite3*); |
| 14133 #endif |
| 14134 |
| 14135 #ifndef SQLITE_AMALGAMATION |
| 14136 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant |
| 14137 ** contains the text of SQLITE_VERSION macro. |
| 14138 */ |
| 14139 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; |
| 14140 #endif |
| 14141 |
| 14142 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns |
| 14143 ** a pointer to the to the sqlite3_version[] string constant. |
| 14144 */ |
| 14145 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; } |
| 14146 |
| 14147 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a |
| 14148 ** pointer to a string constant whose value is the same as the |
| 14149 ** SQLITE_SOURCE_ID C preprocessor macro. |
| 14150 */ |
| 14151 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 14152 |
| 14153 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function |
| 14154 ** returns an integer equal to SQLITE_VERSION_NUMBER. |
| 14155 */ |
| 14156 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } |
| 14157 |
| 14158 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns |
| 14159 ** zero if and only if SQLite was compiled with mutexing code omitted due to |
| 14160 ** the SQLITE_THREADSAFE compile-time option being set to 0. |
| 14161 */ |
| 14162 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
| 14163 |
| 14164 /* |
| 14165 ** When compiling the test fixture or with debugging enabled (on Win32), |
| 14166 ** this variable being set to non-zero will cause OSTRACE macros to emit |
| 14167 ** extra diagnostic information. |
| 14168 */ |
| 14169 #ifdef SQLITE_HAVE_OS_TRACE |
| 14170 # ifndef SQLITE_DEBUG_OS_TRACE |
| 14171 # define SQLITE_DEBUG_OS_TRACE 0 |
| 14172 # endif |
| 14173 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 14174 #endif |
| 14175 |
| 14176 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 14177 /* |
| 14178 ** If the following function pointer is not NULL and if |
| 14179 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing |
| 14180 ** I/O active are written using this function. These messages |
| 14181 ** are intended for debugging activity only. |
| 14182 */ |
| 14183 SQLITE_API void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...) = 0; |
| 14184 #endif |
| 14185 |
| 14186 /* |
| 14187 ** If the following global variable points to a string which is the |
| 14188 ** name of a directory, then that directory will be used to store |
| 14189 ** temporary files. |
| 14190 ** |
| 14191 ** See also the "PRAGMA temp_store_directory" SQL command. |
| 14192 */ |
| 14193 SQLITE_API char *sqlite3_temp_directory = 0; |
| 14194 |
| 14195 /* |
| 14196 ** If the following global variable points to a string which is the |
| 14197 ** name of a directory, then that directory will be used to store |
| 14198 ** all database files specified with a relative pathname. |
| 14199 ** |
| 14200 ** See also the "PRAGMA data_store_directory" SQL command. |
| 14201 */ |
| 14202 SQLITE_API char *sqlite3_data_directory = 0; |
| 14203 |
| 14204 /* |
| 14205 ** Initialize SQLite. |
| 14206 ** |
| 14207 ** This routine must be called to initialize the memory allocation, |
| 14208 ** VFS, and mutex subsystems prior to doing any serious work with |
| 14209 ** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT |
| 14210 ** this routine will be called automatically by key routines such as |
| 14211 ** sqlite3_open(). |
| 14212 ** |
| 14213 ** This routine is a no-op except on its very first call for the process, |
| 14214 ** or for the first call after a call to sqlite3_shutdown. |
| 14215 ** |
| 14216 ** The first thread to call this routine runs the initialization to |
| 14217 ** completion. If subsequent threads call this routine before the first |
| 14218 ** thread has finished the initialization process, then the subsequent |
| 14219 ** threads must block until the first thread finishes with the initialization. |
| 14220 ** |
| 14221 ** The first thread might call this routine recursively. Recursive |
| 14222 ** calls to this routine should not block, of course. Otherwise the |
| 14223 ** initialization process would never complete. |
| 14224 ** |
| 14225 ** Let X be the first thread to enter this routine. Let Y be some other |
| 14226 ** thread. Then while the initial invocation of this routine by X is |
| 14227 ** incomplete, it is required that: |
| 14228 ** |
| 14229 ** * Calls to this routine from Y must block until the outer-most |
| 14230 ** call by X completes. |
| 14231 ** |
| 14232 ** * Recursive calls to this routine from thread X return immediately |
| 14233 ** without blocking. |
| 14234 */ |
| 14235 SQLITE_API int sqlite3_initialize(void){ |
| 14236 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ |
| 14237 int rc; /* Result code */ |
| 14238 #ifdef SQLITE_EXTRA_INIT |
| 14239 int bRunExtraInit = 0; /* Extra initialization needed */ |
| 14240 #endif |
| 14241 |
| 14242 #ifdef SQLITE_OMIT_WSD |
| 14243 rc = sqlite3_wsd_init(4096, 24); |
| 14244 if( rc!=SQLITE_OK ){ |
| 14245 return rc; |
| 14246 } |
| 14247 #endif |
| 14248 |
| 14249 /* If the following assert() fails on some obscure processor/compiler |
| 14250 ** combination, the work-around is to set the correct pointer |
| 14251 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */ |
| 14252 assert( SQLITE_PTRSIZE==sizeof(char*) ); |
| 14253 |
| 14254 /* If SQLite is already completely initialized, then this call |
| 14255 ** to sqlite3_initialize() should be a no-op. But the initialization |
| 14256 ** must be complete. So isInit must not be set until the very end |
| 14257 ** of this routine. |
| 14258 */ |
| 14259 if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; |
| 14260 |
| 14261 /* Make sure the mutex subsystem is initialized. If unable to |
| 14262 ** initialize the mutex subsystem, return early with the error. |
| 14263 ** If the system is so sick that we are unable to allocate a mutex, |
| 14264 ** there is not much SQLite is going to be able to do. |
| 14265 ** |
| 14266 ** The mutex subsystem must take care of serializing its own |
| 14267 ** initialization. |
| 14268 */ |
| 14269 rc = sqlite3MutexInit(); |
| 14270 if( rc ) return rc; |
| 14271 |
| 14272 /* Initialize the malloc() system and the recursive pInitMutex mutex. |
| 14273 ** This operation is protected by the STATIC_MASTER mutex. Note that |
| 14274 ** MutexAlloc() is called for a static mutex prior to initializing the |
| 14275 ** malloc subsystem - this implies that the allocation of a static |
| 14276 ** mutex must not require support from the malloc subsystem. |
| 14277 */ |
| 14278 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 14279 sqlite3_mutex_enter(pMaster); |
| 14280 sqlite3GlobalConfig.isMutexInit = 1; |
| 14281 if( !sqlite3GlobalConfig.isMallocInit ){ |
| 14282 rc = sqlite3MallocInit(); |
| 14283 } |
| 14284 if( rc==SQLITE_OK ){ |
| 14285 sqlite3GlobalConfig.isMallocInit = 1; |
| 14286 if( !sqlite3GlobalConfig.pInitMutex ){ |
| 14287 sqlite3GlobalConfig.pInitMutex = |
| 14288 sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
| 14289 if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ |
| 14290 rc = SQLITE_NOMEM_BKPT; |
| 14291 } |
| 14292 } |
| 14293 } |
| 14294 if( rc==SQLITE_OK ){ |
| 14295 sqlite3GlobalConfig.nRefInitMutex++; |
| 14296 } |
| 14297 sqlite3_mutex_leave(pMaster); |
| 14298 |
| 14299 /* If rc is not SQLITE_OK at this point, then either the malloc |
| 14300 ** subsystem could not be initialized or the system failed to allocate |
| 14301 ** the pInitMutex mutex. Return an error in either case. */ |
| 14302 if( rc!=SQLITE_OK ){ |
| 14303 return rc; |
| 14304 } |
| 14305 |
| 14306 /* Do the rest of the initialization under the recursive mutex so |
| 14307 ** that we will be able to handle recursive calls into |
| 14308 ** sqlite3_initialize(). The recursive calls normally come through |
| 14309 ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other |
| 14310 ** recursive calls might also be possible. |
| 14311 ** |
| 14312 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls |
| 14313 ** to the xInit method, so the xInit method need not be threadsafe. |
| 14314 ** |
| 14315 ** The following mutex is what serializes access to the appdef pcache xInit |
| 14316 ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the |
| 14317 ** call to sqlite3PcacheInitialize(). |
| 14318 */ |
| 14319 sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); |
| 14320 if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ |
| 14321 sqlite3GlobalConfig.inProgress = 1; |
| 14322 #ifdef SQLITE_ENABLE_SQLLOG |
| 14323 { |
| 14324 extern void sqlite3_init_sqllog(void); |
| 14325 sqlite3_init_sqllog(); |
| 14326 } |
| 14327 #endif |
| 14328 memset(&sqlite3BuiltinFunctions, 0, sizeof(sqlite3BuiltinFunctions)); |
| 14329 sqlite3RegisterBuiltinFunctions(); |
| 14330 if( sqlite3GlobalConfig.isPCacheInit==0 ){ |
| 14331 rc = sqlite3PcacheInitialize(); |
| 14332 } |
| 14333 if( rc==SQLITE_OK ){ |
| 14334 sqlite3GlobalConfig.isPCacheInit = 1; |
| 14335 rc = sqlite3OsInit(); |
| 14336 } |
| 14337 if( rc==SQLITE_OK ){ |
| 14338 sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, |
| 14339 sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); |
| 14340 sqlite3GlobalConfig.isInit = 1; |
| 14341 #ifdef SQLITE_EXTRA_INIT |
| 14342 bRunExtraInit = 1; |
| 14343 #endif |
| 14344 } |
| 14345 sqlite3GlobalConfig.inProgress = 0; |
| 14346 } |
| 14347 sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); |
| 14348 |
| 14349 /* Go back under the static mutex and clean up the recursive |
| 14350 ** mutex to prevent a resource leak. |
| 14351 */ |
| 14352 sqlite3_mutex_enter(pMaster); |
| 14353 sqlite3GlobalConfig.nRefInitMutex--; |
| 14354 if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ |
| 14355 assert( sqlite3GlobalConfig.nRefInitMutex==0 ); |
| 14356 sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); |
| 14357 sqlite3GlobalConfig.pInitMutex = 0; |
| 14358 } |
| 14359 sqlite3_mutex_leave(pMaster); |
| 14360 |
| 14361 /* The following is just a sanity check to make sure SQLite has |
| 14362 ** been compiled correctly. It is important to run this code, but |
| 14363 ** we don't want to run it too often and soak up CPU cycles for no |
| 14364 ** reason. So we run it once during initialization. |
| 14365 */ |
| 14366 #ifndef NDEBUG |
| 14367 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 14368 /* This section of code's only "output" is via assert() statements. */ |
| 14369 if ( rc==SQLITE_OK ){ |
| 14370 u64 x = (((u64)1)<<63)-1; |
| 14371 double y; |
| 14372 assert(sizeof(x)==8); |
| 14373 assert(sizeof(x)==sizeof(y)); |
| 14374 memcpy(&y, &x, 8); |
| 14375 assert( sqlite3IsNaN(y) ); |
| 14376 } |
| 14377 #endif |
| 14378 #endif |
| 14379 |
| 14380 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT |
| 14381 ** compile-time option. |
| 14382 */ |
| 14383 #ifdef SQLITE_EXTRA_INIT |
| 14384 if( bRunExtraInit ){ |
| 14385 int SQLITE_EXTRA_INIT(const char*); |
| 14386 rc = SQLITE_EXTRA_INIT(0); |
| 14387 } |
| 14388 #endif |
| 14389 |
| 14390 return rc; |
| 14391 } |
| 14392 |
| 14393 /* |
| 14394 ** Undo the effects of sqlite3_initialize(). Must not be called while |
| 14395 ** there are outstanding database connections or memory allocations or |
| 14396 ** while any part of SQLite is otherwise in use in any thread. This |
| 14397 ** routine is not threadsafe. But it is safe to invoke this routine |
| 14398 ** on when SQLite is already shut down. If SQLite is already shut down |
| 14399 ** when this routine is invoked, then this routine is a harmless no-op. |
| 14400 */ |
| 14401 SQLITE_API int sqlite3_shutdown(void){ |
| 14402 #ifdef SQLITE_OMIT_WSD |
| 14403 int rc = sqlite3_wsd_init(4096, 24); |
| 14404 if( rc!=SQLITE_OK ){ |
| 14405 return rc; |
| 14406 } |
| 14407 #endif |
| 14408 |
| 14409 if( sqlite3GlobalConfig.isInit ){ |
| 14410 #ifdef SQLITE_EXTRA_SHUTDOWN |
| 14411 void SQLITE_EXTRA_SHUTDOWN(void); |
| 14412 SQLITE_EXTRA_SHUTDOWN(); |
| 14413 #endif |
| 14414 sqlite3_os_end(); |
| 14415 sqlite3_reset_auto_extension(); |
| 14416 sqlite3GlobalConfig.isInit = 0; |
| 14417 } |
| 14418 if( sqlite3GlobalConfig.isPCacheInit ){ |
| 14419 sqlite3PcacheShutdown(); |
| 14420 sqlite3GlobalConfig.isPCacheInit = 0; |
| 14421 } |
| 14422 if( sqlite3GlobalConfig.isMallocInit ){ |
| 14423 sqlite3MallocEnd(); |
| 14424 sqlite3GlobalConfig.isMallocInit = 0; |
| 14425 |
| 14426 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES |
| 14427 /* The heap subsystem has now been shutdown and these values are supposed |
| 14428 ** to be NULL or point to memory that was obtained from sqlite3_malloc(), |
| 14429 ** which would rely on that heap subsystem; therefore, make sure these |
| 14430 ** values cannot refer to heap memory that was just invalidated when the |
| 14431 ** heap subsystem was shutdown. This is only done if the current call to |
| 14432 ** this function resulted in the heap subsystem actually being shutdown. |
| 14433 */ |
| 14434 sqlite3_data_directory = 0; |
| 14435 sqlite3_temp_directory = 0; |
| 14436 #endif |
| 14437 } |
| 14438 if( sqlite3GlobalConfig.isMutexInit ){ |
| 14439 sqlite3MutexEnd(); |
| 14440 sqlite3GlobalConfig.isMutexInit = 0; |
| 14441 } |
| 14442 |
| 14443 return SQLITE_OK; |
| 14444 } |
| 14445 |
| 14446 /* |
| 14447 ** This API allows applications to modify the global configuration of |
| 14448 ** the SQLite library at run-time. |
| 14449 ** |
| 14450 ** This routine should only be called when there are no outstanding |
| 14451 ** database connections or memory allocations. This routine is not |
| 14452 ** threadsafe. Failure to heed these warnings can lead to unpredictable |
| 14453 ** behavior. |
| 14454 */ |
| 14455 SQLITE_API int sqlite3_config(int op, ...){ |
| 14456 va_list ap; |
| 14457 int rc = SQLITE_OK; |
| 14458 |
| 14459 /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while |
| 14460 ** the SQLite library is in use. */ |
| 14461 if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; |
| 14462 |
| 14463 va_start(ap, op); |
| 14464 switch( op ){ |
| 14465 |
| 14466 /* Mutex configuration options are only available in a threadsafe |
| 14467 ** compile. |
| 14468 */ |
| 14469 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */ |
| 14470 case SQLITE_CONFIG_SINGLETHREAD: { |
| 14471 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to |
| 14472 ** Single-thread. */ |
| 14473 sqlite3GlobalConfig.bCoreMutex = 0; /* Disable mutex on core */ |
| 14474 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ |
| 14475 break; |
| 14476 } |
| 14477 #endif |
| 14478 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */ |
| 14479 case SQLITE_CONFIG_MULTITHREAD: { |
| 14480 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to |
| 14481 ** Multi-thread. */ |
| 14482 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ |
| 14483 sqlite3GlobalConfig.bFullMutex = 0; /* Disable mutex on connections */ |
| 14484 break; |
| 14485 } |
| 14486 #endif |
| 14487 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */ |
| 14488 case SQLITE_CONFIG_SERIALIZED: { |
| 14489 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to |
| 14490 ** Serialized. */ |
| 14491 sqlite3GlobalConfig.bCoreMutex = 1; /* Enable mutex on core */ |
| 14492 sqlite3GlobalConfig.bFullMutex = 1; /* Enable mutex on connections */ |
| 14493 break; |
| 14494 } |
| 14495 #endif |
| 14496 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */ |
| 14497 case SQLITE_CONFIG_MUTEX: { |
| 14498 /* Specify an alternative mutex implementation */ |
| 14499 sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); |
| 14500 break; |
| 14501 } |
| 14502 #endif |
| 14503 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */ |
| 14504 case SQLITE_CONFIG_GETMUTEX: { |
| 14505 /* Retrieve the current mutex implementation */ |
| 14506 *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; |
| 14507 break; |
| 14508 } |
| 14509 #endif |
| 14510 |
| 14511 case SQLITE_CONFIG_MALLOC: { |
| 14512 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a |
| 14513 ** single argument which is a pointer to an instance of the |
| 14514 ** sqlite3_mem_methods structure. The argument specifies alternative |
| 14515 ** low-level memory allocation routines to be used in place of the memory |
| 14516 ** allocation routines built into SQLite. */ |
| 14517 sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); |
| 14518 break; |
| 14519 } |
| 14520 case SQLITE_CONFIG_GETMALLOC: { |
| 14521 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a |
| 14522 ** single argument which is a pointer to an instance of the |
| 14523 ** sqlite3_mem_methods structure. The sqlite3_mem_methods structure is |
| 14524 ** filled with the currently defined memory allocation routines. */ |
| 14525 if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); |
| 14526 *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; |
| 14527 break; |
| 14528 } |
| 14529 case SQLITE_CONFIG_MEMSTATUS: { |
| 14530 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes |
| 14531 ** single argument of type int, interpreted as a boolean, which enables |
| 14532 ** or disables the collection of memory allocation statistics. */ |
| 14533 sqlite3GlobalConfig.bMemstat = va_arg(ap, int); |
| 14534 break; |
| 14535 } |
| 14536 case SQLITE_CONFIG_SCRATCH: { |
| 14537 /* EVIDENCE-OF: R-08404-60887 There are three arguments to |
| 14538 ** SQLITE_CONFIG_SCRATCH: A pointer an 8-byte aligned memory buffer from |
| 14539 ** which the scratch allocations will be drawn, the size of each scratch |
| 14540 ** allocation (sz), and the maximum number of scratch allocations (N). */ |
| 14541 sqlite3GlobalConfig.pScratch = va_arg(ap, void*); |
| 14542 sqlite3GlobalConfig.szScratch = va_arg(ap, int); |
| 14543 sqlite3GlobalConfig.nScratch = va_arg(ap, int); |
| 14544 break; |
| 14545 } |
| 14546 case SQLITE_CONFIG_PAGECACHE: { |
| 14547 /* EVIDENCE-OF: R-18761-36601 There are three arguments to |
| 14548 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem), |
| 14549 ** the size of each page cache line (sz), and the number of cache lines |
| 14550 ** (N). */ |
| 14551 sqlite3GlobalConfig.pPage = va_arg(ap, void*); |
| 14552 sqlite3GlobalConfig.szPage = va_arg(ap, int); |
| 14553 sqlite3GlobalConfig.nPage = va_arg(ap, int); |
| 14554 break; |
| 14555 } |
| 14556 case SQLITE_CONFIG_PCACHE_HDRSZ: { |
| 14557 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes |
| 14558 ** a single parameter which is a pointer to an integer and writes into |
| 14559 ** that integer the number of extra bytes per page required for each page |
| 14560 ** in SQLITE_CONFIG_PAGECACHE. */ |
| 14561 *va_arg(ap, int*) = |
| 14562 sqlite3HeaderSizeBtree() + |
| 14563 sqlite3HeaderSizePcache() + |
| 14564 sqlite3HeaderSizePcache1(); |
| 14565 break; |
| 14566 } |
| 14567 |
| 14568 case SQLITE_CONFIG_PCACHE: { |
| 14569 /* no-op */ |
| 14570 break; |
| 14571 } |
| 14572 case SQLITE_CONFIG_GETPCACHE: { |
| 14573 /* now an error */ |
| 14574 rc = SQLITE_ERROR; |
| 14575 break; |
| 14576 } |
| 14577 |
| 14578 case SQLITE_CONFIG_PCACHE2: { |
| 14579 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a |
| 14580 ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 14581 ** object. This object specifies the interface to a custom page cache |
| 14582 ** implementation. */ |
| 14583 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*); |
| 14584 break; |
| 14585 } |
| 14586 case SQLITE_CONFIG_GETPCACHE2: { |
| 14587 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a |
| 14588 ** single argument which is a pointer to an sqlite3_pcache_methods2 |
| 14589 ** object. SQLite copies of the current page cache implementation into |
| 14590 ** that object. */ |
| 14591 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 14592 sqlite3PCacheSetDefault(); |
| 14593 } |
| 14594 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2; |
| 14595 break; |
| 14596 } |
| 14597 |
| 14598 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only |
| 14599 ** available if SQLite is compiled with either SQLITE_ENABLE_MEMSYS3 or |
| 14600 ** SQLITE_ENABLE_MEMSYS5 and returns SQLITE_ERROR if invoked otherwise. */ |
| 14601 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
| 14602 case SQLITE_CONFIG_HEAP: { |
| 14603 /* EVIDENCE-OF: R-19854-42126 There are three arguments to |
| 14604 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the |
| 14605 ** number of bytes in the memory buffer, and the minimum allocation size. |
| 14606 */ |
| 14607 sqlite3GlobalConfig.pHeap = va_arg(ap, void*); |
| 14608 sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 14609 sqlite3GlobalConfig.mnReq = va_arg(ap, int); |
| 14610 |
| 14611 if( sqlite3GlobalConfig.mnReq<1 ){ |
| 14612 sqlite3GlobalConfig.mnReq = 1; |
| 14613 }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ |
| 14614 /* cap min request size at 2^12 */ |
| 14615 sqlite3GlobalConfig.mnReq = (1<<12); |
| 14616 } |
| 14617 |
| 14618 if( sqlite3GlobalConfig.pHeap==0 ){ |
| 14619 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer) |
| 14620 ** is NULL, then SQLite reverts to using its default memory allocator |
| 14621 ** (the system malloc() implementation), undoing any prior invocation of |
| 14622 ** SQLITE_CONFIG_MALLOC. |
| 14623 ** |
| 14624 ** Setting sqlite3GlobalConfig.m to all zeros will cause malloc to |
| 14625 ** revert to its default implementation when sqlite3_initialize() is run |
| 14626 */ |
| 14627 memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); |
| 14628 }else{ |
| 14629 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the |
| 14630 ** alternative memory allocator is engaged to handle all of SQLites |
| 14631 ** memory allocation needs. */ |
| 14632 #ifdef SQLITE_ENABLE_MEMSYS3 |
| 14633 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); |
| 14634 #endif |
| 14635 #ifdef SQLITE_ENABLE_MEMSYS5 |
| 14636 sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); |
| 14637 #endif |
| 14638 } |
| 14639 break; |
| 14640 } |
| 14641 #endif |
| 14642 |
| 14643 case SQLITE_CONFIG_LOOKASIDE: { |
| 14644 sqlite3GlobalConfig.szLookaside = va_arg(ap, int); |
| 14645 sqlite3GlobalConfig.nLookaside = va_arg(ap, int); |
| 14646 break; |
| 14647 } |
| 14648 |
| 14649 /* Record a pointer to the logger function and its first argument. |
| 14650 ** The default is NULL. Logging is disabled if the function pointer is |
| 14651 ** NULL. |
| 14652 */ |
| 14653 case SQLITE_CONFIG_LOG: { |
| 14654 /* MSVC is picky about pulling func ptrs from va lists. |
| 14655 ** http://support.microsoft.com/kb/47961 |
| 14656 ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); |
| 14657 */ |
| 14658 typedef void(*LOGFUNC_t)(void*,int,const char*); |
| 14659 sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); |
| 14660 sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); |
| 14661 break; |
| 14662 } |
| 14663 |
| 14664 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames |
| 14665 ** can be changed at start-time using the |
| 14666 ** sqlite3_config(SQLITE_CONFIG_URI,1) or |
| 14667 ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls. |
| 14668 */ |
| 14669 case SQLITE_CONFIG_URI: { |
| 14670 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single |
| 14671 ** argument of type int. If non-zero, then URI handling is globally |
| 14672 ** enabled. If the parameter is zero, then URI handling is globally |
| 14673 ** disabled. */ |
| 14674 sqlite3GlobalConfig.bOpenUri = va_arg(ap, int); |
| 14675 break; |
| 14676 } |
| 14677 |
| 14678 case SQLITE_CONFIG_COVERING_INDEX_SCAN: { |
| 14679 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN |
| 14680 ** option takes a single integer argument which is interpreted as a |
| 14681 ** boolean in order to enable or disable the use of covering indices for |
| 14682 ** full table scans in the query optimizer. */ |
| 14683 sqlite3GlobalConfig.bUseCis = va_arg(ap, int); |
| 14684 break; |
| 14685 } |
| 14686 |
| 14687 #ifdef SQLITE_ENABLE_SQLLOG |
| 14688 case SQLITE_CONFIG_SQLLOG: { |
| 14689 typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int); |
| 14690 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t); |
| 14691 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *); |
| 14692 break; |
| 14693 } |
| 14694 #endif |
| 14695 |
| 14696 case SQLITE_CONFIG_MMAP_SIZE: { |
| 14697 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit |
| 14698 ** integer (sqlite3_int64) values that are the default mmap size limit |
| 14699 ** (the default setting for PRAGMA mmap_size) and the maximum allowed |
| 14700 ** mmap size limit. */ |
| 14701 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64); |
| 14702 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64); |
| 14703 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is |
| 14704 ** negative, then that argument is changed to its compile-time default. |
| 14705 ** |
| 14706 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be |
| 14707 ** silently truncated if necessary so that it does not exceed the |
| 14708 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE |
| 14709 ** compile-time option. |
| 14710 */ |
| 14711 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){ |
| 14712 mxMmap = SQLITE_MAX_MMAP_SIZE; |
| 14713 } |
| 14714 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE; |
| 14715 if( szMmap>mxMmap) szMmap = mxMmap; |
| 14716 sqlite3GlobalConfig.mxMmap = mxMmap; |
| 14717 sqlite3GlobalConfig.szMmap = szMmap; |
| 14718 break; |
| 14719 } |
| 14720 |
| 14721 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */ |
| 14722 case SQLITE_CONFIG_WIN32_HEAPSIZE: { |
| 14723 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit |
| 14724 ** unsigned integer value that specifies the maximum size of the created |
| 14725 ** heap. */ |
| 14726 sqlite3GlobalConfig.nHeap = va_arg(ap, int); |
| 14727 break; |
| 14728 } |
| 14729 #endif |
| 14730 |
| 14731 case SQLITE_CONFIG_PMASZ: { |
| 14732 sqlite3GlobalConfig.szPma = va_arg(ap, unsigned int); |
| 14733 break; |
| 14734 } |
| 14735 |
| 14736 case SQLITE_CONFIG_STMTJRNL_SPILL: { |
| 14737 sqlite3GlobalConfig.nStmtSpill = va_arg(ap, int); |
| 14738 break; |
| 14739 } |
| 14740 |
| 14741 default: { |
| 14742 rc = SQLITE_ERROR; |
| 14743 break; |
| 14744 } |
| 14745 } |
| 14746 va_end(ap); |
| 14747 return rc; |
| 14748 } |
| 14749 |
| 14750 /* |
| 14751 ** Set up the lookaside buffers for a database connection. |
| 14752 ** Return SQLITE_OK on success. |
| 14753 ** If lookaside is already active, return SQLITE_BUSY. |
| 14754 ** |
| 14755 ** The sz parameter is the number of bytes in each lookaside slot. |
| 14756 ** The cnt parameter is the number of slots. If pStart is NULL the |
| 14757 ** space for the lookaside memory is obtained from sqlite3_malloc(). |
| 14758 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for |
| 14759 ** the lookaside memory. |
| 14760 */ |
| 14761 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ |
| 14762 #ifndef SQLITE_OMIT_LOOKASIDE |
| 14763 void *pStart; |
| 14764 if( db->lookaside.nOut ){ |
| 14765 return SQLITE_BUSY; |
| 14766 } |
| 14767 /* Free any existing lookaside buffer for this handle before |
| 14768 ** allocating a new one so we don't have to have space for |
| 14769 ** both at the same time. |
| 14770 */ |
| 14771 if( db->lookaside.bMalloced ){ |
| 14772 sqlite3_free(db->lookaside.pStart); |
| 14773 } |
| 14774 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger |
| 14775 ** than a pointer to be useful. |
| 14776 */ |
| 14777 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ |
| 14778 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; |
| 14779 if( cnt<0 ) cnt = 0; |
| 14780 if( sz==0 || cnt==0 ){ |
| 14781 sz = 0; |
| 14782 pStart = 0; |
| 14783 }else if( pBuf==0 ){ |
| 14784 sqlite3BeginBenignMalloc(); |
| 14785 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ |
| 14786 sqlite3EndBenignMalloc(); |
| 14787 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz; |
| 14788 }else{ |
| 14789 pStart = pBuf; |
| 14790 } |
| 14791 db->lookaside.pStart = pStart; |
| 14792 db->lookaside.pFree = 0; |
| 14793 db->lookaside.sz = (u16)sz; |
| 14794 if( pStart ){ |
| 14795 int i; |
| 14796 LookasideSlot *p; |
| 14797 assert( sz > (int)sizeof(LookasideSlot*) ); |
| 14798 p = (LookasideSlot*)pStart; |
| 14799 for(i=cnt-1; i>=0; i--){ |
| 14800 p->pNext = db->lookaside.pFree; |
| 14801 db->lookaside.pFree = p; |
| 14802 p = (LookasideSlot*)&((u8*)p)[sz]; |
| 14803 } |
| 14804 db->lookaside.pEnd = p; |
| 14805 db->lookaside.bDisable = 0; |
| 14806 db->lookaside.bMalloced = pBuf==0 ?1:0; |
| 14807 }else{ |
| 14808 db->lookaside.pStart = db; |
| 14809 db->lookaside.pEnd = db; |
| 14810 db->lookaside.bDisable = 1; |
| 14811 db->lookaside.bMalloced = 0; |
| 14812 } |
| 14813 #endif /* SQLITE_OMIT_LOOKASIDE */ |
| 14814 return SQLITE_OK; |
| 14815 } |
| 14816 |
| 14817 /* |
| 14818 ** Return the mutex associated with a database connection. |
| 14819 */ |
| 14820 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ |
| 14821 #ifdef SQLITE_ENABLE_API_ARMOR |
| 14822 if( !sqlite3SafetyCheckOk(db) ){ |
| 14823 (void)SQLITE_MISUSE_BKPT; |
| 14824 return 0; |
| 14825 } |
| 14826 #endif |
| 14827 return db->mutex; |
| 14828 } |
| 14829 |
| 14830 /* |
| 14831 ** Free up as much memory as we can from the given database |
| 14832 ** connection. |
| 14833 */ |
| 14834 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){ |
| 14835 int i; |
| 14836 |
| 14837 #ifdef SQLITE_ENABLE_API_ARMOR |
| 14838 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 14839 #endif |
| 14840 sqlite3_mutex_enter(db->mutex); |
| 14841 sqlite3BtreeEnterAll(db); |
| 14842 for(i=0; i<db->nDb; i++){ |
| 14843 Btree *pBt = db->aDb[i].pBt; |
| 14844 if( pBt ){ |
| 14845 Pager *pPager = sqlite3BtreePager(pBt); |
| 14846 sqlite3PagerShrink(pPager); |
| 14847 } |
| 14848 } |
| 14849 sqlite3BtreeLeaveAll(db); |
| 14850 sqlite3_mutex_leave(db->mutex); |
| 14851 return SQLITE_OK; |
| 14852 } |
| 14853 |
| 14854 /* |
| 14855 ** Flush any dirty pages in the pager-cache for any attached database |
| 14856 ** to disk. |
| 14857 */ |
| 14858 SQLITE_API int sqlite3_db_cacheflush(sqlite3 *db){ |
| 14859 int i; |
| 14860 int rc = SQLITE_OK; |
| 14861 int bSeenBusy = 0; |
| 14862 |
| 14863 #ifdef SQLITE_ENABLE_API_ARMOR |
| 14864 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 14865 #endif |
| 14866 sqlite3_mutex_enter(db->mutex); |
| 14867 sqlite3BtreeEnterAll(db); |
| 14868 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 14869 Btree *pBt = db->aDb[i].pBt; |
| 14870 if( pBt && sqlite3BtreeIsInTrans(pBt) ){ |
| 14871 Pager *pPager = sqlite3BtreePager(pBt); |
| 14872 rc = sqlite3PagerFlush(pPager); |
| 14873 if( rc==SQLITE_BUSY ){ |
| 14874 bSeenBusy = 1; |
| 14875 rc = SQLITE_OK; |
| 14876 } |
| 14877 } |
| 14878 } |
| 14879 sqlite3BtreeLeaveAll(db); |
| 14880 sqlite3_mutex_leave(db->mutex); |
| 14881 return ((rc==SQLITE_OK && bSeenBusy) ? SQLITE_BUSY : rc); |
| 14882 } |
| 14883 |
| 14884 /* |
| 14885 ** Configuration settings for an individual database connection |
| 14886 */ |
| 14887 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){ |
| 14888 va_list ap; |
| 14889 int rc; |
| 14890 va_start(ap, op); |
| 14891 switch( op ){ |
| 14892 case SQLITE_DBCONFIG_MAINDBNAME: { |
| 14893 db->aDb[0].zDbSName = va_arg(ap,char*); |
| 14894 rc = SQLITE_OK; |
| 14895 break; |
| 14896 } |
| 14897 case SQLITE_DBCONFIG_LOOKASIDE: { |
| 14898 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */ |
| 14899 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ |
| 14900 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ |
| 14901 rc = setupLookaside(db, pBuf, sz, cnt); |
| 14902 break; |
| 14903 } |
| 14904 default: { |
| 14905 static const struct { |
| 14906 int op; /* The opcode */ |
| 14907 u32 mask; /* Mask of the bit in sqlite3.flags to set/clear */ |
| 14908 } aFlagOp[] = { |
| 14909 { SQLITE_DBCONFIG_ENABLE_FKEY, SQLITE_ForeignKeys }, |
| 14910 { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger }, |
| 14911 { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer }, |
| 14912 { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension }, |
| 14913 { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE, SQLITE_NoCkptOnClose }, |
| 14914 }; |
| 14915 unsigned int i; |
| 14916 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ |
| 14917 for(i=0; i<ArraySize(aFlagOp); i++){ |
| 14918 if( aFlagOp[i].op==op ){ |
| 14919 int onoff = va_arg(ap, int); |
| 14920 int *pRes = va_arg(ap, int*); |
| 14921 int oldFlags = db->flags; |
| 14922 if( onoff>0 ){ |
| 14923 db->flags |= aFlagOp[i].mask; |
| 14924 }else if( onoff==0 ){ |
| 14925 db->flags &= ~aFlagOp[i].mask; |
| 14926 } |
| 14927 if( oldFlags!=db->flags ){ |
| 14928 sqlite3ExpirePreparedStatements(db); |
| 14929 } |
| 14930 if( pRes ){ |
| 14931 *pRes = (db->flags & aFlagOp[i].mask)!=0; |
| 14932 } |
| 14933 rc = SQLITE_OK; |
| 14934 break; |
| 14935 } |
| 14936 } |
| 14937 break; |
| 14938 } |
| 14939 } |
| 14940 va_end(ap); |
| 14941 return rc; |
| 14942 } |
| 14943 |
| 14944 |
| 14945 /* |
| 14946 ** Return true if the buffer z[0..n-1] contains all spaces. |
| 14947 */ |
| 14948 static int allSpaces(const char *z, int n){ |
| 14949 while( n>0 && z[n-1]==' ' ){ n--; } |
| 14950 return n==0; |
| 14951 } |
| 14952 |
| 14953 /* |
| 14954 ** This is the default collating function named "BINARY" which is always |
| 14955 ** available. |
| 14956 ** |
| 14957 ** If the padFlag argument is not NULL then space padding at the end |
| 14958 ** of strings is ignored. This implements the RTRIM collation. |
| 14959 */ |
| 14960 static int binCollFunc( |
| 14961 void *padFlag, |
| 14962 int nKey1, const void *pKey1, |
| 14963 int nKey2, const void *pKey2 |
| 14964 ){ |
| 14965 int rc, n; |
| 14966 n = nKey1<nKey2 ? nKey1 : nKey2; |
| 14967 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares |
| 14968 ** strings byte by byte using the memcmp() function from the standard C |
| 14969 ** library. */ |
| 14970 rc = memcmp(pKey1, pKey2, n); |
| 14971 if( rc==0 ){ |
| 14972 if( padFlag |
| 14973 && allSpaces(((char*)pKey1)+n, nKey1-n) |
| 14974 && allSpaces(((char*)pKey2)+n, nKey2-n) |
| 14975 ){ |
| 14976 /* EVIDENCE-OF: R-31624-24737 RTRIM is like BINARY except that extra |
| 14977 ** spaces at the end of either string do not change the result. In other |
| 14978 ** words, strings will compare equal to one another as long as they |
| 14979 ** differ only in the number of spaces at the end. |
| 14980 */ |
| 14981 }else{ |
| 14982 rc = nKey1 - nKey2; |
| 14983 } |
| 14984 } |
| 14985 return rc; |
| 14986 } |
| 14987 |
| 14988 /* |
| 14989 ** Another built-in collating sequence: NOCASE. |
| 14990 ** |
| 14991 ** This collating sequence is intended to be used for "case independent |
| 14992 ** comparison". SQLite's knowledge of upper and lower case equivalents |
| 14993 ** extends only to the 26 characters used in the English language. |
| 14994 ** |
| 14995 ** At the moment there is only a UTF-8 implementation. |
| 14996 */ |
| 14997 static int nocaseCollatingFunc( |
| 14998 void *NotUsed, |
| 14999 int nKey1, const void *pKey1, |
| 15000 int nKey2, const void *pKey2 |
| 15001 ){ |
| 15002 int r = sqlite3StrNICmp( |
| 15003 (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2); |
| 15004 UNUSED_PARAMETER(NotUsed); |
| 15005 if( 0==r ){ |
| 15006 r = nKey1-nKey2; |
| 15007 } |
| 15008 return r; |
| 15009 } |
| 15010 |
| 15011 /* |
| 15012 ** Return the ROWID of the most recent insert |
| 15013 */ |
| 15014 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){ |
| 15015 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15016 if( !sqlite3SafetyCheckOk(db) ){ |
| 15017 (void)SQLITE_MISUSE_BKPT; |
| 15018 return 0; |
| 15019 } |
| 15020 #endif |
| 15021 return db->lastRowid; |
| 15022 } |
| 15023 |
| 15024 /* |
| 15025 ** Return the number of changes in the most recent call to sqlite3_exec(). |
| 15026 */ |
| 15027 SQLITE_API int sqlite3_changes(sqlite3 *db){ |
| 15028 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15029 if( !sqlite3SafetyCheckOk(db) ){ |
| 15030 (void)SQLITE_MISUSE_BKPT; |
| 15031 return 0; |
| 15032 } |
| 15033 #endif |
| 15034 return db->nChange; |
| 15035 } |
| 15036 |
| 15037 /* |
| 15038 ** Return the number of changes since the database handle was opened. |
| 15039 */ |
| 15040 SQLITE_API int sqlite3_total_changes(sqlite3 *db){ |
| 15041 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15042 if( !sqlite3SafetyCheckOk(db) ){ |
| 15043 (void)SQLITE_MISUSE_BKPT; |
| 15044 return 0; |
| 15045 } |
| 15046 #endif |
| 15047 return db->nTotalChange; |
| 15048 } |
| 15049 |
| 15050 /* |
| 15051 ** Close all open savepoints. This function only manipulates fields of the |
| 15052 ** database handle object, it does not close any savepoints that may be open |
| 15053 ** at the b-tree/pager level. |
| 15054 */ |
| 15055 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){ |
| 15056 while( db->pSavepoint ){ |
| 15057 Savepoint *pTmp = db->pSavepoint; |
| 15058 db->pSavepoint = pTmp->pNext; |
| 15059 sqlite3DbFree(db, pTmp); |
| 15060 } |
| 15061 db->nSavepoint = 0; |
| 15062 db->nStatement = 0; |
| 15063 db->isTransactionSavepoint = 0; |
| 15064 } |
| 15065 |
| 15066 /* |
| 15067 ** Invoke the destructor function associated with FuncDef p, if any. Except, |
| 15068 ** if this is not the last copy of the function, do not invoke it. Multiple |
| 15069 ** copies of a single function are created when create_function() is called |
| 15070 ** with SQLITE_ANY as the encoding. |
| 15071 */ |
| 15072 static void functionDestroy(sqlite3 *db, FuncDef *p){ |
| 15073 FuncDestructor *pDestructor = p->u.pDestructor; |
| 15074 if( pDestructor ){ |
| 15075 pDestructor->nRef--; |
| 15076 if( pDestructor->nRef==0 ){ |
| 15077 pDestructor->xDestroy(pDestructor->pUserData); |
| 15078 sqlite3DbFree(db, pDestructor); |
| 15079 } |
| 15080 } |
| 15081 } |
| 15082 |
| 15083 /* |
| 15084 ** Disconnect all sqlite3_vtab objects that belong to database connection |
| 15085 ** db. This is called when db is being closed. |
| 15086 */ |
| 15087 static void disconnectAllVtab(sqlite3 *db){ |
| 15088 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 15089 int i; |
| 15090 HashElem *p; |
| 15091 sqlite3BtreeEnterAll(db); |
| 15092 for(i=0; i<db->nDb; i++){ |
| 15093 Schema *pSchema = db->aDb[i].pSchema; |
| 15094 if( db->aDb[i].pSchema ){ |
| 15095 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ |
| 15096 Table *pTab = (Table *)sqliteHashData(p); |
| 15097 if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab); |
| 15098 } |
| 15099 } |
| 15100 } |
| 15101 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){ |
| 15102 Module *pMod = (Module *)sqliteHashData(p); |
| 15103 if( pMod->pEpoTab ){ |
| 15104 sqlite3VtabDisconnect(db, pMod->pEpoTab); |
| 15105 } |
| 15106 } |
| 15107 sqlite3VtabUnlockList(db); |
| 15108 sqlite3BtreeLeaveAll(db); |
| 15109 #else |
| 15110 UNUSED_PARAMETER(db); |
| 15111 #endif |
| 15112 } |
| 15113 |
| 15114 /* |
| 15115 ** Return TRUE if database connection db has unfinalized prepared |
| 15116 ** statements or unfinished sqlite3_backup objects. |
| 15117 */ |
| 15118 static int connectionIsBusy(sqlite3 *db){ |
| 15119 int j; |
| 15120 assert( sqlite3_mutex_held(db->mutex) ); |
| 15121 if( db->pVdbe ) return 1; |
| 15122 for(j=0; j<db->nDb; j++){ |
| 15123 Btree *pBt = db->aDb[j].pBt; |
| 15124 if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1; |
| 15125 } |
| 15126 return 0; |
| 15127 } |
| 15128 |
| 15129 /* |
| 15130 ** Close an existing SQLite database |
| 15131 */ |
| 15132 static int sqlite3Close(sqlite3 *db, int forceZombie){ |
| 15133 if( !db ){ |
| 15134 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or |
| 15135 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */ |
| 15136 return SQLITE_OK; |
| 15137 } |
| 15138 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
| 15139 return SQLITE_MISUSE_BKPT; |
| 15140 } |
| 15141 sqlite3_mutex_enter(db->mutex); |
| 15142 if( db->mTrace & SQLITE_TRACE_CLOSE ){ |
| 15143 db->xTrace(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0); |
| 15144 } |
| 15145 |
| 15146 /* Force xDisconnect calls on all virtual tables */ |
| 15147 disconnectAllVtab(db); |
| 15148 |
| 15149 /* If a transaction is open, the disconnectAllVtab() call above |
| 15150 ** will not have called the xDisconnect() method on any virtual |
| 15151 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() |
| 15152 ** call will do so. We need to do this before the check for active |
| 15153 ** SQL statements below, as the v-table implementation may be storing |
| 15154 ** some prepared statements internally. |
| 15155 */ |
| 15156 sqlite3VtabRollback(db); |
| 15157 |
| 15158 /* Legacy behavior (sqlite3_close() behavior) is to return |
| 15159 ** SQLITE_BUSY if the connection can not be closed immediately. |
| 15160 */ |
| 15161 if( !forceZombie && connectionIsBusy(db) ){ |
| 15162 sqlite3ErrorWithMsg(db, SQLITE_BUSY, "unable to close due to unfinalized " |
| 15163 "statements or unfinished backups"); |
| 15164 sqlite3_mutex_leave(db->mutex); |
| 15165 return SQLITE_BUSY; |
| 15166 } |
| 15167 |
| 15168 #ifdef SQLITE_ENABLE_SQLLOG |
| 15169 if( sqlite3GlobalConfig.xSqllog ){ |
| 15170 /* Closing the handle. Fourth parameter is passed the value 2. */ |
| 15171 sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2); |
| 15172 } |
| 15173 #endif |
| 15174 |
| 15175 /* Convert the connection into a zombie and then close it. |
| 15176 */ |
| 15177 db->magic = SQLITE_MAGIC_ZOMBIE; |
| 15178 sqlite3LeaveMutexAndCloseZombie(db); |
| 15179 return SQLITE_OK; |
| 15180 } |
| 15181 |
| 15182 /* |
| 15183 ** Two variations on the public interface for closing a database |
| 15184 ** connection. The sqlite3_close() version returns SQLITE_BUSY and |
| 15185 ** leaves the connection option if there are unfinalized prepared |
| 15186 ** statements or unfinished sqlite3_backups. The sqlite3_close_v2() |
| 15187 ** version forces the connection to become a zombie if there are |
| 15188 ** unclosed resources, and arranges for deallocation when the last |
| 15189 ** prepare statement or sqlite3_backup closes. |
| 15190 */ |
| 15191 SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); } |
| 15192 SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); } |
| 15193 |
| 15194 |
| 15195 /* |
| 15196 ** Close the mutex on database connection db. |
| 15197 ** |
| 15198 ** Furthermore, if database connection db is a zombie (meaning that there |
| 15199 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and |
| 15200 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has |
| 15201 ** finished, then free all resources. |
| 15202 */ |
| 15203 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){ |
| 15204 HashElem *i; /* Hash table iterator */ |
| 15205 int j; |
| 15206 |
| 15207 /* If there are outstanding sqlite3_stmt or sqlite3_backup objects |
| 15208 ** or if the connection has not yet been closed by sqlite3_close_v2(), |
| 15209 ** then just leave the mutex and return. |
| 15210 */ |
| 15211 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){ |
| 15212 sqlite3_mutex_leave(db->mutex); |
| 15213 return; |
| 15214 } |
| 15215 |
| 15216 /* If we reach this point, it means that the database connection has |
| 15217 ** closed all sqlite3_stmt and sqlite3_backup objects and has been |
| 15218 ** passed to sqlite3_close (meaning that it is a zombie). Therefore, |
| 15219 ** go ahead and free all resources. |
| 15220 */ |
| 15221 |
| 15222 /* If a transaction is open, roll it back. This also ensures that if |
| 15223 ** any database schemas have been modified by an uncommitted transaction |
| 15224 ** they are reset. And that the required b-tree mutex is held to make |
| 15225 ** the pager rollback and schema reset an atomic operation. */ |
| 15226 sqlite3RollbackAll(db, SQLITE_OK); |
| 15227 |
| 15228 /* Free any outstanding Savepoint structures. */ |
| 15229 sqlite3CloseSavepoints(db); |
| 15230 |
| 15231 /* Close all database connections */ |
| 15232 for(j=0; j<db->nDb; j++){ |
| 15233 struct Db *pDb = &db->aDb[j]; |
| 15234 if( pDb->pBt ){ |
| 15235 sqlite3BtreeClose(pDb->pBt); |
| 15236 pDb->pBt = 0; |
| 15237 if( j!=1 ){ |
| 15238 pDb->pSchema = 0; |
| 15239 } |
| 15240 } |
| 15241 } |
| 15242 /* Clear the TEMP schema separately and last */ |
| 15243 if( db->aDb[1].pSchema ){ |
| 15244 sqlite3SchemaClear(db->aDb[1].pSchema); |
| 15245 } |
| 15246 sqlite3VtabUnlockList(db); |
| 15247 |
| 15248 /* Free up the array of auxiliary databases */ |
| 15249 sqlite3CollapseDatabaseArray(db); |
| 15250 assert( db->nDb<=2 ); |
| 15251 assert( db->aDb==db->aDbStatic ); |
| 15252 |
| 15253 /* Tell the code in notify.c that the connection no longer holds any |
| 15254 ** locks and does not require any further unlock-notify callbacks. |
| 15255 */ |
| 15256 sqlite3ConnectionClosed(db); |
| 15257 |
| 15258 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 15259 FuncDef *pNext, *p; |
| 15260 p = sqliteHashData(i); |
| 15261 do{ |
| 15262 functionDestroy(db, p); |
| 15263 pNext = p->pNext; |
| 15264 sqlite3DbFree(db, p); |
| 15265 p = pNext; |
| 15266 }while( p ); |
| 15267 } |
| 15268 sqlite3HashClear(&db->aFunc); |
| 15269 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
| 15270 CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
| 15271 /* Invoke any destructors registered for collation sequence user data. */ |
| 15272 for(j=0; j<3; j++){ |
| 15273 if( pColl[j].xDel ){ |
| 15274 pColl[j].xDel(pColl[j].pUser); |
| 15275 } |
| 15276 } |
| 15277 sqlite3DbFree(db, pColl); |
| 15278 } |
| 15279 sqlite3HashClear(&db->aCollSeq); |
| 15280 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 15281 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ |
| 15282 Module *pMod = (Module *)sqliteHashData(i); |
| 15283 if( pMod->xDestroy ){ |
| 15284 pMod->xDestroy(pMod->pAux); |
| 15285 } |
| 15286 sqlite3VtabEponymousTableClear(db, pMod); |
| 15287 sqlite3DbFree(db, pMod); |
| 15288 } |
| 15289 sqlite3HashClear(&db->aModule); |
| 15290 #endif |
| 15291 |
| 15292 sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */ |
| 15293 sqlite3ValueFree(db->pErr); |
| 15294 sqlite3CloseExtensions(db); |
| 15295 #if SQLITE_USER_AUTHENTICATION |
| 15296 sqlite3_free(db->auth.zAuthUser); |
| 15297 sqlite3_free(db->auth.zAuthPW); |
| 15298 #endif |
| 15299 |
| 15300 db->magic = SQLITE_MAGIC_ERROR; |
| 15301 |
| 15302 /* The temp-database schema is allocated differently from the other schema |
| 15303 ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). |
| 15304 ** So it needs to be freed here. Todo: Why not roll the temp schema into |
| 15305 ** the same sqliteMalloc() as the one that allocates the database |
| 15306 ** structure? |
| 15307 */ |
| 15308 sqlite3DbFree(db, db->aDb[1].pSchema); |
| 15309 sqlite3_mutex_leave(db->mutex); |
| 15310 db->magic = SQLITE_MAGIC_CLOSED; |
| 15311 sqlite3_mutex_free(db->mutex); |
| 15312 assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ |
| 15313 if( db->lookaside.bMalloced ){ |
| 15314 sqlite3_free(db->lookaside.pStart); |
| 15315 } |
| 15316 sqlite3_free(db); |
| 15317 } |
| 15318 |
| 15319 /* |
| 15320 ** Rollback all database files. If tripCode is not SQLITE_OK, then |
| 15321 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit |
| 15322 ** breaker") and made to return tripCode if there are any further |
| 15323 ** attempts to use that cursor. Read cursors remain open and valid |
| 15324 ** but are "saved" in case the table pages are moved around. |
| 15325 */ |
| 15326 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){ |
| 15327 int i; |
| 15328 int inTrans = 0; |
| 15329 int schemaChange; |
| 15330 assert( sqlite3_mutex_held(db->mutex) ); |
| 15331 sqlite3BeginBenignMalloc(); |
| 15332 |
| 15333 /* Obtain all b-tree mutexes before making any calls to BtreeRollback(). |
| 15334 ** This is important in case the transaction being rolled back has |
| 15335 ** modified the database schema. If the b-tree mutexes are not taken |
| 15336 ** here, then another shared-cache connection might sneak in between |
| 15337 ** the database rollback and schema reset, which can cause false |
| 15338 ** corruption reports in some cases. */ |
| 15339 sqlite3BtreeEnterAll(db); |
| 15340 schemaChange = (db->flags & SQLITE_InternChanges)!=0 && db->init.busy==0; |
| 15341 |
| 15342 for(i=0; i<db->nDb; i++){ |
| 15343 Btree *p = db->aDb[i].pBt; |
| 15344 if( p ){ |
| 15345 if( sqlite3BtreeIsInTrans(p) ){ |
| 15346 inTrans = 1; |
| 15347 } |
| 15348 sqlite3BtreeRollback(p, tripCode, !schemaChange); |
| 15349 } |
| 15350 } |
| 15351 sqlite3VtabRollback(db); |
| 15352 sqlite3EndBenignMalloc(); |
| 15353 |
| 15354 if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){ |
| 15355 sqlite3ExpirePreparedStatements(db); |
| 15356 sqlite3ResetAllSchemasOfConnection(db); |
| 15357 } |
| 15358 sqlite3BtreeLeaveAll(db); |
| 15359 |
| 15360 /* Any deferred constraint violations have now been resolved. */ |
| 15361 db->nDeferredCons = 0; |
| 15362 db->nDeferredImmCons = 0; |
| 15363 db->flags &= ~SQLITE_DeferFKs; |
| 15364 |
| 15365 /* If one has been configured, invoke the rollback-hook callback */ |
| 15366 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ |
| 15367 db->xRollbackCallback(db->pRollbackArg); |
| 15368 } |
| 15369 } |
| 15370 |
| 15371 /* |
| 15372 ** Return a static string containing the name corresponding to the error code |
| 15373 ** specified in the argument. |
| 15374 */ |
| 15375 #if defined(SQLITE_NEED_ERR_NAME) |
| 15376 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){ |
| 15377 const char *zName = 0; |
| 15378 int i, origRc = rc; |
| 15379 for(i=0; i<2 && zName==0; i++, rc &= 0xff){ |
| 15380 switch( rc ){ |
| 15381 case SQLITE_OK: zName = "SQLITE_OK"; break; |
| 15382 case SQLITE_ERROR: zName = "SQLITE_ERROR"; break; |
| 15383 case SQLITE_INTERNAL: zName = "SQLITE_INTERNAL"; break; |
| 15384 case SQLITE_PERM: zName = "SQLITE_PERM"; break; |
| 15385 case SQLITE_ABORT: zName = "SQLITE_ABORT"; break; |
| 15386 case SQLITE_ABORT_ROLLBACK: zName = "SQLITE_ABORT_ROLLBACK"; break; |
| 15387 case SQLITE_BUSY: zName = "SQLITE_BUSY"; break; |
| 15388 case SQLITE_BUSY_RECOVERY: zName = "SQLITE_BUSY_RECOVERY"; break; |
| 15389 case SQLITE_BUSY_SNAPSHOT: zName = "SQLITE_BUSY_SNAPSHOT"; break; |
| 15390 case SQLITE_LOCKED: zName = "SQLITE_LOCKED"; break; |
| 15391 case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break; |
| 15392 case SQLITE_NOMEM: zName = "SQLITE_NOMEM"; break; |
| 15393 case SQLITE_READONLY: zName = "SQLITE_READONLY"; break; |
| 15394 case SQLITE_READONLY_RECOVERY: zName = "SQLITE_READONLY_RECOVERY"; break; |
| 15395 case SQLITE_READONLY_CANTLOCK: zName = "SQLITE_READONLY_CANTLOCK"; break; |
| 15396 case SQLITE_READONLY_ROLLBACK: zName = "SQLITE_READONLY_ROLLBACK"; break; |
| 15397 case SQLITE_READONLY_DBMOVED: zName = "SQLITE_READONLY_DBMOVED"; break; |
| 15398 case SQLITE_INTERRUPT: zName = "SQLITE_INTERRUPT"; break; |
| 15399 case SQLITE_IOERR: zName = "SQLITE_IOERR"; break; |
| 15400 case SQLITE_IOERR_READ: zName = "SQLITE_IOERR_READ"; break; |
| 15401 case SQLITE_IOERR_SHORT_READ: zName = "SQLITE_IOERR_SHORT_READ"; break; |
| 15402 case SQLITE_IOERR_WRITE: zName = "SQLITE_IOERR_WRITE"; break; |
| 15403 case SQLITE_IOERR_FSYNC: zName = "SQLITE_IOERR_FSYNC"; break; |
| 15404 case SQLITE_IOERR_DIR_FSYNC: zName = "SQLITE_IOERR_DIR_FSYNC"; break; |
| 15405 case SQLITE_IOERR_TRUNCATE: zName = "SQLITE_IOERR_TRUNCATE"; break; |
| 15406 case SQLITE_IOERR_FSTAT: zName = "SQLITE_IOERR_FSTAT"; break; |
| 15407 case SQLITE_IOERR_UNLOCK: zName = "SQLITE_IOERR_UNLOCK"; break; |
| 15408 case SQLITE_IOERR_RDLOCK: zName = "SQLITE_IOERR_RDLOCK"; break; |
| 15409 case SQLITE_IOERR_DELETE: zName = "SQLITE_IOERR_DELETE"; break; |
| 15410 case SQLITE_IOERR_NOMEM: zName = "SQLITE_IOERR_NOMEM"; break; |
| 15411 case SQLITE_IOERR_ACCESS: zName = "SQLITE_IOERR_ACCESS"; break; |
| 15412 case SQLITE_IOERR_CHECKRESERVEDLOCK: |
| 15413 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; |
| 15414 case SQLITE_IOERR_LOCK: zName = "SQLITE_IOERR_LOCK"; break; |
| 15415 case SQLITE_IOERR_CLOSE: zName = "SQLITE_IOERR_CLOSE"; break; |
| 15416 case SQLITE_IOERR_DIR_CLOSE: zName = "SQLITE_IOERR_DIR_CLOSE"; break; |
| 15417 case SQLITE_IOERR_SHMOPEN: zName = "SQLITE_IOERR_SHMOPEN"; break; |
| 15418 case SQLITE_IOERR_SHMSIZE: zName = "SQLITE_IOERR_SHMSIZE"; break; |
| 15419 case SQLITE_IOERR_SHMLOCK: zName = "SQLITE_IOERR_SHMLOCK"; break; |
| 15420 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break; |
| 15421 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break; |
| 15422 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break; |
| 15423 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break; |
| 15424 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break; |
| 15425 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break; |
| 15426 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break; |
| 15427 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break; |
| 15428 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break; |
| 15429 case SQLITE_FULL: zName = "SQLITE_FULL"; break; |
| 15430 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break; |
| 15431 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break; |
| 15432 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break; |
| 15433 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break; |
| 15434 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break; |
| 15435 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break; |
| 15436 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break; |
| 15437 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break; |
| 15438 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break; |
| 15439 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break; |
| 15440 case SQLITE_CONSTRAINT_UNIQUE: zName = "SQLITE_CONSTRAINT_UNIQUE"; break; |
| 15441 case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break; |
| 15442 case SQLITE_CONSTRAINT_FOREIGNKEY: |
| 15443 zName = "SQLITE_CONSTRAINT_FOREIGNKEY"; break; |
| 15444 case SQLITE_CONSTRAINT_CHECK: zName = "SQLITE_CONSTRAINT_CHECK"; break; |
| 15445 case SQLITE_CONSTRAINT_PRIMARYKEY: |
| 15446 zName = "SQLITE_CONSTRAINT_PRIMARYKEY"; break; |
| 15447 case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break; |
| 15448 case SQLITE_CONSTRAINT_COMMITHOOK: |
| 15449 zName = "SQLITE_CONSTRAINT_COMMITHOOK"; break; |
| 15450 case SQLITE_CONSTRAINT_VTAB: zName = "SQLITE_CONSTRAINT_VTAB"; break; |
| 15451 case SQLITE_CONSTRAINT_FUNCTION: |
| 15452 zName = "SQLITE_CONSTRAINT_FUNCTION"; break; |
| 15453 case SQLITE_CONSTRAINT_ROWID: zName = "SQLITE_CONSTRAINT_ROWID"; break; |
| 15454 case SQLITE_MISMATCH: zName = "SQLITE_MISMATCH"; break; |
| 15455 case SQLITE_MISUSE: zName = "SQLITE_MISUSE"; break; |
| 15456 case SQLITE_NOLFS: zName = "SQLITE_NOLFS"; break; |
| 15457 case SQLITE_AUTH: zName = "SQLITE_AUTH"; break; |
| 15458 case SQLITE_FORMAT: zName = "SQLITE_FORMAT"; break; |
| 15459 case SQLITE_RANGE: zName = "SQLITE_RANGE"; break; |
| 15460 case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break; |
| 15461 case SQLITE_ROW: zName = "SQLITE_ROW"; break; |
| 15462 case SQLITE_NOTICE: zName = "SQLITE_NOTICE"; break; |
| 15463 case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break; |
| 15464 case SQLITE_NOTICE_RECOVER_ROLLBACK: |
| 15465 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break; |
| 15466 case SQLITE_WARNING: zName = "SQLITE_WARNING"; break; |
| 15467 case SQLITE_WARNING_AUTOINDEX: zName = "SQLITE_WARNING_AUTOINDEX"; break; |
| 15468 case SQLITE_DONE: zName = "SQLITE_DONE"; break; |
| 15469 } |
| 15470 } |
| 15471 if( zName==0 ){ |
| 15472 static char zBuf[50]; |
| 15473 sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc); |
| 15474 zName = zBuf; |
| 15475 } |
| 15476 return zName; |
| 15477 } |
| 15478 #endif |
| 15479 |
| 15480 /* |
| 15481 ** Return a static string that describes the kind of error specified in the |
| 15482 ** argument. |
| 15483 */ |
| 15484 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){ |
| 15485 static const char* const aMsg[] = { |
| 15486 /* SQLITE_OK */ "not an error", |
| 15487 /* SQLITE_ERROR */ "SQL logic error or missing database", |
| 15488 /* SQLITE_INTERNAL */ 0, |
| 15489 /* SQLITE_PERM */ "access permission denied", |
| 15490 /* SQLITE_ABORT */ "callback requested query abort", |
| 15491 /* SQLITE_BUSY */ "database is locked", |
| 15492 /* SQLITE_LOCKED */ "database table is locked", |
| 15493 /* SQLITE_NOMEM */ "out of memory", |
| 15494 /* SQLITE_READONLY */ "attempt to write a readonly database", |
| 15495 /* SQLITE_INTERRUPT */ "interrupted", |
| 15496 /* SQLITE_IOERR */ "disk I/O error", |
| 15497 /* SQLITE_CORRUPT */ "database disk image is malformed", |
| 15498 /* SQLITE_NOTFOUND */ "unknown operation", |
| 15499 /* SQLITE_FULL */ "database or disk is full", |
| 15500 /* SQLITE_CANTOPEN */ "unable to open database file", |
| 15501 /* SQLITE_PROTOCOL */ "locking protocol", |
| 15502 /* SQLITE_EMPTY */ "table contains no data", |
| 15503 /* SQLITE_SCHEMA */ "database schema has changed", |
| 15504 /* SQLITE_TOOBIG */ "string or blob too big", |
| 15505 /* SQLITE_CONSTRAINT */ "constraint failed", |
| 15506 /* SQLITE_MISMATCH */ "datatype mismatch", |
| 15507 /* SQLITE_MISUSE */ "library routine called out of sequence", |
| 15508 /* SQLITE_NOLFS */ "large file support is disabled", |
| 15509 /* SQLITE_AUTH */ "authorization denied", |
| 15510 /* SQLITE_FORMAT */ "auxiliary database format error", |
| 15511 /* SQLITE_RANGE */ "bind or column index out of range", |
| 15512 /* SQLITE_NOTADB */ "file is encrypted or is not a database", |
| 15513 }; |
| 15514 const char *zErr = "unknown error"; |
| 15515 switch( rc ){ |
| 15516 case SQLITE_ABORT_ROLLBACK: { |
| 15517 zErr = "abort due to ROLLBACK"; |
| 15518 break; |
| 15519 } |
| 15520 default: { |
| 15521 rc &= 0xff; |
| 15522 if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){ |
| 15523 zErr = aMsg[rc]; |
| 15524 } |
| 15525 break; |
| 15526 } |
| 15527 } |
| 15528 return zErr; |
| 15529 } |
| 15530 |
| 15531 /* |
| 15532 ** This routine implements a busy callback that sleeps and tries |
| 15533 ** again until a timeout value is reached. The timeout value is |
| 15534 ** an integer number of milliseconds passed in as the first |
| 15535 ** argument. |
| 15536 */ |
| 15537 static int sqliteDefaultBusyCallback( |
| 15538 void *ptr, /* Database connection */ |
| 15539 int count /* Number of times table has been busy */ |
| 15540 ){ |
| 15541 #if SQLITE_OS_WIN || HAVE_USLEEP |
| 15542 static const u8 delays[] = |
| 15543 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; |
| 15544 static const u8 totals[] = |
| 15545 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; |
| 15546 # define NDELAY ArraySize(delays) |
| 15547 sqlite3 *db = (sqlite3 *)ptr; |
| 15548 int timeout = db->busyTimeout; |
| 15549 int delay, prior; |
| 15550 |
| 15551 assert( count>=0 ); |
| 15552 if( count < NDELAY ){ |
| 15553 delay = delays[count]; |
| 15554 prior = totals[count]; |
| 15555 }else{ |
| 15556 delay = delays[NDELAY-1]; |
| 15557 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); |
| 15558 } |
| 15559 if( prior + delay > timeout ){ |
| 15560 delay = timeout - prior; |
| 15561 if( delay<=0 ) return 0; |
| 15562 } |
| 15563 sqlite3OsSleep(db->pVfs, delay*1000); |
| 15564 return 1; |
| 15565 #else |
| 15566 sqlite3 *db = (sqlite3 *)ptr; |
| 15567 int timeout = ((sqlite3 *)ptr)->busyTimeout; |
| 15568 if( (count+1)*1000 > timeout ){ |
| 15569 return 0; |
| 15570 } |
| 15571 sqlite3OsSleep(db->pVfs, 1000000); |
| 15572 return 1; |
| 15573 #endif |
| 15574 } |
| 15575 |
| 15576 /* |
| 15577 ** Invoke the given busy handler. |
| 15578 ** |
| 15579 ** This routine is called when an operation failed with a lock. |
| 15580 ** If this routine returns non-zero, the lock is retried. If it |
| 15581 ** returns 0, the operation aborts with an SQLITE_BUSY error. |
| 15582 */ |
| 15583 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){ |
| 15584 int rc; |
| 15585 if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; |
| 15586 rc = p->xFunc(p->pArg, p->nBusy); |
| 15587 if( rc==0 ){ |
| 15588 p->nBusy = -1; |
| 15589 }else{ |
| 15590 p->nBusy++; |
| 15591 } |
| 15592 return rc; |
| 15593 } |
| 15594 |
| 15595 /* |
| 15596 ** This routine sets the busy callback for an Sqlite database to the |
| 15597 ** given callback function with the given argument. |
| 15598 */ |
| 15599 SQLITE_API int sqlite3_busy_handler( |
| 15600 sqlite3 *db, |
| 15601 int (*xBusy)(void*,int), |
| 15602 void *pArg |
| 15603 ){ |
| 15604 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15605 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 15606 #endif |
| 15607 sqlite3_mutex_enter(db->mutex); |
| 15608 db->busyHandler.xFunc = xBusy; |
| 15609 db->busyHandler.pArg = pArg; |
| 15610 db->busyHandler.nBusy = 0; |
| 15611 db->busyTimeout = 0; |
| 15612 sqlite3_mutex_leave(db->mutex); |
| 15613 return SQLITE_OK; |
| 15614 } |
| 15615 |
| 15616 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 15617 /* |
| 15618 ** This routine sets the progress callback for an Sqlite database to the |
| 15619 ** given callback function with the given argument. The progress callback will |
| 15620 ** be invoked every nOps opcodes. |
| 15621 */ |
| 15622 SQLITE_API void sqlite3_progress_handler( |
| 15623 sqlite3 *db, |
| 15624 int nOps, |
| 15625 int (*xProgress)(void*), |
| 15626 void *pArg |
| 15627 ){ |
| 15628 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15629 if( !sqlite3SafetyCheckOk(db) ){ |
| 15630 (void)SQLITE_MISUSE_BKPT; |
| 15631 return; |
| 15632 } |
| 15633 #endif |
| 15634 sqlite3_mutex_enter(db->mutex); |
| 15635 if( nOps>0 ){ |
| 15636 db->xProgress = xProgress; |
| 15637 db->nProgressOps = (unsigned)nOps; |
| 15638 db->pProgressArg = pArg; |
| 15639 }else{ |
| 15640 db->xProgress = 0; |
| 15641 db->nProgressOps = 0; |
| 15642 db->pProgressArg = 0; |
| 15643 } |
| 15644 sqlite3_mutex_leave(db->mutex); |
| 15645 } |
| 15646 #endif |
| 15647 |
| 15648 |
| 15649 /* |
| 15650 ** This routine installs a default busy handler that waits for the |
| 15651 ** specified number of milliseconds before returning 0. |
| 15652 */ |
| 15653 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
| 15654 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15655 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 15656 #endif |
| 15657 if( ms>0 ){ |
| 15658 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); |
| 15659 db->busyTimeout = ms; |
| 15660 }else{ |
| 15661 sqlite3_busy_handler(db, 0, 0); |
| 15662 } |
| 15663 return SQLITE_OK; |
| 15664 } |
| 15665 |
| 15666 /* |
| 15667 ** Cause any pending operation to stop at its earliest opportunity. |
| 15668 */ |
| 15669 SQLITE_API void sqlite3_interrupt(sqlite3 *db){ |
| 15670 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15671 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){ |
| 15672 (void)SQLITE_MISUSE_BKPT; |
| 15673 return; |
| 15674 } |
| 15675 #endif |
| 15676 db->u1.isInterrupted = 1; |
| 15677 } |
| 15678 |
| 15679 |
| 15680 /* |
| 15681 ** This function is exactly the same as sqlite3_create_function(), except |
| 15682 ** that it is designed to be called by internal code. The difference is |
| 15683 ** that if a malloc() fails in sqlite3_create_function(), an error code |
| 15684 ** is returned and the mallocFailed flag cleared. |
| 15685 */ |
| 15686 SQLITE_PRIVATE int sqlite3CreateFunc( |
| 15687 sqlite3 *db, |
| 15688 const char *zFunctionName, |
| 15689 int nArg, |
| 15690 int enc, |
| 15691 void *pUserData, |
| 15692 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), |
| 15693 void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 15694 void (*xFinal)(sqlite3_context*), |
| 15695 FuncDestructor *pDestructor |
| 15696 ){ |
| 15697 FuncDef *p; |
| 15698 int nName; |
| 15699 int extraFlags; |
| 15700 |
| 15701 assert( sqlite3_mutex_held(db->mutex) ); |
| 15702 if( zFunctionName==0 || |
| 15703 (xSFunc && (xFinal || xStep)) || |
| 15704 (!xSFunc && (xFinal && !xStep)) || |
| 15705 (!xSFunc && (!xFinal && xStep)) || |
| 15706 (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || |
| 15707 (255<(nName = sqlite3Strlen30( zFunctionName))) ){ |
| 15708 return SQLITE_MISUSE_BKPT; |
| 15709 } |
| 15710 |
| 15711 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC ); |
| 15712 extraFlags = enc & SQLITE_DETERMINISTIC; |
| 15713 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY); |
| 15714 |
| 15715 #ifndef SQLITE_OMIT_UTF16 |
| 15716 /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 15717 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 15718 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 15719 ** |
| 15720 ** If SQLITE_ANY is specified, add three versions of the function |
| 15721 ** to the hash table. |
| 15722 */ |
| 15723 if( enc==SQLITE_UTF16 ){ |
| 15724 enc = SQLITE_UTF16NATIVE; |
| 15725 }else if( enc==SQLITE_ANY ){ |
| 15726 int rc; |
| 15727 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags, |
| 15728 pUserData, xSFunc, xStep, xFinal, pDestructor); |
| 15729 if( rc==SQLITE_OK ){ |
| 15730 rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags, |
| 15731 pUserData, xSFunc, xStep, xFinal, pDestructor); |
| 15732 } |
| 15733 if( rc!=SQLITE_OK ){ |
| 15734 return rc; |
| 15735 } |
| 15736 enc = SQLITE_UTF16BE; |
| 15737 } |
| 15738 #else |
| 15739 enc = SQLITE_UTF8; |
| 15740 #endif |
| 15741 |
| 15742 /* Check if an existing function is being overridden or deleted. If so, |
| 15743 ** and there are active VMs, then return SQLITE_BUSY. If a function |
| 15744 ** is being overridden/deleted but there are no active VMs, allow the |
| 15745 ** operation to continue but invalidate all precompiled statements. |
| 15746 */ |
| 15747 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 0); |
| 15748 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){ |
| 15749 if( db->nVdbeActive ){ |
| 15750 sqlite3ErrorWithMsg(db, SQLITE_BUSY, |
| 15751 "unable to delete/modify user-function due to active statements"); |
| 15752 assert( !db->mallocFailed ); |
| 15753 return SQLITE_BUSY; |
| 15754 }else{ |
| 15755 sqlite3ExpirePreparedStatements(db); |
| 15756 } |
| 15757 } |
| 15758 |
| 15759 p = sqlite3FindFunction(db, zFunctionName, nArg, (u8)enc, 1); |
| 15760 assert(p || db->mallocFailed); |
| 15761 if( !p ){ |
| 15762 return SQLITE_NOMEM_BKPT; |
| 15763 } |
| 15764 |
| 15765 /* If an older version of the function with a configured destructor is |
| 15766 ** being replaced invoke the destructor function here. */ |
| 15767 functionDestroy(db, p); |
| 15768 |
| 15769 if( pDestructor ){ |
| 15770 pDestructor->nRef++; |
| 15771 } |
| 15772 p->u.pDestructor = pDestructor; |
| 15773 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags; |
| 15774 testcase( p->funcFlags & SQLITE_DETERMINISTIC ); |
| 15775 p->xSFunc = xSFunc ? xSFunc : xStep; |
| 15776 p->xFinalize = xFinal; |
| 15777 p->pUserData = pUserData; |
| 15778 p->nArg = (u16)nArg; |
| 15779 return SQLITE_OK; |
| 15780 } |
| 15781 |
| 15782 /* |
| 15783 ** Create new user functions. |
| 15784 */ |
| 15785 SQLITE_API int sqlite3_create_function( |
| 15786 sqlite3 *db, |
| 15787 const char *zFunc, |
| 15788 int nArg, |
| 15789 int enc, |
| 15790 void *p, |
| 15791 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), |
| 15792 void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 15793 void (*xFinal)(sqlite3_context*) |
| 15794 ){ |
| 15795 return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xSFunc, xStep, |
| 15796 xFinal, 0); |
| 15797 } |
| 15798 |
| 15799 SQLITE_API int sqlite3_create_function_v2( |
| 15800 sqlite3 *db, |
| 15801 const char *zFunc, |
| 15802 int nArg, |
| 15803 int enc, |
| 15804 void *p, |
| 15805 void (*xSFunc)(sqlite3_context*,int,sqlite3_value **), |
| 15806 void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 15807 void (*xFinal)(sqlite3_context*), |
| 15808 void (*xDestroy)(void *) |
| 15809 ){ |
| 15810 int rc = SQLITE_ERROR; |
| 15811 FuncDestructor *pArg = 0; |
| 15812 |
| 15813 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15814 if( !sqlite3SafetyCheckOk(db) ){ |
| 15815 return SQLITE_MISUSE_BKPT; |
| 15816 } |
| 15817 #endif |
| 15818 sqlite3_mutex_enter(db->mutex); |
| 15819 if( xDestroy ){ |
| 15820 pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); |
| 15821 if( !pArg ){ |
| 15822 xDestroy(p); |
| 15823 goto out; |
| 15824 } |
| 15825 pArg->xDestroy = xDestroy; |
| 15826 pArg->pUserData = p; |
| 15827 } |
| 15828 rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg); |
| 15829 if( pArg && pArg->nRef==0 ){ |
| 15830 assert( rc!=SQLITE_OK ); |
| 15831 xDestroy(p); |
| 15832 sqlite3DbFree(db, pArg); |
| 15833 } |
| 15834 |
| 15835 out: |
| 15836 rc = sqlite3ApiExit(db, rc); |
| 15837 sqlite3_mutex_leave(db->mutex); |
| 15838 return rc; |
| 15839 } |
| 15840 |
| 15841 #ifndef SQLITE_OMIT_UTF16 |
| 15842 SQLITE_API int sqlite3_create_function16( |
| 15843 sqlite3 *db, |
| 15844 const void *zFunctionName, |
| 15845 int nArg, |
| 15846 int eTextRep, |
| 15847 void *p, |
| 15848 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**), |
| 15849 void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 15850 void (*xFinal)(sqlite3_context*) |
| 15851 ){ |
| 15852 int rc; |
| 15853 char *zFunc8; |
| 15854 |
| 15855 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15856 if( !sqlite3SafetyCheckOk(db) || zFunctionName==0 ) return SQLITE_MISUSE_BKPT; |
| 15857 #endif |
| 15858 sqlite3_mutex_enter(db->mutex); |
| 15859 assert( !db->mallocFailed ); |
| 15860 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); |
| 15861 rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xSFunc,xStep,xFinal,0); |
| 15862 sqlite3DbFree(db, zFunc8); |
| 15863 rc = sqlite3ApiExit(db, rc); |
| 15864 sqlite3_mutex_leave(db->mutex); |
| 15865 return rc; |
| 15866 } |
| 15867 #endif |
| 15868 |
| 15869 |
| 15870 /* |
| 15871 ** Declare that a function has been overloaded by a virtual table. |
| 15872 ** |
| 15873 ** If the function already exists as a regular global function, then |
| 15874 ** this routine is a no-op. If the function does not exist, then create |
| 15875 ** a new one that always throws a run-time error. |
| 15876 ** |
| 15877 ** When virtual tables intend to provide an overloaded function, they |
| 15878 ** should call this routine to make sure the global function exists. |
| 15879 ** A global function must exist in order for name resolution to work |
| 15880 ** properly. |
| 15881 */ |
| 15882 SQLITE_API int sqlite3_overload_function( |
| 15883 sqlite3 *db, |
| 15884 const char *zName, |
| 15885 int nArg |
| 15886 ){ |
| 15887 int rc = SQLITE_OK; |
| 15888 |
| 15889 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15890 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){ |
| 15891 return SQLITE_MISUSE_BKPT; |
| 15892 } |
| 15893 #endif |
| 15894 sqlite3_mutex_enter(db->mutex); |
| 15895 if( sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)==0 ){ |
| 15896 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, |
| 15897 0, sqlite3InvalidFunction, 0, 0, 0); |
| 15898 } |
| 15899 rc = sqlite3ApiExit(db, rc); |
| 15900 sqlite3_mutex_leave(db->mutex); |
| 15901 return rc; |
| 15902 } |
| 15903 |
| 15904 #ifndef SQLITE_OMIT_TRACE |
| 15905 /* |
| 15906 ** Register a trace function. The pArg from the previously registered trace |
| 15907 ** is returned. |
| 15908 ** |
| 15909 ** A NULL trace function means that no tracing is executes. A non-NULL |
| 15910 ** trace is a pointer to a function that is invoked at the start of each |
| 15911 ** SQL statement. |
| 15912 */ |
| 15913 #ifndef SQLITE_OMIT_DEPRECATED |
| 15914 SQLITE_API void *sqlite3_trace(sqlite3 *db, void(*xTrace)(void*,const char*), vo
id *pArg){ |
| 15915 void *pOld; |
| 15916 |
| 15917 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15918 if( !sqlite3SafetyCheckOk(db) ){ |
| 15919 (void)SQLITE_MISUSE_BKPT; |
| 15920 return 0; |
| 15921 } |
| 15922 #endif |
| 15923 sqlite3_mutex_enter(db->mutex); |
| 15924 pOld = db->pTraceArg; |
| 15925 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0; |
| 15926 db->xTrace = (int(*)(u32,void*,void*,void*))xTrace; |
| 15927 db->pTraceArg = pArg; |
| 15928 sqlite3_mutex_leave(db->mutex); |
| 15929 return pOld; |
| 15930 } |
| 15931 #endif /* SQLITE_OMIT_DEPRECATED */ |
| 15932 |
| 15933 /* Register a trace callback using the version-2 interface. |
| 15934 */ |
| 15935 SQLITE_API int sqlite3_trace_v2( |
| 15936 sqlite3 *db, /* Trace this connection */ |
| 15937 unsigned mTrace, /* Mask of events to be traced */ |
| 15938 int(*xTrace)(unsigned,void*,void*,void*), /* Callback to invoke */ |
| 15939 void *pArg /* Context */ |
| 15940 ){ |
| 15941 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15942 if( !sqlite3SafetyCheckOk(db) ){ |
| 15943 return SQLITE_MISUSE_BKPT; |
| 15944 } |
| 15945 #endif |
| 15946 sqlite3_mutex_enter(db->mutex); |
| 15947 if( mTrace==0 ) xTrace = 0; |
| 15948 if( xTrace==0 ) mTrace = 0; |
| 15949 db->mTrace = mTrace; |
| 15950 db->xTrace = xTrace; |
| 15951 db->pTraceArg = pArg; |
| 15952 sqlite3_mutex_leave(db->mutex); |
| 15953 return SQLITE_OK; |
| 15954 } |
| 15955 |
| 15956 #ifndef SQLITE_OMIT_DEPRECATED |
| 15957 /* |
| 15958 ** Register a profile function. The pArg from the previously registered |
| 15959 ** profile function is returned. |
| 15960 ** |
| 15961 ** A NULL profile function means that no profiling is executes. A non-NULL |
| 15962 ** profile is a pointer to a function that is invoked at the conclusion of |
| 15963 ** each SQL statement that is run. |
| 15964 */ |
| 15965 SQLITE_API void *sqlite3_profile( |
| 15966 sqlite3 *db, |
| 15967 void (*xProfile)(void*,const char*,sqlite_uint64), |
| 15968 void *pArg |
| 15969 ){ |
| 15970 void *pOld; |
| 15971 |
| 15972 #ifdef SQLITE_ENABLE_API_ARMOR |
| 15973 if( !sqlite3SafetyCheckOk(db) ){ |
| 15974 (void)SQLITE_MISUSE_BKPT; |
| 15975 return 0; |
| 15976 } |
| 15977 #endif |
| 15978 sqlite3_mutex_enter(db->mutex); |
| 15979 pOld = db->pProfileArg; |
| 15980 db->xProfile = xProfile; |
| 15981 db->pProfileArg = pArg; |
| 15982 sqlite3_mutex_leave(db->mutex); |
| 15983 return pOld; |
| 15984 } |
| 15985 #endif /* SQLITE_OMIT_DEPRECATED */ |
| 15986 #endif /* SQLITE_OMIT_TRACE */ |
| 15987 |
| 15988 /* |
| 15989 ** Register a function to be invoked when a transaction commits. |
| 15990 ** If the invoked function returns non-zero, then the commit becomes a |
| 15991 ** rollback. |
| 15992 */ |
| 15993 SQLITE_API void *sqlite3_commit_hook( |
| 15994 sqlite3 *db, /* Attach the hook to this database */ |
| 15995 int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 15996 void *pArg /* Argument to the function */ |
| 15997 ){ |
| 15998 void *pOld; |
| 15999 |
| 16000 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16001 if( !sqlite3SafetyCheckOk(db) ){ |
| 16002 (void)SQLITE_MISUSE_BKPT; |
| 16003 return 0; |
| 16004 } |
| 16005 #endif |
| 16006 sqlite3_mutex_enter(db->mutex); |
| 16007 pOld = db->pCommitArg; |
| 16008 db->xCommitCallback = xCallback; |
| 16009 db->pCommitArg = pArg; |
| 16010 sqlite3_mutex_leave(db->mutex); |
| 16011 return pOld; |
| 16012 } |
| 16013 |
| 16014 /* |
| 16015 ** Register a callback to be invoked each time a row is updated, |
| 16016 ** inserted or deleted using this database connection. |
| 16017 */ |
| 16018 SQLITE_API void *sqlite3_update_hook( |
| 16019 sqlite3 *db, /* Attach the hook to this database */ |
| 16020 void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), |
| 16021 void *pArg /* Argument to the function */ |
| 16022 ){ |
| 16023 void *pRet; |
| 16024 |
| 16025 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16026 if( !sqlite3SafetyCheckOk(db) ){ |
| 16027 (void)SQLITE_MISUSE_BKPT; |
| 16028 return 0; |
| 16029 } |
| 16030 #endif |
| 16031 sqlite3_mutex_enter(db->mutex); |
| 16032 pRet = db->pUpdateArg; |
| 16033 db->xUpdateCallback = xCallback; |
| 16034 db->pUpdateArg = pArg; |
| 16035 sqlite3_mutex_leave(db->mutex); |
| 16036 return pRet; |
| 16037 } |
| 16038 |
| 16039 /* |
| 16040 ** Register a callback to be invoked each time a transaction is rolled |
| 16041 ** back by this database connection. |
| 16042 */ |
| 16043 SQLITE_API void *sqlite3_rollback_hook( |
| 16044 sqlite3 *db, /* Attach the hook to this database */ |
| 16045 void (*xCallback)(void*), /* Callback function */ |
| 16046 void *pArg /* Argument to the function */ |
| 16047 ){ |
| 16048 void *pRet; |
| 16049 |
| 16050 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16051 if( !sqlite3SafetyCheckOk(db) ){ |
| 16052 (void)SQLITE_MISUSE_BKPT; |
| 16053 return 0; |
| 16054 } |
| 16055 #endif |
| 16056 sqlite3_mutex_enter(db->mutex); |
| 16057 pRet = db->pRollbackArg; |
| 16058 db->xRollbackCallback = xCallback; |
| 16059 db->pRollbackArg = pArg; |
| 16060 sqlite3_mutex_leave(db->mutex); |
| 16061 return pRet; |
| 16062 } |
| 16063 |
| 16064 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 16065 /* |
| 16066 ** Register a callback to be invoked each time a row is updated, |
| 16067 ** inserted or deleted using this database connection. |
| 16068 */ |
| 16069 SQLITE_API void *sqlite3_preupdate_hook( |
| 16070 sqlite3 *db, /* Attach the hook to this database */ |
| 16071 void(*xCallback)( /* Callback function */ |
| 16072 void*,sqlite3*,int,char const*,char const*,sqlite3_int64,sqlite3_int64), |
| 16073 void *pArg /* First callback argument */ |
| 16074 ){ |
| 16075 void *pRet; |
| 16076 sqlite3_mutex_enter(db->mutex); |
| 16077 pRet = db->pPreUpdateArg; |
| 16078 db->xPreUpdateCallback = xCallback; |
| 16079 db->pPreUpdateArg = pArg; |
| 16080 sqlite3_mutex_leave(db->mutex); |
| 16081 return pRet; |
| 16082 } |
| 16083 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 16084 |
| 16085 #ifndef SQLITE_OMIT_WAL |
| 16086 /* |
| 16087 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). |
| 16088 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file |
| 16089 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by |
| 16090 ** wal_autocheckpoint()). |
| 16091 */ |
| 16092 SQLITE_PRIVATE int sqlite3WalDefaultHook( |
| 16093 void *pClientData, /* Argument */ |
| 16094 sqlite3 *db, /* Connection */ |
| 16095 const char *zDb, /* Database */ |
| 16096 int nFrame /* Size of WAL */ |
| 16097 ){ |
| 16098 if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){ |
| 16099 sqlite3BeginBenignMalloc(); |
| 16100 sqlite3_wal_checkpoint(db, zDb); |
| 16101 sqlite3EndBenignMalloc(); |
| 16102 } |
| 16103 return SQLITE_OK; |
| 16104 } |
| 16105 #endif /* SQLITE_OMIT_WAL */ |
| 16106 |
| 16107 /* |
| 16108 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint |
| 16109 ** a database after committing a transaction if there are nFrame or |
| 16110 ** more frames in the log file. Passing zero or a negative value as the |
| 16111 ** nFrame parameter disables automatic checkpoints entirely. |
| 16112 ** |
| 16113 ** The callback registered by this function replaces any existing callback |
| 16114 ** registered using sqlite3_wal_hook(). Likewise, registering a callback |
| 16115 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism |
| 16116 ** configured by this function. |
| 16117 */ |
| 16118 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ |
| 16119 #ifdef SQLITE_OMIT_WAL |
| 16120 UNUSED_PARAMETER(db); |
| 16121 UNUSED_PARAMETER(nFrame); |
| 16122 #else |
| 16123 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16124 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 16125 #endif |
| 16126 if( nFrame>0 ){ |
| 16127 sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); |
| 16128 }else{ |
| 16129 sqlite3_wal_hook(db, 0, 0); |
| 16130 } |
| 16131 #endif |
| 16132 return SQLITE_OK; |
| 16133 } |
| 16134 |
| 16135 /* |
| 16136 ** Register a callback to be invoked each time a transaction is written |
| 16137 ** into the write-ahead-log by this database connection. |
| 16138 */ |
| 16139 SQLITE_API void *sqlite3_wal_hook( |
| 16140 sqlite3 *db, /* Attach the hook to this db handle */ |
| 16141 int(*xCallback)(void *, sqlite3*, const char*, int), |
| 16142 void *pArg /* First argument passed to xCallback() */ |
| 16143 ){ |
| 16144 #ifndef SQLITE_OMIT_WAL |
| 16145 void *pRet; |
| 16146 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16147 if( !sqlite3SafetyCheckOk(db) ){ |
| 16148 (void)SQLITE_MISUSE_BKPT; |
| 16149 return 0; |
| 16150 } |
| 16151 #endif |
| 16152 sqlite3_mutex_enter(db->mutex); |
| 16153 pRet = db->pWalArg; |
| 16154 db->xWalCallback = xCallback; |
| 16155 db->pWalArg = pArg; |
| 16156 sqlite3_mutex_leave(db->mutex); |
| 16157 return pRet; |
| 16158 #else |
| 16159 return 0; |
| 16160 #endif |
| 16161 } |
| 16162 |
| 16163 /* |
| 16164 ** Checkpoint database zDb. |
| 16165 */ |
| 16166 SQLITE_API int sqlite3_wal_checkpoint_v2( |
| 16167 sqlite3 *db, /* Database handle */ |
| 16168 const char *zDb, /* Name of attached database (or NULL) */ |
| 16169 int eMode, /* SQLITE_CHECKPOINT_* value */ |
| 16170 int *pnLog, /* OUT: Size of WAL log in frames */ |
| 16171 int *pnCkpt /* OUT: Total number of frames checkpointed */ |
| 16172 ){ |
| 16173 #ifdef SQLITE_OMIT_WAL |
| 16174 return SQLITE_OK; |
| 16175 #else |
| 16176 int rc; /* Return code */ |
| 16177 int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ |
| 16178 |
| 16179 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16180 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 16181 #endif |
| 16182 |
| 16183 /* Initialize the output variables to -1 in case an error occurs. */ |
| 16184 if( pnLog ) *pnLog = -1; |
| 16185 if( pnCkpt ) *pnCkpt = -1; |
| 16186 |
| 16187 assert( SQLITE_CHECKPOINT_PASSIVE==0 ); |
| 16188 assert( SQLITE_CHECKPOINT_FULL==1 ); |
| 16189 assert( SQLITE_CHECKPOINT_RESTART==2 ); |
| 16190 assert( SQLITE_CHECKPOINT_TRUNCATE==3 ); |
| 16191 if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_TRUNCATE ){ |
| 16192 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint |
| 16193 ** mode: */ |
| 16194 return SQLITE_MISUSE; |
| 16195 } |
| 16196 |
| 16197 sqlite3_mutex_enter(db->mutex); |
| 16198 if( zDb && zDb[0] ){ |
| 16199 iDb = sqlite3FindDbName(db, zDb); |
| 16200 } |
| 16201 if( iDb<0 ){ |
| 16202 rc = SQLITE_ERROR; |
| 16203 sqlite3ErrorWithMsg(db, SQLITE_ERROR, "unknown database: %s", zDb); |
| 16204 }else{ |
| 16205 db->busyHandler.nBusy = 0; |
| 16206 rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); |
| 16207 sqlite3Error(db, rc); |
| 16208 } |
| 16209 rc = sqlite3ApiExit(db, rc); |
| 16210 |
| 16211 /* If there are no active statements, clear the interrupt flag at this |
| 16212 ** point. */ |
| 16213 if( db->nVdbeActive==0 ){ |
| 16214 db->u1.isInterrupted = 0; |
| 16215 } |
| 16216 |
| 16217 sqlite3_mutex_leave(db->mutex); |
| 16218 return rc; |
| 16219 #endif |
| 16220 } |
| 16221 |
| 16222 |
| 16223 /* |
| 16224 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points |
| 16225 ** to contains a zero-length string, all attached databases are |
| 16226 ** checkpointed. |
| 16227 */ |
| 16228 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ |
| 16229 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to |
| 16230 ** sqlite3_wal_checkpoint_v2(D,X,SQLITE_CHECKPOINT_PASSIVE,0,0). */ |
| 16231 return sqlite3_wal_checkpoint_v2(db,zDb,SQLITE_CHECKPOINT_PASSIVE,0,0); |
| 16232 } |
| 16233 |
| 16234 #ifndef SQLITE_OMIT_WAL |
| 16235 /* |
| 16236 ** Run a checkpoint on database iDb. This is a no-op if database iDb is |
| 16237 ** not currently open in WAL mode. |
| 16238 ** |
| 16239 ** If a transaction is open on the database being checkpointed, this |
| 16240 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If |
| 16241 ** an error occurs while running the checkpoint, an SQLite error code is |
| 16242 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. |
| 16243 ** |
| 16244 ** The mutex on database handle db should be held by the caller. The mutex |
| 16245 ** associated with the specific b-tree being checkpointed is taken by |
| 16246 ** this function while the checkpoint is running. |
| 16247 ** |
| 16248 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are |
| 16249 ** checkpointed. If an error is encountered it is returned immediately - |
| 16250 ** no attempt is made to checkpoint any remaining databases. |
| 16251 ** |
| 16252 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. |
| 16253 */ |
| 16254 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog
, int *pnCkpt){ |
| 16255 int rc = SQLITE_OK; /* Return code */ |
| 16256 int i; /* Used to iterate through attached dbs */ |
| 16257 int bBusy = 0; /* True if SQLITE_BUSY has been encountered */ |
| 16258 |
| 16259 assert( sqlite3_mutex_held(db->mutex) ); |
| 16260 assert( !pnLog || *pnLog==-1 ); |
| 16261 assert( !pnCkpt || *pnCkpt==-1 ); |
| 16262 |
| 16263 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){ |
| 16264 if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ |
| 16265 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt); |
| 16266 pnLog = 0; |
| 16267 pnCkpt = 0; |
| 16268 if( rc==SQLITE_BUSY ){ |
| 16269 bBusy = 1; |
| 16270 rc = SQLITE_OK; |
| 16271 } |
| 16272 } |
| 16273 } |
| 16274 |
| 16275 return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc; |
| 16276 } |
| 16277 #endif /* SQLITE_OMIT_WAL */ |
| 16278 |
| 16279 /* |
| 16280 ** This function returns true if main-memory should be used instead of |
| 16281 ** a temporary file for transient pager files and statement journals. |
| 16282 ** The value returned depends on the value of db->temp_store (runtime |
| 16283 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The |
| 16284 ** following table describes the relationship between these two values |
| 16285 ** and this functions return value. |
| 16286 ** |
| 16287 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database |
| 16288 ** ----------------- -------------- ------------------------------ |
| 16289 ** 0 any file (return 0) |
| 16290 ** 1 1 file (return 0) |
| 16291 ** 1 2 memory (return 1) |
| 16292 ** 1 0 file (return 0) |
| 16293 ** 2 1 file (return 0) |
| 16294 ** 2 2 memory (return 1) |
| 16295 ** 2 0 memory (return 1) |
| 16296 ** 3 any memory (return 1) |
| 16297 */ |
| 16298 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){ |
| 16299 #if SQLITE_TEMP_STORE==1 |
| 16300 return ( db->temp_store==2 ); |
| 16301 #endif |
| 16302 #if SQLITE_TEMP_STORE==2 |
| 16303 return ( db->temp_store!=1 ); |
| 16304 #endif |
| 16305 #if SQLITE_TEMP_STORE==3 |
| 16306 UNUSED_PARAMETER(db); |
| 16307 return 1; |
| 16308 #endif |
| 16309 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 |
| 16310 UNUSED_PARAMETER(db); |
| 16311 return 0; |
| 16312 #endif |
| 16313 } |
| 16314 |
| 16315 /* |
| 16316 ** Return UTF-8 encoded English language explanation of the most recent |
| 16317 ** error. |
| 16318 */ |
| 16319 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){ |
| 16320 const char *z; |
| 16321 if( !db ){ |
| 16322 return sqlite3ErrStr(SQLITE_NOMEM_BKPT); |
| 16323 } |
| 16324 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
| 16325 return sqlite3ErrStr(SQLITE_MISUSE_BKPT); |
| 16326 } |
| 16327 sqlite3_mutex_enter(db->mutex); |
| 16328 if( db->mallocFailed ){ |
| 16329 z = sqlite3ErrStr(SQLITE_NOMEM_BKPT); |
| 16330 }else{ |
| 16331 testcase( db->pErr==0 ); |
| 16332 z = (char*)sqlite3_value_text(db->pErr); |
| 16333 assert( !db->mallocFailed ); |
| 16334 if( z==0 ){ |
| 16335 z = sqlite3ErrStr(db->errCode); |
| 16336 } |
| 16337 } |
| 16338 sqlite3_mutex_leave(db->mutex); |
| 16339 return z; |
| 16340 } |
| 16341 |
| 16342 #ifndef SQLITE_OMIT_UTF16 |
| 16343 /* |
| 16344 ** Return UTF-16 encoded English language explanation of the most recent |
| 16345 ** error. |
| 16346 */ |
| 16347 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){ |
| 16348 static const u16 outOfMem[] = { |
| 16349 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 |
| 16350 }; |
| 16351 static const u16 misuse[] = { |
| 16352 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', |
| 16353 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', |
| 16354 'c', 'a', 'l', 'l', 'e', 'd', ' ', |
| 16355 'o', 'u', 't', ' ', |
| 16356 'o', 'f', ' ', |
| 16357 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0 |
| 16358 }; |
| 16359 |
| 16360 const void *z; |
| 16361 if( !db ){ |
| 16362 return (void *)outOfMem; |
| 16363 } |
| 16364 if( !sqlite3SafetyCheckSickOrOk(db) ){ |
| 16365 return (void *)misuse; |
| 16366 } |
| 16367 sqlite3_mutex_enter(db->mutex); |
| 16368 if( db->mallocFailed ){ |
| 16369 z = (void *)outOfMem; |
| 16370 }else{ |
| 16371 z = sqlite3_value_text16(db->pErr); |
| 16372 if( z==0 ){ |
| 16373 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode)); |
| 16374 z = sqlite3_value_text16(db->pErr); |
| 16375 } |
| 16376 /* A malloc() may have failed within the call to sqlite3_value_text16() |
| 16377 ** above. If this is the case, then the db->mallocFailed flag needs to |
| 16378 ** be cleared before returning. Do this directly, instead of via |
| 16379 ** sqlite3ApiExit(), to avoid setting the database handle error message. |
| 16380 */ |
| 16381 sqlite3OomClear(db); |
| 16382 } |
| 16383 sqlite3_mutex_leave(db->mutex); |
| 16384 return z; |
| 16385 } |
| 16386 #endif /* SQLITE_OMIT_UTF16 */ |
| 16387 |
| 16388 /* |
| 16389 ** Return the most recent error code generated by an SQLite routine. If NULL is |
| 16390 ** passed to this function, we assume a malloc() failed during sqlite3_open(). |
| 16391 */ |
| 16392 SQLITE_API int sqlite3_errcode(sqlite3 *db){ |
| 16393 if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
| 16394 return SQLITE_MISUSE_BKPT; |
| 16395 } |
| 16396 if( !db || db->mallocFailed ){ |
| 16397 return SQLITE_NOMEM_BKPT; |
| 16398 } |
| 16399 return db->errCode & db->errMask; |
| 16400 } |
| 16401 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){ |
| 16402 if( db && !sqlite3SafetyCheckSickOrOk(db) ){ |
| 16403 return SQLITE_MISUSE_BKPT; |
| 16404 } |
| 16405 if( !db || db->mallocFailed ){ |
| 16406 return SQLITE_NOMEM_BKPT; |
| 16407 } |
| 16408 return db->errCode; |
| 16409 } |
| 16410 SQLITE_API int sqlite3_system_errno(sqlite3 *db){ |
| 16411 return db ? db->iSysErrno : 0; |
| 16412 } |
| 16413 |
| 16414 /* |
| 16415 ** Return a string that describes the kind of error specified in the |
| 16416 ** argument. For now, this simply calls the internal sqlite3ErrStr() |
| 16417 ** function. |
| 16418 */ |
| 16419 SQLITE_API const char *sqlite3_errstr(int rc){ |
| 16420 return sqlite3ErrStr(rc); |
| 16421 } |
| 16422 |
| 16423 /* |
| 16424 ** Create a new collating function for database "db". The name is zName |
| 16425 ** and the encoding is enc. |
| 16426 */ |
| 16427 static int createCollation( |
| 16428 sqlite3* db, |
| 16429 const char *zName, |
| 16430 u8 enc, |
| 16431 void* pCtx, |
| 16432 int(*xCompare)(void*,int,const void*,int,const void*), |
| 16433 void(*xDel)(void*) |
| 16434 ){ |
| 16435 CollSeq *pColl; |
| 16436 int enc2; |
| 16437 |
| 16438 assert( sqlite3_mutex_held(db->mutex) ); |
| 16439 |
| 16440 /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 16441 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 16442 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 16443 */ |
| 16444 enc2 = enc; |
| 16445 testcase( enc2==SQLITE_UTF16 ); |
| 16446 testcase( enc2==SQLITE_UTF16_ALIGNED ); |
| 16447 if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ |
| 16448 enc2 = SQLITE_UTF16NATIVE; |
| 16449 } |
| 16450 if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){ |
| 16451 return SQLITE_MISUSE_BKPT; |
| 16452 } |
| 16453 |
| 16454 /* Check if this call is removing or replacing an existing collation |
| 16455 ** sequence. If so, and there are active VMs, return busy. If there |
| 16456 ** are no active VMs, invalidate any pre-compiled statements. |
| 16457 */ |
| 16458 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); |
| 16459 if( pColl && pColl->xCmp ){ |
| 16460 if( db->nVdbeActive ){ |
| 16461 sqlite3ErrorWithMsg(db, SQLITE_BUSY, |
| 16462 "unable to delete/modify collation sequence due to active statements"); |
| 16463 return SQLITE_BUSY; |
| 16464 } |
| 16465 sqlite3ExpirePreparedStatements(db); |
| 16466 |
| 16467 /* If collation sequence pColl was created directly by a call to |
| 16468 ** sqlite3_create_collation, and not generated by synthCollSeq(), |
| 16469 ** then any copies made by synthCollSeq() need to be invalidated. |
| 16470 ** Also, collation destructor - CollSeq.xDel() - function may need |
| 16471 ** to be called. |
| 16472 */ |
| 16473 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ |
| 16474 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName); |
| 16475 int j; |
| 16476 for(j=0; j<3; j++){ |
| 16477 CollSeq *p = &aColl[j]; |
| 16478 if( p->enc==pColl->enc ){ |
| 16479 if( p->xDel ){ |
| 16480 p->xDel(p->pUser); |
| 16481 } |
| 16482 p->xCmp = 0; |
| 16483 } |
| 16484 } |
| 16485 } |
| 16486 } |
| 16487 |
| 16488 pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); |
| 16489 if( pColl==0 ) return SQLITE_NOMEM_BKPT; |
| 16490 pColl->xCmp = xCompare; |
| 16491 pColl->pUser = pCtx; |
| 16492 pColl->xDel = xDel; |
| 16493 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); |
| 16494 sqlite3Error(db, SQLITE_OK); |
| 16495 return SQLITE_OK; |
| 16496 } |
| 16497 |
| 16498 |
| 16499 /* |
| 16500 ** This array defines hard upper bounds on limit values. The |
| 16501 ** initializer must be kept in sync with the SQLITE_LIMIT_* |
| 16502 ** #defines in sqlite3.h. |
| 16503 */ |
| 16504 static const int aHardLimit[] = { |
| 16505 SQLITE_MAX_LENGTH, |
| 16506 SQLITE_MAX_SQL_LENGTH, |
| 16507 SQLITE_MAX_COLUMN, |
| 16508 SQLITE_MAX_EXPR_DEPTH, |
| 16509 SQLITE_MAX_COMPOUND_SELECT, |
| 16510 SQLITE_MAX_VDBE_OP, |
| 16511 SQLITE_MAX_FUNCTION_ARG, |
| 16512 SQLITE_MAX_ATTACHED, |
| 16513 SQLITE_MAX_LIKE_PATTERN_LENGTH, |
| 16514 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */ |
| 16515 SQLITE_MAX_TRIGGER_DEPTH, |
| 16516 SQLITE_MAX_WORKER_THREADS, |
| 16517 }; |
| 16518 |
| 16519 /* |
| 16520 ** Make sure the hard limits are set to reasonable values |
| 16521 */ |
| 16522 #if SQLITE_MAX_LENGTH<100 |
| 16523 # error SQLITE_MAX_LENGTH must be at least 100 |
| 16524 #endif |
| 16525 #if SQLITE_MAX_SQL_LENGTH<100 |
| 16526 # error SQLITE_MAX_SQL_LENGTH must be at least 100 |
| 16527 #endif |
| 16528 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH |
| 16529 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH |
| 16530 #endif |
| 16531 #if SQLITE_MAX_COMPOUND_SELECT<2 |
| 16532 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2 |
| 16533 #endif |
| 16534 #if SQLITE_MAX_VDBE_OP<40 |
| 16535 # error SQLITE_MAX_VDBE_OP must be at least 40 |
| 16536 #endif |
| 16537 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>127 |
| 16538 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 127 |
| 16539 #endif |
| 16540 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>125 |
| 16541 # error SQLITE_MAX_ATTACHED must be between 0 and 125 |
| 16542 #endif |
| 16543 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 |
| 16544 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 |
| 16545 #endif |
| 16546 #if SQLITE_MAX_COLUMN>32767 |
| 16547 # error SQLITE_MAX_COLUMN must not exceed 32767 |
| 16548 #endif |
| 16549 #if SQLITE_MAX_TRIGGER_DEPTH<1 |
| 16550 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 |
| 16551 #endif |
| 16552 #if SQLITE_MAX_WORKER_THREADS<0 || SQLITE_MAX_WORKER_THREADS>50 |
| 16553 # error SQLITE_MAX_WORKER_THREADS must be between 0 and 50 |
| 16554 #endif |
| 16555 |
| 16556 |
| 16557 /* |
| 16558 ** Change the value of a limit. Report the old value. |
| 16559 ** If an invalid limit index is supplied, report -1. |
| 16560 ** Make no changes but still report the old value if the |
| 16561 ** new limit is negative. |
| 16562 ** |
| 16563 ** A new lower limit does not shrink existing constructs. |
| 16564 ** It merely prevents new constructs that exceed the limit |
| 16565 ** from forming. |
| 16566 */ |
| 16567 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ |
| 16568 int oldLimit; |
| 16569 |
| 16570 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16571 if( !sqlite3SafetyCheckOk(db) ){ |
| 16572 (void)SQLITE_MISUSE_BKPT; |
| 16573 return -1; |
| 16574 } |
| 16575 #endif |
| 16576 |
| 16577 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME |
| 16578 ** there is a hard upper bound set at compile-time by a C preprocessor |
| 16579 ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to |
| 16580 ** "_MAX_".) |
| 16581 */ |
| 16582 assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); |
| 16583 assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); |
| 16584 assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); |
| 16585 assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); |
| 16586 assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); |
| 16587 assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); |
| 16588 assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); |
| 16589 assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); |
| 16590 assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== |
| 16591 SQLITE_MAX_LIKE_PATTERN_LENGTH ); |
| 16592 assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); |
| 16593 assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); |
| 16594 assert( aHardLimit[SQLITE_LIMIT_WORKER_THREADS]==SQLITE_MAX_WORKER_THREADS ); |
| 16595 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) ); |
| 16596 |
| 16597 |
| 16598 if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ |
| 16599 return -1; |
| 16600 } |
| 16601 oldLimit = db->aLimit[limitId]; |
| 16602 if( newLimit>=0 ){ /* IMP: R-52476-28732 */ |
| 16603 if( newLimit>aHardLimit[limitId] ){ |
| 16604 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ |
| 16605 } |
| 16606 db->aLimit[limitId] = newLimit; |
| 16607 } |
| 16608 return oldLimit; /* IMP: R-53341-35419 */ |
| 16609 } |
| 16610 |
| 16611 /* |
| 16612 ** This function is used to parse both URIs and non-URI filenames passed by the |
| 16613 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database |
| 16614 ** URIs specified as part of ATTACH statements. |
| 16615 ** |
| 16616 ** The first argument to this function is the name of the VFS to use (or |
| 16617 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx" |
| 16618 ** query parameter. The second argument contains the URI (or non-URI filename) |
| 16619 ** itself. When this function is called the *pFlags variable should contain |
| 16620 ** the default flags to open the database handle with. The value stored in |
| 16621 ** *pFlags may be updated before returning if the URI filename contains |
| 16622 ** "cache=xxx" or "mode=xxx" query parameters. |
| 16623 ** |
| 16624 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to |
| 16625 ** the VFS that should be used to open the database file. *pzFile is set to |
| 16626 ** point to a buffer containing the name of the file to open. It is the |
| 16627 ** responsibility of the caller to eventually call sqlite3_free() to release |
| 16628 ** this buffer. |
| 16629 ** |
| 16630 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg |
| 16631 ** may be set to point to a buffer containing an English language error |
| 16632 ** message. It is the responsibility of the caller to eventually release |
| 16633 ** this buffer by calling sqlite3_free(). |
| 16634 */ |
| 16635 SQLITE_PRIVATE int sqlite3ParseUri( |
| 16636 const char *zDefaultVfs, /* VFS to use if no "vfs=xxx" query option */ |
| 16637 const char *zUri, /* Nul-terminated URI to parse */ |
| 16638 unsigned int *pFlags, /* IN/OUT: SQLITE_OPEN_XXX flags */ |
| 16639 sqlite3_vfs **ppVfs, /* OUT: VFS to use */ |
| 16640 char **pzFile, /* OUT: Filename component of URI */ |
| 16641 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */ |
| 16642 ){ |
| 16643 int rc = SQLITE_OK; |
| 16644 unsigned int flags = *pFlags; |
| 16645 const char *zVfs = zDefaultVfs; |
| 16646 char *zFile; |
| 16647 char c; |
| 16648 int nUri = sqlite3Strlen30(zUri); |
| 16649 |
| 16650 assert( *pzErrMsg==0 ); |
| 16651 |
| 16652 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */ |
| 16653 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */ |
| 16654 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */ |
| 16655 ){ |
| 16656 char *zOpt; |
| 16657 int eState; /* Parser state when parsing URI */ |
| 16658 int iIn; /* Input character index */ |
| 16659 int iOut = 0; /* Output character index */ |
| 16660 u64 nByte = nUri+2; /* Bytes of space to allocate */ |
| 16661 |
| 16662 /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen |
| 16663 ** method that there may be extra parameters following the file-name. */ |
| 16664 flags |= SQLITE_OPEN_URI; |
| 16665 |
| 16666 for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); |
| 16667 zFile = sqlite3_malloc64(nByte); |
| 16668 if( !zFile ) return SQLITE_NOMEM_BKPT; |
| 16669 |
| 16670 iIn = 5; |
| 16671 #ifdef SQLITE_ALLOW_URI_AUTHORITY |
| 16672 if( strncmp(zUri+5, "///", 3)==0 ){ |
| 16673 iIn = 7; |
| 16674 /* The following condition causes URIs with five leading / characters |
| 16675 ** like file://///host/path to be converted into UNCs like //host/path. |
| 16676 ** The correct URI for that UNC has only two or four leading / characters |
| 16677 ** file://host/path or file:////host/path. But 5 leading slashes is a |
| 16678 ** common error, we are told, so we handle it as a special case. */ |
| 16679 if( strncmp(zUri+7, "///", 3)==0 ){ iIn++; } |
| 16680 }else if( strncmp(zUri+5, "//localhost/", 12)==0 ){ |
| 16681 iIn = 16; |
| 16682 } |
| 16683 #else |
| 16684 /* Discard the scheme and authority segments of the URI. */ |
| 16685 if( zUri[5]=='/' && zUri[6]=='/' ){ |
| 16686 iIn = 7; |
| 16687 while( zUri[iIn] && zUri[iIn]!='/' ) iIn++; |
| 16688 if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){ |
| 16689 *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s", |
| 16690 iIn-7, &zUri[7]); |
| 16691 rc = SQLITE_ERROR; |
| 16692 goto parse_uri_out; |
| 16693 } |
| 16694 } |
| 16695 #endif |
| 16696 |
| 16697 /* Copy the filename and any query parameters into the zFile buffer. |
| 16698 ** Decode %HH escape codes along the way. |
| 16699 ** |
| 16700 ** Within this loop, variable eState may be set to 0, 1 or 2, depending |
| 16701 ** on the parsing context. As follows: |
| 16702 ** |
| 16703 ** 0: Parsing file-name. |
| 16704 ** 1: Parsing name section of a name=value query parameter. |
| 16705 ** 2: Parsing value section of a name=value query parameter. |
| 16706 */ |
| 16707 eState = 0; |
| 16708 while( (c = zUri[iIn])!=0 && c!='#' ){ |
| 16709 iIn++; |
| 16710 if( c=='%' |
| 16711 && sqlite3Isxdigit(zUri[iIn]) |
| 16712 && sqlite3Isxdigit(zUri[iIn+1]) |
| 16713 ){ |
| 16714 int octet = (sqlite3HexToInt(zUri[iIn++]) << 4); |
| 16715 octet += sqlite3HexToInt(zUri[iIn++]); |
| 16716 |
| 16717 assert( octet>=0 && octet<256 ); |
| 16718 if( octet==0 ){ |
| 16719 #ifndef SQLITE_ENABLE_URI_00_ERROR |
| 16720 /* This branch is taken when "%00" appears within the URI. In this |
| 16721 ** case we ignore all text in the remainder of the path, name or |
| 16722 ** value currently being parsed. So ignore the current character |
| 16723 ** and skip to the next "?", "=" or "&", as appropriate. */ |
| 16724 while( (c = zUri[iIn])!=0 && c!='#' |
| 16725 && (eState!=0 || c!='?') |
| 16726 && (eState!=1 || (c!='=' && c!='&')) |
| 16727 && (eState!=2 || c!='&') |
| 16728 ){ |
| 16729 iIn++; |
| 16730 } |
| 16731 continue; |
| 16732 #else |
| 16733 /* If ENABLE_URI_00_ERROR is defined, "%00" in a URI is an error. */ |
| 16734 *pzErrMsg = sqlite3_mprintf("unexpected %%00 in uri"); |
| 16735 rc = SQLITE_ERROR; |
| 16736 goto parse_uri_out; |
| 16737 #endif |
| 16738 } |
| 16739 c = octet; |
| 16740 }else if( eState==1 && (c=='&' || c=='=') ){ |
| 16741 if( zFile[iOut-1]==0 ){ |
| 16742 /* An empty option name. Ignore this option altogether. */ |
| 16743 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++; |
| 16744 continue; |
| 16745 } |
| 16746 if( c=='&' ){ |
| 16747 zFile[iOut++] = '\0'; |
| 16748 }else{ |
| 16749 eState = 2; |
| 16750 } |
| 16751 c = 0; |
| 16752 }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){ |
| 16753 c = 0; |
| 16754 eState = 1; |
| 16755 } |
| 16756 zFile[iOut++] = c; |
| 16757 } |
| 16758 if( eState==1 ) zFile[iOut++] = '\0'; |
| 16759 zFile[iOut++] = '\0'; |
| 16760 zFile[iOut++] = '\0'; |
| 16761 |
| 16762 /* Check if there were any options specified that should be interpreted |
| 16763 ** here. Options that are interpreted here include "vfs" and those that |
| 16764 ** correspond to flags that may be passed to the sqlite3_open_v2() |
| 16765 ** method. */ |
| 16766 zOpt = &zFile[sqlite3Strlen30(zFile)+1]; |
| 16767 while( zOpt[0] ){ |
| 16768 int nOpt = sqlite3Strlen30(zOpt); |
| 16769 char *zVal = &zOpt[nOpt+1]; |
| 16770 int nVal = sqlite3Strlen30(zVal); |
| 16771 |
| 16772 if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){ |
| 16773 zVfs = zVal; |
| 16774 }else{ |
| 16775 struct OpenMode { |
| 16776 const char *z; |
| 16777 int mode; |
| 16778 } *aMode = 0; |
| 16779 char *zModeType = 0; |
| 16780 int mask = 0; |
| 16781 int limit = 0; |
| 16782 |
| 16783 if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){ |
| 16784 static struct OpenMode aCacheMode[] = { |
| 16785 { "shared", SQLITE_OPEN_SHAREDCACHE }, |
| 16786 { "private", SQLITE_OPEN_PRIVATECACHE }, |
| 16787 { 0, 0 } |
| 16788 }; |
| 16789 |
| 16790 mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE; |
| 16791 aMode = aCacheMode; |
| 16792 limit = mask; |
| 16793 zModeType = "cache"; |
| 16794 } |
| 16795 if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){ |
| 16796 static struct OpenMode aOpenMode[] = { |
| 16797 { "ro", SQLITE_OPEN_READONLY }, |
| 16798 { "rw", SQLITE_OPEN_READWRITE }, |
| 16799 { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE }, |
| 16800 { "memory", SQLITE_OPEN_MEMORY }, |
| 16801 { 0, 0 } |
| 16802 }; |
| 16803 |
| 16804 mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE |
| 16805 | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY; |
| 16806 aMode = aOpenMode; |
| 16807 limit = mask & flags; |
| 16808 zModeType = "access"; |
| 16809 } |
| 16810 |
| 16811 if( aMode ){ |
| 16812 int i; |
| 16813 int mode = 0; |
| 16814 for(i=0; aMode[i].z; i++){ |
| 16815 const char *z = aMode[i].z; |
| 16816 if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){ |
| 16817 mode = aMode[i].mode; |
| 16818 break; |
| 16819 } |
| 16820 } |
| 16821 if( mode==0 ){ |
| 16822 *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal); |
| 16823 rc = SQLITE_ERROR; |
| 16824 goto parse_uri_out; |
| 16825 } |
| 16826 if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){ |
| 16827 *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s", |
| 16828 zModeType, zVal); |
| 16829 rc = SQLITE_PERM; |
| 16830 goto parse_uri_out; |
| 16831 } |
| 16832 flags = (flags & ~mask) | mode; |
| 16833 } |
| 16834 } |
| 16835 |
| 16836 zOpt = &zVal[nVal+1]; |
| 16837 } |
| 16838 |
| 16839 }else{ |
| 16840 zFile = sqlite3_malloc64(nUri+2); |
| 16841 if( !zFile ) return SQLITE_NOMEM_BKPT; |
| 16842 if( nUri ){ |
| 16843 memcpy(zFile, zUri, nUri); |
| 16844 } |
| 16845 zFile[nUri] = '\0'; |
| 16846 zFile[nUri+1] = '\0'; |
| 16847 flags &= ~SQLITE_OPEN_URI; |
| 16848 } |
| 16849 |
| 16850 *ppVfs = sqlite3_vfs_find(zVfs); |
| 16851 if( *ppVfs==0 ){ |
| 16852 *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs); |
| 16853 rc = SQLITE_ERROR; |
| 16854 } |
| 16855 parse_uri_out: |
| 16856 if( rc!=SQLITE_OK ){ |
| 16857 sqlite3_free(zFile); |
| 16858 zFile = 0; |
| 16859 } |
| 16860 *pFlags = flags; |
| 16861 *pzFile = zFile; |
| 16862 return rc; |
| 16863 } |
| 16864 |
| 16865 |
| 16866 /* |
| 16867 ** This routine does the work of opening a database on behalf of |
| 16868 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| 16869 ** is UTF-8 encoded. |
| 16870 */ |
| 16871 static int openDatabase( |
| 16872 const char *zFilename, /* Database filename UTF-8 encoded */ |
| 16873 sqlite3 **ppDb, /* OUT: Returned database handle */ |
| 16874 unsigned int flags, /* Operational flags */ |
| 16875 const char *zVfs /* Name of the VFS to use */ |
| 16876 ){ |
| 16877 sqlite3 *db; /* Store allocated handle here */ |
| 16878 int rc; /* Return code */ |
| 16879 int isThreadsafe; /* True for threadsafe connections */ |
| 16880 char *zOpen = 0; /* Filename argument to pass to BtreeOpen() */ |
| 16881 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */ |
| 16882 |
| 16883 #ifdef SQLITE_ENABLE_API_ARMOR |
| 16884 if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 16885 #endif |
| 16886 *ppDb = 0; |
| 16887 #ifndef SQLITE_OMIT_AUTOINIT |
| 16888 rc = sqlite3_initialize(); |
| 16889 if( rc ) return rc; |
| 16890 #endif |
| 16891 |
| 16892 /* Only allow sensible combinations of bits in the flags argument. |
| 16893 ** Throw an error if any non-sense combination is used. If we |
| 16894 ** do not block illegal combinations here, it could trigger |
| 16895 ** assert() statements in deeper layers. Sensible combinations |
| 16896 ** are: |
| 16897 ** |
| 16898 ** 1: SQLITE_OPEN_READONLY |
| 16899 ** 2: SQLITE_OPEN_READWRITE |
| 16900 ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
| 16901 */ |
| 16902 assert( SQLITE_OPEN_READONLY == 0x01 ); |
| 16903 assert( SQLITE_OPEN_READWRITE == 0x02 ); |
| 16904 assert( SQLITE_OPEN_CREATE == 0x04 ); |
| 16905 testcase( (1<<(flags&7))==0x02 ); /* READONLY */ |
| 16906 testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ |
| 16907 testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ |
| 16908 if( ((1<<(flags&7)) & 0x46)==0 ){ |
| 16909 return SQLITE_MISUSE_BKPT; /* IMP: R-65497-44594 */ |
| 16910 } |
| 16911 |
| 16912 if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 16913 isThreadsafe = 0; |
| 16914 }else if( flags & SQLITE_OPEN_NOMUTEX ){ |
| 16915 isThreadsafe = 0; |
| 16916 }else if( flags & SQLITE_OPEN_FULLMUTEX ){ |
| 16917 isThreadsafe = 1; |
| 16918 }else{ |
| 16919 isThreadsafe = sqlite3GlobalConfig.bFullMutex; |
| 16920 } |
| 16921 if( flags & SQLITE_OPEN_PRIVATECACHE ){ |
| 16922 flags &= ~SQLITE_OPEN_SHAREDCACHE; |
| 16923 }else if( sqlite3GlobalConfig.sharedCacheEnabled ){ |
| 16924 flags |= SQLITE_OPEN_SHAREDCACHE; |
| 16925 } |
| 16926 |
| 16927 /* Remove harmful bits from the flags parameter |
| 16928 ** |
| 16929 ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were |
| 16930 ** dealt with in the previous code block. Besides these, the only |
| 16931 ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, |
| 16932 ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, |
| 16933 ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits. Silently mask |
| 16934 ** off all other flags. |
| 16935 */ |
| 16936 flags &= ~( SQLITE_OPEN_DELETEONCLOSE | |
| 16937 SQLITE_OPEN_EXCLUSIVE | |
| 16938 SQLITE_OPEN_MAIN_DB | |
| 16939 SQLITE_OPEN_TEMP_DB | |
| 16940 SQLITE_OPEN_TRANSIENT_DB | |
| 16941 SQLITE_OPEN_MAIN_JOURNAL | |
| 16942 SQLITE_OPEN_TEMP_JOURNAL | |
| 16943 SQLITE_OPEN_SUBJOURNAL | |
| 16944 SQLITE_OPEN_MASTER_JOURNAL | |
| 16945 SQLITE_OPEN_NOMUTEX | |
| 16946 SQLITE_OPEN_FULLMUTEX | |
| 16947 SQLITE_OPEN_WAL |
| 16948 ); |
| 16949 |
| 16950 /* Allocate the sqlite data structure */ |
| 16951 db = sqlite3MallocZero( sizeof(sqlite3) ); |
| 16952 if( db==0 ) goto opendb_out; |
| 16953 if( isThreadsafe ){ |
| 16954 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); |
| 16955 if( db->mutex==0 ){ |
| 16956 sqlite3_free(db); |
| 16957 db = 0; |
| 16958 goto opendb_out; |
| 16959 } |
| 16960 } |
| 16961 sqlite3_mutex_enter(db->mutex); |
| 16962 db->errMask = 0xff; |
| 16963 db->nDb = 2; |
| 16964 db->magic = SQLITE_MAGIC_BUSY; |
| 16965 db->aDb = db->aDbStatic; |
| 16966 |
| 16967 assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); |
| 16968 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); |
| 16969 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS; |
| 16970 db->autoCommit = 1; |
| 16971 db->nextAutovac = -1; |
| 16972 db->szMmap = sqlite3GlobalConfig.szMmap; |
| 16973 db->nextPagesize = 0; |
| 16974 db->nMaxSorterMmap = 0x7FFFFFFF; |
| 16975 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill |
| 16976 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX |
| 16977 | SQLITE_AutoIndex |
| 16978 #endif |
| 16979 #if SQLITE_DEFAULT_CKPTFULLFSYNC |
| 16980 | SQLITE_CkptFullFSync |
| 16981 #endif |
| 16982 #if SQLITE_DEFAULT_FILE_FORMAT<4 |
| 16983 | SQLITE_LegacyFileFmt |
| 16984 #endif |
| 16985 #ifdef SQLITE_ENABLE_LOAD_EXTENSION |
| 16986 | SQLITE_LoadExtension |
| 16987 #endif |
| 16988 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS |
| 16989 | SQLITE_RecTriggers |
| 16990 #endif |
| 16991 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS |
| 16992 | SQLITE_ForeignKeys |
| 16993 #endif |
| 16994 #if defined(SQLITE_REVERSE_UNORDERED_SELECTS) |
| 16995 | SQLITE_ReverseOrder |
| 16996 #endif |
| 16997 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) |
| 16998 | SQLITE_CellSizeCk |
| 16999 #endif |
| 17000 #if defined(SQLITE_ENABLE_FTS3_TOKENIZER) |
| 17001 | SQLITE_Fts3Tokenizer |
| 17002 #endif |
| 17003 ; |
| 17004 sqlite3HashInit(&db->aCollSeq); |
| 17005 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 17006 sqlite3HashInit(&db->aModule); |
| 17007 #endif |
| 17008 |
| 17009 /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 17010 ** and UTF-16, so add a version for each to avoid any unnecessary |
| 17011 ** conversions. The only error that can occur here is a malloc() failure. |
| 17012 ** |
| 17013 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating |
| 17014 ** functions: |
| 17015 */ |
| 17016 createCollation(db, sqlite3StrBINARY, SQLITE_UTF8, 0, binCollFunc, 0); |
| 17017 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16BE, 0, binCollFunc, 0); |
| 17018 createCollation(db, sqlite3StrBINARY, SQLITE_UTF16LE, 0, binCollFunc, 0); |
| 17019 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0); |
| 17020 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0); |
| 17021 if( db->mallocFailed ){ |
| 17022 goto opendb_out; |
| 17023 } |
| 17024 /* EVIDENCE-OF: R-08308-17224 The default collating function for all |
| 17025 ** strings is BINARY. |
| 17026 */ |
| 17027 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, sqlite3StrBINARY, 0); |
| 17028 assert( db->pDfltColl!=0 ); |
| 17029 |
| 17030 /* Parse the filename/URI argument. */ |
| 17031 db->openFlags = flags; |
| 17032 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg); |
| 17033 if( rc!=SQLITE_OK ){ |
| 17034 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db); |
| 17035 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg); |
| 17036 sqlite3_free(zErrMsg); |
| 17037 goto opendb_out; |
| 17038 } |
| 17039 |
| 17040 /* Open the backend database driver */ |
| 17041 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0, |
| 17042 flags | SQLITE_OPEN_MAIN_DB); |
| 17043 if( rc!=SQLITE_OK ){ |
| 17044 if( rc==SQLITE_IOERR_NOMEM ){ |
| 17045 rc = SQLITE_NOMEM_BKPT; |
| 17046 } |
| 17047 sqlite3Error(db, rc); |
| 17048 goto opendb_out; |
| 17049 } |
| 17050 sqlite3BtreeEnter(db->aDb[0].pBt); |
| 17051 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); |
| 17052 if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db); |
| 17053 sqlite3BtreeLeave(db->aDb[0].pBt); |
| 17054 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); |
| 17055 |
| 17056 /* The default safety_level for the main database is FULL; for the temp |
| 17057 ** database it is OFF. This matches the pager layer defaults. |
| 17058 */ |
| 17059 db->aDb[0].zDbSName = "main"; |
| 17060 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1; |
| 17061 db->aDb[1].zDbSName = "temp"; |
| 17062 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF; |
| 17063 |
| 17064 db->magic = SQLITE_MAGIC_OPEN; |
| 17065 if( db->mallocFailed ){ |
| 17066 goto opendb_out; |
| 17067 } |
| 17068 |
| 17069 /* Register all built-in functions, but do not attempt to read the |
| 17070 ** database schema yet. This is delayed until the first time the database |
| 17071 ** is accessed. |
| 17072 */ |
| 17073 sqlite3Error(db, SQLITE_OK); |
| 17074 sqlite3RegisterPerConnectionBuiltinFunctions(db); |
| 17075 rc = sqlite3_errcode(db); |
| 17076 |
| 17077 #ifdef SQLITE_ENABLE_FTS5 |
| 17078 /* Register any built-in FTS5 module before loading the automatic |
| 17079 ** extensions. This allows automatic extensions to register FTS5 |
| 17080 ** tokenizers and auxiliary functions. */ |
| 17081 if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 17082 rc = sqlite3Fts5Init(db); |
| 17083 } |
| 17084 #endif |
| 17085 |
| 17086 /* Load automatic extensions - extensions that have been registered |
| 17087 ** using the sqlite3_automatic_extension() API. |
| 17088 */ |
| 17089 if( rc==SQLITE_OK ){ |
| 17090 sqlite3AutoLoadExtensions(db); |
| 17091 rc = sqlite3_errcode(db); |
| 17092 if( rc!=SQLITE_OK ){ |
| 17093 goto opendb_out; |
| 17094 } |
| 17095 } |
| 17096 |
| 17097 #ifdef SQLITE_ENABLE_FTS1 |
| 17098 if( !db->mallocFailed ){ |
| 17099 extern int sqlite3Fts1Init(sqlite3*); |
| 17100 rc = sqlite3Fts1Init(db); |
| 17101 } |
| 17102 #endif |
| 17103 |
| 17104 #ifdef SQLITE_ENABLE_FTS2 |
| 17105 if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 17106 extern int sqlite3Fts2Init(sqlite3*); |
| 17107 rc = sqlite3Fts2Init(db); |
| 17108 } |
| 17109 #endif |
| 17110 |
| 17111 #ifdef SQLITE_ENABLE_FTS3 /* automatically defined by SQLITE_ENABLE_FTS4 */ |
| 17112 if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 17113 rc = sqlite3Fts3Init(db); |
| 17114 } |
| 17115 #endif |
| 17116 |
| 17117 #ifdef DEFAULT_ENABLE_RECOVER |
| 17118 /* Initialize recover virtual table for testing. */ |
| 17119 extern int recoverVtableInit(sqlite3 *db); |
| 17120 if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 17121 rc = recoverVtableInit(db); |
| 17122 } |
| 17123 #endif |
| 17124 |
| 17125 #ifdef SQLITE_ENABLE_ICU |
| 17126 if( !db->mallocFailed && rc==SQLITE_OK ){ |
| 17127 rc = sqlite3IcuInit(db); |
| 17128 } |
| 17129 #endif |
| 17130 |
| 17131 #ifdef SQLITE_ENABLE_RTREE |
| 17132 if( !db->mallocFailed && rc==SQLITE_OK){ |
| 17133 rc = sqlite3RtreeInit(db); |
| 17134 } |
| 17135 #endif |
| 17136 |
| 17137 #ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 17138 if( !db->mallocFailed && rc==SQLITE_OK){ |
| 17139 rc = sqlite3DbstatRegister(db); |
| 17140 } |
| 17141 #endif |
| 17142 |
| 17143 #ifdef SQLITE_ENABLE_JSON1 |
| 17144 if( !db->mallocFailed && rc==SQLITE_OK){ |
| 17145 rc = sqlite3Json1Init(db); |
| 17146 } |
| 17147 #endif |
| 17148 |
| 17149 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
| 17150 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
| 17151 ** mode. Doing nothing at all also makes NORMAL the default. |
| 17152 */ |
| 17153 #ifdef SQLITE_DEFAULT_LOCKING_MODE |
| 17154 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; |
| 17155 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), |
| 17156 SQLITE_DEFAULT_LOCKING_MODE); |
| 17157 #endif |
| 17158 |
| 17159 if( rc ) sqlite3Error(db, rc); |
| 17160 |
| 17161 /* Enable the lookaside-malloc subsystem */ |
| 17162 setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, |
| 17163 sqlite3GlobalConfig.nLookaside); |
| 17164 |
| 17165 sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); |
| 17166 |
| 17167 opendb_out: |
| 17168 if( db ){ |
| 17169 assert( db->mutex!=0 || isThreadsafe==0 |
| 17170 || sqlite3GlobalConfig.bFullMutex==0 ); |
| 17171 sqlite3_mutex_leave(db->mutex); |
| 17172 } |
| 17173 rc = sqlite3_errcode(db); |
| 17174 assert( db!=0 || rc==SQLITE_NOMEM ); |
| 17175 if( rc==SQLITE_NOMEM ){ |
| 17176 sqlite3_close(db); |
| 17177 db = 0; |
| 17178 }else if( rc!=SQLITE_OK ){ |
| 17179 db->magic = SQLITE_MAGIC_SICK; |
| 17180 } |
| 17181 *ppDb = db; |
| 17182 #ifdef SQLITE_ENABLE_SQLLOG |
| 17183 if( sqlite3GlobalConfig.xSqllog ){ |
| 17184 /* Opening a db handle. Fourth parameter is passed 0. */ |
| 17185 void *pArg = sqlite3GlobalConfig.pSqllogArg; |
| 17186 sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0); |
| 17187 } |
| 17188 #endif |
| 17189 #if defined(SQLITE_HAS_CODEC) |
| 17190 if( rc==SQLITE_OK ){ |
| 17191 const char *zHexKey = sqlite3_uri_parameter(zOpen, "hexkey"); |
| 17192 if( zHexKey && zHexKey[0] ){ |
| 17193 u8 iByte; |
| 17194 int i; |
| 17195 char zKey[40]; |
| 17196 for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zHexKey[i]); i++){ |
| 17197 iByte = (iByte<<4) + sqlite3HexToInt(zHexKey[i]); |
| 17198 if( (i&1)!=0 ) zKey[i/2] = iByte; |
| 17199 } |
| 17200 sqlite3_key_v2(db, 0, zKey, i/2); |
| 17201 } |
| 17202 } |
| 17203 #endif |
| 17204 sqlite3_free(zOpen); |
| 17205 return rc & 0xff; |
| 17206 } |
| 17207 |
| 17208 /* |
| 17209 ** Open a new database handle. |
| 17210 */ |
| 17211 SQLITE_API int sqlite3_open( |
| 17212 const char *zFilename, |
| 17213 sqlite3 **ppDb |
| 17214 ){ |
| 17215 return openDatabase(zFilename, ppDb, |
| 17216 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
| 17217 } |
| 17218 SQLITE_API int sqlite3_open_v2( |
| 17219 const char *filename, /* Database filename (UTF-8) */ |
| 17220 sqlite3 **ppDb, /* OUT: SQLite db handle */ |
| 17221 int flags, /* Flags */ |
| 17222 const char *zVfs /* Name of VFS module to use */ |
| 17223 ){ |
| 17224 return openDatabase(filename, ppDb, (unsigned int)flags, zVfs); |
| 17225 } |
| 17226 |
| 17227 #ifndef SQLITE_OMIT_UTF16 |
| 17228 /* |
| 17229 ** Open a new database handle. |
| 17230 */ |
| 17231 SQLITE_API int sqlite3_open16( |
| 17232 const void *zFilename, |
| 17233 sqlite3 **ppDb |
| 17234 ){ |
| 17235 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 17236 sqlite3_value *pVal; |
| 17237 int rc; |
| 17238 |
| 17239 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17240 if( ppDb==0 ) return SQLITE_MISUSE_BKPT; |
| 17241 #endif |
| 17242 *ppDb = 0; |
| 17243 #ifndef SQLITE_OMIT_AUTOINIT |
| 17244 rc = sqlite3_initialize(); |
| 17245 if( rc ) return rc; |
| 17246 #endif |
| 17247 if( zFilename==0 ) zFilename = "\000\000"; |
| 17248 pVal = sqlite3ValueNew(0); |
| 17249 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 17250 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 17251 if( zFilename8 ){ |
| 17252 rc = openDatabase(zFilename8, ppDb, |
| 17253 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); |
| 17254 assert( *ppDb || rc==SQLITE_NOMEM ); |
| 17255 if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ |
| 17256 SCHEMA_ENC(*ppDb) = ENC(*ppDb) = SQLITE_UTF16NATIVE; |
| 17257 } |
| 17258 }else{ |
| 17259 rc = SQLITE_NOMEM_BKPT; |
| 17260 } |
| 17261 sqlite3ValueFree(pVal); |
| 17262 |
| 17263 return rc & 0xff; |
| 17264 } |
| 17265 #endif /* SQLITE_OMIT_UTF16 */ |
| 17266 |
| 17267 /* |
| 17268 ** Register a new collation sequence with the database handle db. |
| 17269 */ |
| 17270 SQLITE_API int sqlite3_create_collation( |
| 17271 sqlite3* db, |
| 17272 const char *zName, |
| 17273 int enc, |
| 17274 void* pCtx, |
| 17275 int(*xCompare)(void*,int,const void*,int,const void*) |
| 17276 ){ |
| 17277 return sqlite3_create_collation_v2(db, zName, enc, pCtx, xCompare, 0); |
| 17278 } |
| 17279 |
| 17280 /* |
| 17281 ** Register a new collation sequence with the database handle db. |
| 17282 */ |
| 17283 SQLITE_API int sqlite3_create_collation_v2( |
| 17284 sqlite3* db, |
| 17285 const char *zName, |
| 17286 int enc, |
| 17287 void* pCtx, |
| 17288 int(*xCompare)(void*,int,const void*,int,const void*), |
| 17289 void(*xDel)(void*) |
| 17290 ){ |
| 17291 int rc; |
| 17292 |
| 17293 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17294 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 17295 #endif |
| 17296 sqlite3_mutex_enter(db->mutex); |
| 17297 assert( !db->mallocFailed ); |
| 17298 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel); |
| 17299 rc = sqlite3ApiExit(db, rc); |
| 17300 sqlite3_mutex_leave(db->mutex); |
| 17301 return rc; |
| 17302 } |
| 17303 |
| 17304 #ifndef SQLITE_OMIT_UTF16 |
| 17305 /* |
| 17306 ** Register a new collation sequence with the database handle db. |
| 17307 */ |
| 17308 SQLITE_API int sqlite3_create_collation16( |
| 17309 sqlite3* db, |
| 17310 const void *zName, |
| 17311 int enc, |
| 17312 void* pCtx, |
| 17313 int(*xCompare)(void*,int,const void*,int,const void*) |
| 17314 ){ |
| 17315 int rc = SQLITE_OK; |
| 17316 char *zName8; |
| 17317 |
| 17318 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17319 if( !sqlite3SafetyCheckOk(db) || zName==0 ) return SQLITE_MISUSE_BKPT; |
| 17320 #endif |
| 17321 sqlite3_mutex_enter(db->mutex); |
| 17322 assert( !db->mallocFailed ); |
| 17323 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); |
| 17324 if( zName8 ){ |
| 17325 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0); |
| 17326 sqlite3DbFree(db, zName8); |
| 17327 } |
| 17328 rc = sqlite3ApiExit(db, rc); |
| 17329 sqlite3_mutex_leave(db->mutex); |
| 17330 return rc; |
| 17331 } |
| 17332 #endif /* SQLITE_OMIT_UTF16 */ |
| 17333 |
| 17334 /* |
| 17335 ** Register a collation sequence factory callback with the database handle |
| 17336 ** db. Replace any previously installed collation sequence factory. |
| 17337 */ |
| 17338 SQLITE_API int sqlite3_collation_needed( |
| 17339 sqlite3 *db, |
| 17340 void *pCollNeededArg, |
| 17341 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 17342 ){ |
| 17343 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17344 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 17345 #endif |
| 17346 sqlite3_mutex_enter(db->mutex); |
| 17347 db->xCollNeeded = xCollNeeded; |
| 17348 db->xCollNeeded16 = 0; |
| 17349 db->pCollNeededArg = pCollNeededArg; |
| 17350 sqlite3_mutex_leave(db->mutex); |
| 17351 return SQLITE_OK; |
| 17352 } |
| 17353 |
| 17354 #ifndef SQLITE_OMIT_UTF16 |
| 17355 /* |
| 17356 ** Register a collation sequence factory callback with the database handle |
| 17357 ** db. Replace any previously installed collation sequence factory. |
| 17358 */ |
| 17359 SQLITE_API int sqlite3_collation_needed16( |
| 17360 sqlite3 *db, |
| 17361 void *pCollNeededArg, |
| 17362 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 17363 ){ |
| 17364 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17365 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 17366 #endif |
| 17367 sqlite3_mutex_enter(db->mutex); |
| 17368 db->xCollNeeded = 0; |
| 17369 db->xCollNeeded16 = xCollNeeded16; |
| 17370 db->pCollNeededArg = pCollNeededArg; |
| 17371 sqlite3_mutex_leave(db->mutex); |
| 17372 return SQLITE_OK; |
| 17373 } |
| 17374 #endif /* SQLITE_OMIT_UTF16 */ |
| 17375 |
| 17376 #ifndef SQLITE_OMIT_DEPRECATED |
| 17377 /* |
| 17378 ** This function is now an anachronism. It used to be used to recover from a |
| 17379 ** malloc() failure, but SQLite now does this automatically. |
| 17380 */ |
| 17381 SQLITE_API int sqlite3_global_recover(void){ |
| 17382 return SQLITE_OK; |
| 17383 } |
| 17384 #endif |
| 17385 |
| 17386 /* |
| 17387 ** Test to see whether or not the database connection is in autocommit |
| 17388 ** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on |
| 17389 ** by default. Autocommit is disabled by a BEGIN statement and reenabled |
| 17390 ** by the next COMMIT or ROLLBACK. |
| 17391 */ |
| 17392 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ |
| 17393 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17394 if( !sqlite3SafetyCheckOk(db) ){ |
| 17395 (void)SQLITE_MISUSE_BKPT; |
| 17396 return 0; |
| 17397 } |
| 17398 #endif |
| 17399 return db->autoCommit; |
| 17400 } |
| 17401 |
| 17402 /* |
| 17403 ** The following routines are substitutes for constants SQLITE_CORRUPT, |
| 17404 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_NOMEM and possibly other error |
| 17405 ** constants. They serve two purposes: |
| 17406 ** |
| 17407 ** 1. Serve as a convenient place to set a breakpoint in a debugger |
| 17408 ** to detect when version error conditions occurs. |
| 17409 ** |
| 17410 ** 2. Invoke sqlite3_log() to provide the source code location where |
| 17411 ** a low-level error is first detected. |
| 17412 */ |
| 17413 static int reportError(int iErr, int lineno, const char *zType){ |
| 17414 sqlite3_log(iErr, "%s at line %d of [%.10s]", |
| 17415 zType, lineno, 20+sqlite3_sourceid()); |
| 17416 return iErr; |
| 17417 } |
| 17418 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){ |
| 17419 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 17420 return reportError(SQLITE_CORRUPT, lineno, "database corruption"); |
| 17421 } |
| 17422 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){ |
| 17423 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 17424 return reportError(SQLITE_MISUSE, lineno, "misuse"); |
| 17425 } |
| 17426 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){ |
| 17427 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 17428 return reportError(SQLITE_CANTOPEN, lineno, "cannot open file"); |
| 17429 } |
| 17430 #ifdef SQLITE_DEBUG |
| 17431 SQLITE_PRIVATE int sqlite3NomemError(int lineno){ |
| 17432 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 17433 return reportError(SQLITE_NOMEM, lineno, "OOM"); |
| 17434 } |
| 17435 SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){ |
| 17436 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 17437 return reportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error"); |
| 17438 } |
| 17439 #endif |
| 17440 |
| 17441 #ifndef SQLITE_OMIT_DEPRECATED |
| 17442 /* |
| 17443 ** This is a convenience routine that makes sure that all thread-specific |
| 17444 ** data for this thread has been deallocated. |
| 17445 ** |
| 17446 ** SQLite no longer uses thread-specific data so this routine is now a |
| 17447 ** no-op. It is retained for historical compatibility. |
| 17448 */ |
| 17449 SQLITE_API void sqlite3_thread_cleanup(void){ |
| 17450 } |
| 17451 #endif |
| 17452 |
| 17453 /* |
| 17454 ** Return meta information about a specific column of a database table. |
| 17455 ** See comment in sqlite3.h (sqlite.h.in) for details. |
| 17456 */ |
| 17457 SQLITE_API int sqlite3_table_column_metadata( |
| 17458 sqlite3 *db, /* Connection handle */ |
| 17459 const char *zDbName, /* Database name or NULL */ |
| 17460 const char *zTableName, /* Table name */ |
| 17461 const char *zColumnName, /* Column name */ |
| 17462 char const **pzDataType, /* OUTPUT: Declared data type */ |
| 17463 char const **pzCollSeq, /* OUTPUT: Collation sequence name */ |
| 17464 int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ |
| 17465 int *pPrimaryKey, /* OUTPUT: True if column part of PK */ |
| 17466 int *pAutoinc /* OUTPUT: True if column is auto-increment */ |
| 17467 ){ |
| 17468 int rc; |
| 17469 char *zErrMsg = 0; |
| 17470 Table *pTab = 0; |
| 17471 Column *pCol = 0; |
| 17472 int iCol = 0; |
| 17473 char const *zDataType = 0; |
| 17474 char const *zCollSeq = 0; |
| 17475 int notnull = 0; |
| 17476 int primarykey = 0; |
| 17477 int autoinc = 0; |
| 17478 |
| 17479 |
| 17480 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17481 if( !sqlite3SafetyCheckOk(db) || zTableName==0 ){ |
| 17482 return SQLITE_MISUSE_BKPT; |
| 17483 } |
| 17484 #endif |
| 17485 |
| 17486 /* Ensure the database schema has been loaded */ |
| 17487 sqlite3_mutex_enter(db->mutex); |
| 17488 sqlite3BtreeEnterAll(db); |
| 17489 rc = sqlite3Init(db, &zErrMsg); |
| 17490 if( SQLITE_OK!=rc ){ |
| 17491 goto error_out; |
| 17492 } |
| 17493 |
| 17494 /* Locate the table in question */ |
| 17495 pTab = sqlite3FindTable(db, zTableName, zDbName); |
| 17496 if( !pTab || pTab->pSelect ){ |
| 17497 pTab = 0; |
| 17498 goto error_out; |
| 17499 } |
| 17500 |
| 17501 /* Find the column for which info is requested */ |
| 17502 if( zColumnName==0 ){ |
| 17503 /* Query for existance of table only */ |
| 17504 }else{ |
| 17505 for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 17506 pCol = &pTab->aCol[iCol]; |
| 17507 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ |
| 17508 break; |
| 17509 } |
| 17510 } |
| 17511 if( iCol==pTab->nCol ){ |
| 17512 if( HasRowid(pTab) && sqlite3IsRowid(zColumnName) ){ |
| 17513 iCol = pTab->iPKey; |
| 17514 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0; |
| 17515 }else{ |
| 17516 pTab = 0; |
| 17517 goto error_out; |
| 17518 } |
| 17519 } |
| 17520 } |
| 17521 |
| 17522 /* The following block stores the meta information that will be returned |
| 17523 ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey |
| 17524 ** and autoinc. At this point there are two possibilities: |
| 17525 ** |
| 17526 ** 1. The specified column name was rowid", "oid" or "_rowid_" |
| 17527 ** and there is no explicitly declared IPK column. |
| 17528 ** |
| 17529 ** 2. The table is not a view and the column name identified an |
| 17530 ** explicitly declared column. Copy meta information from *pCol. |
| 17531 */ |
| 17532 if( pCol ){ |
| 17533 zDataType = sqlite3ColumnType(pCol,0); |
| 17534 zCollSeq = pCol->zColl; |
| 17535 notnull = pCol->notNull!=0; |
| 17536 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0; |
| 17537 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; |
| 17538 }else{ |
| 17539 zDataType = "INTEGER"; |
| 17540 primarykey = 1; |
| 17541 } |
| 17542 if( !zCollSeq ){ |
| 17543 zCollSeq = sqlite3StrBINARY; |
| 17544 } |
| 17545 |
| 17546 error_out: |
| 17547 sqlite3BtreeLeaveAll(db); |
| 17548 |
| 17549 /* Whether the function call succeeded or failed, set the output parameters |
| 17550 ** to whatever their local counterparts contain. If an error did occur, |
| 17551 ** this has the effect of zeroing all output parameters. |
| 17552 */ |
| 17553 if( pzDataType ) *pzDataType = zDataType; |
| 17554 if( pzCollSeq ) *pzCollSeq = zCollSeq; |
| 17555 if( pNotNull ) *pNotNull = notnull; |
| 17556 if( pPrimaryKey ) *pPrimaryKey = primarykey; |
| 17557 if( pAutoinc ) *pAutoinc = autoinc; |
| 17558 |
| 17559 if( SQLITE_OK==rc && !pTab ){ |
| 17560 sqlite3DbFree(db, zErrMsg); |
| 17561 zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, |
| 17562 zColumnName); |
| 17563 rc = SQLITE_ERROR; |
| 17564 } |
| 17565 sqlite3ErrorWithMsg(db, rc, (zErrMsg?"%s":0), zErrMsg); |
| 17566 sqlite3DbFree(db, zErrMsg); |
| 17567 rc = sqlite3ApiExit(db, rc); |
| 17568 sqlite3_mutex_leave(db->mutex); |
| 17569 return rc; |
| 17570 } |
| 17571 |
| 17572 /* |
| 17573 ** Sleep for a little while. Return the amount of time slept. |
| 17574 */ |
| 17575 SQLITE_API int sqlite3_sleep(int ms){ |
| 17576 sqlite3_vfs *pVfs; |
| 17577 int rc; |
| 17578 pVfs = sqlite3_vfs_find(0); |
| 17579 if( pVfs==0 ) return 0; |
| 17580 |
| 17581 /* This function works in milliseconds, but the underlying OsSleep() |
| 17582 ** API uses microseconds. Hence the 1000's. |
| 17583 */ |
| 17584 rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); |
| 17585 return rc; |
| 17586 } |
| 17587 |
| 17588 /* |
| 17589 ** Enable or disable the extended result codes. |
| 17590 */ |
| 17591 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ |
| 17592 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17593 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 17594 #endif |
| 17595 sqlite3_mutex_enter(db->mutex); |
| 17596 db->errMask = onoff ? 0xffffffff : 0xff; |
| 17597 sqlite3_mutex_leave(db->mutex); |
| 17598 return SQLITE_OK; |
| 17599 } |
| 17600 |
| 17601 /* |
| 17602 ** Invoke the xFileControl method on a particular database. |
| 17603 */ |
| 17604 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, vo
id *pArg){ |
| 17605 int rc = SQLITE_ERROR; |
| 17606 Btree *pBtree; |
| 17607 |
| 17608 #ifdef SQLITE_ENABLE_API_ARMOR |
| 17609 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; |
| 17610 #endif |
| 17611 sqlite3_mutex_enter(db->mutex); |
| 17612 pBtree = sqlite3DbNameToBtree(db, zDbName); |
| 17613 if( pBtree ){ |
| 17614 Pager *pPager; |
| 17615 sqlite3_file *fd; |
| 17616 sqlite3BtreeEnter(pBtree); |
| 17617 pPager = sqlite3BtreePager(pBtree); |
| 17618 assert( pPager!=0 ); |
| 17619 fd = sqlite3PagerFile(pPager); |
| 17620 assert( fd!=0 ); |
| 17621 if( op==SQLITE_FCNTL_FILE_POINTER ){ |
| 17622 *(sqlite3_file**)pArg = fd; |
| 17623 rc = SQLITE_OK; |
| 17624 }else if( op==SQLITE_FCNTL_VFS_POINTER ){ |
| 17625 *(sqlite3_vfs**)pArg = sqlite3PagerVfs(pPager); |
| 17626 rc = SQLITE_OK; |
| 17627 }else if( op==SQLITE_FCNTL_JOURNAL_POINTER ){ |
| 17628 *(sqlite3_file**)pArg = sqlite3PagerJrnlFile(pPager); |
| 17629 rc = SQLITE_OK; |
| 17630 }else if( fd->pMethods ){ |
| 17631 rc = sqlite3OsFileControl(fd, op, pArg); |
| 17632 }else{ |
| 17633 rc = SQLITE_NOTFOUND; |
| 17634 } |
| 17635 sqlite3BtreeLeave(pBtree); |
| 17636 } |
| 17637 sqlite3_mutex_leave(db->mutex); |
| 17638 return rc; |
| 17639 } |
| 17640 |
| 17641 /* |
| 17642 ** Interface to the testing logic. |
| 17643 */ |
| 17644 SQLITE_API int sqlite3_test_control(int op, ...){ |
| 17645 int rc = 0; |
| 17646 #ifdef SQLITE_UNTESTABLE |
| 17647 UNUSED_PARAMETER(op); |
| 17648 #else |
| 17649 va_list ap; |
| 17650 va_start(ap, op); |
| 17651 switch( op ){ |
| 17652 |
| 17653 /* |
| 17654 ** Save the current state of the PRNG. |
| 17655 */ |
| 17656 case SQLITE_TESTCTRL_PRNG_SAVE: { |
| 17657 sqlite3PrngSaveState(); |
| 17658 break; |
| 17659 } |
| 17660 |
| 17661 /* |
| 17662 ** Restore the state of the PRNG to the last state saved using |
| 17663 ** PRNG_SAVE. If PRNG_SAVE has never before been called, then |
| 17664 ** this verb acts like PRNG_RESET. |
| 17665 */ |
| 17666 case SQLITE_TESTCTRL_PRNG_RESTORE: { |
| 17667 sqlite3PrngRestoreState(); |
| 17668 break; |
| 17669 } |
| 17670 |
| 17671 /* |
| 17672 ** Reset the PRNG back to its uninitialized state. The next call |
| 17673 ** to sqlite3_randomness() will reseed the PRNG using a single call |
| 17674 ** to the xRandomness method of the default VFS. |
| 17675 */ |
| 17676 case SQLITE_TESTCTRL_PRNG_RESET: { |
| 17677 sqlite3_randomness(0,0); |
| 17678 break; |
| 17679 } |
| 17680 |
| 17681 /* |
| 17682 ** sqlite3_test_control(BITVEC_TEST, size, program) |
| 17683 ** |
| 17684 ** Run a test against a Bitvec object of size. The program argument |
| 17685 ** is an array of integers that defines the test. Return -1 on a |
| 17686 ** memory allocation error, 0 on success, or non-zero for an error. |
| 17687 ** See the sqlite3BitvecBuiltinTest() for additional information. |
| 17688 */ |
| 17689 case SQLITE_TESTCTRL_BITVEC_TEST: { |
| 17690 int sz = va_arg(ap, int); |
| 17691 int *aProg = va_arg(ap, int*); |
| 17692 rc = sqlite3BitvecBuiltinTest(sz, aProg); |
| 17693 break; |
| 17694 } |
| 17695 |
| 17696 /* |
| 17697 ** sqlite3_test_control(FAULT_INSTALL, xCallback) |
| 17698 ** |
| 17699 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called, |
| 17700 ** if xCallback is not NULL. |
| 17701 ** |
| 17702 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0) |
| 17703 ** is called immediately after installing the new callback and the return |
| 17704 ** value from sqlite3FaultSim(0) becomes the return from |
| 17705 ** sqlite3_test_control(). |
| 17706 */ |
| 17707 case SQLITE_TESTCTRL_FAULT_INSTALL: { |
| 17708 /* MSVC is picky about pulling func ptrs from va lists. |
| 17709 ** http://support.microsoft.com/kb/47961 |
| 17710 ** sqlite3GlobalConfig.xTestCallback = va_arg(ap, int(*)(int)); |
| 17711 */ |
| 17712 typedef int(*TESTCALLBACKFUNC_t)(int); |
| 17713 sqlite3GlobalConfig.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t); |
| 17714 rc = sqlite3FaultSim(0); |
| 17715 break; |
| 17716 } |
| 17717 |
| 17718 /* |
| 17719 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) |
| 17720 ** |
| 17721 ** Register hooks to call to indicate which malloc() failures |
| 17722 ** are benign. |
| 17723 */ |
| 17724 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { |
| 17725 typedef void (*void_function)(void); |
| 17726 void_function xBenignBegin; |
| 17727 void_function xBenignEnd; |
| 17728 xBenignBegin = va_arg(ap, void_function); |
| 17729 xBenignEnd = va_arg(ap, void_function); |
| 17730 sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); |
| 17731 break; |
| 17732 } |
| 17733 |
| 17734 /* |
| 17735 ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) |
| 17736 ** |
| 17737 ** Set the PENDING byte to the value in the argument, if X>0. |
| 17738 ** Make no changes if X==0. Return the value of the pending byte |
| 17739 ** as it existing before this routine was called. |
| 17740 ** |
| 17741 ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in |
| 17742 ** an incompatible database file format. Changing the PENDING byte |
| 17743 ** while any database connection is open results in undefined and |
| 17744 ** deleterious behavior. |
| 17745 */ |
| 17746 case SQLITE_TESTCTRL_PENDING_BYTE: { |
| 17747 rc = PENDING_BYTE; |
| 17748 #ifndef SQLITE_OMIT_WSD |
| 17749 { |
| 17750 unsigned int newVal = va_arg(ap, unsigned int); |
| 17751 if( newVal ) sqlite3PendingByte = newVal; |
| 17752 } |
| 17753 #endif |
| 17754 break; |
| 17755 } |
| 17756 |
| 17757 /* |
| 17758 ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) |
| 17759 ** |
| 17760 ** This action provides a run-time test to see whether or not |
| 17761 ** assert() was enabled at compile-time. If X is true and assert() |
| 17762 ** is enabled, then the return value is true. If X is true and |
| 17763 ** assert() is disabled, then the return value is zero. If X is |
| 17764 ** false and assert() is enabled, then the assertion fires and the |
| 17765 ** process aborts. If X is false and assert() is disabled, then the |
| 17766 ** return value is zero. |
| 17767 */ |
| 17768 case SQLITE_TESTCTRL_ASSERT: { |
| 17769 volatile int x = 0; |
| 17770 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 ); |
| 17771 rc = x; |
| 17772 break; |
| 17773 } |
| 17774 |
| 17775 |
| 17776 /* |
| 17777 ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) |
| 17778 ** |
| 17779 ** This action provides a run-time test to see how the ALWAYS and |
| 17780 ** NEVER macros were defined at compile-time. |
| 17781 ** |
| 17782 ** The return value is ALWAYS(X). |
| 17783 ** |
| 17784 ** The recommended test is X==2. If the return value is 2, that means |
| 17785 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the |
| 17786 ** default setting. If the return value is 1, then ALWAYS() is either |
| 17787 ** hard-coded to true or else it asserts if its argument is false. |
| 17788 ** The first behavior (hard-coded to true) is the case if |
| 17789 ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second |
| 17790 ** behavior (assert if the argument to ALWAYS() is false) is the case if |
| 17791 ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. |
| 17792 ** |
| 17793 ** The run-time test procedure might look something like this: |
| 17794 ** |
| 17795 ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ |
| 17796 ** // ALWAYS() and NEVER() are no-op pass-through macros |
| 17797 ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ |
| 17798 ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. |
| 17799 ** }else{ |
| 17800 ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. |
| 17801 ** } |
| 17802 */ |
| 17803 case SQLITE_TESTCTRL_ALWAYS: { |
| 17804 int x = va_arg(ap,int); |
| 17805 rc = ALWAYS(x); |
| 17806 break; |
| 17807 } |
| 17808 |
| 17809 /* |
| 17810 ** sqlite3_test_control(SQLITE_TESTCTRL_BYTEORDER); |
| 17811 ** |
| 17812 ** The integer returned reveals the byte-order of the computer on which |
| 17813 ** SQLite is running: |
| 17814 ** |
| 17815 ** 1 big-endian, determined at run-time |
| 17816 ** 10 little-endian, determined at run-time |
| 17817 ** 432101 big-endian, determined at compile-time |
| 17818 ** 123410 little-endian, determined at compile-time |
| 17819 */ |
| 17820 case SQLITE_TESTCTRL_BYTEORDER: { |
| 17821 rc = SQLITE_BYTEORDER*100 + SQLITE_LITTLEENDIAN*10 + SQLITE_BIGENDIAN; |
| 17822 break; |
| 17823 } |
| 17824 |
| 17825 /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) |
| 17826 ** |
| 17827 ** Set the nReserve size to N for the main database on the database |
| 17828 ** connection db. |
| 17829 */ |
| 17830 case SQLITE_TESTCTRL_RESERVE: { |
| 17831 sqlite3 *db = va_arg(ap, sqlite3*); |
| 17832 int x = va_arg(ap,int); |
| 17833 sqlite3_mutex_enter(db->mutex); |
| 17834 sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); |
| 17835 sqlite3_mutex_leave(db->mutex); |
| 17836 break; |
| 17837 } |
| 17838 |
| 17839 /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) |
| 17840 ** |
| 17841 ** Enable or disable various optimizations for testing purposes. The |
| 17842 ** argument N is a bitmask of optimizations to be disabled. For normal |
| 17843 ** operation N should be 0. The idea is that a test program (like the |
| 17844 ** SQL Logic Test or SLT test module) can run the same SQL multiple times |
| 17845 ** with various optimizations disabled to verify that the same answer |
| 17846 ** is obtained in every case. |
| 17847 */ |
| 17848 case SQLITE_TESTCTRL_OPTIMIZATIONS: { |
| 17849 sqlite3 *db = va_arg(ap, sqlite3*); |
| 17850 db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff); |
| 17851 break; |
| 17852 } |
| 17853 |
| 17854 #ifdef SQLITE_N_KEYWORD |
| 17855 /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) |
| 17856 ** |
| 17857 ** If zWord is a keyword recognized by the parser, then return the |
| 17858 ** number of keywords. Or if zWord is not a keyword, return 0. |
| 17859 ** |
| 17860 ** This test feature is only available in the amalgamation since |
| 17861 ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite |
| 17862 ** is built using separate source files. |
| 17863 */ |
| 17864 case SQLITE_TESTCTRL_ISKEYWORD: { |
| 17865 const char *zWord = va_arg(ap, const char*); |
| 17866 int n = sqlite3Strlen30(zWord); |
| 17867 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; |
| 17868 break; |
| 17869 } |
| 17870 #endif |
| 17871 |
| 17872 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); |
| 17873 ** |
| 17874 ** Pass pFree into sqlite3ScratchFree(). |
| 17875 ** If sz>0 then allocate a scratch buffer into pNew. |
| 17876 */ |
| 17877 case SQLITE_TESTCTRL_SCRATCHMALLOC: { |
| 17878 void *pFree, **ppNew; |
| 17879 int sz; |
| 17880 sz = va_arg(ap, int); |
| 17881 ppNew = va_arg(ap, void**); |
| 17882 pFree = va_arg(ap, void*); |
| 17883 if( sz ) *ppNew = sqlite3ScratchMalloc(sz); |
| 17884 sqlite3ScratchFree(pFree); |
| 17885 break; |
| 17886 } |
| 17887 |
| 17888 /* sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff); |
| 17889 ** |
| 17890 ** If parameter onoff is non-zero, configure the wrappers so that all |
| 17891 ** subsequent calls to localtime() and variants fail. If onoff is zero, |
| 17892 ** undo this setting. |
| 17893 */ |
| 17894 case SQLITE_TESTCTRL_LOCALTIME_FAULT: { |
| 17895 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int); |
| 17896 break; |
| 17897 } |
| 17898 |
| 17899 /* sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int); |
| 17900 ** |
| 17901 ** Set or clear a flag that indicates that the database file is always well- |
| 17902 ** formed and never corrupt. This flag is clear by default, indicating that |
| 17903 ** database files might have arbitrary corruption. Setting the flag during |
| 17904 ** testing causes certain assert() statements in the code to be activated |
| 17905 ** that demonstrat invariants on well-formed database files. |
| 17906 */ |
| 17907 case SQLITE_TESTCTRL_NEVER_CORRUPT: { |
| 17908 sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int); |
| 17909 break; |
| 17910 } |
| 17911 |
| 17912 /* Set the threshold at which OP_Once counters reset back to zero. |
| 17913 ** By default this is 0x7ffffffe (over 2 billion), but that value is |
| 17914 ** too big to test in a reasonable amount of time, so this control is |
| 17915 ** provided to set a small and easily reachable reset value. |
| 17916 */ |
| 17917 case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: { |
| 17918 sqlite3GlobalConfig.iOnceResetThreshold = va_arg(ap, int); |
| 17919 break; |
| 17920 } |
| 17921 |
| 17922 /* sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr); |
| 17923 ** |
| 17924 ** Set the VDBE coverage callback function to xCallback with context |
| 17925 ** pointer ptr. |
| 17926 */ |
| 17927 case SQLITE_TESTCTRL_VDBE_COVERAGE: { |
| 17928 #ifdef SQLITE_VDBE_COVERAGE |
| 17929 typedef void (*branch_callback)(void*,int,u8,u8); |
| 17930 sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback); |
| 17931 sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*); |
| 17932 #endif |
| 17933 break; |
| 17934 } |
| 17935 |
| 17936 /* sqlite3_test_control(SQLITE_TESTCTRL_SORTER_MMAP, db, nMax); */ |
| 17937 case SQLITE_TESTCTRL_SORTER_MMAP: { |
| 17938 sqlite3 *db = va_arg(ap, sqlite3*); |
| 17939 db->nMaxSorterMmap = va_arg(ap, int); |
| 17940 break; |
| 17941 } |
| 17942 |
| 17943 /* sqlite3_test_control(SQLITE_TESTCTRL_ISINIT); |
| 17944 ** |
| 17945 ** Return SQLITE_OK if SQLite has been initialized and SQLITE_ERROR if |
| 17946 ** not. |
| 17947 */ |
| 17948 case SQLITE_TESTCTRL_ISINIT: { |
| 17949 if( sqlite3GlobalConfig.isInit==0 ) rc = SQLITE_ERROR; |
| 17950 break; |
| 17951 } |
| 17952 |
| 17953 /* sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, db, dbName, onOff, tnum); |
| 17954 ** |
| 17955 ** This test control is used to create imposter tables. "db" is a pointer |
| 17956 ** to the database connection. dbName is the database name (ex: "main" or |
| 17957 ** "temp") which will receive the imposter. "onOff" turns imposter mode on |
| 17958 ** or off. "tnum" is the root page of the b-tree to which the imposter |
| 17959 ** table should connect. |
| 17960 ** |
| 17961 ** Enable imposter mode only when the schema has already been parsed. Then |
| 17962 ** run a single CREATE TABLE statement to construct the imposter table in |
| 17963 ** the parsed schema. Then turn imposter mode back off again. |
| 17964 ** |
| 17965 ** If onOff==0 and tnum>0 then reset the schema for all databases, causing |
| 17966 ** the schema to be reparsed the next time it is needed. This has the |
| 17967 ** effect of erasing all imposter tables. |
| 17968 */ |
| 17969 case SQLITE_TESTCTRL_IMPOSTER: { |
| 17970 sqlite3 *db = va_arg(ap, sqlite3*); |
| 17971 sqlite3_mutex_enter(db->mutex); |
| 17972 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*)); |
| 17973 db->init.busy = db->init.imposterTable = va_arg(ap,int); |
| 17974 db->init.newTnum = va_arg(ap,int); |
| 17975 if( db->init.busy==0 && db->init.newTnum>0 ){ |
| 17976 sqlite3ResetAllSchemasOfConnection(db); |
| 17977 } |
| 17978 sqlite3_mutex_leave(db->mutex); |
| 17979 break; |
| 17980 } |
| 17981 } |
| 17982 va_end(ap); |
| 17983 #endif /* SQLITE_UNTESTABLE */ |
| 17984 return rc; |
| 17985 } |
| 17986 |
| 17987 /* |
| 17988 ** This is a utility routine, useful to VFS implementations, that checks |
| 17989 ** to see if a database file was a URI that contained a specific query |
| 17990 ** parameter, and if so obtains the value of the query parameter. |
| 17991 ** |
| 17992 ** The zFilename argument is the filename pointer passed into the xOpen() |
| 17993 ** method of a VFS implementation. The zParam argument is the name of the |
| 17994 ** query parameter we seek. This routine returns the value of the zParam |
| 17995 ** parameter if it exists. If the parameter does not exist, this routine |
| 17996 ** returns a NULL pointer. |
| 17997 */ |
| 17998 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *
zParam){ |
| 17999 if( zFilename==0 || zParam==0 ) return 0; |
| 18000 zFilename += sqlite3Strlen30(zFilename) + 1; |
| 18001 while( zFilename[0] ){ |
| 18002 int x = strcmp(zFilename, zParam); |
| 18003 zFilename += sqlite3Strlen30(zFilename) + 1; |
| 18004 if( x==0 ) return zFilename; |
| 18005 zFilename += sqlite3Strlen30(zFilename) + 1; |
| 18006 } |
| 18007 return 0; |
| 18008 } |
| 18009 |
| 18010 /* |
| 18011 ** Return a boolean value for a query parameter. |
| 18012 */ |
| 18013 SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, in
t bDflt){ |
| 18014 const char *z = sqlite3_uri_parameter(zFilename, zParam); |
| 18015 bDflt = bDflt!=0; |
| 18016 return z ? sqlite3GetBoolean(z, bDflt) : bDflt; |
| 18017 } |
| 18018 |
| 18019 /* |
| 18020 ** Return a 64-bit integer value for a query parameter. |
| 18021 */ |
| 18022 SQLITE_API sqlite3_int64 sqlite3_uri_int64( |
| 18023 const char *zFilename, /* Filename as passed to xOpen */ |
| 18024 const char *zParam, /* URI parameter sought */ |
| 18025 sqlite3_int64 bDflt /* return if parameter is missing */ |
| 18026 ){ |
| 18027 const char *z = sqlite3_uri_parameter(zFilename, zParam); |
| 18028 sqlite3_int64 v; |
| 18029 if( z && sqlite3DecOrHexToI64(z, &v)==SQLITE_OK ){ |
| 18030 bDflt = v; |
| 18031 } |
| 18032 return bDflt; |
| 18033 } |
| 18034 |
| 18035 /* |
| 18036 ** Return the Btree pointer identified by zDbName. Return NULL if not found. |
| 18037 */ |
| 18038 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){ |
| 18039 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0; |
| 18040 return iDb<0 ? 0 : db->aDb[iDb].pBt; |
| 18041 } |
| 18042 |
| 18043 /* |
| 18044 ** Return the filename of the database associated with a database |
| 18045 ** connection. |
| 18046 */ |
| 18047 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){ |
| 18048 Btree *pBt; |
| 18049 #ifdef SQLITE_ENABLE_API_ARMOR |
| 18050 if( !sqlite3SafetyCheckOk(db) ){ |
| 18051 (void)SQLITE_MISUSE_BKPT; |
| 18052 return 0; |
| 18053 } |
| 18054 #endif |
| 18055 pBt = sqlite3DbNameToBtree(db, zDbName); |
| 18056 return pBt ? sqlite3BtreeGetFilename(pBt) : 0; |
| 18057 } |
| 18058 |
| 18059 /* |
| 18060 ** Return 1 if database is read-only or 0 if read/write. Return -1 if |
| 18061 ** no such database exists. |
| 18062 */ |
| 18063 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){ |
| 18064 Btree *pBt; |
| 18065 #ifdef SQLITE_ENABLE_API_ARMOR |
| 18066 if( !sqlite3SafetyCheckOk(db) ){ |
| 18067 (void)SQLITE_MISUSE_BKPT; |
| 18068 return -1; |
| 18069 } |
| 18070 #endif |
| 18071 pBt = sqlite3DbNameToBtree(db, zDbName); |
| 18072 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1; |
| 18073 } |
| 18074 |
| 18075 #ifdef SQLITE_ENABLE_SNAPSHOT |
| 18076 /* |
| 18077 ** Obtain a snapshot handle for the snapshot of database zDb currently |
| 18078 ** being read by handle db. |
| 18079 */ |
| 18080 SQLITE_API int sqlite3_snapshot_get( |
| 18081 sqlite3 *db, |
| 18082 const char *zDb, |
| 18083 sqlite3_snapshot **ppSnapshot |
| 18084 ){ |
| 18085 int rc = SQLITE_ERROR; |
| 18086 #ifndef SQLITE_OMIT_WAL |
| 18087 |
| 18088 #ifdef SQLITE_ENABLE_API_ARMOR |
| 18089 if( !sqlite3SafetyCheckOk(db) ){ |
| 18090 return SQLITE_MISUSE_BKPT; |
| 18091 } |
| 18092 #endif |
| 18093 sqlite3_mutex_enter(db->mutex); |
| 18094 |
| 18095 if( db->autoCommit==0 ){ |
| 18096 int iDb = sqlite3FindDbName(db, zDb); |
| 18097 if( iDb==0 || iDb>1 ){ |
| 18098 Btree *pBt = db->aDb[iDb].pBt; |
| 18099 if( 0==sqlite3BtreeIsInTrans(pBt) ){ |
| 18100 rc = sqlite3BtreeBeginTrans(pBt, 0); |
| 18101 if( rc==SQLITE_OK ){ |
| 18102 rc = sqlite3PagerSnapshotGet(sqlite3BtreePager(pBt), ppSnapshot); |
| 18103 } |
| 18104 } |
| 18105 } |
| 18106 } |
| 18107 |
| 18108 sqlite3_mutex_leave(db->mutex); |
| 18109 #endif /* SQLITE_OMIT_WAL */ |
| 18110 return rc; |
| 18111 } |
| 18112 |
| 18113 /* |
| 18114 ** Open a read-transaction on the snapshot idendified by pSnapshot. |
| 18115 */ |
| 18116 SQLITE_API int sqlite3_snapshot_open( |
| 18117 sqlite3 *db, |
| 18118 const char *zDb, |
| 18119 sqlite3_snapshot *pSnapshot |
| 18120 ){ |
| 18121 int rc = SQLITE_ERROR; |
| 18122 #ifndef SQLITE_OMIT_WAL |
| 18123 |
| 18124 #ifdef SQLITE_ENABLE_API_ARMOR |
| 18125 if( !sqlite3SafetyCheckOk(db) ){ |
| 18126 return SQLITE_MISUSE_BKPT; |
| 18127 } |
| 18128 #endif |
| 18129 sqlite3_mutex_enter(db->mutex); |
| 18130 if( db->autoCommit==0 ){ |
| 18131 int iDb; |
| 18132 iDb = sqlite3FindDbName(db, zDb); |
| 18133 if( iDb==0 || iDb>1 ){ |
| 18134 Btree *pBt = db->aDb[iDb].pBt; |
| 18135 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ |
| 18136 rc = sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), pSnapshot); |
| 18137 if( rc==SQLITE_OK ){ |
| 18138 rc = sqlite3BtreeBeginTrans(pBt, 0); |
| 18139 sqlite3PagerSnapshotOpen(sqlite3BtreePager(pBt), 0); |
| 18140 } |
| 18141 } |
| 18142 } |
| 18143 } |
| 18144 |
| 18145 sqlite3_mutex_leave(db->mutex); |
| 18146 #endif /* SQLITE_OMIT_WAL */ |
| 18147 return rc; |
| 18148 } |
| 18149 |
| 18150 /* |
| 18151 ** Recover as many snapshots as possible from the wal file associated with |
| 18152 ** schema zDb of database db. |
| 18153 */ |
| 18154 SQLITE_API int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb){ |
| 18155 int rc = SQLITE_ERROR; |
| 18156 int iDb; |
| 18157 #ifndef SQLITE_OMIT_WAL |
| 18158 |
| 18159 #ifdef SQLITE_ENABLE_API_ARMOR |
| 18160 if( !sqlite3SafetyCheckOk(db) ){ |
| 18161 return SQLITE_MISUSE_BKPT; |
| 18162 } |
| 18163 #endif |
| 18164 |
| 18165 sqlite3_mutex_enter(db->mutex); |
| 18166 iDb = sqlite3FindDbName(db, zDb); |
| 18167 if( iDb==0 || iDb>1 ){ |
| 18168 Btree *pBt = db->aDb[iDb].pBt; |
| 18169 if( 0==sqlite3BtreeIsInReadTrans(pBt) ){ |
| 18170 rc = sqlite3BtreeBeginTrans(pBt, 0); |
| 18171 if( rc==SQLITE_OK ){ |
| 18172 rc = sqlite3PagerSnapshotRecover(sqlite3BtreePager(pBt)); |
| 18173 sqlite3BtreeCommit(pBt); |
| 18174 } |
| 18175 } |
| 18176 } |
| 18177 sqlite3_mutex_leave(db->mutex); |
| 18178 #endif /* SQLITE_OMIT_WAL */ |
| 18179 return rc; |
| 18180 } |
| 18181 |
| 18182 /* |
| 18183 ** Free a snapshot handle obtained from sqlite3_snapshot_get(). |
| 18184 */ |
| 18185 SQLITE_API void sqlite3_snapshot_free(sqlite3_snapshot *pSnapshot){ |
| 18186 sqlite3_free(pSnapshot); |
| 18187 } |
| 18188 #endif /* SQLITE_ENABLE_SNAPSHOT */ |
| 18189 |
| 18190 /************** End of main.c ************************************************/ |
| 18191 /************** Begin file notify.c ******************************************/ |
| 18192 /* |
| 18193 ** 2009 March 3 |
| 18194 ** |
| 18195 ** The author disclaims copyright to this source code. In place of |
| 18196 ** a legal notice, here is a blessing: |
| 18197 ** |
| 18198 ** May you do good and not evil. |
| 18199 ** May you find forgiveness for yourself and forgive others. |
| 18200 ** May you share freely, never taking more than you give. |
| 18201 ** |
| 18202 ************************************************************************* |
| 18203 ** |
| 18204 ** This file contains the implementation of the sqlite3_unlock_notify() |
| 18205 ** API method and its associated functionality. |
| 18206 */ |
| 18207 /* #include "sqliteInt.h" */ |
| 18208 /* #include "btreeInt.h" */ |
| 18209 |
| 18210 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */ |
| 18211 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY |
| 18212 |
| 18213 /* |
| 18214 ** Public interfaces: |
| 18215 ** |
| 18216 ** sqlite3ConnectionBlocked() |
| 18217 ** sqlite3ConnectionUnlocked() |
| 18218 ** sqlite3ConnectionClosed() |
| 18219 ** sqlite3_unlock_notify() |
| 18220 */ |
| 18221 |
| 18222 #define assertMutexHeld() \ |
| 18223 assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) ) |
| 18224 |
| 18225 /* |
| 18226 ** Head of a linked list of all sqlite3 objects created by this process |
| 18227 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection |
| 18228 ** is not NULL. This variable may only accessed while the STATIC_MASTER |
| 18229 ** mutex is held. |
| 18230 */ |
| 18231 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0; |
| 18232 |
| 18233 #ifndef NDEBUG |
| 18234 /* |
| 18235 ** This function is a complex assert() that verifies the following |
| 18236 ** properties of the blocked connections list: |
| 18237 ** |
| 18238 ** 1) Each entry in the list has a non-NULL value for either |
| 18239 ** pUnlockConnection or pBlockingConnection, or both. |
| 18240 ** |
| 18241 ** 2) All entries in the list that share a common value for |
| 18242 ** xUnlockNotify are grouped together. |
| 18243 ** |
| 18244 ** 3) If the argument db is not NULL, then none of the entries in the |
| 18245 ** blocked connections list have pUnlockConnection or pBlockingConnection |
| 18246 ** set to db. This is used when closing connection db. |
| 18247 */ |
| 18248 static void checkListProperties(sqlite3 *db){ |
| 18249 sqlite3 *p; |
| 18250 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){ |
| 18251 int seen = 0; |
| 18252 sqlite3 *p2; |
| 18253 |
| 18254 /* Verify property (1) */ |
| 18255 assert( p->pUnlockConnection || p->pBlockingConnection ); |
| 18256 |
| 18257 /* Verify property (2) */ |
| 18258 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){ |
| 18259 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1; |
| 18260 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen ); |
| 18261 assert( db==0 || p->pUnlockConnection!=db ); |
| 18262 assert( db==0 || p->pBlockingConnection!=db ); |
| 18263 } |
| 18264 } |
| 18265 } |
| 18266 #else |
| 18267 # define checkListProperties(x) |
| 18268 #endif |
| 18269 |
| 18270 /* |
| 18271 ** Remove connection db from the blocked connections list. If connection |
| 18272 ** db is not currently a part of the list, this function is a no-op. |
| 18273 */ |
| 18274 static void removeFromBlockedList(sqlite3 *db){ |
| 18275 sqlite3 **pp; |
| 18276 assertMutexHeld(); |
| 18277 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){ |
| 18278 if( *pp==db ){ |
| 18279 *pp = (*pp)->pNextBlocked; |
| 18280 break; |
| 18281 } |
| 18282 } |
| 18283 } |
| 18284 |
| 18285 /* |
| 18286 ** Add connection db to the blocked connections list. It is assumed |
| 18287 ** that it is not already a part of the list. |
| 18288 */ |
| 18289 static void addToBlockedList(sqlite3 *db){ |
| 18290 sqlite3 **pp; |
| 18291 assertMutexHeld(); |
| 18292 for( |
| 18293 pp=&sqlite3BlockedList; |
| 18294 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; |
| 18295 pp=&(*pp)->pNextBlocked |
| 18296 ); |
| 18297 db->pNextBlocked = *pp; |
| 18298 *pp = db; |
| 18299 } |
| 18300 |
| 18301 /* |
| 18302 ** Obtain the STATIC_MASTER mutex. |
| 18303 */ |
| 18304 static void enterMutex(void){ |
| 18305 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 18306 checkListProperties(0); |
| 18307 } |
| 18308 |
| 18309 /* |
| 18310 ** Release the STATIC_MASTER mutex. |
| 18311 */ |
| 18312 static void leaveMutex(void){ |
| 18313 assertMutexHeld(); |
| 18314 checkListProperties(0); |
| 18315 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 18316 } |
| 18317 |
| 18318 /* |
| 18319 ** Register an unlock-notify callback. |
| 18320 ** |
| 18321 ** This is called after connection "db" has attempted some operation |
| 18322 ** but has received an SQLITE_LOCKED error because another connection |
| 18323 ** (call it pOther) in the same process was busy using the same shared |
| 18324 ** cache. pOther is found by looking at db->pBlockingConnection. |
| 18325 ** |
| 18326 ** If there is no blocking connection, the callback is invoked immediately, |
| 18327 ** before this routine returns. |
| 18328 ** |
| 18329 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate |
| 18330 ** a deadlock. |
| 18331 ** |
| 18332 ** Otherwise, make arrangements to invoke xNotify when pOther drops |
| 18333 ** its locks. |
| 18334 ** |
| 18335 ** Each call to this routine overrides any prior callbacks registered |
| 18336 ** on the same "db". If xNotify==0 then any prior callbacks are immediately |
| 18337 ** cancelled. |
| 18338 */ |
| 18339 SQLITE_API int sqlite3_unlock_notify( |
| 18340 sqlite3 *db, |
| 18341 void (*xNotify)(void **, int), |
| 18342 void *pArg |
| 18343 ){ |
| 18344 int rc = SQLITE_OK; |
| 18345 |
| 18346 sqlite3_mutex_enter(db->mutex); |
| 18347 enterMutex(); |
| 18348 |
| 18349 if( xNotify==0 ){ |
| 18350 removeFromBlockedList(db); |
| 18351 db->pBlockingConnection = 0; |
| 18352 db->pUnlockConnection = 0; |
| 18353 db->xUnlockNotify = 0; |
| 18354 db->pUnlockArg = 0; |
| 18355 }else if( 0==db->pBlockingConnection ){ |
| 18356 /* The blocking transaction has been concluded. Or there never was a |
| 18357 ** blocking transaction. In either case, invoke the notify callback |
| 18358 ** immediately. |
| 18359 */ |
| 18360 xNotify(&pArg, 1); |
| 18361 }else{ |
| 18362 sqlite3 *p; |
| 18363 |
| 18364 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){} |
| 18365 if( p ){ |
| 18366 rc = SQLITE_LOCKED; /* Deadlock detected. */ |
| 18367 }else{ |
| 18368 db->pUnlockConnection = db->pBlockingConnection; |
| 18369 db->xUnlockNotify = xNotify; |
| 18370 db->pUnlockArg = pArg; |
| 18371 removeFromBlockedList(db); |
| 18372 addToBlockedList(db); |
| 18373 } |
| 18374 } |
| 18375 |
| 18376 leaveMutex(); |
| 18377 assert( !db->mallocFailed ); |
| 18378 sqlite3ErrorWithMsg(db, rc, (rc?"database is deadlocked":0)); |
| 18379 sqlite3_mutex_leave(db->mutex); |
| 18380 return rc; |
| 18381 } |
| 18382 |
| 18383 /* |
| 18384 ** This function is called while stepping or preparing a statement |
| 18385 ** associated with connection db. The operation will return SQLITE_LOCKED |
| 18386 ** to the user because it requires a lock that will not be available |
| 18387 ** until connection pBlocker concludes its current transaction. |
| 18388 */ |
| 18389 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){ |
| 18390 enterMutex(); |
| 18391 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){ |
| 18392 addToBlockedList(db); |
| 18393 } |
| 18394 db->pBlockingConnection = pBlocker; |
| 18395 leaveMutex(); |
| 18396 } |
| 18397 |
| 18398 /* |
| 18399 ** This function is called when |
| 18400 ** the transaction opened by database db has just finished. Locks held |
| 18401 ** by database connection db have been released. |
| 18402 ** |
| 18403 ** This function loops through each entry in the blocked connections |
| 18404 ** list and does the following: |
| 18405 ** |
| 18406 ** 1) If the sqlite3.pBlockingConnection member of a list entry is |
| 18407 ** set to db, then set pBlockingConnection=0. |
| 18408 ** |
| 18409 ** 2) If the sqlite3.pUnlockConnection member of a list entry is |
| 18410 ** set to db, then invoke the configured unlock-notify callback and |
| 18411 ** set pUnlockConnection=0. |
| 18412 ** |
| 18413 ** 3) If the two steps above mean that pBlockingConnection==0 and |
| 18414 ** pUnlockConnection==0, remove the entry from the blocked connections |
| 18415 ** list. |
| 18416 */ |
| 18417 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){ |
| 18418 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */ |
| 18419 int nArg = 0; /* Number of entries in aArg[] */ |
| 18420 sqlite3 **pp; /* Iterator variable */ |
| 18421 void **aArg; /* Arguments to the unlock callback */ |
| 18422 void **aDyn = 0; /* Dynamically allocated space for aArg[] */ |
| 18423 void *aStatic[16]; /* Starter space for aArg[]. No malloc required */ |
| 18424 |
| 18425 aArg = aStatic; |
| 18426 enterMutex(); /* Enter STATIC_MASTER mutex */ |
| 18427 |
| 18428 /* This loop runs once for each entry in the blocked-connections list. */ |
| 18429 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){ |
| 18430 sqlite3 *p = *pp; |
| 18431 |
| 18432 /* Step 1. */ |
| 18433 if( p->pBlockingConnection==db ){ |
| 18434 p->pBlockingConnection = 0; |
| 18435 } |
| 18436 |
| 18437 /* Step 2. */ |
| 18438 if( p->pUnlockConnection==db ){ |
| 18439 assert( p->xUnlockNotify ); |
| 18440 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){ |
| 18441 xUnlockNotify(aArg, nArg); |
| 18442 nArg = 0; |
| 18443 } |
| 18444 |
| 18445 sqlite3BeginBenignMalloc(); |
| 18446 assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) ); |
| 18447 assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn ); |
| 18448 if( (!aDyn && nArg==(int)ArraySize(aStatic)) |
| 18449 || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*))) |
| 18450 ){ |
| 18451 /* The aArg[] array needs to grow. */ |
| 18452 void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2); |
| 18453 if( pNew ){ |
| 18454 memcpy(pNew, aArg, nArg*sizeof(void *)); |
| 18455 sqlite3_free(aDyn); |
| 18456 aDyn = aArg = pNew; |
| 18457 }else{ |
| 18458 /* This occurs when the array of context pointers that need to |
| 18459 ** be passed to the unlock-notify callback is larger than the |
| 18460 ** aStatic[] array allocated on the stack and the attempt to |
| 18461 ** allocate a larger array from the heap has failed. |
| 18462 ** |
| 18463 ** This is a difficult situation to handle. Returning an error |
| 18464 ** code to the caller is insufficient, as even if an error code |
| 18465 ** is returned the transaction on connection db will still be |
| 18466 ** closed and the unlock-notify callbacks on blocked connections |
| 18467 ** will go unissued. This might cause the application to wait |
| 18468 ** indefinitely for an unlock-notify callback that will never |
| 18469 ** arrive. |
| 18470 ** |
| 18471 ** Instead, invoke the unlock-notify callback with the context |
| 18472 ** array already accumulated. We can then clear the array and |
| 18473 ** begin accumulating any further context pointers without |
| 18474 ** requiring any dynamic allocation. This is sub-optimal because |
| 18475 ** it means that instead of one callback with a large array of |
| 18476 ** context pointers the application will receive two or more |
| 18477 ** callbacks with smaller arrays of context pointers, which will |
| 18478 ** reduce the applications ability to prioritize multiple |
| 18479 ** connections. But it is the best that can be done under the |
| 18480 ** circumstances. |
| 18481 */ |
| 18482 xUnlockNotify(aArg, nArg); |
| 18483 nArg = 0; |
| 18484 } |
| 18485 } |
| 18486 sqlite3EndBenignMalloc(); |
| 18487 |
| 18488 aArg[nArg++] = p->pUnlockArg; |
| 18489 xUnlockNotify = p->xUnlockNotify; |
| 18490 p->pUnlockConnection = 0; |
| 18491 p->xUnlockNotify = 0; |
| 18492 p->pUnlockArg = 0; |
| 18493 } |
| 18494 |
| 18495 /* Step 3. */ |
| 18496 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){ |
| 18497 /* Remove connection p from the blocked connections list. */ |
| 18498 *pp = p->pNextBlocked; |
| 18499 p->pNextBlocked = 0; |
| 18500 }else{ |
| 18501 pp = &p->pNextBlocked; |
| 18502 } |
| 18503 } |
| 18504 |
| 18505 if( nArg!=0 ){ |
| 18506 xUnlockNotify(aArg, nArg); |
| 18507 } |
| 18508 sqlite3_free(aDyn); |
| 18509 leaveMutex(); /* Leave STATIC_MASTER mutex */ |
| 18510 } |
| 18511 |
| 18512 /* |
| 18513 ** This is called when the database connection passed as an argument is |
| 18514 ** being closed. The connection is removed from the blocked list. |
| 18515 */ |
| 18516 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){ |
| 18517 sqlite3ConnectionUnlocked(db); |
| 18518 enterMutex(); |
| 18519 removeFromBlockedList(db); |
| 18520 checkListProperties(db); |
| 18521 leaveMutex(); |
| 18522 } |
| 18523 #endif |
| 18524 |
| 18525 /************** End of notify.c **********************************************/ |
| 18526 |
| 18527 /* Chain include. */ |
| 18528 #include "sqlite3.07.c" |
OLD | NEW |