| | @@ -189,11 +189,11 @@ |
| 189 | 189 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 190 | 190 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 191 | 191 | */ |
| 192 | 192 | #define SQLITE_VERSION "3.8.4" |
| 193 | 193 | #define SQLITE_VERSION_NUMBER 3008004 |
| 194 | | -#define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a" |
| 194 | +#define SQLITE_SOURCE_ID "2014-03-04 13:18:23 9830c343bc954b828f6ca752f8ae63e2c0a980c1" |
| 195 | 195 | |
| 196 | 196 | /* |
| 197 | 197 | ** CAPI3REF: Run-Time Library Version Numbers |
| 198 | 198 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 199 | 199 | ** |
| | @@ -9334,12 +9334,15 @@ |
| 9334 | 9334 | #ifndef SQLITE_OMIT_TRACE |
| 9335 | 9335 | SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); |
| 9336 | 9336 | #endif |
| 9337 | 9337 | |
| 9338 | 9338 | SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); |
| 9339 | | -SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); |
| 9339 | +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int); |
| 9340 | 9340 | SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); |
| 9341 | + |
| 9342 | +typedef int (*RecordCompare)(int,const void*,const UnpackedRecord*,int); |
| 9343 | +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); |
| 9341 | 9344 | |
| 9342 | 9345 | #ifndef SQLITE_OMIT_TRIGGER |
| 9343 | 9346 | SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); |
| 9344 | 9347 | #endif |
| 9345 | 9348 | |
| | @@ -10950,23 +10953,23 @@ |
| 10950 | 10953 | ** the OP_MakeRecord opcode of the VDBE and is disassembled by the |
| 10951 | 10954 | ** OP_Column opcode. |
| 10952 | 10955 | ** |
| 10953 | 10956 | ** This structure holds a record that has already been disassembled |
| 10954 | 10957 | ** into its constituent fields. |
| 10958 | +** |
| 10959 | +** The r1 and r2 member variables are only used by the optimized comparison |
| 10960 | +** functions vdbeRecordCompareInt() and vdbeRecordCompareString(). |
| 10955 | 10961 | */ |
| 10956 | 10962 | struct UnpackedRecord { |
| 10957 | 10963 | KeyInfo *pKeyInfo; /* Collation and sort-order information */ |
| 10958 | 10964 | u16 nField; /* Number of entries in apMem[] */ |
| 10959 | | - u8 flags; /* Boolean settings. UNPACKED_... below */ |
| 10965 | + i8 default_rc; /* Comparison result if keys are equal */ |
| 10960 | 10966 | Mem *aMem; /* Values */ |
| 10967 | + int r1; /* Value to return if (lhs > rhs) */ |
| 10968 | + int r2; /* Value to return if (rhs < lhs) */ |
| 10961 | 10969 | }; |
| 10962 | 10970 | |
| 10963 | | -/* |
| 10964 | | -** Allowed values of UnpackedRecord.flags |
| 10965 | | -*/ |
| 10966 | | -#define UNPACKED_INCRKEY 0x01 /* Make this key an epsilon larger */ |
| 10967 | | -#define UNPACKED_PREFIX_MATCH 0x02 /* A prefix match is considered OK */ |
| 10968 | 10971 | |
| 10969 | 10972 | /* |
| 10970 | 10973 | ** Each SQL index is represented in memory by an |
| 10971 | 10974 | ** instance of the following structure. |
| 10972 | 10975 | ** |
| | @@ -13840,11 +13843,11 @@ |
| 13840 | 13843 | ** the following flags must be set to determine the memory management |
| 13841 | 13844 | ** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| 13842 | 13845 | ** string is \000 or \u0000 terminated |
| 13843 | 13846 | */ |
| 13844 | 13847 | #define MEM_Term 0x0200 /* String rep is nul terminated */ |
| 13845 | | -#define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */ |
| 13848 | +#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */ |
| 13846 | 13849 | #define MEM_Static 0x0800 /* Mem.z points to a static string */ |
| 13847 | 13850 | #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */ |
| 13848 | 13851 | #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */ |
| 13849 | 13852 | #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */ |
| 13850 | 13853 | #ifdef SQLITE_OMIT_INCRBLOB |
| | @@ -14023,11 +14026,11 @@ |
| 14023 | 14026 | SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32); |
| 14024 | 14027 | SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); |
| 14025 | 14028 | SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int); |
| 14026 | 14029 | |
| 14027 | 14030 | int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *); |
| 14028 | | -SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*); |
| 14031 | +SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*); |
| 14029 | 14032 | SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *); |
| 14030 | 14033 | SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); |
| 14031 | 14034 | SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*); |
| 14032 | 14035 | SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*); |
| 14033 | 14036 | SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*); |
| | @@ -14088,10 +14091,11 @@ |
| 14088 | 14091 | # define sqlite3VdbeLeave(X) |
| 14089 | 14092 | #endif |
| 14090 | 14093 | |
| 14091 | 14094 | #ifdef SQLITE_DEBUG |
| 14092 | 14095 | SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*); |
| 14096 | +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*); |
| 14093 | 14097 | #endif |
| 14094 | 14098 | |
| 14095 | 14099 | #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 14096 | 14100 | SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int); |
| 14097 | 14101 | #else |
| | @@ -21471,11 +21475,11 @@ |
| 21471 | 21475 | assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); |
| 21472 | 21476 | |
| 21473 | 21477 | sqlite3VdbeMemRelease(pMem); |
| 21474 | 21478 | pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem); |
| 21475 | 21479 | pMem->enc = desiredEnc; |
| 21476 | | - pMem->flags |= (MEM_Term|MEM_Dyn); |
| 21480 | + pMem->flags |= (MEM_Term); |
| 21477 | 21481 | pMem->z = (char*)zOut; |
| 21478 | 21482 | pMem->zMalloc = pMem->z; |
| 21479 | 21483 | |
| 21480 | 21484 | translate_out: |
| 21481 | 21485 | #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) |
| | @@ -21599,11 +21603,10 @@ |
| 21599 | 21603 | sqlite3VdbeMemRelease(&m); |
| 21600 | 21604 | m.z = 0; |
| 21601 | 21605 | } |
| 21602 | 21606 | assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); |
| 21603 | 21607 | assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); |
| 21604 | | - assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed ); |
| 21605 | 21608 | assert( m.z || db->mallocFailed ); |
| 21606 | 21609 | return m.z; |
| 21607 | 21610 | } |
| 21608 | 21611 | |
| 21609 | 21612 | /* |
| | @@ -55305,10 +55308,11 @@ |
| 55305 | 55308 | i64 intKey, /* The table key */ |
| 55306 | 55309 | int biasRight, /* If true, bias the search to the high end */ |
| 55307 | 55310 | int *pRes /* Write search results here */ |
| 55308 | 55311 | ){ |
| 55309 | 55312 | int rc; |
| 55313 | + RecordCompare xRecordCompare; |
| 55310 | 55314 | |
| 55311 | 55315 | assert( cursorHoldsMutex(pCur) ); |
| 55312 | 55316 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 55313 | 55317 | assert( pRes ); |
| 55314 | 55318 | assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); |
| | @@ -55325,10 +55329,20 @@ |
| 55325 | 55329 | if( pCur->atLast && pCur->info.nKey<intKey ){ |
| 55326 | 55330 | *pRes = -1; |
| 55327 | 55331 | return SQLITE_OK; |
| 55328 | 55332 | } |
| 55329 | 55333 | } |
| 55334 | + |
| 55335 | + if( pIdxKey ){ |
| 55336 | + xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); |
| 55337 | + assert( pIdxKey->default_rc==1 |
| 55338 | + || pIdxKey->default_rc==0 |
| 55339 | + || pIdxKey->default_rc==-1 |
| 55340 | + ); |
| 55341 | + }else{ |
| 55342 | + xRecordCompare = 0; /* Not actually used. Avoids a compiler warning. */ |
| 55343 | + } |
| 55330 | 55344 | |
| 55331 | 55345 | rc = moveToRoot(pCur); |
| 55332 | 55346 | if( rc ){ |
| 55333 | 55347 | return rc; |
| 55334 | 55348 | } |
| | @@ -55410,18 +55424,18 @@ |
| 55410 | 55424 | if( nCell<=pPage->max1bytePayload ){ |
| 55411 | 55425 | /* This branch runs if the record-size field of the cell is a |
| 55412 | 55426 | ** single byte varint and the record fits entirely on the main |
| 55413 | 55427 | ** b-tree page. */ |
| 55414 | 55428 | testcase( pCell+nCell+1==pPage->aDataEnd ); |
| 55415 | | - c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey); |
| 55429 | + c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0); |
| 55416 | 55430 | }else if( !(pCell[1] & 0x80) |
| 55417 | 55431 | && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal |
| 55418 | 55432 | ){ |
| 55419 | 55433 | /* The record-size field is a 2 byte varint and the record |
| 55420 | 55434 | ** fits entirely on the main b-tree page. */ |
| 55421 | 55435 | testcase( pCell+nCell+2==pPage->aDataEnd ); |
| 55422 | | - c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey); |
| 55436 | + c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0); |
| 55423 | 55437 | }else{ |
| 55424 | 55438 | /* The record flows over onto one or more overflow pages. In |
| 55425 | 55439 | ** this case the whole cell needs to be parsed, a buffer allocated |
| 55426 | 55440 | ** and accessPayload() used to retrieve the record into the |
| 55427 | 55441 | ** buffer before VdbeRecordCompare() can be called. */ |
| | @@ -55438,11 +55452,11 @@ |
| 55438 | 55452 | rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); |
| 55439 | 55453 | if( rc ){ |
| 55440 | 55454 | sqlite3_free(pCellKey); |
| 55441 | 55455 | goto moveto_finish; |
| 55442 | 55456 | } |
| 55443 | | - c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey); |
| 55457 | + c = xRecordCompare(nCell, pCellKey, pIdxKey, 0); |
| 55444 | 55458 | sqlite3_free(pCellKey); |
| 55445 | 55459 | } |
| 55446 | 55460 | if( c<0 ){ |
| 55447 | 55461 | lwr = idx+1; |
| 55448 | 55462 | }else if( c>0 ){ |
| | @@ -60001,10 +60015,46 @@ |
| 60001 | 60015 | ** This file contains code use to manipulate "Mem" structure. A "Mem" |
| 60002 | 60016 | ** stores a single value in the VDBE. Mem is an opaque structure visible |
| 60003 | 60017 | ** only within the VDBE. Interface routines refer to a Mem using the |
| 60004 | 60018 | ** name sqlite_value |
| 60005 | 60019 | */ |
| 60020 | + |
| 60021 | +#ifdef SQLITE_DEBUG |
| 60022 | +/* |
| 60023 | +** Check invariants on a Mem object. |
| 60024 | +** |
| 60025 | +** This routine is intended for use inside of assert() statements, like |
| 60026 | +** this: assert( sqlite3VdbeCheckMemInvariants(pMem) ); |
| 60027 | +*/ |
| 60028 | +SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){ |
| 60029 | + /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor |
| 60030 | + ** function for Mem.z |
| 60031 | + */ |
| 60032 | + assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); |
| 60033 | + assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 ); |
| 60034 | + |
| 60035 | + /* If p holds a string or blob, the Mem.z must point to exactly |
| 60036 | + ** one of the following: |
| 60037 | + ** |
| 60038 | + ** (1) Memory in Mem.zMalloc and managed by the Mem object |
| 60039 | + ** (2) Memory to be freed using Mem.xDel |
| 60040 | + ** (3) An ephermal string or blob |
| 60041 | + ** (4) A static string or blob |
| 60042 | + */ |
| 60043 | + if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){ |
| 60044 | + assert( |
| 60045 | + ((p->z==p->zMalloc)? 1 : 0) + |
| 60046 | + ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + |
| 60047 | + ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + |
| 60048 | + ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1 |
| 60049 | + ); |
| 60050 | + } |
| 60051 | + |
| 60052 | + return 1; |
| 60053 | +} |
| 60054 | +#endif |
| 60055 | + |
| 60006 | 60056 | |
| 60007 | 60057 | /* |
| 60008 | 60058 | ** If pMem is an object with a valid string representation, this routine |
| 60009 | 60059 | ** ensures the internal encoding for the string representation is |
| 60010 | 60060 | ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. |
| | @@ -60051,16 +60101,11 @@ |
| 60051 | 60101 | ** pMem->z into the new allocation. pMem must be either a string or |
| 60052 | 60102 | ** blob if bPreserve is true. If bPreserve is false, any prior content |
| 60053 | 60103 | ** in pMem->z is discarded. |
| 60054 | 60104 | */ |
| 60055 | 60105 | SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ |
| 60056 | | - assert( 1 >= |
| 60057 | | - ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) + |
| 60058 | | - (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + |
| 60059 | | - ((pMem->flags&MEM_Ephem) ? 1 : 0) + |
| 60060 | | - ((pMem->flags&MEM_Static) ? 1 : 0) |
| 60061 | | - ); |
| 60106 | + assert( sqlite3VdbeCheckMemInvariants(pMem) ); |
| 60062 | 60107 | assert( (pMem->flags&MEM_RowSet)==0 ); |
| 60063 | 60108 | |
| 60064 | 60109 | /* If the bPreserve flag is set to true, then the memory cell must already |
| 60065 | 60110 | ** contain a valid string or blob value. */ |
| 60066 | 60111 | assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) ); |
| | @@ -60074,26 +60119,26 @@ |
| 60074 | 60119 | }else{ |
| 60075 | 60120 | sqlite3DbFree(pMem->db, pMem->zMalloc); |
| 60076 | 60121 | pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); |
| 60077 | 60122 | } |
| 60078 | 60123 | if( pMem->zMalloc==0 ){ |
| 60079 | | - sqlite3VdbeMemRelease(pMem); |
| 60124 | + VdbeMemRelease(pMem); |
| 60080 | 60125 | pMem->flags = MEM_Null; |
| 60081 | 60126 | return SQLITE_NOMEM; |
| 60082 | 60127 | } |
| 60083 | 60128 | } |
| 60084 | 60129 | |
| 60085 | 60130 | if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){ |
| 60086 | 60131 | memcpy(pMem->zMalloc, pMem->z, pMem->n); |
| 60087 | 60132 | } |
| 60088 | | - if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){ |
| 60089 | | - assert( pMem->xDel!=SQLITE_DYNAMIC ); |
| 60133 | + if( (pMem->flags&MEM_Dyn)!=0 ){ |
| 60134 | + assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC ); |
| 60090 | 60135 | pMem->xDel((void *)(pMem->z)); |
| 60091 | 60136 | } |
| 60092 | 60137 | |
| 60093 | 60138 | pMem->z = pMem->zMalloc; |
| 60094 | | - pMem->flags &= ~(MEM_Ephem|MEM_Static); |
| 60139 | + pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static); |
| 60095 | 60140 | pMem->xDel = 0; |
| 60096 | 60141 | return SQLITE_OK; |
| 60097 | 60142 | } |
| 60098 | 60143 | |
| 60099 | 60144 | /* |
| | @@ -60258,13 +60303,13 @@ |
| 60258 | 60303 | assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); |
| 60259 | 60304 | if( p->flags&MEM_Agg ){ |
| 60260 | 60305 | sqlite3VdbeMemFinalize(p, p->u.pDef); |
| 60261 | 60306 | assert( (p->flags & MEM_Agg)==0 ); |
| 60262 | 60307 | sqlite3VdbeMemRelease(p); |
| 60263 | | - }else if( p->flags&MEM_Dyn && p->xDel ){ |
| 60308 | + }else if( p->flags&MEM_Dyn ){ |
| 60264 | 60309 | assert( (p->flags&MEM_RowSet)==0 ); |
| 60265 | | - assert( p->xDel!=SQLITE_DYNAMIC ); |
| 60310 | + assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 ); |
| 60266 | 60311 | p->xDel((void *)p->z); |
| 60267 | 60312 | p->xDel = 0; |
| 60268 | 60313 | }else if( p->flags&MEM_RowSet ){ |
| 60269 | 60314 | sqlite3RowSetClear(p->u.pRowSet); |
| 60270 | 60315 | }else if( p->flags&MEM_Frame ){ |
| | @@ -60276,10 +60321,11 @@ |
| 60276 | 60321 | ** Release any memory held by the Mem. This may leave the Mem in an |
| 60277 | 60322 | ** inconsistent state, for example with (Mem.z==0) and |
| 60278 | 60323 | ** (Mem.memType==MEM_Str). |
| 60279 | 60324 | */ |
| 60280 | 60325 | SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){ |
| 60326 | + assert( sqlite3VdbeCheckMemInvariants(p) ); |
| 60281 | 60327 | VdbeMemRelease(p); |
| 60282 | 60328 | if( p->zMalloc ){ |
| 60283 | 60329 | sqlite3DbFree(p->db, p->zMalloc); |
| 60284 | 60330 | p->zMalloc = 0; |
| 60285 | 60331 | } |
| | @@ -60613,10 +60659,11 @@ |
| 60613 | 60659 | |
| 60614 | 60660 | assert( (pFrom->flags & MEM_RowSet)==0 ); |
| 60615 | 60661 | VdbeMemRelease(pTo); |
| 60616 | 60662 | memcpy(pTo, pFrom, MEMCELLSIZE); |
| 60617 | 60663 | pTo->flags &= ~MEM_Dyn; |
| 60664 | + pTo->xDel = 0; |
| 60618 | 60665 | |
| 60619 | 60666 | if( pTo->flags&(MEM_Str|MEM_Blob) ){ |
| 60620 | 60667 | if( 0==(pFrom->flags&MEM_Static) ){ |
| 60621 | 60668 | pTo->flags |= MEM_Ephem; |
| 60622 | 60669 | rc = sqlite3VdbeMemMakeWriteable(pTo); |
| | @@ -60738,123 +60785,10 @@ |
| 60738 | 60785 | } |
| 60739 | 60786 | |
| 60740 | 60787 | return SQLITE_OK; |
| 60741 | 60788 | } |
| 60742 | 60789 | |
| 60743 | | -/* |
| 60744 | | -** Compare the values contained by the two memory cells, returning |
| 60745 | | -** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 60746 | | -** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 60747 | | -** and reals) sorted numerically, followed by text ordered by the collating |
| 60748 | | -** sequence pColl and finally blob's ordered by memcmp(). |
| 60749 | | -** |
| 60750 | | -** Two NULL values are considered equal by this function. |
| 60751 | | -*/ |
| 60752 | | -SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 60753 | | - int rc; |
| 60754 | | - int f1, f2; |
| 60755 | | - int combined_flags; |
| 60756 | | - |
| 60757 | | - f1 = pMem1->flags; |
| 60758 | | - f2 = pMem2->flags; |
| 60759 | | - combined_flags = f1|f2; |
| 60760 | | - assert( (combined_flags & MEM_RowSet)==0 ); |
| 60761 | | - |
| 60762 | | - /* If one value is NULL, it is less than the other. If both values |
| 60763 | | - ** are NULL, return 0. |
| 60764 | | - */ |
| 60765 | | - if( combined_flags&MEM_Null ){ |
| 60766 | | - return (f2&MEM_Null) - (f1&MEM_Null); |
| 60767 | | - } |
| 60768 | | - |
| 60769 | | - /* If one value is a number and the other is not, the number is less. |
| 60770 | | - ** If both are numbers, compare as reals if one is a real, or as integers |
| 60771 | | - ** if both values are integers. |
| 60772 | | - */ |
| 60773 | | - if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 60774 | | - double r1, r2; |
| 60775 | | - if( (f1 & f2 & MEM_Int)!=0 ){ |
| 60776 | | - if( pMem1->u.i < pMem2->u.i ) return -1; |
| 60777 | | - if( pMem1->u.i > pMem2->u.i ) return 1; |
| 60778 | | - return 0; |
| 60779 | | - } |
| 60780 | | - if( (f1&MEM_Real)!=0 ){ |
| 60781 | | - r1 = pMem1->r; |
| 60782 | | - }else if( (f1&MEM_Int)!=0 ){ |
| 60783 | | - r1 = (double)pMem1->u.i; |
| 60784 | | - }else{ |
| 60785 | | - return 1; |
| 60786 | | - } |
| 60787 | | - if( (f2&MEM_Real)!=0 ){ |
| 60788 | | - r2 = pMem2->r; |
| 60789 | | - }else if( (f2&MEM_Int)!=0 ){ |
| 60790 | | - r2 = (double)pMem2->u.i; |
| 60791 | | - }else{ |
| 60792 | | - return -1; |
| 60793 | | - } |
| 60794 | | - if( r1<r2 ) return -1; |
| 60795 | | - if( r1>r2 ) return 1; |
| 60796 | | - return 0; |
| 60797 | | - } |
| 60798 | | - |
| 60799 | | - /* If one value is a string and the other is a blob, the string is less. |
| 60800 | | - ** If both are strings, compare using the collating functions. |
| 60801 | | - */ |
| 60802 | | - if( combined_flags&MEM_Str ){ |
| 60803 | | - if( (f1 & MEM_Str)==0 ){ |
| 60804 | | - return 1; |
| 60805 | | - } |
| 60806 | | - if( (f2 & MEM_Str)==0 ){ |
| 60807 | | - return -1; |
| 60808 | | - } |
| 60809 | | - |
| 60810 | | - assert( pMem1->enc==pMem2->enc ); |
| 60811 | | - assert( pMem1->enc==SQLITE_UTF8 || |
| 60812 | | - pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
| 60813 | | - |
| 60814 | | - /* The collation sequence must be defined at this point, even if |
| 60815 | | - ** the user deletes the collation sequence after the vdbe program is |
| 60816 | | - ** compiled (this was not always the case). |
| 60817 | | - */ |
| 60818 | | - assert( !pColl || pColl->xCmp ); |
| 60819 | | - |
| 60820 | | - if( pColl ){ |
| 60821 | | - if( pMem1->enc==pColl->enc ){ |
| 60822 | | - /* The strings are already in the correct encoding. Call the |
| 60823 | | - ** comparison function directly */ |
| 60824 | | - return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
| 60825 | | - }else{ |
| 60826 | | - const void *v1, *v2; |
| 60827 | | - int n1, n2; |
| 60828 | | - Mem c1; |
| 60829 | | - Mem c2; |
| 60830 | | - memset(&c1, 0, sizeof(c1)); |
| 60831 | | - memset(&c2, 0, sizeof(c2)); |
| 60832 | | - sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 60833 | | - sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 60834 | | - v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 60835 | | - n1 = v1==0 ? 0 : c1.n; |
| 60836 | | - v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 60837 | | - n2 = v2==0 ? 0 : c2.n; |
| 60838 | | - rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 60839 | | - sqlite3VdbeMemRelease(&c1); |
| 60840 | | - sqlite3VdbeMemRelease(&c2); |
| 60841 | | - return rc; |
| 60842 | | - } |
| 60843 | | - } |
| 60844 | | - /* If a NULL pointer was passed as the collate function, fall through |
| 60845 | | - ** to the blob case and use memcmp(). */ |
| 60846 | | - } |
| 60847 | | - |
| 60848 | | - /* Both values must be blobs. Compare using memcmp(). */ |
| 60849 | | - rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 60850 | | - if( rc==0 ){ |
| 60851 | | - rc = pMem1->n - pMem2->n; |
| 60852 | | - } |
| 60853 | | - return rc; |
| 60854 | | -} |
| 60855 | | - |
| 60856 | 60790 | /* |
| 60857 | 60791 | ** Move data out of a btree key or data field and into a Mem structure. |
| 60858 | 60792 | ** The data or key is taken from the entry that pCur is currently pointing |
| 60859 | 60793 | ** to. offset and amt determine what portion of the data or key to retrieve. |
| 60860 | 60794 | ** key is true to get the key or false to get data. The result is written |
| | @@ -60891,26 +60825,27 @@ |
| 60891 | 60825 | |
| 60892 | 60826 | if( offset+amt<=available ){ |
| 60893 | 60827 | sqlite3VdbeMemRelease(pMem); |
| 60894 | 60828 | pMem->z = &zData[offset]; |
| 60895 | 60829 | pMem->flags = MEM_Blob|MEM_Ephem; |
| 60830 | + pMem->n = (int)amt; |
| 60896 | 60831 | }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ |
| 60897 | | - pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; |
| 60898 | | - pMem->enc = 0; |
| 60899 | | - pMem->memType = MEM_Blob; |
| 60900 | 60832 | if( key ){ |
| 60901 | 60833 | rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); |
| 60902 | 60834 | }else{ |
| 60903 | 60835 | rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); |
| 60904 | 60836 | } |
| 60905 | | - pMem->z[amt] = 0; |
| 60906 | | - pMem->z[amt+1] = 0; |
| 60907 | | - if( rc!=SQLITE_OK ){ |
| 60837 | + if( rc==SQLITE_OK ){ |
| 60838 | + pMem->z[amt] = 0; |
| 60839 | + pMem->z[amt+1] = 0; |
| 60840 | + pMem->flags = MEM_Blob|MEM_Term; |
| 60841 | + pMem->memType = MEM_Blob; |
| 60842 | + pMem->n = (int)amt; |
| 60843 | + }else{ |
| 60908 | 60844 | sqlite3VdbeMemRelease(pMem); |
| 60909 | 60845 | } |
| 60910 | 60846 | } |
| 60911 | | - pMem->n = (int)amt; |
| 60912 | 60847 | |
| 60913 | 60848 | return rc; |
| 60914 | 60849 | } |
| 60915 | 60850 | |
| 60916 | 60851 | /* This function is only available internally, it is not part of the |
| | @@ -61010,11 +60945,10 @@ |
| 61010 | 60945 | if( pRec ){ |
| 61011 | 60946 | pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx); |
| 61012 | 60947 | if( pRec->pKeyInfo ){ |
| 61013 | 60948 | assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol ); |
| 61014 | 60949 | assert( pRec->pKeyInfo->enc==ENC(db) ); |
| 61015 | | - pRec->flags = UNPACKED_PREFIX_MATCH; |
| 61016 | 60950 | pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))); |
| 61017 | 60951 | for(i=0; i<nCol; i++){ |
| 61018 | 60952 | pRec->aMem[i].flags = MEM_Null; |
| 61019 | 60953 | pRec->aMem[i].memType = MEM_Null; |
| 61020 | 60954 | pRec->aMem[i].db = db; |
| | @@ -62591,10 +62525,11 @@ |
| 62591 | 62525 | } |
| 62592 | 62526 | return; |
| 62593 | 62527 | } |
| 62594 | 62528 | for(pEnd=&p[N]; p<pEnd; p++){ |
| 62595 | 62529 | assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
| 62530 | + assert( sqlite3VdbeCheckMemInvariants(p) ); |
| 62596 | 62531 | |
| 62597 | 62532 | /* This block is really an inlined version of sqlite3VdbeMemRelease() |
| 62598 | 62533 | ** that takes advantage of the fact that the memory cell value is |
| 62599 | 62534 | ** being set to NULL after releasing any dynamic resources. |
| 62600 | 62535 | ** |
| | @@ -62784,11 +62719,11 @@ |
| 62784 | 62719 | |
| 62785 | 62720 | if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ |
| 62786 | 62721 | assert( p->db->mallocFailed ); |
| 62787 | 62722 | return SQLITE_ERROR; |
| 62788 | 62723 | } |
| 62789 | | - pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 62724 | + pMem->flags = MEM_Str|MEM_Term; |
| 62790 | 62725 | zP4 = displayP4(pOp, pMem->z, 32); |
| 62791 | 62726 | if( zP4!=pMem->z ){ |
| 62792 | 62727 | sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
| 62793 | 62728 | }else{ |
| 62794 | 62729 | assert( pMem->z!=0 ); |
| | @@ -62801,11 +62736,11 @@ |
| 62801 | 62736 | if( p->explain==1 ){ |
| 62802 | 62737 | if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ |
| 62803 | 62738 | assert( p->db->mallocFailed ); |
| 62804 | 62739 | return SQLITE_ERROR; |
| 62805 | 62740 | } |
| 62806 | | - pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 62741 | + pMem->flags = MEM_Str|MEM_Term; |
| 62807 | 62742 | pMem->n = 2; |
| 62808 | 62743 | sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
| 62809 | 62744 | pMem->memType = MEM_Str; |
| 62810 | 62745 | pMem->enc = SQLITE_UTF8; |
| 62811 | 62746 | pMem++; |
| | @@ -62813,11 +62748,11 @@ |
| 62813 | 62748 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 62814 | 62749 | if( sqlite3VdbeMemGrow(pMem, 500, 0) ){ |
| 62815 | 62750 | assert( p->db->mallocFailed ); |
| 62816 | 62751 | return SQLITE_ERROR; |
| 62817 | 62752 | } |
| 62818 | | - pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
| 62753 | + pMem->flags = MEM_Str|MEM_Term; |
| 62819 | 62754 | pMem->n = displayComment(pOp, zP4, pMem->z, 500); |
| 62820 | 62755 | pMem->memType = MEM_Str; |
| 62821 | 62756 | pMem->enc = SQLITE_UTF8; |
| 62822 | 62757 | #else |
| 62823 | 62758 | pMem->flags = MEM_Null; /* Comment */ |
| | @@ -64312,10 +64247,18 @@ |
| 64312 | 64247 | |
| 64313 | 64248 | /* NULL or constants 0 or 1 */ |
| 64314 | 64249 | return 0; |
| 64315 | 64250 | } |
| 64316 | 64251 | |
| 64252 | +/* Input "x" is a sequence of unsigned characters that represent a |
| 64253 | +** big-endian integer. Return the equivalent native integer |
| 64254 | +*/ |
| 64255 | +#define ONE_BYTE_INT(x) ((i8)(x)[0]) |
| 64256 | +#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) |
| 64257 | +#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) |
| 64258 | +#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) |
| 64259 | + |
| 64317 | 64260 | /* |
| 64318 | 64261 | ** Deserialize the data blob pointed to by buf as serial type serial_type |
| 64319 | 64262 | ** and store the result in pMem. Return the number of bytes read. |
| 64320 | 64263 | */ |
| 64321 | 64264 | SQLITE_PRIVATE u32 sqlite3VdbeSerialGet( |
| | @@ -64323,46 +64266,40 @@ |
| 64323 | 64266 | u32 serial_type, /* Serial type to deserialize */ |
| 64324 | 64267 | Mem *pMem /* Memory cell to write value into */ |
| 64325 | 64268 | ){ |
| 64326 | 64269 | u64 x; |
| 64327 | 64270 | u32 y; |
| 64328 | | - int i; |
| 64329 | 64271 | switch( serial_type ){ |
| 64330 | 64272 | case 10: /* Reserved for future use */ |
| 64331 | 64273 | case 11: /* Reserved for future use */ |
| 64332 | 64274 | case 0: { /* NULL */ |
| 64333 | 64275 | pMem->flags = MEM_Null; |
| 64334 | 64276 | break; |
| 64335 | 64277 | } |
| 64336 | 64278 | case 1: { /* 1-byte signed integer */ |
| 64337 | | - pMem->u.i = (signed char)buf[0]; |
| 64279 | + pMem->u.i = ONE_BYTE_INT(buf); |
| 64338 | 64280 | pMem->flags = MEM_Int; |
| 64339 | 64281 | return 1; |
| 64340 | 64282 | } |
| 64341 | 64283 | case 2: { /* 2-byte signed integer */ |
| 64342 | | - i = 256*(signed char)buf[0] | buf[1]; |
| 64343 | | - pMem->u.i = (i64)i; |
| 64284 | + pMem->u.i = TWO_BYTE_INT(buf); |
| 64344 | 64285 | pMem->flags = MEM_Int; |
| 64345 | 64286 | return 2; |
| 64346 | 64287 | } |
| 64347 | 64288 | case 3: { /* 3-byte signed integer */ |
| 64348 | | - i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2]; |
| 64349 | | - pMem->u.i = (i64)i; |
| 64289 | + pMem->u.i = THREE_BYTE_INT(buf); |
| 64350 | 64290 | pMem->flags = MEM_Int; |
| 64351 | 64291 | return 3; |
| 64352 | 64292 | } |
| 64353 | 64293 | case 4: { /* 4-byte signed integer */ |
| 64354 | | - y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
| 64294 | + y = FOUR_BYTE_UINT(buf); |
| 64355 | 64295 | pMem->u.i = (i64)*(int*)&y; |
| 64356 | 64296 | pMem->flags = MEM_Int; |
| 64357 | 64297 | return 4; |
| 64358 | 64298 | } |
| 64359 | 64299 | case 5: { /* 6-byte signed integer */ |
| 64360 | | - x = 256*(signed char)buf[0] + buf[1]; |
| 64361 | | - y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
| 64362 | | - x = (x<<32) | y; |
| 64363 | | - pMem->u.i = *(i64*)&x; |
| 64300 | + pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 64364 | 64301 | pMem->flags = MEM_Int; |
| 64365 | 64302 | return 6; |
| 64366 | 64303 | } |
| 64367 | 64304 | case 6: /* 8-byte signed integer */ |
| 64368 | 64305 | case 7: { /* IEEE floating point */ |
| | @@ -64376,12 +64313,12 @@ |
| 64376 | 64313 | static const double r1 = 1.0; |
| 64377 | 64314 | u64 t2 = t1; |
| 64378 | 64315 | swapMixedEndianFloat(t2); |
| 64379 | 64316 | assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
| 64380 | 64317 | #endif |
| 64381 | | - x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
| 64382 | | - y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
| 64318 | + x = FOUR_BYTE_UINT(buf); |
| 64319 | + y = FOUR_BYTE_UINT(buf+4); |
| 64383 | 64320 | x = (x<<32) | y; |
| 64384 | 64321 | if( serial_type==6 ){ |
| 64385 | 64322 | pMem->u.i = *(i64*)&x; |
| 64386 | 64323 | pMem->flags = MEM_Int; |
| 64387 | 64324 | }else{ |
| | @@ -64473,11 +64410,11 @@ |
| 64473 | 64410 | u32 idx; /* Offset in aKey[] to read from */ |
| 64474 | 64411 | u16 u; /* Unsigned loop counter */ |
| 64475 | 64412 | u32 szHdr; |
| 64476 | 64413 | Mem *pMem = p->aMem; |
| 64477 | 64414 | |
| 64478 | | - p->flags = 0; |
| 64415 | + p->default_rc = 0; |
| 64479 | 64416 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 64480 | 64417 | idx = getVarint32(aKey, szHdr); |
| 64481 | 64418 | d = szHdr; |
| 64482 | 64419 | u = 0; |
| 64483 | 64420 | while( idx<szHdr && u<p->nField && d<=nKey ){ |
| | @@ -64494,30 +64431,22 @@ |
| 64494 | 64431 | } |
| 64495 | 64432 | assert( u<=pKeyInfo->nField + 1 ); |
| 64496 | 64433 | p->nField = u; |
| 64497 | 64434 | } |
| 64498 | 64435 | |
| 64436 | +#if SQLITE_DEBUG |
| 64499 | 64437 | /* |
| 64500 | | -** This function compares the two table rows or index records |
| 64501 | | -** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
| 64502 | | -** or positive integer if key1 is less than, equal to or |
| 64503 | | -** greater than key2. The {nKey1, pKey1} key must be a blob |
| 64504 | | -** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 64505 | | -** key must be a parsed key such as obtained from |
| 64506 | | -** sqlite3VdbeParseRecord. |
| 64507 | | -** |
| 64508 | | -** Key1 and Key2 do not have to contain the same number of fields. |
| 64509 | | -** The key with fewer fields is usually compares less than the |
| 64510 | | -** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set |
| 64511 | | -** and the common prefixes are equal, then key1 is less than key2. |
| 64512 | | -** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are |
| 64513 | | -** equal, then the keys are considered to be equal and |
| 64514 | | -** the parts beyond the common prefix are ignored. |
| 64438 | +** This function compares two index or table record keys in the same way |
| 64439 | +** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), |
| 64440 | +** this function deserializes and compares values using the |
| 64441 | +** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used |
| 64442 | +** in assert() statements to ensure that the optimized code in |
| 64443 | +** sqlite3VdbeRecordCompare() returns results with these two primitives. |
| 64515 | 64444 | */ |
| 64516 | | -SQLITE_PRIVATE int sqlite3VdbeRecordCompare( |
| 64445 | +static int vdbeRecordCompareDebug( |
| 64517 | 64446 | int nKey1, const void *pKey1, /* Left key */ |
| 64518 | | - UnpackedRecord *pPKey2 /* Right key */ |
| 64447 | + const UnpackedRecord *pPKey2 /* Right key */ |
| 64519 | 64448 | ){ |
| 64520 | 64449 | u32 d1; /* Offset into aKey[] of next data element */ |
| 64521 | 64450 | u32 idx1; /* Offset into aKey[] of next header element */ |
| 64522 | 64451 | u32 szHdr1; /* Number of bytes in header */ |
| 64523 | 64452 | int i = 0; |
| | @@ -64587,28 +64516,558 @@ |
| 64587 | 64516 | ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). |
| 64588 | 64517 | */ |
| 64589 | 64518 | assert( mem1.zMalloc==0 ); |
| 64590 | 64519 | |
| 64591 | 64520 | /* rc==0 here means that one of the keys ran out of fields and |
| 64592 | | - ** all the fields up to that point were equal. If the UNPACKED_INCRKEY |
| 64593 | | - ** flag is set, then break the tie by treating key2 as larger. |
| 64594 | | - ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes |
| 64595 | | - ** are considered to be equal. Otherwise, the longer key is the |
| 64596 | | - ** larger. As it happens, the pPKey2 will always be the longer |
| 64597 | | - ** if there is a difference. |
| 64598 | | - */ |
| 64599 | | - assert( rc==0 ); |
| 64600 | | - if( pPKey2->flags & UNPACKED_INCRKEY ){ |
| 64601 | | - rc = -1; |
| 64602 | | - }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){ |
| 64603 | | - /* Leave rc==0 */ |
| 64604 | | - }else if( idx1<szHdr1 ){ |
| 64605 | | - rc = 1; |
| 64606 | | - } |
| 64607 | | - return rc; |
| 64608 | | -} |
| 64609 | | - |
| 64521 | + ** all the fields up to that point were equal. Return the the default_rc |
| 64522 | + ** value. */ |
| 64523 | + return pPKey2->default_rc; |
| 64524 | +} |
| 64525 | +#endif |
| 64526 | + |
| 64527 | +/* |
| 64528 | +** Both *pMem1 and *pMem2 contain string values. Compare the two values |
| 64529 | +** using the collation sequence pColl. As usual, return a negative , zero |
| 64530 | +** or positive value if *pMem1 is less than, equal to or greater than |
| 64531 | +** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". |
| 64532 | +*/ |
| 64533 | +static int vdbeCompareMemString( |
| 64534 | + const Mem *pMem1, |
| 64535 | + const Mem *pMem2, |
| 64536 | + const CollSeq *pColl |
| 64537 | +){ |
| 64538 | + if( pMem1->enc==pColl->enc ){ |
| 64539 | + /* The strings are already in the correct encoding. Call the |
| 64540 | + ** comparison function directly */ |
| 64541 | + return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); |
| 64542 | + }else{ |
| 64543 | + int rc; |
| 64544 | + const void *v1, *v2; |
| 64545 | + int n1, n2; |
| 64546 | + Mem c1; |
| 64547 | + Mem c2; |
| 64548 | + memset(&c1, 0, sizeof(c1)); |
| 64549 | + memset(&c2, 0, sizeof(c2)); |
| 64550 | + sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 64551 | + sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 64552 | + v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 64553 | + n1 = v1==0 ? 0 : c1.n; |
| 64554 | + v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 64555 | + n2 = v2==0 ? 0 : c2.n; |
| 64556 | + rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 64557 | + sqlite3VdbeMemRelease(&c1); |
| 64558 | + sqlite3VdbeMemRelease(&c2); |
| 64559 | + return rc; |
| 64560 | + } |
| 64561 | +} |
| 64562 | + |
| 64563 | +/* |
| 64564 | +** Compare the values contained by the two memory cells, returning |
| 64565 | +** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 64566 | +** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 64567 | +** and reals) sorted numerically, followed by text ordered by the collating |
| 64568 | +** sequence pColl and finally blob's ordered by memcmp(). |
| 64569 | +** |
| 64570 | +** Two NULL values are considered equal by this function. |
| 64571 | +*/ |
| 64572 | +SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 64573 | + int rc; |
| 64574 | + int f1, f2; |
| 64575 | + int combined_flags; |
| 64576 | + |
| 64577 | + f1 = pMem1->flags; |
| 64578 | + f2 = pMem2->flags; |
| 64579 | + combined_flags = f1|f2; |
| 64580 | + assert( (combined_flags & MEM_RowSet)==0 ); |
| 64581 | + |
| 64582 | + /* If one value is NULL, it is less than the other. If both values |
| 64583 | + ** are NULL, return 0. |
| 64584 | + */ |
| 64585 | + if( combined_flags&MEM_Null ){ |
| 64586 | + return (f2&MEM_Null) - (f1&MEM_Null); |
| 64587 | + } |
| 64588 | + |
| 64589 | + /* If one value is a number and the other is not, the number is less. |
| 64590 | + ** If both are numbers, compare as reals if one is a real, or as integers |
| 64591 | + ** if both values are integers. |
| 64592 | + */ |
| 64593 | + if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 64594 | + double r1, r2; |
| 64595 | + if( (f1 & f2 & MEM_Int)!=0 ){ |
| 64596 | + if( pMem1->u.i < pMem2->u.i ) return -1; |
| 64597 | + if( pMem1->u.i > pMem2->u.i ) return 1; |
| 64598 | + return 0; |
| 64599 | + } |
| 64600 | + if( (f1&MEM_Real)!=0 ){ |
| 64601 | + r1 = pMem1->r; |
| 64602 | + }else if( (f1&MEM_Int)!=0 ){ |
| 64603 | + r1 = (double)pMem1->u.i; |
| 64604 | + }else{ |
| 64605 | + return 1; |
| 64606 | + } |
| 64607 | + if( (f2&MEM_Real)!=0 ){ |
| 64608 | + r2 = pMem2->r; |
| 64609 | + }else if( (f2&MEM_Int)!=0 ){ |
| 64610 | + r2 = (double)pMem2->u.i; |
| 64611 | + }else{ |
| 64612 | + return -1; |
| 64613 | + } |
| 64614 | + if( r1<r2 ) return -1; |
| 64615 | + if( r1>r2 ) return 1; |
| 64616 | + return 0; |
| 64617 | + } |
| 64618 | + |
| 64619 | + /* If one value is a string and the other is a blob, the string is less. |
| 64620 | + ** If both are strings, compare using the collating functions. |
| 64621 | + */ |
| 64622 | + if( combined_flags&MEM_Str ){ |
| 64623 | + if( (f1 & MEM_Str)==0 ){ |
| 64624 | + return 1; |
| 64625 | + } |
| 64626 | + if( (f2 & MEM_Str)==0 ){ |
| 64627 | + return -1; |
| 64628 | + } |
| 64629 | + |
| 64630 | + assert( pMem1->enc==pMem2->enc ); |
| 64631 | + assert( pMem1->enc==SQLITE_UTF8 || |
| 64632 | + pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
| 64633 | + |
| 64634 | + /* The collation sequence must be defined at this point, even if |
| 64635 | + ** the user deletes the collation sequence after the vdbe program is |
| 64636 | + ** compiled (this was not always the case). |
| 64637 | + */ |
| 64638 | + assert( !pColl || pColl->xCmp ); |
| 64639 | + |
| 64640 | + if( pColl ){ |
| 64641 | + return vdbeCompareMemString(pMem1, pMem2, pColl); |
| 64642 | + } |
| 64643 | + /* If a NULL pointer was passed as the collate function, fall through |
| 64644 | + ** to the blob case and use memcmp(). */ |
| 64645 | + } |
| 64646 | + |
| 64647 | + /* Both values must be blobs. Compare using memcmp(). */ |
| 64648 | + rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); |
| 64649 | + if( rc==0 ){ |
| 64650 | + rc = pMem1->n - pMem2->n; |
| 64651 | + } |
| 64652 | + return rc; |
| 64653 | +} |
| 64654 | + |
| 64655 | + |
| 64656 | +/* |
| 64657 | +** The first argument passed to this function is a serial-type that |
| 64658 | +** corresponds to an integer - all values between 1 and 9 inclusive |
| 64659 | +** except 7. The second points to a buffer containing an integer value |
| 64660 | +** serialized according to serial_type. This function deserializes |
| 64661 | +** and returns the value. |
| 64662 | +*/ |
| 64663 | +static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ |
| 64664 | + u32 y; |
| 64665 | + assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); |
| 64666 | + switch( serial_type ){ |
| 64667 | + case 0: |
| 64668 | + case 1: |
| 64669 | + return ONE_BYTE_INT(aKey); |
| 64670 | + case 2: |
| 64671 | + return TWO_BYTE_INT(aKey); |
| 64672 | + case 3: |
| 64673 | + return THREE_BYTE_INT(aKey); |
| 64674 | + case 4: { |
| 64675 | + y = FOUR_BYTE_UINT(aKey); |
| 64676 | + return (i64)*(int*)&y; |
| 64677 | + } |
| 64678 | + case 5: { |
| 64679 | + return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
| 64680 | + } |
| 64681 | + case 6: { |
| 64682 | + u64 x = FOUR_BYTE_UINT(aKey); |
| 64683 | + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
| 64684 | + return (i64)*(i64*)&x; |
| 64685 | + } |
| 64686 | + } |
| 64687 | + |
| 64688 | + return (serial_type - 8); |
| 64689 | +} |
| 64690 | + |
| 64691 | +/* |
| 64692 | +** This function compares the two table rows or index records |
| 64693 | +** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
| 64694 | +** or positive integer if key1 is less than, equal to or |
| 64695 | +** greater than key2. The {nKey1, pKey1} key must be a blob |
| 64696 | +** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
| 64697 | +** key must be a parsed key such as obtained from |
| 64698 | +** sqlite3VdbeParseRecord. |
| 64699 | +** |
| 64700 | +** If argument bSkip is non-zero, it is assumed that the caller has already |
| 64701 | +** determined that the first fields of the keys are equal. |
| 64702 | +** |
| 64703 | +** Key1 and Key2 do not have to contain the same number of fields. If all |
| 64704 | +** fields that appear in both keys are equal, then pPKey2->default_rc is |
| 64705 | +** returned. |
| 64706 | +*/ |
| 64707 | +SQLITE_PRIVATE int sqlite3VdbeRecordCompare( |
| 64708 | + int nKey1, const void *pKey1, /* Left key */ |
| 64709 | + const UnpackedRecord *pPKey2, /* Right key */ |
| 64710 | + int bSkip /* If true, skip the first field */ |
| 64711 | +){ |
| 64712 | + u32 d1; /* Offset into aKey[] of next data element */ |
| 64713 | + int i; /* Index of next field to compare */ |
| 64714 | + u32 szHdr1; /* Size of record header in bytes */ |
| 64715 | + u32 idx1; /* Offset of first type in header */ |
| 64716 | + int rc = 0; /* Return value */ |
| 64717 | + Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ |
| 64718 | + KeyInfo *pKeyInfo = pPKey2->pKeyInfo; |
| 64719 | + const unsigned char *aKey1 = (const unsigned char *)pKey1; |
| 64720 | + Mem mem1; |
| 64721 | + |
| 64722 | + /* If bSkip is true, then the caller has already determined that the first |
| 64723 | + ** two elements in the keys are equal. Fix the various stack variables so |
| 64724 | + ** that this routine begins comparing at the second field. */ |
| 64725 | + if( bSkip ){ |
| 64726 | + u32 s1; |
| 64727 | + idx1 = 1 + getVarint32(&aKey1[1], s1); |
| 64728 | + szHdr1 = aKey1[0]; |
| 64729 | + d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); |
| 64730 | + i = 1; |
| 64731 | + pRhs++; |
| 64732 | + }else{ |
| 64733 | + idx1 = getVarint32(aKey1, szHdr1); |
| 64734 | + d1 = szHdr1; |
| 64735 | + i = 0; |
| 64736 | + } |
| 64737 | + |
| 64738 | + VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */ |
| 64739 | + assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField |
| 64740 | + || CORRUPT_DB ); |
| 64741 | + assert( pPKey2->pKeyInfo->aSortOrder!=0 ); |
| 64742 | + assert( pPKey2->pKeyInfo->nField>0 ); |
| 64743 | + assert( idx1<=szHdr1 || CORRUPT_DB ); |
| 64744 | + do{ |
| 64745 | + u32 serial_type; |
| 64746 | + |
| 64747 | + /* RHS is an integer */ |
| 64748 | + if( pRhs->flags & MEM_Int ){ |
| 64749 | + serial_type = aKey1[idx1]; |
| 64750 | + if( serial_type>=12 ){ |
| 64751 | + rc = +1; |
| 64752 | + }else if( serial_type==0 ){ |
| 64753 | + rc = -1; |
| 64754 | + }else if( serial_type==7 ){ |
| 64755 | + double rhs = (double)pRhs->u.i; |
| 64756 | + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 64757 | + if( mem1.r<rhs ){ |
| 64758 | + rc = -1; |
| 64759 | + }else if( mem1.r>rhs ){ |
| 64760 | + rc = +1; |
| 64761 | + } |
| 64762 | + }else{ |
| 64763 | + i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); |
| 64764 | + i64 rhs = pRhs->u.i; |
| 64765 | + if( lhs<rhs ){ |
| 64766 | + rc = -1; |
| 64767 | + }else if( lhs>rhs ){ |
| 64768 | + rc = +1; |
| 64769 | + } |
| 64770 | + } |
| 64771 | + } |
| 64772 | + |
| 64773 | + /* RHS is real */ |
| 64774 | + else if( pRhs->flags & MEM_Real ){ |
| 64775 | + serial_type = aKey1[idx1]; |
| 64776 | + if( serial_type>=12 ){ |
| 64777 | + rc = +1; |
| 64778 | + }else if( serial_type==0 ){ |
| 64779 | + rc = -1; |
| 64780 | + }else{ |
| 64781 | + double rhs = pRhs->r; |
| 64782 | + double lhs; |
| 64783 | + sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 64784 | + if( serial_type==7 ){ |
| 64785 | + lhs = mem1.r; |
| 64786 | + }else{ |
| 64787 | + lhs = (double)mem1.u.i; |
| 64788 | + } |
| 64789 | + if( lhs<rhs ){ |
| 64790 | + rc = -1; |
| 64791 | + }else if( lhs>rhs ){ |
| 64792 | + rc = +1; |
| 64793 | + } |
| 64794 | + } |
| 64795 | + } |
| 64796 | + |
| 64797 | + /* RHS is a string */ |
| 64798 | + else if( pRhs->flags & MEM_Str ){ |
| 64799 | + getVarint32(&aKey1[idx1], serial_type); |
| 64800 | + if( serial_type<12 ){ |
| 64801 | + rc = -1; |
| 64802 | + }else if( !(serial_type & 0x01) ){ |
| 64803 | + rc = +1; |
| 64804 | + }else{ |
| 64805 | + mem1.n = (serial_type - 12) / 2; |
| 64806 | + if( (d1+mem1.n) > (unsigned)nKey1 ){ |
| 64807 | + rc = 1; /* Corruption */ |
| 64808 | + }else if( pKeyInfo->aColl[i] ){ |
| 64809 | + mem1.enc = pKeyInfo->enc; |
| 64810 | + mem1.db = pKeyInfo->db; |
| 64811 | + mem1.flags = MEM_Str; |
| 64812 | + mem1.z = (char*)&aKey1[d1]; |
| 64813 | + rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]); |
| 64814 | + }else{ |
| 64815 | + int nCmp = MIN(mem1.n, pRhs->n); |
| 64816 | + rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 64817 | + if( rc==0 ) rc = mem1.n - pRhs->n; |
| 64818 | + } |
| 64819 | + } |
| 64820 | + } |
| 64821 | + |
| 64822 | + /* RHS is a blob */ |
| 64823 | + else if( pRhs->flags & MEM_Blob ){ |
| 64824 | + getVarint32(&aKey1[idx1], serial_type); |
| 64825 | + if( serial_type<12 || (serial_type & 0x01) ){ |
| 64826 | + rc = -1; |
| 64827 | + }else{ |
| 64828 | + int nStr = (serial_type - 12) / 2; |
| 64829 | + if( (d1+nStr) > (unsigned)nKey1 ){ |
| 64830 | + rc = 1; /* Corruption */ |
| 64831 | + }else{ |
| 64832 | + int nCmp = MIN(nStr, pRhs->n); |
| 64833 | + rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 64834 | + if( rc==0 ) rc = nStr - pRhs->n; |
| 64835 | + } |
| 64836 | + } |
| 64837 | + } |
| 64838 | + |
| 64839 | + /* RHS is null */ |
| 64840 | + else{ |
| 64841 | + serial_type = aKey1[idx1]; |
| 64842 | + rc = (serial_type!=0); |
| 64843 | + } |
| 64844 | + |
| 64845 | + if( rc!=0 ){ |
| 64846 | + if( pKeyInfo->aSortOrder[i] ){ |
| 64847 | + rc = -rc; |
| 64848 | + } |
| 64849 | + assert( CORRUPT_DB |
| 64850 | + || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0) |
| 64851 | + || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0) |
| 64852 | + ); |
| 64853 | + assert( mem1.zMalloc==0 ); /* See comment below */ |
| 64854 | + return rc; |
| 64855 | + } |
| 64856 | + |
| 64857 | + i++; |
| 64858 | + pRhs++; |
| 64859 | + d1 += sqlite3VdbeSerialTypeLen(serial_type); |
| 64860 | + idx1 += sqlite3VarintLen(serial_type); |
| 64861 | + }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 ); |
| 64862 | + |
| 64863 | + /* No memory allocation is ever used on mem1. Prove this using |
| 64864 | + ** the following assert(). If the assert() fails, it indicates a |
| 64865 | + ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ |
| 64866 | + assert( mem1.zMalloc==0 ); |
| 64867 | + |
| 64868 | + /* rc==0 here means that one or both of the keys ran out of fields and |
| 64869 | + ** all the fields up to that point were equal. Return the the default_rc |
| 64870 | + ** value. */ |
| 64871 | + assert( CORRUPT_DB |
| 64872 | + || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2) |
| 64873 | + ); |
| 64874 | + return pPKey2->default_rc; |
| 64875 | +} |
| 64876 | + |
| 64877 | +/* |
| 64878 | +** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 64879 | +** that (a) the first field of pPKey2 is an integer, and (b) the |
| 64880 | +** size-of-header varint at the start of (pKey1/nKey1) fits in a single |
| 64881 | +** byte (i.e. is less than 128). |
| 64882 | +*/ |
| 64883 | +static int vdbeRecordCompareInt( |
| 64884 | + int nKey1, const void *pKey1, /* Left key */ |
| 64885 | + const UnpackedRecord *pPKey2, /* Right key */ |
| 64886 | + int bSkip /* Ignored */ |
| 64887 | +){ |
| 64888 | + const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; |
| 64889 | + int serial_type = ((const u8*)pKey1)[1]; |
| 64890 | + int res; |
| 64891 | + u32 y; |
| 64892 | + u64 x; |
| 64893 | + i64 v = pPKey2->aMem[0].u.i; |
| 64894 | + i64 lhs; |
| 64895 | + UNUSED_PARAMETER(bSkip); |
| 64896 | + |
| 64897 | + assert( bSkip==0 ); |
| 64898 | + switch( serial_type ){ |
| 64899 | + case 1: { /* 1-byte signed integer */ |
| 64900 | + lhs = ONE_BYTE_INT(aKey); |
| 64901 | + break; |
| 64902 | + } |
| 64903 | + case 2: { /* 2-byte signed integer */ |
| 64904 | + lhs = TWO_BYTE_INT(aKey); |
| 64905 | + break; |
| 64906 | + } |
| 64907 | + case 3: { /* 3-byte signed integer */ |
| 64908 | + lhs = THREE_BYTE_INT(aKey); |
| 64909 | + break; |
| 64910 | + } |
| 64911 | + case 4: { /* 4-byte signed integer */ |
| 64912 | + y = FOUR_BYTE_UINT(aKey); |
| 64913 | + lhs = (i64)*(int*)&y; |
| 64914 | + break; |
| 64915 | + } |
| 64916 | + case 5: { /* 6-byte signed integer */ |
| 64917 | + lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); |
| 64918 | + break; |
| 64919 | + } |
| 64920 | + case 6: { /* 8-byte signed integer */ |
| 64921 | + x = FOUR_BYTE_UINT(aKey); |
| 64922 | + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); |
| 64923 | + lhs = *(i64*)&x; |
| 64924 | + break; |
| 64925 | + } |
| 64926 | + case 8: |
| 64927 | + lhs = 0; |
| 64928 | + break; |
| 64929 | + case 9: |
| 64930 | + lhs = 1; |
| 64931 | + break; |
| 64932 | + |
| 64933 | + /* This case could be removed without changing the results of running |
| 64934 | + ** this code. Including it causes gcc to generate a faster switch |
| 64935 | + ** statement (since the range of switch targets now starts at zero and |
| 64936 | + ** is contiguous) but does not cause any duplicate code to be generated |
| 64937 | + ** (as gcc is clever enough to combine the two like cases). Other |
| 64938 | + ** compilers might be similar. */ |
| 64939 | + case 0: case 7: |
| 64940 | + return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0); |
| 64941 | + |
| 64942 | + default: |
| 64943 | + return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0); |
| 64944 | + } |
| 64945 | + |
| 64946 | + if( v>lhs ){ |
| 64947 | + res = pPKey2->r1; |
| 64948 | + }else if( v<lhs ){ |
| 64949 | + res = pPKey2->r2; |
| 64950 | + }else if( pPKey2->nField>1 ){ |
| 64951 | + /* The first fields of the two keys are equal. Compare the trailing |
| 64952 | + ** fields. */ |
| 64953 | + res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1); |
| 64954 | + }else{ |
| 64955 | + /* The first fields of the two keys are equal and there are no trailing |
| 64956 | + ** fields. Return pPKey2->default_rc in this case. */ |
| 64957 | + res = pPKey2->default_rc; |
| 64958 | + } |
| 64959 | + |
| 64960 | + assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0) |
| 64961 | + || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0) |
| 64962 | + || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0) |
| 64963 | + || CORRUPT_DB |
| 64964 | + ); |
| 64965 | + return res; |
| 64966 | +} |
| 64967 | + |
| 64968 | +/* |
| 64969 | +** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 64970 | +** that (a) the first field of pPKey2 is a string, that (b) the first field |
| 64971 | +** uses the collation sequence BINARY and (c) that the size-of-header varint |
| 64972 | +** at the start of (pKey1/nKey1) fits in a single byte. |
| 64973 | +*/ |
| 64974 | +static int vdbeRecordCompareString( |
| 64975 | + int nKey1, const void *pKey1, /* Left key */ |
| 64976 | + const UnpackedRecord *pPKey2, /* Right key */ |
| 64977 | + int bSkip |
| 64978 | +){ |
| 64979 | + const u8 *aKey1 = (const u8*)pKey1; |
| 64980 | + int serial_type; |
| 64981 | + int res; |
| 64982 | + UNUSED_PARAMETER(bSkip); |
| 64983 | + |
| 64984 | + assert( bSkip==0 ); |
| 64985 | + getVarint32(&aKey1[1], serial_type); |
| 64986 | + |
| 64987 | + if( serial_type<12 ){ |
| 64988 | + res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ |
| 64989 | + }else if( !(serial_type & 0x01) ){ |
| 64990 | + res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ |
| 64991 | + }else{ |
| 64992 | + int nCmp; |
| 64993 | + int nStr; |
| 64994 | + int szHdr = aKey1[0]; |
| 64995 | + |
| 64996 | + nStr = (serial_type-12) / 2; |
| 64997 | + if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */ |
| 64998 | + nCmp = MIN( pPKey2->aMem[0].n, nStr ); |
| 64999 | + res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); |
| 65000 | + |
| 65001 | + if( res==0 ){ |
| 65002 | + res = nStr - pPKey2->aMem[0].n; |
| 65003 | + if( res==0 ){ |
| 65004 | + if( pPKey2->nField>1 ){ |
| 65005 | + res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1); |
| 65006 | + }else{ |
| 65007 | + res = pPKey2->default_rc; |
| 65008 | + } |
| 65009 | + }else if( res>0 ){ |
| 65010 | + res = pPKey2->r2; |
| 65011 | + }else{ |
| 65012 | + res = pPKey2->r1; |
| 65013 | + } |
| 65014 | + }else if( res>0 ){ |
| 65015 | + res = pPKey2->r2; |
| 65016 | + }else{ |
| 65017 | + res = pPKey2->r1; |
| 65018 | + } |
| 65019 | + } |
| 65020 | + |
| 65021 | + assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0) |
| 65022 | + || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0) |
| 65023 | + || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0) |
| 65024 | + || CORRUPT_DB |
| 65025 | + ); |
| 65026 | + return res; |
| 65027 | +} |
| 65028 | + |
| 65029 | +/* |
| 65030 | +** Return a pointer to an sqlite3VdbeRecordCompare() compatible function |
| 65031 | +** suitable for comparing serialized records to the unpacked record passed |
| 65032 | +** as the only argument. |
| 65033 | +*/ |
| 65034 | +SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ |
| 65035 | + /* varintRecordCompareInt() and varintRecordCompareString() both assume |
| 65036 | + ** that the size-of-header varint that occurs at the start of each record |
| 65037 | + ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() |
| 65038 | + ** also assumes that it is safe to overread a buffer by at least the |
| 65039 | + ** maximum possible legal header size plus 8 bytes. Because there is |
| 65040 | + ** guaranteed to be at least 74 (but not 136) bytes of padding following each |
| 65041 | + ** buffer passed to varintRecordCompareInt() this makes it convenient to |
| 65042 | + ** limit the size of the header to 64 bytes in cases where the first field |
| 65043 | + ** is an integer. |
| 65044 | + ** |
| 65045 | + ** The easiest way to enforce this limit is to consider only records with |
| 65046 | + ** 13 fields or less. If the first field is an integer, the maximum legal |
| 65047 | + ** header size is (12*5 + 1 + 1) bytes. */ |
| 65048 | + if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ |
| 65049 | + int flags = p->aMem[0].flags; |
| 65050 | + if( p->pKeyInfo->aSortOrder[0] ){ |
| 65051 | + p->r1 = 1; |
| 65052 | + p->r2 = -1; |
| 65053 | + }else{ |
| 65054 | + p->r1 = -1; |
| 65055 | + p->r2 = 1; |
| 65056 | + } |
| 65057 | + if( (flags & MEM_Int) ){ |
| 65058 | + return vdbeRecordCompareInt; |
| 65059 | + } |
| 65060 | + if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0 |
| 65061 | + && p->pKeyInfo->aColl[0]==0 |
| 65062 | + ){ |
| 65063 | + return vdbeRecordCompareString; |
| 65064 | + } |
| 65065 | + } |
| 65066 | + |
| 65067 | + return sqlite3VdbeRecordCompare; |
| 65068 | +} |
| 64610 | 65069 | |
| 64611 | 65070 | /* |
| 64612 | 65071 | ** pCur points at an index entry created using the OP_MakeRecord opcode. |
| 64613 | 65072 | ** Read the rowid (the last field in the record) and store it in *rowid. |
| 64614 | 65073 | ** Return SQLITE_OK if everything works, or an error code otherwise. |
| | @@ -64695,23 +65154,23 @@ |
| 64695 | 65154 | ** omits the rowid at the end. The rowid at the end of the index entry |
| 64696 | 65155 | ** is ignored as well. Hence, this routine only compares the prefixes |
| 64697 | 65156 | ** of the keys prior to the final rowid, not the entire key. |
| 64698 | 65157 | */ |
| 64699 | 65158 | SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare( |
| 64700 | | - VdbeCursor *pC, /* The cursor to compare against */ |
| 64701 | | - UnpackedRecord *pUnpacked, /* Unpacked version of key to compare against */ |
| 64702 | | - int *res /* Write the comparison result here */ |
| 65159 | + VdbeCursor *pC, /* The cursor to compare against */ |
| 65160 | + const UnpackedRecord *pUnpacked, /* Unpacked version of key */ |
| 65161 | + int *res /* Write the comparison result here */ |
| 64703 | 65162 | ){ |
| 64704 | 65163 | i64 nCellKey = 0; |
| 64705 | 65164 | int rc; |
| 64706 | 65165 | BtCursor *pCur = pC->pCursor; |
| 64707 | 65166 | Mem m; |
| 64708 | 65167 | |
| 64709 | 65168 | assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 64710 | 65169 | VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 64711 | 65170 | assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 64712 | | - /* nCellKey will always be between 0 and 0xffffffff because of the say |
| 65171 | + /* nCellKey will always be between 0 and 0xffffffff because of the way |
| 64713 | 65172 | ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 64714 | 65173 | if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 64715 | 65174 | *res = 0; |
| 64716 | 65175 | return SQLITE_CORRUPT_BKPT; |
| 64717 | 65176 | } |
| | @@ -64718,12 +65177,11 @@ |
| 64718 | 65177 | memset(&m, 0, sizeof(m)); |
| 64719 | 65178 | rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m); |
| 64720 | 65179 | if( rc ){ |
| 64721 | 65180 | return rc; |
| 64722 | 65181 | } |
| 64723 | | - assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH ); |
| 64724 | | - *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
| 65182 | + *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0); |
| 64725 | 65183 | sqlite3VdbeMemRelease(&m); |
| 64726 | 65184 | return SQLITE_OK; |
| 64727 | 65185 | } |
| 64728 | 65186 | |
| 64729 | 65187 | /* |
| | @@ -66629,11 +67087,11 @@ |
| 66629 | 67087 | ** does not control the string, it might be deleted without the register |
| 66630 | 67088 | ** knowing it. |
| 66631 | 67089 | ** |
| 66632 | 67090 | ** This routine converts an ephemeral string into a dynamically allocated |
| 66633 | 67091 | ** string that the register itself controls. In other words, it |
| 66634 | | -** converts an MEM_Ephem string into an MEM_Dyn string. |
| 67092 | +** converts an MEM_Ephem string into a string with P.z==P.zMalloc. |
| 66635 | 67093 | */ |
| 66636 | 67094 | #define Deephemeralize(P) \ |
| 66637 | 67095 | if( ((P)->flags&MEM_Ephem)!=0 \ |
| 66638 | 67096 | && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} |
| 66639 | 67097 | |
| | @@ -67166,22 +67624,25 @@ |
| 67166 | 67624 | #ifdef SQLITE_DEBUG |
| 67167 | 67625 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 67168 | 67626 | assert( pOp->p1>0 ); |
| 67169 | 67627 | assert( pOp->p1<=(p->nMem-p->nCursor) ); |
| 67170 | 67628 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 67629 | + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| 67171 | 67630 | REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); |
| 67172 | 67631 | } |
| 67173 | 67632 | if( (pOp->opflags & OPFLG_IN2)!=0 ){ |
| 67174 | 67633 | assert( pOp->p2>0 ); |
| 67175 | 67634 | assert( pOp->p2<=(p->nMem-p->nCursor) ); |
| 67176 | 67635 | assert( memIsValid(&aMem[pOp->p2]) ); |
| 67636 | + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); |
| 67177 | 67637 | REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); |
| 67178 | 67638 | } |
| 67179 | 67639 | if( (pOp->opflags & OPFLG_IN3)!=0 ){ |
| 67180 | 67640 | assert( pOp->p3>0 ); |
| 67181 | 67641 | assert( pOp->p3<=(p->nMem-p->nCursor) ); |
| 67182 | 67642 | assert( memIsValid(&aMem[pOp->p3]) ); |
| 67643 | + assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); |
| 67183 | 67644 | REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); |
| 67184 | 67645 | } |
| 67185 | 67646 | if( (pOp->opflags & OPFLG_OUT2)!=0 ){ |
| 67186 | 67647 | assert( pOp->p2>0 ); |
| 67187 | 67648 | assert( pOp->p2<=(p->nMem-p->nCursor) ); |
| | @@ -67279,11 +67740,11 @@ |
| 67279 | 67740 | ** and then jump to address P2. |
| 67280 | 67741 | */ |
| 67281 | 67742 | case OP_Gosub: { /* jump */ |
| 67282 | 67743 | assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); |
| 67283 | 67744 | pIn1 = &aMem[pOp->p1]; |
| 67284 | | - assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 67745 | + assert( VdbeMemDynamic(pIn1)==0 ); |
| 67285 | 67746 | memAboutToChange(p, pIn1); |
| 67286 | 67747 | pIn1->flags = MEM_Int; |
| 67287 | 67748 | pIn1->u.i = pc; |
| 67288 | 67749 | REGISTER_TRACE(pOp->p1, pIn1); |
| 67289 | 67750 | pc = pOp->p2 - 1; |
| | @@ -67352,11 +67813,11 @@ |
| 67352 | 67813 | ** OP_EndCoroutine, jump immediately to P2. |
| 67353 | 67814 | */ |
| 67354 | 67815 | case OP_Yield: { /* in1, jump */ |
| 67355 | 67816 | int pcDest; |
| 67356 | 67817 | pIn1 = &aMem[pOp->p1]; |
| 67357 | | - assert( (pIn1->flags & MEM_Dyn)==0 ); |
| 67818 | + assert( VdbeMemDynamic(pIn1)==0 ); |
| 67358 | 67819 | pIn1->flags = MEM_Int; |
| 67359 | 67820 | pcDest = (int)pIn1->u.i; |
| 67360 | 67821 | pIn1->u.i = pc; |
| 67361 | 67822 | REGISTER_TRACE(pOp->p1, pIn1); |
| 67362 | 67823 | pc = pcDest; |
| | @@ -67525,14 +67986,13 @@ |
| 67525 | 67986 | if( encoding!=SQLITE_UTF8 ){ |
| 67526 | 67987 | rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); |
| 67527 | 67988 | if( rc==SQLITE_TOOBIG ) goto too_big; |
| 67528 | 67989 | if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; |
| 67529 | 67990 | assert( pOut->zMalloc==pOut->z ); |
| 67530 | | - assert( pOut->flags & MEM_Dyn ); |
| 67991 | + assert( VdbeMemDynamic(pOut)==0 ); |
| 67531 | 67992 | pOut->zMalloc = 0; |
| 67532 | 67993 | pOut->flags |= MEM_Static; |
| 67533 | | - pOut->flags &= ~MEM_Dyn; |
| 67534 | 67994 | if( pOp->p4type==P4_DYNAMIC ){ |
| 67535 | 67995 | sqlite3DbFree(db, pOp->p4.z); |
| 67536 | 67996 | } |
| 67537 | 67997 | pOp->p4type = P4_DYNAMIC; |
| 67538 | 67998 | pOp->p4.z = pOut->z; |
| | @@ -67664,18 +68124,20 @@ |
| 67664 | 68124 | do{ |
| 67665 | 68125 | assert( pOut<=&aMem[(p->nMem-p->nCursor)] ); |
| 67666 | 68126 | assert( pIn1<=&aMem[(p->nMem-p->nCursor)] ); |
| 67667 | 68127 | assert( memIsValid(pIn1) ); |
| 67668 | 68128 | memAboutToChange(p, pOut); |
| 68129 | + VdbeMemRelease(pOut); |
| 67669 | 68130 | zMalloc = pOut->zMalloc; |
| 67670 | | - pOut->zMalloc = 0; |
| 67671 | | - sqlite3VdbeMemMove(pOut, pIn1); |
| 68131 | + memcpy(pOut, pIn1, sizeof(Mem)); |
| 67672 | 68132 | #ifdef SQLITE_DEBUG |
| 67673 | 68133 | if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){ |
| 67674 | 68134 | pOut->pScopyFrom += p1 - pOp->p2; |
| 67675 | 68135 | } |
| 67676 | 68136 | #endif |
| 68137 | + pIn1->flags = MEM_Undefined; |
| 68138 | + pIn1->xDel = 0; |
| 67677 | 68139 | pIn1->zMalloc = zMalloc; |
| 67678 | 68140 | REGISTER_TRACE(p2++, pOut); |
| 67679 | 68141 | pIn1++; |
| 67680 | 68142 | pOut++; |
| 67681 | 68143 | }while( n-- ); |
| | @@ -67848,14 +68310,14 @@ |
| 67848 | 68310 | Stringify(pIn2, encoding); |
| 67849 | 68311 | nByte = pIn1->n + pIn2->n; |
| 67850 | 68312 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 67851 | 68313 | goto too_big; |
| 67852 | 68314 | } |
| 67853 | | - MemSetTypeFlag(pOut, MEM_Str); |
| 67854 | 68315 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
| 67855 | 68316 | goto no_mem; |
| 67856 | 68317 | } |
| 68318 | + MemSetTypeFlag(pOut, MEM_Str); |
| 67857 | 68319 | if( pOut!=pIn2 ){ |
| 67858 | 68320 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 67859 | 68321 | } |
| 67860 | 68322 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 67861 | 68323 | pOut->z[nByte]=0; |
| | @@ -69026,10 +69488,11 @@ |
| 69026 | 69488 | ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are |
| 69027 | 69489 | ** all valid. |
| 69028 | 69490 | */ |
| 69029 | 69491 | assert( p2<pC->nHdrParsed ); |
| 69030 | 69492 | assert( rc==SQLITE_OK ); |
| 69493 | + assert( sqlite3VdbeCheckMemInvariants(pDest) ); |
| 69031 | 69494 | if( pC->szRow>=aOffset[p2+1] ){ |
| 69032 | 69495 | /* This is the common case where the desired content fits on the original |
| 69033 | 69496 | ** page - where the content is not on an overflow page */ |
| 69034 | 69497 | VdbeMemRelease(pDest); |
| 69035 | 69498 | sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest); |
| | @@ -69063,12 +69526,12 @@ |
| 69063 | 69526 | ** sqlite3VdbeMemFromBtree() call above) then transfer control of that |
| 69064 | 69527 | ** dynamically allocated space over to the pDest structure. |
| 69065 | 69528 | ** This prevents a memory copy. */ |
| 69066 | 69529 | if( sMem.zMalloc ){ |
| 69067 | 69530 | assert( sMem.z==sMem.zMalloc ); |
| 69068 | | - assert( !(pDest->flags & MEM_Dyn) ); |
| 69069 | | - assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z ); |
| 69531 | + assert( VdbeMemDynamic(pDest)==0 ); |
| 69532 | + assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z ); |
| 69070 | 69533 | pDest->flags &= ~(MEM_Ephem|MEM_Static); |
| 69071 | 69534 | pDest->flags |= MEM_Term; |
| 69072 | 69535 | pDest->z = sMem.z; |
| 69073 | 69536 | pDest->zMalloc = sMem.zMalloc; |
| 69074 | 69537 | } |
| | @@ -69247,11 +69710,11 @@ |
| 69247 | 69710 | assert( i==nHdr ); |
| 69248 | 69711 | assert( j==nByte ); |
| 69249 | 69712 | |
| 69250 | 69713 | assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); |
| 69251 | 69714 | pOut->n = (int)nByte; |
| 69252 | | - pOut->flags = MEM_Blob | MEM_Dyn; |
| 69715 | + pOut->flags = MEM_Blob; |
| 69253 | 69716 | pOut->xDel = 0; |
| 69254 | 69717 | if( nZero ){ |
| 69255 | 69718 | pOut->u.nZero = nZero; |
| 69256 | 69719 | pOut->flags |= MEM_Zero; |
| 69257 | 69720 | } |
| | @@ -70121,20 +70584,20 @@ |
| 70121 | 70584 | r.pKeyInfo = pC->pKeyInfo; |
| 70122 | 70585 | r.nField = (u16)nField; |
| 70123 | 70586 | |
| 70124 | 70587 | /* The next line of code computes as follows, only faster: |
| 70125 | 70588 | ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ |
| 70126 | | - ** r.flags = UNPACKED_INCRKEY; |
| 70589 | + ** r.default_rc = -1; |
| 70127 | 70590 | ** }else{ |
| 70128 | | - ** r.flags = 0; |
| 70591 | + ** r.default_rc = +1; |
| 70129 | 70592 | ** } |
| 70130 | 70593 | */ |
| 70131 | | - r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLT))); |
| 70132 | | - assert( oc!=OP_SeekGT || r.flags==UNPACKED_INCRKEY ); |
| 70133 | | - assert( oc!=OP_SeekLE || r.flags==UNPACKED_INCRKEY ); |
| 70134 | | - assert( oc!=OP_SeekGE || r.flags==0 ); |
| 70135 | | - assert( oc!=OP_SeekLT || r.flags==0 ); |
| 70594 | + r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); |
| 70595 | + assert( oc!=OP_SeekGT || r.default_rc==-1 ); |
| 70596 | + assert( oc!=OP_SeekLE || r.default_rc==-1 ); |
| 70597 | + assert( oc!=OP_SeekGE || r.default_rc==+1 ); |
| 70598 | + assert( oc!=OP_SeekLT || r.default_rc==+1 ); |
| 70136 | 70599 | |
| 70137 | 70600 | r.aMem = &aMem[pOp->p3]; |
| 70138 | 70601 | #ifdef SQLITE_DEBUG |
| 70139 | 70602 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 70140 | 70603 | #endif |
| | @@ -70288,22 +70751,21 @@ |
| 70288 | 70751 | ExpandBlob(&r.aMem[ii]); |
| 70289 | 70752 | #ifdef SQLITE_DEBUG |
| 70290 | 70753 | if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); |
| 70291 | 70754 | #endif |
| 70292 | 70755 | } |
| 70293 | | - r.flags = UNPACKED_PREFIX_MATCH; |
| 70294 | 70756 | pIdxKey = &r; |
| 70295 | 70757 | }else{ |
| 70296 | 70758 | pIdxKey = sqlite3VdbeAllocUnpackedRecord( |
| 70297 | 70759 | pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree |
| 70298 | 70760 | ); |
| 70299 | 70761 | if( pIdxKey==0 ) goto no_mem; |
| 70300 | 70762 | assert( pIn3->flags & MEM_Blob ); |
| 70301 | 70763 | assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ |
| 70302 | 70764 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 70303 | | - pIdxKey->flags |= UNPACKED_PREFIX_MATCH; |
| 70304 | 70765 | } |
| 70766 | + pIdxKey->default_rc = 0; |
| 70305 | 70767 | if( pOp->opcode==OP_NoConflict ){ |
| 70306 | 70768 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 70307 | 70769 | ** input fields are NULL, since any key with a NULL will not |
| 70308 | 70770 | ** conflict */ |
| 70309 | 70771 | for(ii=0; ii<r.nField; ii++){ |
| | @@ -71188,11 +71650,11 @@ |
| 71188 | 71650 | pCrsr = pC->pCursor; |
| 71189 | 71651 | assert( pCrsr!=0 ); |
| 71190 | 71652 | assert( pOp->p5==0 ); |
| 71191 | 71653 | r.pKeyInfo = pC->pKeyInfo; |
| 71192 | 71654 | r.nField = (u16)pOp->p3; |
| 71193 | | - r.flags = UNPACKED_PREFIX_MATCH; |
| 71655 | + r.default_rc = 0; |
| 71194 | 71656 | r.aMem = &aMem[pOp->p2]; |
| 71195 | 71657 | #ifdef SQLITE_DEBUG |
| 71196 | 71658 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 71197 | 71659 | #endif |
| 71198 | 71660 | rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); |
| | @@ -71302,14 +71764,14 @@ |
| 71302 | 71764 | assert( pOp->p4type==P4_INT32 ); |
| 71303 | 71765 | r.pKeyInfo = pC->pKeyInfo; |
| 71304 | 71766 | r.nField = (u16)pOp->p4.i; |
| 71305 | 71767 | if( pOp->opcode<OP_IdxLT ){ |
| 71306 | 71768 | assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); |
| 71307 | | - r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH; |
| 71769 | + r.default_rc = -1; |
| 71308 | 71770 | }else{ |
| 71309 | 71771 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); |
| 71310 | | - r.flags = UNPACKED_PREFIX_MATCH; |
| 71772 | + r.default_rc = 0; |
| 71311 | 71773 | } |
| 71312 | 71774 | r.aMem = &aMem[pOp->p3]; |
| 71313 | 71775 | #ifdef SQLITE_DEBUG |
| 71314 | 71776 | { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } |
| 71315 | 71777 | #endif |
| | @@ -73809,14 +74271,14 @@ |
| 73809 | 74271 | if( r2->aMem[i].flags & MEM_Null ){ |
| 73810 | 74272 | *pRes = -1; |
| 73811 | 74273 | return; |
| 73812 | 74274 | } |
| 73813 | 74275 | } |
| 73814 | | - r2->flags |= UNPACKED_PREFIX_MATCH; |
| 74276 | + assert( r2->default_rc==0 ); |
| 73815 | 74277 | } |
| 73816 | 74278 | |
| 73817 | | - *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2); |
| 74279 | + *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0); |
| 73818 | 74280 | } |
| 73819 | 74281 | |
| 73820 | 74282 | /* |
| 73821 | 74283 | ** This function is called to compare two iterator keys when merging |
| 73822 | 74284 | ** multiple b-tree segments. Parameter iOut is the index of the aTree[] |
| | @@ -75449,10 +75911,12 @@ |
| 75449 | 75911 | pExpr->iTable = 1; |
| 75450 | 75912 | pTab = pParse->pTriggerTab; |
| 75451 | 75913 | }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){ |
| 75452 | 75914 | pExpr->iTable = 0; |
| 75453 | 75915 | pTab = pParse->pTriggerTab; |
| 75916 | + }else{ |
| 75917 | + pTab = 0; |
| 75454 | 75918 | } |
| 75455 | 75919 | |
| 75456 | 75920 | if( pTab ){ |
| 75457 | 75921 | int iCol; |
| 75458 | 75922 | pSchema = pTab->pSchema; |
| | @@ -100117,11 +100581,11 @@ |
| 100117 | 100581 | }else if( eDest!=SRT_Exists ){ |
| 100118 | 100582 | /* If the destination is an EXISTS(...) expression, the actual |
| 100119 | 100583 | ** values returned by the SELECT are not required. |
| 100120 | 100584 | */ |
| 100121 | 100585 | sqlite3ExprCodeExprList(pParse, pEList, regResult, |
| 100122 | | - (eDest==SRT_Output)?SQLITE_ECEL_DUP:0); |
| 100586 | + (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0); |
| 100123 | 100587 | } |
| 100124 | 100588 | |
| 100125 | 100589 | /* If the DISTINCT keyword was present on the SELECT statement |
| 100126 | 100590 | ** and this row has been seen before, then do not make this row |
| 100127 | 100591 | ** part of the result. |
| | @@ -110767,11 +111231,11 @@ |
| 110767 | 111231 | iCol = pRec->nField - 1; |
| 110768 | 111232 | assert( pIdx->nSample>0 ); |
| 110769 | 111233 | assert( pRec->nField>0 && iCol<pIdx->nSampleCol ); |
| 110770 | 111234 | do{ |
| 110771 | 111235 | iTest = (iMin+i)/2; |
| 110772 | | - res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec); |
| 111236 | + res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0); |
| 110773 | 111237 | if( res<0 ){ |
| 110774 | 111238 | iMin = iTest+1; |
| 110775 | 111239 | }else{ |
| 110776 | 111240 | i = iTest; |
| 110777 | 111241 | } |
| | @@ -110782,20 +111246,20 @@ |
| 110782 | 111246 | ** above found the right answer. This block serves no purpose other |
| 110783 | 111247 | ** than to invoke the asserts. */ |
| 110784 | 111248 | if( res==0 ){ |
| 110785 | 111249 | /* If (res==0) is true, then sample $i must be equal to pRec */ |
| 110786 | 111250 | assert( i<pIdx->nSample ); |
| 110787 | | - assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 111251 | + assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0) |
| 110788 | 111252 | || pParse->db->mallocFailed ); |
| 110789 | 111253 | }else{ |
| 110790 | 111254 | /* Otherwise, pRec must be smaller than sample $i and larger than |
| 110791 | 111255 | ** sample ($i-1). */ |
| 110792 | 111256 | assert( i==pIdx->nSample |
| 110793 | | - || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 111257 | + || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0 |
| 110794 | 111258 | || pParse->db->mallocFailed ); |
| 110795 | 111259 | assert( i==0 |
| 110796 | | - || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 111260 | + || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0 |
| 110797 | 111261 | || pParse->db->mallocFailed ); |
| 110798 | 111262 | } |
| 110799 | 111263 | #endif /* ifdef SQLITE_DEBUG */ |
| 110800 | 111264 | |
| 110801 | 111265 | /* At this point, aSample[i] is the first sample that is greater than |
| | @@ -114724,11 +115188,11 @@ |
| 114724 | 115188 | |
| 114725 | 115189 | /* For a co-routine, change all OP_Column references to the table of |
| 114726 | 115190 | ** the co-routine into OP_SCopy of result contained in a register. |
| 114727 | 115191 | ** OP_Rowid becomes OP_Null. |
| 114728 | 115192 | */ |
| 114729 | | - if( pTabItem->viaCoroutine ){ |
| 115193 | + if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
| 114730 | 115194 | last = sqlite3VdbeCurrentAddr(v); |
| 114731 | 115195 | k = pLevel->addrBody; |
| 114732 | 115196 | pOp = sqlite3VdbeGetOp(v, k); |
| 114733 | 115197 | for(; k<last; k++, pOp++){ |
| 114734 | 115198 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 114735 | 115199 | |