| | @@ -231,11 +231,11 @@ |
| 231 | 231 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 232 | 232 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 233 | 233 | */ |
| 234 | 234 | #define SQLITE_VERSION "3.8.7" |
| 235 | 235 | #define SQLITE_VERSION_NUMBER 3008007 |
| 236 | | -#define SQLITE_SOURCE_ID "2014-10-04 19:31:53 b8f7f19dc06c59de2e194d83e6c052fb7d28c71d" |
| 236 | +#define SQLITE_SOURCE_ID "2014-10-13 23:39:02 005e5b388a8a97bca6d1f0e06c40d68d92aa1212" |
| 237 | 237 | |
| 238 | 238 | /* |
| 239 | 239 | ** CAPI3REF: Run-Time Library Version Numbers |
| 240 | 240 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 241 | 241 | ** |
| | @@ -7944,11 +7944,11 @@ |
| 7944 | 7944 | ** A macro to hint to the compiler that a function should not be |
| 7945 | 7945 | ** inlined. |
| 7946 | 7946 | */ |
| 7947 | 7947 | #if defined(__GNUC__) |
| 7948 | 7948 | # define SQLITE_NOINLINE __attribute__((noinline)) |
| 7949 | | -#elif defined(_MSC_VER) |
| 7949 | +#elif defined(_MSC_VER) && _MSC_VER>=1310 |
| 7950 | 7950 | # define SQLITE_NOINLINE __declspec(noinline) |
| 7951 | 7951 | #else |
| 7952 | 7952 | # define SQLITE_NOINLINE |
| 7953 | 7953 | #endif |
| 7954 | 7954 | |
| | @@ -11322,10 +11322,11 @@ |
| 11322 | 11322 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 11323 | 11323 | int nSample; /* Number of elements in aSample[] */ |
| 11324 | 11324 | int nSampleCol; /* Size of IndexSample.anEq[] and so on */ |
| 11325 | 11325 | tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */ |
| 11326 | 11326 | IndexSample *aSample; /* Samples of the left-most key */ |
| 11327 | + tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this table */ |
| 11327 | 11328 | #endif |
| 11328 | 11329 | }; |
| 11329 | 11330 | |
| 11330 | 11331 | /* |
| 11331 | 11332 | ** Allowed values for Index.idxType |
| | @@ -12186,11 +12187,10 @@ |
| 12186 | 12187 | #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */ |
| 12187 | 12188 | #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */ |
| 12188 | 12189 | #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */ |
| 12189 | 12190 | #define OPFLAG_APPEND 0x08 /* This is likely to be an append */ |
| 12190 | 12191 | #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */ |
| 12191 | | -#define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */ |
| 12192 | 12192 | #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */ |
| 12193 | 12193 | #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */ |
| 12194 | 12194 | #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */ |
| 12195 | 12195 | #define OPFLAG_P2ISREG 0x02 /* P2 to OP_Open** is a register number */ |
| 12196 | 12196 | #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */ |
| | @@ -13320,14 +13320,13 @@ |
| 13320 | 13320 | # define sqlite3MemdebugSetType(X,Y) /* no-op */ |
| 13321 | 13321 | # define sqlite3MemdebugHasType(X,Y) 1 |
| 13322 | 13322 | # define sqlite3MemdebugNoType(X,Y) 1 |
| 13323 | 13323 | #endif |
| 13324 | 13324 | #define MEMTYPE_HEAP 0x01 /* General heap allocations */ |
| 13325 | | -#define MEMTYPE_LOOKASIDE 0x02 /* Might have been lookaside memory */ |
| 13325 | +#define MEMTYPE_LOOKASIDE 0x02 /* Heap that might have been lookaside */ |
| 13326 | 13326 | #define MEMTYPE_SCRATCH 0x04 /* Scratch allocations */ |
| 13327 | 13327 | #define MEMTYPE_PCACHE 0x08 /* Page cache allocations */ |
| 13328 | | -#define MEMTYPE_DB 0x10 /* Uses sqlite3DbMalloc, not sqlite_malloc */ |
| 13329 | 13328 | |
| 13330 | 13329 | /* |
| 13331 | 13330 | ** Threading interface |
| 13332 | 13331 | */ |
| 13333 | 13332 | #if SQLITE_MAX_WORKER_THREADS>0 |
| | @@ -14095,21 +14094,19 @@ |
| 14095 | 14094 | #ifdef SQLITE_DEBUG |
| 14096 | 14095 | u8 seekOp; /* Most recent seek operation on this cursor */ |
| 14097 | 14096 | #endif |
| 14098 | 14097 | i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */ |
| 14099 | 14098 | u8 nullRow; /* True if pointing to a row with no data */ |
| 14100 | | - u8 rowidIsValid; /* True if lastRowid is valid */ |
| 14101 | 14099 | u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */ |
| 14102 | 14100 | Bool isEphemeral:1; /* True for an ephemeral table */ |
| 14103 | 14101 | Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */ |
| 14104 | 14102 | Bool isTable:1; /* True if a table requiring integer keys */ |
| 14105 | 14103 | Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */ |
| 14106 | 14104 | Pgno pgnoRoot; /* Root page of the open btree cursor */ |
| 14107 | 14105 | sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */ |
| 14108 | 14106 | i64 seqCount; /* Sequence counter */ |
| 14109 | 14107 | i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */ |
| 14110 | | - i64 lastRowid; /* Rowid being deleted by OP_Delete */ |
| 14111 | 14108 | VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */ |
| 14112 | 14109 | |
| 14113 | 14110 | /* Cached information about the header for the data record that the |
| 14114 | 14111 | ** cursor is currently pointing to. Only valid if cacheStatus matches |
| 14115 | 14112 | ** Vdbe.cacheCtr. Vdbe.cacheCtr will never take on the value of |
| | @@ -14122,10 +14119,11 @@ |
| 14122 | 14119 | u32 cacheStatus; /* Cache is valid if this matches Vdbe.cacheCtr */ |
| 14123 | 14120 | u32 payloadSize; /* Total number of bytes in the record */ |
| 14124 | 14121 | u32 szRow; /* Byte available in aRow */ |
| 14125 | 14122 | u32 iHdrOffset; /* Offset to next unparsed byte of the header */ |
| 14126 | 14123 | const u8 *aRow; /* Data for the current row, if all on one page */ |
| 14124 | + u32 *aOffset; /* Pointer to aType[nField] */ |
| 14127 | 14125 | u32 aType[1]; /* Type values for all entries in the record */ |
| 14128 | 14126 | /* 2*nField extra array elements allocated for aType[], beyond the one |
| 14129 | 14127 | ** static element declared in the structure. nField total array slots for |
| 14130 | 14128 | ** aType[] and nField+1 array slots for aOffset[] */ |
| 14131 | 14129 | }; |
| | @@ -14198,11 +14196,11 @@ |
| 14198 | 14196 | int n; /* Number of characters in string value, excluding '\0' */ |
| 14199 | 14197 | char *z; /* String or BLOB value */ |
| 14200 | 14198 | /* ShallowCopy only needs to copy the information above */ |
| 14201 | 14199 | char *zMalloc; /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */ |
| 14202 | 14200 | int szMalloc; /* Size of the zMalloc allocation */ |
| 14203 | | - int iPadding1; /* Padding for 8-byte alignment */ |
| 14201 | + u32 uTemp; /* Transient storage for serial_type in OP_MakeRecord */ |
| 14204 | 14202 | sqlite3 *db; /* The associated database connection */ |
| 14205 | 14203 | void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */ |
| 14206 | 14204 | #ifdef SQLITE_DEBUG |
| 14207 | 14205 | Mem *pScopyFrom; /* This Mem is a shallow copy of pScopyFrom */ |
| 14208 | 14206 | void *pFiller; /* So that sizeof(Mem) is a multiple of 8 */ |
| | @@ -14406,10 +14404,11 @@ |
| 14406 | 14404 | ** Function prototypes |
| 14407 | 14405 | */ |
| 14408 | 14406 | SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*); |
| 14409 | 14407 | void sqliteVdbePopStack(Vdbe*,int); |
| 14410 | 14408 | SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*); |
| 14409 | +SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*); |
| 14411 | 14410 | #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 14412 | 14411 | SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*); |
| 14413 | 14412 | #endif |
| 14414 | 14413 | SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32); |
| 14415 | 14414 | SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int); |
| | @@ -17130,11 +17129,11 @@ |
| 17130 | 17129 | ** allocation p. Also return true if p==NULL. |
| 17131 | 17130 | ** |
| 17132 | 17131 | ** This routine is designed for use within an assert() statement, to |
| 17133 | 17132 | ** verify the type of an allocation. For example: |
| 17134 | 17133 | ** |
| 17135 | | -** assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 17134 | +** assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 17136 | 17135 | */ |
| 17137 | 17136 | SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){ |
| 17138 | 17137 | int rc = 1; |
| 17139 | 17138 | if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 17140 | 17139 | struct MemBlockHdr *pHdr; |
| | @@ -17152,11 +17151,11 @@ |
| 17152 | 17151 | ** allocation p. Also return true if p==NULL. |
| 17153 | 17152 | ** |
| 17154 | 17153 | ** This routine is designed for use within an assert() statement, to |
| 17155 | 17154 | ** verify the type of an allocation. For example: |
| 17156 | 17155 | ** |
| 17157 | | -** assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 17156 | +** assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 17158 | 17157 | */ |
| 17159 | 17158 | SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){ |
| 17160 | 17159 | int rc = 1; |
| 17161 | 17160 | if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){ |
| 17162 | 17161 | struct MemBlockHdr *pHdr; |
| | @@ -20364,39 +20363,41 @@ |
| 20364 | 20363 | ** Return the size of a memory allocation previously obtained from |
| 20365 | 20364 | ** sqlite3Malloc() or sqlite3_malloc(). |
| 20366 | 20365 | */ |
| 20367 | 20366 | SQLITE_PRIVATE int sqlite3MallocSize(void *p){ |
| 20368 | 20367 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20369 | | - assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 20370 | 20368 | return sqlite3GlobalConfig.m.xSize(p); |
| 20371 | 20369 | } |
| 20372 | 20370 | SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 20373 | 20371 | if( db==0 ){ |
| 20372 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20373 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20374 | 20374 | return sqlite3MallocSize(p); |
| 20375 | 20375 | }else{ |
| 20376 | 20376 | assert( sqlite3_mutex_held(db->mutex) ); |
| 20377 | 20377 | if( isLookaside(db, p) ){ |
| 20378 | 20378 | return db->lookaside.sz; |
| 20379 | 20379 | }else{ |
| 20380 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 20381 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 20382 | | - assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 20380 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20381 | + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20383 | 20382 | return sqlite3GlobalConfig.m.xSize(p); |
| 20384 | 20383 | } |
| 20385 | 20384 | } |
| 20386 | 20385 | } |
| 20387 | 20386 | SQLITE_API sqlite3_uint64 sqlite3_msize(void *p){ |
| 20387 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20388 | + assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20388 | 20389 | return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p); |
| 20389 | 20390 | } |
| 20390 | 20391 | |
| 20391 | 20392 | /* |
| 20392 | 20393 | ** Free memory previously obtained from sqlite3Malloc(). |
| 20393 | 20394 | */ |
| 20394 | 20395 | SQLITE_API void sqlite3_free(void *p){ |
| 20395 | 20396 | if( p==0 ) return; /* IMP: R-49053-54554 */ |
| 20396 | | - assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) ); |
| 20397 | 20397 | assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 20398 | + assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) ); |
| 20398 | 20399 | if( sqlite3GlobalConfig.bMemstat ){ |
| 20399 | 20400 | sqlite3_mutex_enter(mem0.mutex); |
| 20400 | 20401 | sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p)); |
| 20401 | 20402 | sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1); |
| 20402 | 20403 | sqlite3GlobalConfig.m.xFree(p); |
| | @@ -20436,12 +20437,12 @@ |
| 20436 | 20437 | db->lookaside.pFree = pBuf; |
| 20437 | 20438 | db->lookaside.nOut--; |
| 20438 | 20439 | return; |
| 20439 | 20440 | } |
| 20440 | 20441 | } |
| 20441 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 20442 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 20442 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20443 | + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20443 | 20444 | assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 20444 | 20445 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20445 | 20446 | sqlite3_free(p); |
| 20446 | 20447 | } |
| 20447 | 20448 | |
| | @@ -20449,10 +20450,12 @@ |
| 20449 | 20450 | ** Change the size of an existing memory allocation |
| 20450 | 20451 | */ |
| 20451 | 20452 | SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ |
| 20452 | 20453 | int nOld, nNew, nDiff; |
| 20453 | 20454 | void *pNew; |
| 20455 | + assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 20456 | + assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 20454 | 20457 | if( pOld==0 ){ |
| 20455 | 20458 | return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
| 20456 | 20459 | } |
| 20457 | 20460 | if( nBytes==0 ){ |
| 20458 | 20461 | sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
| | @@ -20475,12 +20478,10 @@ |
| 20475 | 20478 | nDiff = nNew - nOld; |
| 20476 | 20479 | if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
| 20477 | 20480 | mem0.alarmThreshold-nDiff ){ |
| 20478 | 20481 | sqlite3MallocAlarm(nDiff); |
| 20479 | 20482 | } |
| 20480 | | - assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 20481 | | - assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) ); |
| 20482 | 20483 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20483 | 20484 | if( pNew==0 && mem0.alarmCallback ){ |
| 20484 | 20485 | sqlite3MallocAlarm((int)nBytes); |
| 20485 | 20486 | pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 20486 | 20487 | } |
| | @@ -20589,12 +20590,12 @@ |
| 20589 | 20590 | #endif |
| 20590 | 20591 | p = sqlite3Malloc(n); |
| 20591 | 20592 | if( !p && db ){ |
| 20592 | 20593 | db->mallocFailed = 1; |
| 20593 | 20594 | } |
| 20594 | | - sqlite3MemdebugSetType(p, MEMTYPE_DB | |
| 20595 | | - ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 20595 | + sqlite3MemdebugSetType(p, |
| 20596 | + (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); |
| 20596 | 20597 | return p; |
| 20597 | 20598 | } |
| 20598 | 20599 | |
| 20599 | 20600 | /* |
| 20600 | 20601 | ** Resize the block of memory pointed to by p to n bytes. If the |
| | @@ -20616,19 +20617,18 @@ |
| 20616 | 20617 | if( pNew ){ |
| 20617 | 20618 | memcpy(pNew, p, db->lookaside.sz); |
| 20618 | 20619 | sqlite3DbFree(db, p); |
| 20619 | 20620 | } |
| 20620 | 20621 | }else{ |
| 20621 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) ); |
| 20622 | | - assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) ); |
| 20622 | + assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20623 | + assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 20623 | 20624 | sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 20624 | 20625 | pNew = sqlite3_realloc64(p, n); |
| 20625 | 20626 | if( !pNew ){ |
| 20626 | | - sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP); |
| 20627 | 20627 | db->mallocFailed = 1; |
| 20628 | 20628 | } |
| 20629 | | - sqlite3MemdebugSetType(pNew, MEMTYPE_DB | |
| 20629 | + sqlite3MemdebugSetType(pNew, |
| 20630 | 20630 | (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 20631 | 20631 | } |
| 20632 | 20632 | } |
| 20633 | 20633 | return pNew; |
| 20634 | 20634 | } |
| | @@ -22098,11 +22098,11 @@ |
| 22098 | 22098 | #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ |
| 22099 | 22099 | #include <process.h> |
| 22100 | 22100 | |
| 22101 | 22101 | /* A running thread */ |
| 22102 | 22102 | struct SQLiteThread { |
| 22103 | | - uintptr_t tid; /* The thread handle */ |
| 22103 | + void *tid; /* The thread handle */ |
| 22104 | 22104 | unsigned id; /* The thread identifier */ |
| 22105 | 22105 | void *(*xTask)(void*); /* The routine to run as a thread */ |
| 22106 | 22106 | void *pIn; /* Argument to xTask */ |
| 22107 | 22107 | void *pResult; /* Result of xTask */ |
| 22108 | 22108 | }; |
| | @@ -22146,11 +22146,11 @@ |
| 22146 | 22146 | if( sqlite3GlobalConfig.bCoreMutex==0 ){ |
| 22147 | 22147 | memset(p, 0, sizeof(*p)); |
| 22148 | 22148 | }else{ |
| 22149 | 22149 | p->xTask = xTask; |
| 22150 | 22150 | p->pIn = pIn; |
| 22151 | | - p->tid = _beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); |
| 22151 | + p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); |
| 22152 | 22152 | if( p->tid==0 ){ |
| 22153 | 22153 | memset(p, 0, sizeof(*p)); |
| 22154 | 22154 | } |
| 22155 | 22155 | } |
| 22156 | 22156 | if( p->xTask==0 ){ |
| | @@ -39855,11 +39855,11 @@ |
| 39855 | 39855 | assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); |
| 39856 | 39856 | assert( pCache->n90pct == pCache->nMax*9/10 ); |
| 39857 | 39857 | if( createFlag==1 && ( |
| 39858 | 39858 | nPinned>=pGroup->mxPinned |
| 39859 | 39859 | || nPinned>=pCache->n90pct |
| 39860 | | - || pcache1UnderMemoryPressure(pCache) |
| 39860 | + || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned) |
| 39861 | 39861 | )){ |
| 39862 | 39862 | return 0; |
| 39863 | 39863 | } |
| 39864 | 39864 | |
| 39865 | 39865 | if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache); |
| | @@ -44476,17 +44476,19 @@ |
| 44476 | 44476 | if( !pNew ) rc = SQLITE_NOMEM; |
| 44477 | 44477 | } |
| 44478 | 44478 | |
| 44479 | 44479 | if( rc==SQLITE_OK ){ |
| 44480 | 44480 | pager_reset(pPager); |
| 44481 | | - sqlite3PageFree(pPager->pTmpSpace); |
| 44482 | | - pPager->pTmpSpace = pNew; |
| 44483 | 44481 | rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize); |
| 44484 | 44482 | } |
| 44485 | 44483 | if( rc==SQLITE_OK ){ |
| 44484 | + sqlite3PageFree(pPager->pTmpSpace); |
| 44485 | + pPager->pTmpSpace = pNew; |
| 44486 | 44486 | pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize); |
| 44487 | 44487 | pPager->pageSize = pageSize; |
| 44488 | + }else{ |
| 44489 | + sqlite3PageFree(pNew); |
| 44488 | 44490 | } |
| 44489 | 44491 | } |
| 44490 | 44492 | |
| 44491 | 44493 | *pPageSize = pPager->pageSize; |
| 44492 | 44494 | if( rc==SQLITE_OK ){ |
| | @@ -52947,11 +52949,11 @@ |
| 52947 | 52949 | ** |
| 52948 | 52950 | ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor |
| 52949 | 52951 | ** back to where it ought to be if this routine returns true. |
| 52950 | 52952 | */ |
| 52951 | 52953 | SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ |
| 52952 | | - return pCur && pCur->eState!=CURSOR_VALID; |
| 52954 | + return pCur->eState!=CURSOR_VALID; |
| 52953 | 52955 | } |
| 52954 | 52956 | |
| 52955 | 52957 | /* |
| 52956 | 52958 | ** This routine restores a cursor back to its original position after it |
| 52957 | 52959 | ** has been moved by some outside activity (such as a btree rebalance or |
| | @@ -58016,15 +58018,10 @@ |
| 58016 | 58018 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 58017 | 58019 | ** in pPage->apOvfl[] and make it point to the cell content (either |
| 58018 | 58020 | ** in pTemp or the original pCell) and also record its index. |
| 58019 | 58021 | ** Allocating a new entry in pPage->aCell[] implies that |
| 58020 | 58022 | ** pPage->nOverflow is incremented. |
| 58021 | | -** |
| 58022 | | -** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 58023 | | -** cell. The caller will overwrite them after this function returns. If |
| 58024 | | -** nSkip is non-zero, then pCell may not point to an invalid memory location |
| 58025 | | -** (but pCell+nSkip is always valid). |
| 58026 | 58023 | */ |
| 58027 | 58024 | static void insertCell( |
| 58028 | 58025 | MemPage *pPage, /* Page into which we are copying */ |
| 58029 | 58026 | int i, /* New cell becomes the i-th cell of the page */ |
| 58030 | 58027 | u8 *pCell, /* Content of the new cell */ |
| | @@ -58037,11 +58034,10 @@ |
| 58037 | 58034 | int j; /* Loop counter */ |
| 58038 | 58035 | int end; /* First byte past the last cell pointer in data[] */ |
| 58039 | 58036 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 58040 | 58037 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 58041 | 58038 | u8 *data; /* The content of the whole page */ |
| 58042 | | - int nSkip = (iChild ? 4 : 0); |
| 58043 | 58039 | |
| 58044 | 58040 | if( *pRC ) return; |
| 58045 | 58041 | |
| 58046 | 58042 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 58047 | 58043 | assert( MX_CELL(pPage->pBt)<=10921 ); |
| | @@ -58055,11 +58051,11 @@ |
| 58055 | 58051 | ** might be less than 8 (leaf-size + pointer) on the interior node. Hence |
| 58056 | 58052 | ** the term after the || in the following assert(). */ |
| 58057 | 58053 | assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); |
| 58058 | 58054 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
| 58059 | 58055 | if( pTemp ){ |
| 58060 | | - memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
| 58056 | + memcpy(pTemp, pCell, sz); |
| 58061 | 58057 | pCell = pTemp; |
| 58062 | 58058 | } |
| 58063 | 58059 | if( iChild ){ |
| 58064 | 58060 | put4byte(pCell, iChild); |
| 58065 | 58061 | } |
| | @@ -58084,11 +58080,11 @@ |
| 58084 | 58080 | ** if it returns success */ |
| 58085 | 58081 | assert( idx >= end+2 ); |
| 58086 | 58082 | assert( idx+sz <= (int)pPage->pBt->usableSize ); |
| 58087 | 58083 | pPage->nCell++; |
| 58088 | 58084 | pPage->nFree -= (u16)(2 + sz); |
| 58089 | | - memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
| 58085 | + memcpy(&data[idx], pCell, sz); |
| 58090 | 58086 | if( iChild ){ |
| 58091 | 58087 | put4byte(&data[idx], iChild); |
| 58092 | 58088 | } |
| 58093 | 58089 | memmove(&data[ins+2], &data[ins], end-ins); |
| 58094 | 58090 | put2byte(&data[ins], idx); |
| | @@ -61630,11 +61626,14 @@ |
| 61630 | 61626 | /* If MEM_Dyn is set then Mem.xDel!=0. |
| 61631 | 61627 | ** Mem.xDel is might not be initialized if MEM_Dyn is clear. |
| 61632 | 61628 | */ |
| 61633 | 61629 | assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); |
| 61634 | 61630 | |
| 61635 | | - /* MEM_Dyn may only be set if Mem.szMalloc==0 */ |
| 61631 | + /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we |
| 61632 | + ** ensure that if Mem.szMalloc>0 then it is safe to do |
| 61633 | + ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn. |
| 61634 | + ** That saves a few cycles in inner loops. */ |
| 61636 | 61635 | assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); |
| 61637 | 61636 | |
| 61638 | 61637 | /* Cannot be both MEM_Int and MEM_Real at the same time */ |
| 61639 | 61638 | assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); |
| 61640 | 61639 | |
| | @@ -61766,11 +61765,12 @@ |
| 61766 | 61765 | ** |
| 61767 | 61766 | ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) |
| 61768 | 61767 | ** if unable to complete the resizing. |
| 61769 | 61768 | */ |
| 61770 | 61769 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ |
| 61771 | | - assert( szNew>=0 ); |
| 61770 | + assert( szNew>0 ); |
| 61771 | + assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); |
| 61772 | 61772 | if( pMem->szMalloc<szNew ){ |
| 61773 | 61773 | return sqlite3VdbeMemGrow(pMem, szNew, 0); |
| 61774 | 61774 | } |
| 61775 | 61775 | assert( (pMem->flags & MEM_Dyn)==0 ); |
| 61776 | 61776 | pMem->z = pMem->zMalloc; |
| | @@ -62490,11 +62490,14 @@ |
| 62490 | 62490 | nAlloc += (enc==SQLITE_UTF8?1:2); |
| 62491 | 62491 | } |
| 62492 | 62492 | if( nByte>iLimit ){ |
| 62493 | 62493 | return SQLITE_TOOBIG; |
| 62494 | 62494 | } |
| 62495 | | - if( sqlite3VdbeMemClearAndResize(pMem, nAlloc) ){ |
| 62495 | + testcase( nAlloc==0 ); |
| 62496 | + testcase( nAlloc==31 ); |
| 62497 | + testcase( nAlloc==32 ); |
| 62498 | + if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){ |
| 62496 | 62499 | return SQLITE_NOMEM; |
| 62497 | 62500 | } |
| 62498 | 62501 | memcpy(pMem->z, z, nAlloc); |
| 62499 | 62502 | }else if( xDel==SQLITE_DYNAMIC ){ |
| 62500 | 62503 | sqlite3VdbeMemRelease(pMem); |
| | @@ -62593,11 +62596,11 @@ |
| 62593 | 62596 | /* |
| 62594 | 62597 | ** The pVal argument is known to be a value other than NULL. |
| 62595 | 62598 | ** Convert it into a string with encoding enc and return a pointer |
| 62596 | 62599 | ** to a zero-terminated version of that string. |
| 62597 | 62600 | */ |
| 62598 | | -SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ |
| 62601 | +static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ |
| 62599 | 62602 | assert( pVal!=0 ); |
| 62600 | 62603 | assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); |
| 62601 | 62604 | assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); |
| 62602 | 62605 | assert( (pVal->flags & MEM_RowSet)==0 ); |
| 62603 | 62606 | assert( (pVal->flags & (MEM_Null))==0 ); |
| | @@ -64913,11 +64916,11 @@ |
| 64913 | 64916 | ** the call above. */ |
| 64914 | 64917 | }else if( pCx->pCursor ){ |
| 64915 | 64918 | sqlite3BtreeCloseCursor(pCx->pCursor); |
| 64916 | 64919 | } |
| 64917 | 64920 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 64918 | | - if( pCx->pVtabCursor ){ |
| 64921 | + else if( pCx->pVtabCursor ){ |
| 64919 | 64922 | sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
| 64920 | 64923 | const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; |
| 64921 | 64924 | p->inVtabMethod = 1; |
| 64922 | 64925 | pModule->xClose(pVtabCursor); |
| 64923 | 64926 | p->inVtabMethod = 0; |
| | @@ -64956,13 +64959,14 @@ |
| 64956 | 64959 | static void closeAllCursors(Vdbe *p){ |
| 64957 | 64960 | if( p->pFrame ){ |
| 64958 | 64961 | VdbeFrame *pFrame; |
| 64959 | 64962 | for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 64960 | 64963 | sqlite3VdbeFrameRestore(pFrame); |
| 64964 | + p->pFrame = 0; |
| 64965 | + p->nFrame = 0; |
| 64961 | 64966 | } |
| 64962 | | - p->pFrame = 0; |
| 64963 | | - p->nFrame = 0; |
| 64967 | + assert( p->nFrame==0 ); |
| 64964 | 64968 | |
| 64965 | 64969 | if( p->apCsr ){ |
| 64966 | 64970 | int i; |
| 64967 | 64971 | for(i=0; i<p->nCursor; i++){ |
| 64968 | 64972 | VdbeCursor *pC = p->apCsr[i]; |
| | @@ -64980,11 +64984,11 @@ |
| 64980 | 64984 | p->pDelFrame = pDel->pParent; |
| 64981 | 64985 | sqlite3VdbeFrameDelete(pDel); |
| 64982 | 64986 | } |
| 64983 | 64987 | |
| 64984 | 64988 | /* Delete any auxdata allocations made by the VM */ |
| 64985 | | - sqlite3VdbeDeleteAuxData(p, -1, 0); |
| 64989 | + if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); |
| 64986 | 64990 | assert( p->pAuxData==0 ); |
| 64987 | 64991 | } |
| 64988 | 64992 | |
| 64989 | 64993 | /* |
| 64990 | 64994 | ** Clean up the VM after a single run. |
| | @@ -65886,13 +65890,11 @@ |
| 65886 | 65890 | #endif |
| 65887 | 65891 | assert( p->deferredMoveto ); |
| 65888 | 65892 | assert( p->isTable ); |
| 65889 | 65893 | rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); |
| 65890 | 65894 | if( rc ) return rc; |
| 65891 | | - p->lastRowid = p->movetoTarget; |
| 65892 | 65895 | if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
| 65893 | | - p->rowidIsValid = 1; |
| 65894 | 65896 | #ifdef SQLITE_TEST |
| 65895 | 65897 | sqlite3_search_count++; |
| 65896 | 65898 | #endif |
| 65897 | 65899 | p->deferredMoveto = 0; |
| 65898 | 65900 | p->cacheStatus = CACHE_STALE; |
| | @@ -65913,10 +65915,21 @@ |
| 65913 | 65915 | rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow); |
| 65914 | 65916 | p->cacheStatus = CACHE_STALE; |
| 65915 | 65917 | if( isDifferentRow ) p->nullRow = 1; |
| 65916 | 65918 | return rc; |
| 65917 | 65919 | } |
| 65920 | + |
| 65921 | +/* |
| 65922 | +** Check to ensure that the cursor is valid. Restore the cursor |
| 65923 | +** if need be. Return any I/O error from the restore operation. |
| 65924 | +*/ |
| 65925 | +SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor *p){ |
| 65926 | + if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 65927 | + return handleMovedCursor(p); |
| 65928 | + } |
| 65929 | + return SQLITE_OK; |
| 65930 | +} |
| 65918 | 65931 | |
| 65919 | 65932 | /* |
| 65920 | 65933 | ** Make sure the cursor p is ready to read or write the row to which it |
| 65921 | 65934 | ** was last positioned. Return an error code if an OOM fault or I/O error |
| 65922 | 65935 | ** prevents us from positioning the cursor to its correct position. |
| | @@ -65931,11 +65944,11 @@ |
| 65931 | 65944 | */ |
| 65932 | 65945 | SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
| 65933 | 65946 | if( p->deferredMoveto ){ |
| 65934 | 65947 | return handleDeferredMoveto(p); |
| 65935 | 65948 | } |
| 65936 | | - if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 65949 | + if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){ |
| 65937 | 65950 | return handleMovedCursor(p); |
| 65938 | 65951 | } |
| 65939 | 65952 | return SQLITE_OK; |
| 65940 | 65953 | } |
| 65941 | 65954 | |
| | @@ -69095,10 +69108,11 @@ |
| 69095 | 69108 | if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
| 69096 | 69109 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
| 69097 | 69110 | memset(pCx, 0, sizeof(VdbeCursor)); |
| 69098 | 69111 | pCx->iDb = iDb; |
| 69099 | 69112 | pCx->nField = nField; |
| 69113 | + pCx->aOffset = &pCx->aType[nField]; |
| 69100 | 69114 | if( isBtreeCursor ){ |
| 69101 | 69115 | pCx->pCursor = (BtCursor*) |
| 69102 | 69116 | &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; |
| 69103 | 69117 | sqlite3BtreeCursorZero(pCx->pCursor); |
| 69104 | 69118 | } |
| | @@ -70528,11 +70542,11 @@ |
| 70528 | 70542 | ctx.pFunc = pOp->p4.pFunc; |
| 70529 | 70543 | ctx.iOp = pc; |
| 70530 | 70544 | ctx.pVdbe = p; |
| 70531 | 70545 | MemSetTypeFlag(ctx.pOut, MEM_Null); |
| 70532 | 70546 | ctx.fErrorOrAux = 0; |
| 70533 | | - assert( db->lastRowid==lastRowid ); |
| 70547 | + db->lastRowid = lastRowid; |
| 70534 | 70548 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 70535 | 70549 | lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */ |
| 70536 | 70550 | |
| 70537 | 70551 | /* If the function returned an error, throw an exception */ |
| 70538 | 70552 | if( ctx.fErrorOrAux ){ |
| | @@ -71246,11 +71260,11 @@ |
| 71246 | 71260 | memAboutToChange(p, pDest); |
| 71247 | 71261 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 71248 | 71262 | pC = p->apCsr[pOp->p1]; |
| 71249 | 71263 | assert( pC!=0 ); |
| 71250 | 71264 | assert( p2<pC->nField ); |
| 71251 | | - aOffset = pC->aType + pC->nField; |
| 71265 | + aOffset = pC->aOffset; |
| 71252 | 71266 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 71253 | 71267 | assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */ |
| 71254 | 71268 | #endif |
| 71255 | 71269 | pCrsr = pC->pCursor; |
| 71256 | 71270 | assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */ |
| | @@ -71257,11 +71271,11 @@ |
| 71257 | 71271 | assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */ |
| 71258 | 71272 | |
| 71259 | 71273 | /* If the cursor cache is stale, bring it up-to-date */ |
| 71260 | 71274 | rc = sqlite3VdbeCursorMoveto(pC); |
| 71261 | 71275 | if( rc ) goto abort_due_to_error; |
| 71262 | | - if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){ |
| 71276 | + if( pC->cacheStatus!=p->cacheCtr ){ |
| 71263 | 71277 | if( pC->nullRow ){ |
| 71264 | 71278 | if( pCrsr==0 ){ |
| 71265 | 71279 | assert( pC->pseudoTableReg>0 ); |
| 71266 | 71280 | pReg = &aMem[pC->pseudoTableReg]; |
| 71267 | 71281 | assert( pReg->flags & MEM_Blob ); |
| | @@ -71302,18 +71316,10 @@ |
| 71302 | 71316 | } |
| 71303 | 71317 | pC->cacheStatus = p->cacheCtr; |
| 71304 | 71318 | pC->iHdrOffset = getVarint32(pC->aRow, offset); |
| 71305 | 71319 | pC->nHdrParsed = 0; |
| 71306 | 71320 | aOffset[0] = offset; |
| 71307 | | - if( avail<offset ){ |
| 71308 | | - /* pC->aRow does not have to hold the entire row, but it does at least |
| 71309 | | - ** need to cover the header of the record. If pC->aRow does not contain |
| 71310 | | - ** the complete header, then set it to zero, forcing the header to be |
| 71311 | | - ** dynamically allocated. */ |
| 71312 | | - pC->aRow = 0; |
| 71313 | | - pC->szRow = 0; |
| 71314 | | - } |
| 71315 | 71321 | |
| 71316 | 71322 | /* Make sure a corrupt database has not given us an oversize header. |
| 71317 | 71323 | ** Do this now to avoid an oversize memory allocation. |
| 71318 | 71324 | ** |
| 71319 | 71325 | ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte |
| | @@ -71324,19 +71330,36 @@ |
| 71324 | 71330 | */ |
| 71325 | 71331 | if( offset > 98307 || offset > pC->payloadSize ){ |
| 71326 | 71332 | rc = SQLITE_CORRUPT_BKPT; |
| 71327 | 71333 | goto op_column_error; |
| 71328 | 71334 | } |
| 71335 | + |
| 71336 | + if( avail<offset ){ |
| 71337 | + /* pC->aRow does not have to hold the entire row, but it does at least |
| 71338 | + ** need to cover the header of the record. If pC->aRow does not contain |
| 71339 | + ** the complete header, then set it to zero, forcing the header to be |
| 71340 | + ** dynamically allocated. */ |
| 71341 | + pC->aRow = 0; |
| 71342 | + pC->szRow = 0; |
| 71343 | + } |
| 71344 | + |
| 71345 | + /* The following goto is an optimization. It can be omitted and |
| 71346 | + ** everything will still work. But OP_Column is measurably faster |
| 71347 | + ** by skipping the subsequent conditional, which is always true. |
| 71348 | + */ |
| 71349 | + assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ |
| 71350 | + goto op_column_read_header; |
| 71329 | 71351 | } |
| 71330 | 71352 | |
| 71331 | 71353 | /* Make sure at least the first p2+1 entries of the header have been |
| 71332 | 71354 | ** parsed and valid information is in aOffset[] and pC->aType[]. |
| 71333 | 71355 | */ |
| 71334 | 71356 | if( pC->nHdrParsed<=p2 ){ |
| 71335 | 71357 | /* If there is more header available for parsing in the record, try |
| 71336 | 71358 | ** to extract additional fields up through the p2+1-th field |
| 71337 | 71359 | */ |
| 71360 | + op_column_read_header: |
| 71338 | 71361 | if( pC->iHdrOffset<aOffset[0] ){ |
| 71339 | 71362 | /* Make sure zData points to enough of the record to cover the header. */ |
| 71340 | 71363 | if( pC->aRow==0 ){ |
| 71341 | 71364 | memset(&sMem, 0, sizeof(sMem)); |
| 71342 | 71365 | rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], |
| | @@ -71377,19 +71400,20 @@ |
| 71377 | 71400 | if( pC->aRow==0 ){ |
| 71378 | 71401 | sqlite3VdbeMemRelease(&sMem); |
| 71379 | 71402 | sMem.flags = MEM_Null; |
| 71380 | 71403 | } |
| 71381 | 71404 | |
| 71382 | | - /* If we have read more header data than was contained in the header, |
| 71383 | | - ** or if the end of the last field appears to be past the end of the |
| 71384 | | - ** record, or if the end of the last field appears to be before the end |
| 71385 | | - ** of the record (when all fields present), then we must be dealing |
| 71386 | | - ** with a corrupt database. |
| 71405 | + /* The record is corrupt if any of the following are true: |
| 71406 | + ** (1) the bytes of the header extend past the declared header size |
| 71407 | + ** (zHdr>zEndHdr) |
| 71408 | + ** (2) the entire header was used but not all data was used |
| 71409 | + ** (zHdr==zEndHdr && offset!=pC->payloadSize) |
| 71410 | + ** (3) the end of the data extends beyond the end of the record. |
| 71411 | + ** (offset > pC->payloadSize) |
| 71387 | 71412 | */ |
| 71388 | | - if( (zHdr > zEndHdr) |
| 71413 | + if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset!=pC->payloadSize)) |
| 71389 | 71414 | || (offset > pC->payloadSize) |
| 71390 | | - || (zHdr==zEndHdr && offset!=pC->payloadSize) |
| 71391 | 71415 | ){ |
| 71392 | 71416 | rc = SQLITE_CORRUPT_BKPT; |
| 71393 | 71417 | goto op_column_error; |
| 71394 | 71418 | } |
| 71395 | 71419 | } |
| | @@ -71576,11 +71600,11 @@ |
| 71576 | 71600 | ** out how much space is required for the new record. |
| 71577 | 71601 | */ |
| 71578 | 71602 | pRec = pLast; |
| 71579 | 71603 | do{ |
| 71580 | 71604 | assert( memIsValid(pRec) ); |
| 71581 | | - serial_type = sqlite3VdbeSerialType(pRec, file_format); |
| 71605 | + pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format); |
| 71582 | 71606 | len = sqlite3VdbeSerialTypeLen(serial_type); |
| 71583 | 71607 | if( pRec->flags & MEM_Zero ){ |
| 71584 | 71608 | if( nData ){ |
| 71585 | 71609 | sqlite3VdbeMemExpandBlob(pRec); |
| 71586 | 71610 | }else{ |
| | @@ -71625,11 +71649,11 @@ |
| 71625 | 71649 | i = putVarint32(zNewRecord, nHdr); |
| 71626 | 71650 | j = nHdr; |
| 71627 | 71651 | assert( pData0<=pLast ); |
| 71628 | 71652 | pRec = pData0; |
| 71629 | 71653 | do{ |
| 71630 | | - serial_type = sqlite3VdbeSerialType(pRec, file_format); |
| 71654 | + serial_type = pRec->uTemp; |
| 71631 | 71655 | i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 71632 | 71656 | j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 71633 | 71657 | }while( (++pRec)<=pLast ); |
| 71634 | 71658 | assert( i==nHdr ); |
| 71635 | 71659 | assert( j==nByte ); |
| | @@ -72524,11 +72548,10 @@ |
| 72524 | 72548 | pIn3 = &aMem[pOp->p3]; |
| 72525 | 72549 | if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 72526 | 72550 | applyNumericAffinity(pIn3, 0); |
| 72527 | 72551 | } |
| 72528 | 72552 | iKey = sqlite3VdbeIntValue(pIn3); |
| 72529 | | - pC->rowidIsValid = 0; |
| 72530 | 72553 | |
| 72531 | 72554 | /* If the P3 value could not be converted into an integer without |
| 72532 | 72555 | ** loss of information, then special processing is required... */ |
| 72533 | 72556 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 72534 | 72557 | if( (pIn3->flags & MEM_Real)==0 ){ |
| | @@ -72560,17 +72583,14 @@ |
| 72560 | 72583 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 72561 | 72584 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
| 72562 | 72585 | } |
| 72563 | 72586 | } |
| 72564 | 72587 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); |
| 72588 | + pC->movetoTarget = iKey; /* Used by OP_Delete */ |
| 72565 | 72589 | if( rc!=SQLITE_OK ){ |
| 72566 | 72590 | goto abort_due_to_error; |
| 72567 | 72591 | } |
| 72568 | | - if( res==0 ){ |
| 72569 | | - pC->rowidIsValid = 1; |
| 72570 | | - pC->lastRowid = iKey; |
| 72571 | | - } |
| 72572 | 72592 | }else{ |
| 72573 | 72593 | nField = pOp->p4.i; |
| 72574 | 72594 | assert( pOp->p4type==P4_INT32 ); |
| 72575 | 72595 | assert( nField>0 ); |
| 72576 | 72596 | r.pKeyInfo = pC->pKeyInfo; |
| | @@ -72596,11 +72616,10 @@ |
| 72596 | 72616 | ExpandBlob(r.aMem); |
| 72597 | 72617 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); |
| 72598 | 72618 | if( rc!=SQLITE_OK ){ |
| 72599 | 72619 | goto abort_due_to_error; |
| 72600 | 72620 | } |
| 72601 | | - pC->rowidIsValid = 0; |
| 72602 | 72621 | } |
| 72603 | 72622 | pC->deferredMoveto = 0; |
| 72604 | 72623 | pC->cacheStatus = CACHE_STALE; |
| 72605 | 72624 | #ifdef SQLITE_TEST |
| 72606 | 72625 | sqlite3_search_count++; |
| | @@ -72608,21 +72627,19 @@ |
| 72608 | 72627 | if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); |
| 72609 | 72628 | if( res<0 || (res==0 && oc==OP_SeekGT) ){ |
| 72610 | 72629 | res = 0; |
| 72611 | 72630 | rc = sqlite3BtreeNext(pC->pCursor, &res); |
| 72612 | 72631 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 72613 | | - pC->rowidIsValid = 0; |
| 72614 | 72632 | }else{ |
| 72615 | 72633 | res = 0; |
| 72616 | 72634 | } |
| 72617 | 72635 | }else{ |
| 72618 | 72636 | assert( oc==OP_SeekLT || oc==OP_SeekLE ); |
| 72619 | 72637 | if( res>0 || (res==0 && oc==OP_SeekLT) ){ |
| 72620 | 72638 | res = 0; |
| 72621 | 72639 | rc = sqlite3BtreePrevious(pC->pCursor, &res); |
| 72622 | 72640 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 72623 | | - pC->rowidIsValid = 0; |
| 72624 | 72641 | }else{ |
| 72625 | 72642 | /* res might be negative because the table is empty. Check to |
| 72626 | 72643 | ** see if this is the case. |
| 72627 | 72644 | */ |
| 72628 | 72645 | res = sqlite3BtreeEof(pC->pCursor); |
| | @@ -72655,11 +72672,10 @@ |
| 72655 | 72672 | assert( pC->pCursor!=0 ); |
| 72656 | 72673 | assert( pC->isTable ); |
| 72657 | 72674 | pC->nullRow = 0; |
| 72658 | 72675 | pIn2 = &aMem[pOp->p2]; |
| 72659 | 72676 | pC->movetoTarget = sqlite3VdbeIntValue(pIn2); |
| 72660 | | - pC->rowidIsValid = 0; |
| 72661 | 72677 | pC->deferredMoveto = 1; |
| 72662 | 72678 | break; |
| 72663 | 72679 | } |
| 72664 | 72680 | |
| 72665 | 72681 | |
| | @@ -72841,19 +72857,17 @@ |
| 72841 | 72857 | pCrsr = pC->pCursor; |
| 72842 | 72858 | assert( pCrsr!=0 ); |
| 72843 | 72859 | res = 0; |
| 72844 | 72860 | iKey = pIn3->u.i; |
| 72845 | 72861 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); |
| 72846 | | - pC->lastRowid = pIn3->u.i; |
| 72847 | | - pC->rowidIsValid = res==0 ?1:0; |
| 72862 | + pC->movetoTarget = iKey; /* Used by OP_Delete */ |
| 72848 | 72863 | pC->nullRow = 0; |
| 72849 | 72864 | pC->cacheStatus = CACHE_STALE; |
| 72850 | 72865 | pC->deferredMoveto = 0; |
| 72851 | 72866 | VdbeBranchTaken(res!=0,2); |
| 72852 | 72867 | if( res!=0 ){ |
| 72853 | 72868 | pc = pOp->p2 - 1; |
| 72854 | | - assert( pC->rowidIsValid==0 ); |
| 72855 | 72869 | } |
| 72856 | 72870 | pC->seekResult = res; |
| 72857 | 72871 | break; |
| 72858 | 72872 | } |
| 72859 | 72873 | |
| | @@ -72997,11 +73011,10 @@ |
| 72997 | 73011 | rc = SQLITE_FULL; /* IMP: R-38219-53002 */ |
| 72998 | 73012 | goto abort_due_to_error; |
| 72999 | 73013 | } |
| 73000 | 73014 | assert( v>0 ); /* EV: R-40812-03570 */ |
| 73001 | 73015 | } |
| 73002 | | - pC->rowidIsValid = 0; |
| 73003 | 73016 | pC->deferredMoveto = 0; |
| 73004 | 73017 | pC->cacheStatus = CACHE_STALE; |
| 73005 | 73018 | } |
| 73006 | 73019 | pOut->u.i = v; |
| 73007 | 73020 | break; |
| | @@ -73102,11 +73115,10 @@ |
| 73102 | 73115 | } |
| 73103 | 73116 | rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, |
| 73104 | 73117 | pData->z, pData->n, nZero, |
| 73105 | 73118 | (pOp->p5 & OPFLAG_APPEND)!=0, seekResult |
| 73106 | 73119 | ); |
| 73107 | | - pC->rowidIsValid = 0; |
| 73108 | 73120 | pC->deferredMoveto = 0; |
| 73109 | 73121 | pC->cacheStatus = CACHE_STALE; |
| 73110 | 73122 | |
| 73111 | 73123 | /* Invoke the update-hook if required. */ |
| 73112 | 73124 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ |
| | @@ -73139,37 +73151,36 @@ |
| 73139 | 73151 | ** pointing to. The update hook will be invoked, if it exists. |
| 73140 | 73152 | ** If P4 is not NULL then the P1 cursor must have been positioned |
| 73141 | 73153 | ** using OP_NotFound prior to invoking this opcode. |
| 73142 | 73154 | */ |
| 73143 | 73155 | case OP_Delete: { |
| 73144 | | - i64 iKey; |
| 73145 | 73156 | VdbeCursor *pC; |
| 73146 | 73157 | |
| 73147 | 73158 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 73148 | 73159 | pC = p->apCsr[pOp->p1]; |
| 73149 | 73160 | assert( pC!=0 ); |
| 73150 | 73161 | assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ |
| 73151 | | - iKey = pC->lastRowid; /* Only used for the update hook */ |
| 73152 | | - |
| 73153 | | - /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or |
| 73154 | | - ** OP_Column on the same table without any intervening operations that |
| 73155 | | - ** might move or invalidate the cursor. Hence cursor pC is always pointing |
| 73156 | | - ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation |
| 73157 | | - ** below is always a no-op and cannot fail. We will run it anyhow, though, |
| 73158 | | - ** to guard against future changes to the code generator. |
| 73159 | | - **/ |
| 73160 | 73162 | assert( pC->deferredMoveto==0 ); |
| 73161 | | - rc = sqlite3VdbeCursorMoveto(pC); |
| 73162 | | - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 73163 | 73163 | |
| 73164 | +#ifdef SQLITE_DEBUG |
| 73165 | + /* The seek operation that positioned the cursor prior to OP_Delete will |
| 73166 | + ** have also set the pC->movetoTarget field to the rowid of the row that |
| 73167 | + ** is being deleted */ |
| 73168 | + if( pOp->p4.z && pC->isTable ){ |
| 73169 | + i64 iKey = 0; |
| 73170 | + sqlite3BtreeKeySize(pC->pCursor, &iKey); |
| 73171 | + assert( pC->movetoTarget==iKey ); |
| 73172 | + } |
| 73173 | +#endif |
| 73174 | + |
| 73164 | 73175 | rc = sqlite3BtreeDelete(pC->pCursor); |
| 73165 | 73176 | pC->cacheStatus = CACHE_STALE; |
| 73166 | 73177 | |
| 73167 | 73178 | /* Invoke the update-hook if required. */ |
| 73168 | 73179 | if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){ |
| 73169 | 73180 | db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, |
| 73170 | | - db->aDb[pC->iDb].zName, pOp->p4.z, iKey); |
| 73181 | + db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget); |
| 73171 | 73182 | assert( pC->iDb>=0 ); |
| 73172 | 73183 | } |
| 73173 | 73184 | if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; |
| 73174 | 73185 | break; |
| 73175 | 73186 | } |
| | @@ -73218,23 +73229,32 @@ |
| 73218 | 73229 | pc = pOp->p2-1; |
| 73219 | 73230 | } |
| 73220 | 73231 | break; |
| 73221 | 73232 | }; |
| 73222 | 73233 | |
| 73223 | | -/* Opcode: SorterData P1 P2 * * * |
| 73234 | +/* Opcode: SorterData P1 P2 P3 * * |
| 73224 | 73235 | ** Synopsis: r[P2]=data |
| 73225 | 73236 | ** |
| 73226 | 73237 | ** Write into register P2 the current sorter data for sorter cursor P1. |
| 73238 | +** Then clear the column header cache on cursor P3. |
| 73239 | +** |
| 73240 | +** This opcode is normally use to move a record out of the sorter and into |
| 73241 | +** a register that is the source for a pseudo-table cursor created using |
| 73242 | +** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 73243 | +** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 73244 | +** us from having to issue a separate NullRow instruction to clear that cache. |
| 73227 | 73245 | */ |
| 73228 | 73246 | case OP_SorterData: { |
| 73229 | 73247 | VdbeCursor *pC; |
| 73230 | 73248 | |
| 73231 | 73249 | pOut = &aMem[pOp->p2]; |
| 73232 | 73250 | pC = p->apCsr[pOp->p1]; |
| 73233 | 73251 | assert( isSorter(pC) ); |
| 73234 | 73252 | rc = sqlite3VdbeSorterRowkey(pC, pOut); |
| 73235 | 73253 | assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); |
| 73254 | + assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 73255 | + p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; |
| 73236 | 73256 | break; |
| 73237 | 73257 | } |
| 73238 | 73258 | |
| 73239 | 73259 | /* Opcode: RowData P1 P2 * * * |
| 73240 | 73260 | ** Synopsis: r[P2]=data |
| | @@ -73277,20 +73297,24 @@ |
| 73277 | 73297 | assert( pC!=0 ); |
| 73278 | 73298 | assert( pC->nullRow==0 ); |
| 73279 | 73299 | assert( pC->pseudoTableReg==0 ); |
| 73280 | 73300 | assert( pC->pCursor!=0 ); |
| 73281 | 73301 | pCrsr = pC->pCursor; |
| 73282 | | - assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 73283 | 73302 | |
| 73284 | 73303 | /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or |
| 73285 | 73304 | ** OP_Rewind/Op_Next with no intervening instructions that might invalidate |
| 73286 | | - ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always |
| 73287 | | - ** a no-op and can never fail. But we leave it in place as a safety. |
| 73305 | + ** the cursor. If this where not the case, on of the following assert()s |
| 73306 | + ** would fail. Should this ever change (because of changes in the code |
| 73307 | + ** generator) then the fix would be to insert a call to |
| 73308 | + ** sqlite3VdbeCursorMoveto(). |
| 73288 | 73309 | */ |
| 73289 | 73310 | assert( pC->deferredMoveto==0 ); |
| 73311 | + assert( sqlite3BtreeCursorIsValid(pCrsr) ); |
| 73312 | +#if 0 /* Not required due to the previous to assert() statements */ |
| 73290 | 73313 | rc = sqlite3VdbeCursorMoveto(pC); |
| 73291 | | - if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 73314 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 73315 | +#endif |
| 73292 | 73316 | |
| 73293 | 73317 | if( pC->isTable==0 ){ |
| 73294 | 73318 | assert( !pC->isTable ); |
| 73295 | 73319 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); |
| 73296 | 73320 | assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ |
| | @@ -73303,11 +73327,12 @@ |
| 73303 | 73327 | assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ |
| 73304 | 73328 | if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 73305 | 73329 | goto too_big; |
| 73306 | 73330 | } |
| 73307 | 73331 | } |
| 73308 | | - if( sqlite3VdbeMemClearAndResize(pOut, n) ){ |
| 73332 | + testcase( n==0 ); |
| 73333 | + if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ |
| 73309 | 73334 | goto no_mem; |
| 73310 | 73335 | } |
| 73311 | 73336 | pOut->n = n; |
| 73312 | 73337 | MemSetTypeFlag(pOut, MEM_Blob); |
| 73313 | 73338 | if( pC->isTable==0 ){ |
| | @@ -73354,18 +73379,14 @@ |
| 73354 | 73379 | rc = pModule->xRowid(pC->pVtabCursor, &v); |
| 73355 | 73380 | sqlite3VtabImportErrmsg(p, pVtab); |
| 73356 | 73381 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 73357 | 73382 | }else{ |
| 73358 | 73383 | assert( pC->pCursor!=0 ); |
| 73359 | | - rc = sqlite3VdbeCursorMoveto(pC); |
| 73384 | + rc = sqlite3VdbeCursorRestore(pC); |
| 73360 | 73385 | if( rc ) goto abort_due_to_error; |
| 73361 | | - if( pC->rowidIsValid ){ |
| 73362 | | - v = pC->lastRowid; |
| 73363 | | - }else{ |
| 73364 | | - rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 73365 | | - assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */ |
| 73366 | | - } |
| 73386 | + rc = sqlite3BtreeKeySize(pC->pCursor, &v); |
| 73387 | + assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ |
| 73367 | 73388 | } |
| 73368 | 73389 | pOut->u.i = v; |
| 73369 | 73390 | break; |
| 73370 | 73391 | } |
| 73371 | 73392 | |
| | @@ -73380,11 +73401,10 @@ |
| 73380 | 73401 | |
| 73381 | 73402 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 73382 | 73403 | pC = p->apCsr[pOp->p1]; |
| 73383 | 73404 | assert( pC!=0 ); |
| 73384 | 73405 | pC->nullRow = 1; |
| 73385 | | - pC->rowidIsValid = 0; |
| 73386 | 73406 | pC->cacheStatus = CACHE_STALE; |
| 73387 | 73407 | if( pC->pCursor ){ |
| 73388 | 73408 | sqlite3BtreeClearCursor(pC->pCursor); |
| 73389 | 73409 | } |
| 73390 | 73410 | break; |
| | @@ -73414,11 +73434,10 @@ |
| 73414 | 73434 | res = 0; |
| 73415 | 73435 | assert( pCrsr!=0 ); |
| 73416 | 73436 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 73417 | 73437 | pC->nullRow = (u8)res; |
| 73418 | 73438 | pC->deferredMoveto = 0; |
| 73419 | | - pC->rowidIsValid = 0; |
| 73420 | 73439 | pC->cacheStatus = CACHE_STALE; |
| 73421 | 73440 | #ifdef SQLITE_DEBUG |
| 73422 | 73441 | pC->seekOp = OP_Last; |
| 73423 | 73442 | #endif |
| 73424 | 73443 | if( pOp->p2>0 ){ |
| | @@ -73481,11 +73500,10 @@ |
| 73481 | 73500 | pCrsr = pC->pCursor; |
| 73482 | 73501 | assert( pCrsr ); |
| 73483 | 73502 | rc = sqlite3BtreeFirst(pCrsr, &res); |
| 73484 | 73503 | pC->deferredMoveto = 0; |
| 73485 | 73504 | pC->cacheStatus = CACHE_STALE; |
| 73486 | | - pC->rowidIsValid = 0; |
| 73487 | 73505 | } |
| 73488 | 73506 | pC->nullRow = (u8)res; |
| 73489 | 73507 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 73490 | 73508 | VdbeBranchTaken(res!=0,2); |
| 73491 | 73509 | if( res ){ |
| | @@ -73607,11 +73625,10 @@ |
| 73607 | 73625 | sqlite3_search_count++; |
| 73608 | 73626 | #endif |
| 73609 | 73627 | }else{ |
| 73610 | 73628 | pC->nullRow = 1; |
| 73611 | 73629 | } |
| 73612 | | - pC->rowidIsValid = 0; |
| 73613 | 73630 | goto check_for_interrupt; |
| 73614 | 73631 | } |
| 73615 | 73632 | |
| 73616 | 73633 | /* Opcode: IdxInsert P1 P2 P3 * P5 |
| 73617 | 73634 | ** Synopsis: key=r[P2] |
| | @@ -73723,14 +73740,20 @@ |
| 73723 | 73740 | pC = p->apCsr[pOp->p1]; |
| 73724 | 73741 | assert( pC!=0 ); |
| 73725 | 73742 | pCrsr = pC->pCursor; |
| 73726 | 73743 | assert( pCrsr!=0 ); |
| 73727 | 73744 | pOut->flags = MEM_Null; |
| 73728 | | - rc = sqlite3VdbeCursorMoveto(pC); |
| 73729 | | - if( NEVER(rc) ) goto abort_due_to_error; |
| 73745 | + assert( pC->isTable==0 ); |
| 73730 | 73746 | assert( pC->deferredMoveto==0 ); |
| 73731 | | - assert( pC->isTable==0 ); |
| 73747 | + |
| 73748 | + /* sqlite3VbeCursorRestore() can only fail if the record has been deleted |
| 73749 | + ** out from under the cursor. That will never happend for an IdxRowid |
| 73750 | + ** opcode, hence the NEVER() arround the check of the return value. |
| 73751 | + */ |
| 73752 | + rc = sqlite3VdbeCursorRestore(pC); |
| 73753 | + if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; |
| 73754 | + |
| 73732 | 73755 | if( !pC->nullRow ){ |
| 73733 | 73756 | rowid = 0; /* Not needed. Only used to silence a warning. */ |
| 73734 | 73757 | rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid); |
| 73735 | 73758 | if( rc!=SQLITE_OK ){ |
| 73736 | 73759 | goto abort_due_to_error; |
| | @@ -87168,29 +87191,27 @@ |
| 87168 | 87191 | tRowcnt v; |
| 87169 | 87192 | |
| 87170 | 87193 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87171 | 87194 | if( z==0 ) z = ""; |
| 87172 | 87195 | #else |
| 87173 | | - if( NEVER(z==0) ) z = ""; |
| 87196 | + assert( z!=0 ); |
| 87174 | 87197 | #endif |
| 87175 | 87198 | for(i=0; *z && i<nOut; i++){ |
| 87176 | 87199 | v = 0; |
| 87177 | 87200 | while( (c=z[0])>='0' && c<='9' ){ |
| 87178 | 87201 | v = v*10 + c - '0'; |
| 87179 | 87202 | z++; |
| 87180 | 87203 | } |
| 87181 | 87204 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87182 | | - if( aOut ){ |
| 87183 | | - aOut[i] = v; |
| 87184 | | - }else |
| 87205 | + if( aOut ) aOut[i] = v; |
| 87206 | + if( aLog ) aLog[i] = sqlite3LogEst(v); |
| 87185 | 87207 | #else |
| 87186 | 87208 | assert( aOut==0 ); |
| 87187 | 87209 | UNUSED_PARAMETER(aOut); |
| 87210 | + assert( aLog!=0 ); |
| 87211 | + aLog[i] = sqlite3LogEst(v); |
| 87188 | 87212 | #endif |
| 87189 | | - { |
| 87190 | | - aLog[i] = sqlite3LogEst(v); |
| 87191 | | - } |
| 87192 | 87213 | if( *z==' ' ) z++; |
| 87193 | 87214 | } |
| 87194 | 87215 | #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87195 | 87216 | assert( pIndex!=0 ); |
| 87196 | 87217 | #else |
| | @@ -87247,12 +87268,21 @@ |
| 87247 | 87268 | pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 87248 | 87269 | } |
| 87249 | 87270 | z = argv[2]; |
| 87250 | 87271 | |
| 87251 | 87272 | if( pIndex ){ |
| 87273 | + int nCol = pIndex->nKeyCol+1; |
| 87274 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 87275 | + tRowcnt * const aiRowEst = pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero( |
| 87276 | + sizeof(tRowcnt) * nCol |
| 87277 | + ); |
| 87278 | + if( aiRowEst==0 ) pInfo->db->mallocFailed = 1; |
| 87279 | +#else |
| 87280 | + tRowcnt * const aiRowEst = 0; |
| 87281 | +#endif |
| 87252 | 87282 | pIndex->bUnordered = 0; |
| 87253 | | - decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex); |
| 87283 | + decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); |
| 87254 | 87284 | if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; |
| 87255 | 87285 | }else{ |
| 87256 | 87286 | Index fakeIdx; |
| 87257 | 87287 | fakeIdx.szIdxRow = pTable->szTabRow; |
| 87258 | 87288 | #ifdef SQLITE_ENABLE_COSTMULT |
| | @@ -87307,29 +87337,42 @@ |
| 87307 | 87337 | ** unique. */ |
| 87308 | 87338 | nCol = pIdx->nSampleCol-1; |
| 87309 | 87339 | pIdx->aAvgEq[nCol] = 1; |
| 87310 | 87340 | } |
| 87311 | 87341 | for(iCol=0; iCol<nCol; iCol++){ |
| 87342 | + int nSample = pIdx->nSample; |
| 87312 | 87343 | int i; /* Used to iterate through samples */ |
| 87313 | 87344 | tRowcnt sumEq = 0; /* Sum of the nEq values */ |
| 87314 | | - tRowcnt nSum = 0; /* Number of terms contributing to sumEq */ |
| 87315 | 87345 | tRowcnt avgEq = 0; |
| 87316 | | - tRowcnt nDLt = pFinal->anDLt[iCol]; |
| 87346 | + tRowcnt nRow; /* Number of rows in index */ |
| 87347 | + i64 nSum100 = 0; /* Number of terms contributing to sumEq */ |
| 87348 | + i64 nDist100; /* Number of distinct values in index */ |
| 87349 | + |
| 87350 | + if( pIdx->aiRowEst==0 || pIdx->aiRowEst[iCol+1]==0 ){ |
| 87351 | + nRow = pFinal->anLt[iCol]; |
| 87352 | + nDist100 = (i64)100 * pFinal->anDLt[iCol]; |
| 87353 | + nSample--; |
| 87354 | + }else{ |
| 87355 | + nRow = pIdx->aiRowEst[0]; |
| 87356 | + nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1]; |
| 87357 | + } |
| 87317 | 87358 | |
| 87318 | 87359 | /* Set nSum to the number of distinct (iCol+1) field prefixes that |
| 87319 | | - ** occur in the stat4 table for this index before pFinal. Set |
| 87320 | | - ** sumEq to the sum of the nEq values for column iCol for the same |
| 87321 | | - ** set (adding the value only once where there exist duplicate |
| 87322 | | - ** prefixes). */ |
| 87323 | | - for(i=0; i<(pIdx->nSample-1); i++){ |
| 87324 | | - if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){ |
| 87360 | + ** occur in the stat4 table for this index. Set sumEq to the sum of |
| 87361 | + ** the nEq values for column iCol for the same set (adding the value |
| 87362 | + ** only once where there exist duplicate prefixes). */ |
| 87363 | + for(i=0; i<nSample; i++){ |
| 87364 | + if( i==(pIdx->nSample-1) |
| 87365 | + || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] |
| 87366 | + ){ |
| 87325 | 87367 | sumEq += aSample[i].anEq[iCol]; |
| 87326 | | - nSum++; |
| 87368 | + nSum100 += 100; |
| 87327 | 87369 | } |
| 87328 | 87370 | } |
| 87329 | | - if( nDLt>nSum ){ |
| 87330 | | - avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum); |
| 87371 | + |
| 87372 | + if( nDist100>nSum100 ){ |
| 87373 | + avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100); |
| 87331 | 87374 | } |
| 87332 | 87375 | if( avgEq==0 ) avgEq = 1; |
| 87333 | 87376 | pIdx->aAvgEq[iCol] = avgEq; |
| 87334 | 87377 | } |
| 87335 | 87378 | } |
| | @@ -87576,10 +87619,15 @@ |
| 87576 | 87619 | if( rc==SQLITE_OK ){ |
| 87577 | 87620 | int lookasideEnabled = db->lookaside.bEnabled; |
| 87578 | 87621 | db->lookaside.bEnabled = 0; |
| 87579 | 87622 | rc = loadStat4(db, sInfo.zDatabase); |
| 87580 | 87623 | db->lookaside.bEnabled = lookasideEnabled; |
| 87624 | + } |
| 87625 | + for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ |
| 87626 | + Index *pIdx = sqliteHashData(i); |
| 87627 | + sqlite3_free(pIdx->aiRowEst); |
| 87628 | + pIdx->aiRowEst = 0; |
| 87581 | 87629 | } |
| 87582 | 87630 | #endif |
| 87583 | 87631 | |
| 87584 | 87632 | if( rc==SQLITE_NOMEM ){ |
| 87585 | 87633 | db->mallocFailed = 1; |
| | @@ -88870,10 +88918,13 @@ |
| 88870 | 88918 | #endif |
| 88871 | 88919 | if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo); |
| 88872 | 88920 | sqlite3ExprDelete(db, p->pPartIdxWhere); |
| 88873 | 88921 | sqlite3DbFree(db, p->zColAff); |
| 88874 | 88922 | if( p->isResized ) sqlite3DbFree(db, p->azColl); |
| 88923 | +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 88924 | + sqlite3_free(p->aiRowEst); |
| 88925 | +#endif |
| 88875 | 88926 | sqlite3DbFree(db, p); |
| 88876 | 88927 | } |
| 88877 | 88928 | |
| 88878 | 88929 | /* |
| 88879 | 88930 | ** For the index called zIdxName which is found in the database iDb, |
| | @@ -91179,11 +91230,11 @@ |
| 91179 | 91230 | pIndex->nKeyCol); VdbeCoverage(v); |
| 91180 | 91231 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
| 91181 | 91232 | }else{ |
| 91182 | 91233 | addr2 = sqlite3VdbeCurrentAddr(v); |
| 91183 | 91234 | } |
| 91184 | | - sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord); |
| 91235 | + sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
| 91185 | 91236 | sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); |
| 91186 | 91237 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 91187 | 91238 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 91188 | 91239 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
| 91189 | 91240 | sqlite3VdbeJumpHere(v, addr1); |
| | @@ -105122,11 +105173,10 @@ |
| 105122 | 105173 | int regRow; |
| 105123 | 105174 | int regRowid; |
| 105124 | 105175 | int nKey; |
| 105125 | 105176 | int iSortTab; /* Sorter cursor to read from */ |
| 105126 | 105177 | int nSortData; /* Trailing values to read from sorter */ |
| 105127 | | - u8 p5; /* p5 parameter for 1st OP_Column */ |
| 105128 | 105178 | int i; |
| 105129 | 105179 | int bSeq; /* True if sorter record includes seq. no. */ |
| 105130 | 105180 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 105131 | 105181 | struct ExprList_item *aOutEx = p->pEList->a; |
| 105132 | 105182 | #endif |
| | @@ -105156,23 +105206,20 @@ |
| 105156 | 105206 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, nKey+1+nSortData); |
| 105157 | 105207 | if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); |
| 105158 | 105208 | addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); |
| 105159 | 105209 | VdbeCoverage(v); |
| 105160 | 105210 | codeOffset(v, p->iOffset, addrContinue); |
| 105161 | | - sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut); |
| 105162 | | - p5 = OPFLAG_CLEARCACHE; |
| 105211 | + sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); |
| 105163 | 105212 | bSeq = 0; |
| 105164 | 105213 | }else{ |
| 105165 | 105214 | addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); |
| 105166 | 105215 | codeOffset(v, p->iOffset, addrContinue); |
| 105167 | 105216 | iSortTab = iTab; |
| 105168 | | - p5 = 0; |
| 105169 | 105217 | bSeq = 1; |
| 105170 | 105218 | } |
| 105171 | 105219 | for(i=0; i<nSortData; i++){ |
| 105172 | 105220 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq+i, regRow+i); |
| 105173 | | - if( i==0 ) sqlite3VdbeChangeP5(v, p5); |
| 105174 | 105221 | VdbeComment((v, "%s", aOutEx[i].zName ? aOutEx[i].zName : aOutEx[i].zSpan)); |
| 105175 | 105222 | } |
| 105176 | 105223 | switch( eDest ){ |
| 105177 | 105224 | case SRT_Table: |
| 105178 | 105225 | case SRT_EphemTab: { |
| | @@ -109097,16 +109144,15 @@ |
| 109097 | 109144 | ** from the previous row currently stored in a0, a1, a2... |
| 109098 | 109145 | */ |
| 109099 | 109146 | addrTopOfLoop = sqlite3VdbeCurrentAddr(v); |
| 109100 | 109147 | sqlite3ExprCacheClear(pParse); |
| 109101 | 109148 | if( groupBySort ){ |
| 109102 | | - sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut); |
| 109149 | + sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab); |
| 109103 | 109150 | } |
| 109104 | 109151 | for(j=0; j<pGroupBy->nExpr; j++){ |
| 109105 | 109152 | if( groupBySort ){ |
| 109106 | 109153 | sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); |
| 109107 | | - if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE); |
| 109108 | 109154 | }else{ |
| 109109 | 109155 | sAggInfo.directMode = 1; |
| 109110 | 109156 | sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); |
| 109111 | 109157 | } |
| 109112 | 109158 | } |
| | @@ -115700,21 +115746,28 @@ |
| 115700 | 115746 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 115701 | 115747 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115702 | 115748 | iLower = a[0]; |
| 115703 | 115749 | iUpper = a[0] + a[1]; |
| 115704 | 115750 | } |
| 115751 | + |
| 115752 | + assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 115753 | + assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
| 115754 | + assert( p->aSortOrder!=0 ); |
| 115755 | + if( p->aSortOrder[nEq] ){ |
| 115756 | + /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 115757 | + SWAP(WhereTerm*, pLower, pUpper); |
| 115758 | + } |
| 115705 | 115759 | |
| 115706 | 115760 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 115707 | 115761 | if( pLower ){ |
| 115708 | 115762 | int bOk; /* True if value is extracted from pExpr */ |
| 115709 | 115763 | Expr *pExpr = pLower->pExpr->pRight; |
| 115710 | | - assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 115711 | 115764 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115712 | 115765 | if( rc==SQLITE_OK && bOk ){ |
| 115713 | 115766 | tRowcnt iNew; |
| 115714 | 115767 | whereKeyStats(pParse, p, pRec, 0, a); |
| 115715 | | - iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0); |
| 115768 | + iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115716 | 115769 | if( iNew>iLower ) iLower = iNew; |
| 115717 | 115770 | nOut--; |
| 115718 | 115771 | pLower = 0; |
| 115719 | 115772 | } |
| 115720 | 115773 | } |
| | @@ -115721,16 +115774,15 @@ |
| 115721 | 115774 | |
| 115722 | 115775 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 115723 | 115776 | if( pUpper ){ |
| 115724 | 115777 | int bOk; /* True if value is extracted from pExpr */ |
| 115725 | 115778 | Expr *pExpr = pUpper->pExpr->pRight; |
| 115726 | | - assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
| 115727 | 115779 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 115728 | 115780 | if( rc==SQLITE_OK && bOk ){ |
| 115729 | 115781 | tRowcnt iNew; |
| 115730 | 115782 | whereKeyStats(pParse, p, pRec, 1, a); |
| 115731 | | - iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0); |
| 115783 | + iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
| 115732 | 115784 | if( iNew<iUpper ) iUpper = iNew; |
| 115733 | 115785 | nOut--; |
| 115734 | 115786 | pUpper = 0; |
| 115735 | 115787 | } |
| 115736 | 115788 | } |
| | @@ -116225,65 +116277,52 @@ |
| 116225 | 116277 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 116226 | 116278 | } |
| 116227 | 116279 | |
| 116228 | 116280 | /* |
| 116229 | 116281 | ** Argument pLevel describes a strategy for scanning table pTab. This |
| 116230 | | -** function returns a pointer to a string buffer containing a description |
| 116231 | | -** of the subset of table rows scanned by the strategy in the form of an |
| 116232 | | -** SQL expression. Or, if all rows are scanned, NULL is returned. |
| 116282 | +** function appends text to pStr that describes the subset of table |
| 116283 | +** rows scanned by the strategy in the form of an SQL expression. |
| 116233 | 116284 | ** |
| 116234 | 116285 | ** For example, if the query: |
| 116235 | 116286 | ** |
| 116236 | 116287 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 116237 | 116288 | ** |
| 116238 | 116289 | ** is run and there is an index on (a, b), then this function returns a |
| 116239 | 116290 | ** string similar to: |
| 116240 | 116291 | ** |
| 116241 | 116292 | ** "a=? AND b>?" |
| 116242 | | -** |
| 116243 | | -** The returned pointer points to memory obtained from sqlite3DbMalloc(). |
| 116244 | | -** It is the responsibility of the caller to free the buffer when it is |
| 116245 | | -** no longer required. |
| 116246 | 116293 | */ |
| 116247 | | -static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){ |
| 116294 | +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
| 116248 | 116295 | Index *pIndex = pLoop->u.btree.pIndex; |
| 116249 | 116296 | u16 nEq = pLoop->u.btree.nEq; |
| 116250 | 116297 | u16 nSkip = pLoop->u.btree.nSkip; |
| 116251 | 116298 | int i, j; |
| 116252 | 116299 | Column *aCol = pTab->aCol; |
| 116253 | 116300 | i16 *aiColumn = pIndex->aiColumn; |
| 116254 | | - StrAccum txt; |
| 116255 | | - |
| 116256 | | - if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ |
| 116257 | | - return 0; |
| 116258 | | - } |
| 116259 | | - sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); |
| 116260 | | - txt.db = db; |
| 116261 | | - sqlite3StrAccumAppend(&txt, " (", 2); |
| 116301 | + |
| 116302 | + if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 116303 | + sqlite3StrAccumAppend(pStr, " (", 2); |
| 116262 | 116304 | for(i=0; i<nEq; i++){ |
| 116263 | 116305 | char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName; |
| 116264 | 116306 | if( i>=nSkip ){ |
| 116265 | | - explainAppendTerm(&txt, i, z, "="); |
| 116307 | + explainAppendTerm(pStr, i, z, "="); |
| 116266 | 116308 | }else{ |
| 116267 | | - if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5); |
| 116268 | | - sqlite3StrAccumAppend(&txt, "ANY(", 4); |
| 116269 | | - sqlite3StrAccumAppendAll(&txt, z); |
| 116270 | | - sqlite3StrAccumAppend(&txt, ")", 1); |
| 116309 | + if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 116310 | + sqlite3XPrintf(pStr, 0, "ANY(%s)", z); |
| 116271 | 116311 | } |
| 116272 | 116312 | } |
| 116273 | 116313 | |
| 116274 | 116314 | j = i; |
| 116275 | 116315 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
| 116276 | 116316 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 116277 | | - explainAppendTerm(&txt, i++, z, ">"); |
| 116317 | + explainAppendTerm(pStr, i++, z, ">"); |
| 116278 | 116318 | } |
| 116279 | 116319 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
| 116280 | 116320 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
| 116281 | | - explainAppendTerm(&txt, i, z, "<"); |
| 116321 | + explainAppendTerm(pStr, i, z, "<"); |
| 116282 | 116322 | } |
| 116283 | | - sqlite3StrAccumAppend(&txt, ")", 1); |
| 116284 | | - return sqlite3StrAccumFinish(&txt); |
| 116323 | + sqlite3StrAccumAppend(pStr, ")", 1); |
| 116285 | 116324 | } |
| 116286 | 116325 | |
| 116287 | 116326 | /* |
| 116288 | 116327 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 116289 | 116328 | ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single |
| | @@ -116303,72 +116342,90 @@ |
| 116303 | 116342 | #endif |
| 116304 | 116343 | { |
| 116305 | 116344 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
| 116306 | 116345 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 116307 | 116346 | sqlite3 *db = pParse->db; /* Database handle */ |
| 116308 | | - char *zMsg; /* Text to add to EQP output */ |
| 116309 | 116347 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
| 116310 | 116348 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 116311 | 116349 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 116312 | 116350 | u32 flags; /* Flags that describe this loop */ |
| 116351 | + char *zMsg; /* Text to add to EQP output */ |
| 116352 | + StrAccum str; /* EQP output string */ |
| 116353 | + char zBuf[100]; /* Initial space for EQP output string */ |
| 116313 | 116354 | |
| 116314 | 116355 | pLoop = pLevel->pWLoop; |
| 116315 | 116356 | flags = pLoop->wsFlags; |
| 116316 | 116357 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; |
| 116317 | 116358 | |
| 116318 | 116359 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 116319 | 116360 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 116320 | 116361 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 116321 | 116362 | |
| 116322 | | - zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); |
| 116363 | + sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 116364 | + str.db = db; |
| 116365 | + sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 116323 | 116366 | if( pItem->pSelect ){ |
| 116324 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); |
| 116367 | + sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
| 116325 | 116368 | }else{ |
| 116326 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); |
| 116369 | + sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
| 116327 | 116370 | } |
| 116328 | 116371 | |
| 116329 | 116372 | if( pItem->zAlias ){ |
| 116330 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); |
| 116331 | | - } |
| 116332 | | - if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 |
| 116333 | | - && ALWAYS(pLoop->u.btree.pIndex!=0) |
| 116334 | | - ){ |
| 116335 | | - const char *zFmt; |
| 116336 | | - Index *pIdx = pLoop->u.btree.pIndex; |
| 116337 | | - char *zWhere = explainIndexRange(db, pLoop, pItem->pTab); |
| 116373 | + sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); |
| 116374 | + } |
| 116375 | + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 116376 | + const char *zFmt = 0; |
| 116377 | + Index *pIdx; |
| 116378 | + |
| 116379 | + assert( pLoop->u.btree.pIndex!=0 ); |
| 116380 | + pIdx = pLoop->u.btree.pIndex; |
| 116338 | 116381 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
| 116339 | 116382 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
| 116340 | | - zFmt = zWhere ? "%s USING PRIMARY KEY%.0s%s" : "%s%.0s%s"; |
| 116383 | + if( isSearch ){ |
| 116384 | + zFmt = "PRIMARY KEY"; |
| 116385 | + } |
| 116341 | 116386 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 116342 | | - zFmt = "%s USING AUTOMATIC COVERING INDEX%.0s%s"; |
| 116387 | + zFmt = "AUTOMATIC COVERING INDEX"; |
| 116343 | 116388 | }else if( flags & WHERE_IDX_ONLY ){ |
| 116344 | | - zFmt = "%s USING COVERING INDEX %s%s"; |
| 116389 | + zFmt = "COVERING INDEX %s"; |
| 116345 | 116390 | }else{ |
| 116346 | | - zFmt = "%s USING INDEX %s%s"; |
| 116391 | + zFmt = "INDEX %s"; |
| 116347 | 116392 | } |
| 116348 | | - zMsg = sqlite3MAppendf(db, zMsg, zFmt, zMsg, pIdx->zName, zWhere); |
| 116349 | | - sqlite3DbFree(db, zWhere); |
| 116393 | + if( zFmt ){ |
| 116394 | + sqlite3StrAccumAppend(&str, " USING ", 7); |
| 116395 | + sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); |
| 116396 | + explainIndexRange(&str, pLoop, pItem->pTab); |
| 116397 | + } |
| 116350 | 116398 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
| 116351 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); |
| 116352 | | - |
| 116399 | + const char *zRange; |
| 116353 | 116400 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
| 116354 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); |
| 116401 | + zRange = "(rowid=?)"; |
| 116355 | 116402 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
| 116356 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg); |
| 116403 | + zRange = "(rowid>? AND rowid<?)"; |
| 116357 | 116404 | }else if( flags&WHERE_BTM_LIMIT ){ |
| 116358 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg); |
| 116359 | | - }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){ |
| 116360 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg); |
| 116405 | + zRange = "(rowid>?)"; |
| 116406 | + }else{ |
| 116407 | + assert( flags&WHERE_TOP_LIMIT); |
| 116408 | + zRange = "(rowid<?)"; |
| 116361 | 116409 | } |
| 116410 | + sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY "); |
| 116411 | + sqlite3StrAccumAppendAll(&str, zRange); |
| 116362 | 116412 | } |
| 116363 | 116413 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 116364 | 116414 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
| 116365 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, |
| 116415 | + sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s", |
| 116366 | 116416 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 116367 | 116417 | } |
| 116368 | 116418 | #endif |
| 116369 | | - zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg); |
| 116419 | +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 116420 | + if( pLoop->nOut>=10 ){ |
| 116421 | + sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 116422 | + }else{ |
| 116423 | + sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
| 116424 | + } |
| 116425 | +#endif |
| 116426 | + zMsg = sqlite3StrAccumFinish(&str); |
| 116370 | 116427 | sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); |
| 116371 | 116428 | } |
| 116372 | 116429 | } |
| 116373 | 116430 | #else |
| 116374 | 116431 | # define explainOneScan(u,v,w,x,y,z) |
| | @@ -117633,11 +117690,11 @@ |
| 117633 | 117690 | if( ppPrev==0 ){ |
| 117634 | 117691 | /* There already exists a WhereLoop on the list that is better |
| 117635 | 117692 | ** than pTemplate, so just ignore pTemplate */ |
| 117636 | 117693 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 117637 | 117694 | if( sqlite3WhereTrace & 0x8 ){ |
| 117638 | | - sqlite3DebugPrintf("ins-noop: "); |
| 117695 | + sqlite3DebugPrintf(" skip: "); |
| 117639 | 117696 | whereLoopPrint(pTemplate, pBuilder->pWC); |
| 117640 | 117697 | } |
| 117641 | 117698 | #endif |
| 117642 | 117699 | return SQLITE_OK; |
| 117643 | 117700 | }else{ |
| | @@ -117649,14 +117706,14 @@ |
| 117649 | 117706 | ** WhereLoop and insert it. |
| 117650 | 117707 | */ |
| 117651 | 117708 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 117652 | 117709 | if( sqlite3WhereTrace & 0x8 ){ |
| 117653 | 117710 | if( p!=0 ){ |
| 117654 | | - sqlite3DebugPrintf("ins-del: "); |
| 117711 | + sqlite3DebugPrintf("replace: "); |
| 117655 | 117712 | whereLoopPrint(p, pBuilder->pWC); |
| 117656 | 117713 | } |
| 117657 | | - sqlite3DebugPrintf("ins-new: "); |
| 117714 | + sqlite3DebugPrintf(" add: "); |
| 117658 | 117715 | whereLoopPrint(pTemplate, pBuilder->pWC); |
| 117659 | 117716 | } |
| 117660 | 117717 | #endif |
| 117661 | 117718 | if( p==0 ){ |
| 117662 | 117719 | /* Allocate a new WhereLoop to add to the end of the list */ |
| | @@ -117676,11 +117733,11 @@ |
| 117676 | 117733 | pToDel = *ppTail; |
| 117677 | 117734 | if( pToDel==0 ) break; |
| 117678 | 117735 | *ppTail = pToDel->pNextLoop; |
| 117679 | 117736 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 117680 | 117737 | if( sqlite3WhereTrace & 0x8 ){ |
| 117681 | | - sqlite3DebugPrintf("ins-del: "); |
| 117738 | + sqlite3DebugPrintf(" delete: "); |
| 117682 | 117739 | whereLoopPrint(pToDel, pBuilder->pWC); |
| 117683 | 117740 | } |
| 117684 | 117741 | #endif |
| 117685 | 117742 | whereLoopDelete(db, pToDel); |
| 117686 | 117743 | } |
| | @@ -118843,11 +118900,11 @@ |
| 118843 | 118900 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 118844 | 118901 | } |
| 118845 | 118902 | isMatch = 1; |
| 118846 | 118903 | break; |
| 118847 | 118904 | } |
| 118848 | | - if( isMatch && (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 118905 | + if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ |
| 118849 | 118906 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 118850 | 118907 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 118851 | 118908 | if( revSet ){ |
| 118852 | 118909 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 118853 | 118910 | }else{ |
| | @@ -119308,16 +119365,19 @@ |
| 119308 | 119365 | pWInfo->revMask = pFrom->revLoop; |
| 119309 | 119366 | } |
| 119310 | 119367 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 119311 | 119368 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr |
| 119312 | 119369 | ){ |
| 119313 | | - Bitmask notUsed = 0; |
| 119370 | + Bitmask revMask = 0; |
| 119314 | 119371 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
| 119315 | | - pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used |
| 119372 | + pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
| 119316 | 119373 | ); |
| 119317 | 119374 | assert( pWInfo->sorted==0 ); |
| 119318 | | - pWInfo->sorted = (nOrder==pWInfo->pOrderBy->nExpr); |
| 119375 | + if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| 119376 | + pWInfo->sorted = 1; |
| 119377 | + pWInfo->revMask = revMask; |
| 119378 | + } |
| 119319 | 119379 | } |
| 119320 | 119380 | } |
| 119321 | 119381 | |
| 119322 | 119382 | |
| 119323 | 119383 | pWInfo->nRowOut = pFrom->nRow; |
| | @@ -132620,10 +132680,11 @@ |
| 132620 | 132680 | assert( iIdx==nVal ); |
| 132621 | 132681 | |
| 132622 | 132682 | /* In case the cursor has been used before, clear it now. */ |
| 132623 | 132683 | sqlite3_finalize(pCsr->pStmt); |
| 132624 | 132684 | sqlite3_free(pCsr->aDoclist); |
| 132685 | + sqlite3_free(pCsr->aMatchinfo); |
| 132625 | 132686 | sqlite3Fts3ExprFree(pCsr->pExpr); |
| 132626 | 132687 | memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); |
| 132627 | 132688 | |
| 132628 | 132689 | /* Set the lower and upper bounds on docids to return */ |
| 132629 | 132690 | pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64); |
| | @@ -133930,11 +133991,11 @@ |
| 133930 | 133991 | if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){ |
| 133931 | 133992 | iMax = a[i].iDocid; |
| 133932 | 133993 | bMaxSet = 1; |
| 133933 | 133994 | } |
| 133934 | 133995 | } |
| 133935 | | - assert( rc!=SQLITE_OK || a[p->nToken-1].bIgnore==0 ); |
| 133996 | + assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) ); |
| 133936 | 133997 | assert( rc!=SQLITE_OK || bMaxSet ); |
| 133937 | 133998 | |
| 133938 | 133999 | /* Keep advancing iterators until they all point to the same document */ |
| 133939 | 134000 | for(i=0; i<p->nToken; i++){ |
| 133940 | 134001 | while( rc==SQLITE_OK && bEof==0 |
| | @@ -136047,11 +136108,11 @@ |
| 136047 | 136108 | int i = 0; |
| 136048 | 136109 | |
| 136049 | 136110 | /* Set variable i to the maximum number of bytes of input to tokenize. */ |
| 136050 | 136111 | for(i=0; i<n; i++){ |
| 136051 | 136112 | if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break; |
| 136052 | | - if( z[i]=='*' || z[i]=='"' ) break; |
| 136113 | + if( z[i]=='"' ) break; |
| 136053 | 136114 | } |
| 136054 | 136115 | |
| 136055 | 136116 | *pnConsumed = i; |
| 136056 | 136117 | rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor); |
| 136057 | 136118 | if( rc==SQLITE_OK ){ |
| 136058 | 136119 | |