Fossil SCM

Update to the latest SQLite with new performance enhancements - now tested to ensure that it works on systems like ARM that default to unsigned characters.

drh 2014-03-04 04:16 trunk merge
Commit 8247784beb5b700e613e9ae0e78eb80a5e9fd4bb
+25
--- src/shell.c
+++ src/shell.c
@@ -444,10 +444,11 @@
444444
** state and mode information.
445445
*/
446446
struct callback_data {
447447
sqlite3 *db; /* The database */
448448
int echoOn; /* True to echo input commands */
449
+ int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL statement */
449450
int statsOn; /* True to display memory stats before each finalize */
450451
int cnt; /* Number of records displayed so far */
451452
FILE *out; /* Write results here */
452453
FILE *traceOut; /* Output for sqlite3_trace() */
453454
int nErr; /* Number of errors seen */
@@ -1299,10 +1300,27 @@
12991300
/* echo the sql statement if echo on */
13001301
if( pArg && pArg->echoOn ){
13011302
const char *zStmtSql = sqlite3_sql(pStmt);
13021303
fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
13031304
}
1305
+
1306
+ /* Show the EXPLAIN QUERY PLAN if .eqp is on */
1307
+ if( pArg && pArg->autoEQP ){
1308
+ sqlite3_stmt *pExplain;
1309
+ char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", sqlite3_sql(pStmt));
1310
+ rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1311
+ if( rc==SQLITE_OK ){
1312
+ while( sqlite3_step(pExplain)==SQLITE_ROW ){
1313
+ fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
1314
+ fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
1315
+ fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
1316
+ fprintf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
1317
+ }
1318
+ }
1319
+ sqlite3_finalize(pExplain);
1320
+ sqlite3_free(zEQP);
1321
+ }
13041322
13051323
/* Output TESTCTRL_EXPLAIN text of requested */
13061324
if( pArg && pArg->mode==MODE_Explain ){
13071325
const char *zExplain = 0;
13081326
sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
@@ -2298,10 +2316,14 @@
22982316
}else
22992317
23002318
if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
23012319
p->echoOn = booleanValue(azArg[1]);
23022320
}else
2321
+
2322
+ if( c=='e' && strncmp(azArg[0], "eqp", n)==0 && nArg>1 && nArg<3 ){
2323
+ p->autoEQP = booleanValue(azArg[1]);
2324
+ }else
23032325
23042326
if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
23052327
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
23062328
rc = 2;
23072329
}else
@@ -2871,10 +2893,11 @@
28712893
}else
28722894
28732895
if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
28742896
int i;
28752897
fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
2898
+ fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
28762899
fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
28772900
fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
28782901
fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
28792902
fprintf(p->out,"%9.9s: ", "nullvalue");
28802903
output_c_string(p->out, p->nullvalue);
@@ -3708,10 +3731,12 @@
37083731
data.showHeader = 1;
37093732
}else if( strcmp(z,"-noheader")==0 ){
37103733
data.showHeader = 0;
37113734
}else if( strcmp(z,"-echo")==0 ){
37123735
data.echoOn = 1;
3736
+ }else if( strcmp(z,"-eqp")==0 ){
3737
+ data.autoEQP = 1;
37133738
}else if( strcmp(z,"-stats")==0 ){
37143739
data.statsOn = 1;
37153740
}else if( strcmp(z,"-bail")==0 ){
37163741
bail_on_error = 1;
37173742
}else if( strcmp(z,"-version")==0 ){
37183743
--- src/shell.c
+++ src/shell.c
@@ -444,10 +444,11 @@
444 ** state and mode information.
445 */
446 struct callback_data {
447 sqlite3 *db; /* The database */
448 int echoOn; /* True to echo input commands */
 
449 int statsOn; /* True to display memory stats before each finalize */
450 int cnt; /* Number of records displayed so far */
451 FILE *out; /* Write results here */
452 FILE *traceOut; /* Output for sqlite3_trace() */
453 int nErr; /* Number of errors seen */
@@ -1299,10 +1300,27 @@
1299 /* echo the sql statement if echo on */
1300 if( pArg && pArg->echoOn ){
1301 const char *zStmtSql = sqlite3_sql(pStmt);
1302 fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1303 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1304
1305 /* Output TESTCTRL_EXPLAIN text of requested */
1306 if( pArg && pArg->mode==MODE_Explain ){
1307 const char *zExplain = 0;
1308 sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
@@ -2298,10 +2316,14 @@
2298 }else
2299
2300 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
2301 p->echoOn = booleanValue(azArg[1]);
2302 }else
 
 
 
 
2303
2304 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
2305 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
2306 rc = 2;
2307 }else
@@ -2871,10 +2893,11 @@
2871 }else
2872
2873 if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
2874 int i;
2875 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
 
2876 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
2877 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
2878 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
2879 fprintf(p->out,"%9.9s: ", "nullvalue");
2880 output_c_string(p->out, p->nullvalue);
@@ -3708,10 +3731,12 @@
3708 data.showHeader = 1;
3709 }else if( strcmp(z,"-noheader")==0 ){
3710 data.showHeader = 0;
3711 }else if( strcmp(z,"-echo")==0 ){
3712 data.echoOn = 1;
 
 
3713 }else if( strcmp(z,"-stats")==0 ){
3714 data.statsOn = 1;
3715 }else if( strcmp(z,"-bail")==0 ){
3716 bail_on_error = 1;
3717 }else if( strcmp(z,"-version")==0 ){
3718
--- src/shell.c
+++ src/shell.c
@@ -444,10 +444,11 @@
444 ** state and mode information.
445 */
446 struct callback_data {
447 sqlite3 *db; /* The database */
448 int echoOn; /* True to echo input commands */
449 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL statement */
450 int statsOn; /* True to display memory stats before each finalize */
451 int cnt; /* Number of records displayed so far */
452 FILE *out; /* Write results here */
453 FILE *traceOut; /* Output for sqlite3_trace() */
454 int nErr; /* Number of errors seen */
@@ -1299,10 +1300,27 @@
1300 /* echo the sql statement if echo on */
1301 if( pArg && pArg->echoOn ){
1302 const char *zStmtSql = sqlite3_sql(pStmt);
1303 fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1304 }
1305
1306 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
1307 if( pArg && pArg->autoEQP ){
1308 sqlite3_stmt *pExplain;
1309 char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", sqlite3_sql(pStmt));
1310 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
1311 if( rc==SQLITE_OK ){
1312 while( sqlite3_step(pExplain)==SQLITE_ROW ){
1313 fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
1314 fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
1315 fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
1316 fprintf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
1317 }
1318 }
1319 sqlite3_finalize(pExplain);
1320 sqlite3_free(zEQP);
1321 }
1322
1323 /* Output TESTCTRL_EXPLAIN text of requested */
1324 if( pArg && pArg->mode==MODE_Explain ){
1325 const char *zExplain = 0;
1326 sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
@@ -2298,10 +2316,14 @@
2316 }else
2317
2318 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
2319 p->echoOn = booleanValue(azArg[1]);
2320 }else
2321
2322 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 && nArg>1 && nArg<3 ){
2323 p->autoEQP = booleanValue(azArg[1]);
2324 }else
2325
2326 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
2327 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
2328 rc = 2;
2329 }else
@@ -2871,10 +2893,11 @@
2893 }else
2894
2895 if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
2896 int i;
2897 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
2898 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
2899 fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off");
2900 fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off");
2901 fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]);
2902 fprintf(p->out,"%9.9s: ", "nullvalue");
2903 output_c_string(p->out, p->nullvalue);
@@ -3708,10 +3731,12 @@
3731 data.showHeader = 1;
3732 }else if( strcmp(z,"-noheader")==0 ){
3733 data.showHeader = 0;
3734 }else if( strcmp(z,"-echo")==0 ){
3735 data.echoOn = 1;
3736 }else if( strcmp(z,"-eqp")==0 ){
3737 data.autoEQP = 1;
3738 }else if( strcmp(z,"-stats")==0 ){
3739 data.statsOn = 1;
3740 }else if( strcmp(z,"-bail")==0 ){
3741 bail_on_error = 1;
3742 }else if( strcmp(z,"-version")==0 ){
3743
+692 -231
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -189,11 +189,11 @@
189189
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
190190
** [sqlite_version()] and [sqlite_source_id()].
191191
*/
192192
#define SQLITE_VERSION "3.8.4"
193193
#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 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
195195
196196
/*
197197
** CAPI3REF: Run-Time Library Version Numbers
198198
** KEYWORDS: sqlite3_version, sqlite3_sourceid
199199
**
@@ -9334,12 +9334,15 @@
93349334
#ifndef SQLITE_OMIT_TRACE
93359335
SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
93369336
#endif
93379337
93389338
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);
93409340
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*);
93419344
93429345
#ifndef SQLITE_OMIT_TRIGGER
93439346
SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
93449347
#endif
93459348
@@ -10950,23 +10953,23 @@
1095010953
** the OP_MakeRecord opcode of the VDBE and is disassembled by the
1095110954
** OP_Column opcode.
1095210955
**
1095310956
** This structure holds a record that has already been disassembled
1095410957
** into its constituent fields.
10958
+**
10959
+** The r1 and r2 member variables are only used by the optimized comparison
10960
+** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
1095510961
*/
1095610962
struct UnpackedRecord {
1095710963
KeyInfo *pKeyInfo; /* Collation and sort-order information */
1095810964
u16 nField; /* Number of entries in apMem[] */
10959
- u8 flags; /* Boolean settings. UNPACKED_... below */
10965
+ i8 default_rc; /* Comparison result if keys are equal */
1096010966
Mem *aMem; /* Values */
10967
+ int r1; /* Value to return if (lhs > rhs) */
10968
+ int r2; /* Value to return if (rhs < lhs) */
1096110969
};
1096210970
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 */
1096810971
1096910972
/*
1097010973
** Each SQL index is represented in memory by an
1097110974
** instance of the following structure.
1097210975
**
@@ -13840,11 +13843,11 @@
1384013843
** the following flags must be set to determine the memory management
1384113844
** policy for Mem.z. The MEM_Term flag tells us whether or not the
1384213845
** string is \000 or \u0000 terminated
1384313846
*/
1384413847
#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 */
1384613849
#define MEM_Static 0x0800 /* Mem.z points to a static string */
1384713850
#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
1384813851
#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
1384913852
#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
1385013853
#ifdef SQLITE_OMIT_INCRBLOB
@@ -14023,11 +14026,11 @@
1402314026
SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
1402414027
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
1402514028
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
1402614029
1402714030
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*);
1402914032
SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
1403014033
SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1403114034
SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
1403214035
SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
1403314036
SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
@@ -14088,10 +14091,11 @@
1408814091
# define sqlite3VdbeLeave(X)
1408914092
#endif
1409014093
1409114094
#ifdef SQLITE_DEBUG
1409214095
SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
14096
+SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
1409314097
#endif
1409414098
1409514099
#ifndef SQLITE_OMIT_FOREIGN_KEY
1409614100
SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
1409714101
#else
@@ -21471,11 +21475,11 @@
2147121475
assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
2147221476
2147321477
sqlite3VdbeMemRelease(pMem);
2147421478
pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
2147521479
pMem->enc = desiredEnc;
21476
- pMem->flags |= (MEM_Term|MEM_Dyn);
21480
+ pMem->flags |= (MEM_Term);
2147721481
pMem->z = (char*)zOut;
2147821482
pMem->zMalloc = pMem->z;
2147921483
2148021484
translate_out:
2148121485
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
@@ -21599,11 +21603,10 @@
2159921603
sqlite3VdbeMemRelease(&m);
2160021604
m.z = 0;
2160121605
}
2160221606
assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
2160321607
assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
21604
- assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
2160521608
assert( m.z || db->mallocFailed );
2160621609
return m.z;
2160721610
}
2160821611
2160921612
/*
@@ -55305,10 +55308,11 @@
5530555308
i64 intKey, /* The table key */
5530655309
int biasRight, /* If true, bias the search to the high end */
5530755310
int *pRes /* Write search results here */
5530855311
){
5530955312
int rc;
55313
+ RecordCompare xRecordCompare;
5531055314
5531155315
assert( cursorHoldsMutex(pCur) );
5531255316
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5531355317
assert( pRes );
5531455318
assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
@@ -55325,10 +55329,20 @@
5532555329
if( pCur->atLast && pCur->info.nKey<intKey ){
5532655330
*pRes = -1;
5532755331
return SQLITE_OK;
5532855332
}
5532955333
}
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
+ }
5533055344
5533155345
rc = moveToRoot(pCur);
5533255346
if( rc ){
5533355347
return rc;
5533455348
}
@@ -55410,18 +55424,18 @@
5541055424
if( nCell<=pPage->max1bytePayload ){
5541155425
/* This branch runs if the record-size field of the cell is a
5541255426
** single byte varint and the record fits entirely on the main
5541355427
** b-tree page. */
5541455428
testcase( pCell+nCell+1==pPage->aDataEnd );
55415
- c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
55429
+ c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
5541655430
}else if( !(pCell[1] & 0x80)
5541755431
&& (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5541855432
){
5541955433
/* The record-size field is a 2 byte varint and the record
5542055434
** fits entirely on the main b-tree page. */
5542155435
testcase( pCell+nCell+2==pPage->aDataEnd );
55422
- c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
55436
+ c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
5542355437
}else{
5542455438
/* The record flows over onto one or more overflow pages. In
5542555439
** this case the whole cell needs to be parsed, a buffer allocated
5542655440
** and accessPayload() used to retrieve the record into the
5542755441
** buffer before VdbeRecordCompare() can be called. */
@@ -55438,11 +55452,11 @@
5543855452
rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
5543955453
if( rc ){
5544055454
sqlite3_free(pCellKey);
5544155455
goto moveto_finish;
5544255456
}
55443
- c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
55457
+ c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
5544455458
sqlite3_free(pCellKey);
5544555459
}
5544655460
if( c<0 ){
5544755461
lwr = idx+1;
5544855462
}else if( c>0 ){
@@ -60001,10 +60015,46 @@
6000160015
** This file contains code use to manipulate "Mem" structure. A "Mem"
6000260016
** stores a single value in the VDBE. Mem is an opaque structure visible
6000360017
** only within the VDBE. Interface routines refer to a Mem using the
6000460018
** name sqlite_value
6000560019
*/
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
+
6000660056
6000760057
/*
6000860058
** If pMem is an object with a valid string representation, this routine
6000960059
** ensures the internal encoding for the string representation is
6001060060
** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -60051,16 +60101,11 @@
6005160101
** pMem->z into the new allocation. pMem must be either a string or
6005260102
** blob if bPreserve is true. If bPreserve is false, any prior content
6005360103
** in pMem->z is discarded.
6005460104
*/
6005560105
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) );
6006260107
assert( (pMem->flags&MEM_RowSet)==0 );
6006360108
6006460109
/* If the bPreserve flag is set to true, then the memory cell must already
6006560110
** contain a valid string or blob value. */
6006660111
assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
@@ -60074,26 +60119,26 @@
6007460119
}else{
6007560120
sqlite3DbFree(pMem->db, pMem->zMalloc);
6007660121
pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
6007760122
}
6007860123
if( pMem->zMalloc==0 ){
60079
- sqlite3VdbeMemRelease(pMem);
60124
+ VdbeMemRelease(pMem);
6008060125
pMem->flags = MEM_Null;
6008160126
return SQLITE_NOMEM;
6008260127
}
6008360128
}
6008460129
6008560130
if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
6008660131
memcpy(pMem->zMalloc, pMem->z, pMem->n);
6008760132
}
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 );
6009060135
pMem->xDel((void *)(pMem->z));
6009160136
}
6009260137
6009360138
pMem->z = pMem->zMalloc;
60094
- pMem->flags &= ~(MEM_Ephem|MEM_Static);
60139
+ pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
6009560140
pMem->xDel = 0;
6009660141
return SQLITE_OK;
6009760142
}
6009860143
6009960144
/*
@@ -60258,13 +60303,13 @@
6025860303
assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
6025960304
if( p->flags&MEM_Agg ){
6026060305
sqlite3VdbeMemFinalize(p, p->u.pDef);
6026160306
assert( (p->flags & MEM_Agg)==0 );
6026260307
sqlite3VdbeMemRelease(p);
60263
- }else if( p->flags&MEM_Dyn && p->xDel ){
60308
+ }else if( p->flags&MEM_Dyn ){
6026460309
assert( (p->flags&MEM_RowSet)==0 );
60265
- assert( p->xDel!=SQLITE_DYNAMIC );
60310
+ assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
6026660311
p->xDel((void *)p->z);
6026760312
p->xDel = 0;
6026860313
}else if( p->flags&MEM_RowSet ){
6026960314
sqlite3RowSetClear(p->u.pRowSet);
6027060315
}else if( p->flags&MEM_Frame ){
@@ -60276,10 +60321,11 @@
6027660321
** Release any memory held by the Mem. This may leave the Mem in an
6027760322
** inconsistent state, for example with (Mem.z==0) and
6027860323
** (Mem.memType==MEM_Str).
6027960324
*/
6028060325
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
60326
+ assert( sqlite3VdbeCheckMemInvariants(p) );
6028160327
VdbeMemRelease(p);
6028260328
if( p->zMalloc ){
6028360329
sqlite3DbFree(p->db, p->zMalloc);
6028460330
p->zMalloc = 0;
6028560331
}
@@ -60613,10 +60659,11 @@
6061360659
6061460660
assert( (pFrom->flags & MEM_RowSet)==0 );
6061560661
VdbeMemRelease(pTo);
6061660662
memcpy(pTo, pFrom, MEMCELLSIZE);
6061760663
pTo->flags &= ~MEM_Dyn;
60664
+ pTo->xDel = 0;
6061860665
6061960666
if( pTo->flags&(MEM_Str|MEM_Blob) ){
6062060667
if( 0==(pFrom->flags&MEM_Static) ){
6062160668
pTo->flags |= MEM_Ephem;
6062260669
rc = sqlite3VdbeMemMakeWriteable(pTo);
@@ -60738,123 +60785,10 @@
6073860785
}
6073960786
6074060787
return SQLITE_OK;
6074160788
}
6074260789
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
-
6085660790
/*
6085760791
** Move data out of a btree key or data field and into a Mem structure.
6085860792
** The data or key is taken from the entry that pCur is currently pointing
6085960793
** to. offset and amt determine what portion of the data or key to retrieve.
6086060794
** key is true to get the key or false to get data. The result is written
@@ -60892,11 +60826,11 @@
6089260826
if( offset+amt<=available ){
6089360827
sqlite3VdbeMemRelease(pMem);
6089460828
pMem->z = &zData[offset];
6089560829
pMem->flags = MEM_Blob|MEM_Ephem;
6089660830
}else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
60897
- pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
60831
+ pMem->flags = MEM_Blob|MEM_Term;
6089860832
pMem->enc = 0;
6089960833
pMem->memType = MEM_Blob;
6090060834
if( key ){
6090160835
rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
6090260836
}else{
@@ -61010,11 +60944,10 @@
6101060944
if( pRec ){
6101160945
pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
6101260946
if( pRec->pKeyInfo ){
6101360947
assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
6101460948
assert( pRec->pKeyInfo->enc==ENC(db) );
61015
- pRec->flags = UNPACKED_PREFIX_MATCH;
6101660949
pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
6101760950
for(i=0; i<nCol; i++){
6101860951
pRec->aMem[i].flags = MEM_Null;
6101960952
pRec->aMem[i].memType = MEM_Null;
6102060953
pRec->aMem[i].db = db;
@@ -62591,10 +62524,11 @@
6259162524
}
6259262525
return;
6259362526
}
6259462527
for(pEnd=&p[N]; p<pEnd; p++){
6259562528
assert( (&p[1])==pEnd || p[0].db==p[1].db );
62529
+ assert( sqlite3VdbeCheckMemInvariants(p) );
6259662530
6259762531
/* This block is really an inlined version of sqlite3VdbeMemRelease()
6259862532
** that takes advantage of the fact that the memory cell value is
6259962533
** being set to NULL after releasing any dynamic resources.
6260062534
**
@@ -62784,11 +62718,11 @@
6278462718
6278562719
if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
6278662720
assert( p->db->mallocFailed );
6278762721
return SQLITE_ERROR;
6278862722
}
62789
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62723
+ pMem->flags = MEM_Str|MEM_Term;
6279062724
zP4 = displayP4(pOp, pMem->z, 32);
6279162725
if( zP4!=pMem->z ){
6279262726
sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
6279362727
}else{
6279462728
assert( pMem->z!=0 );
@@ -62801,11 +62735,11 @@
6280162735
if( p->explain==1 ){
6280262736
if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
6280362737
assert( p->db->mallocFailed );
6280462738
return SQLITE_ERROR;
6280562739
}
62806
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62740
+ pMem->flags = MEM_Str|MEM_Term;
6280762741
pMem->n = 2;
6280862742
sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
6280962743
pMem->memType = MEM_Str;
6281062744
pMem->enc = SQLITE_UTF8;
6281162745
pMem++;
@@ -62813,11 +62747,11 @@
6281362747
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
6281462748
if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
6281562749
assert( p->db->mallocFailed );
6281662750
return SQLITE_ERROR;
6281762751
}
62818
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62752
+ pMem->flags = MEM_Str|MEM_Term;
6281962753
pMem->n = displayComment(pOp, zP4, pMem->z, 500);
6282062754
pMem->memType = MEM_Str;
6282162755
pMem->enc = SQLITE_UTF8;
6282262756
#else
6282362757
pMem->flags = MEM_Null; /* Comment */
@@ -64312,10 +64246,18 @@
6431264246
6431364247
/* NULL or constants 0 or 1 */
6431464248
return 0;
6431564249
}
6431664250
64251
+/* Input "x" is a sequence of unsigned characters that represent a
64252
+** big-endian integer. Return the equivalent native integer
64253
+*/
64254
+#define ONE_BYTE_INT(x) ((i8)(x)[0])
64255
+#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
64256
+#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
64257
+#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
64258
+
6431764259
/*
6431864260
** Deserialize the data blob pointed to by buf as serial type serial_type
6431964261
** and store the result in pMem. Return the number of bytes read.
6432064262
*/
6432164263
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
@@ -64323,46 +64265,40 @@
6432364265
u32 serial_type, /* Serial type to deserialize */
6432464266
Mem *pMem /* Memory cell to write value into */
6432564267
){
6432664268
u64 x;
6432764269
u32 y;
64328
- int i;
6432964270
switch( serial_type ){
6433064271
case 10: /* Reserved for future use */
6433164272
case 11: /* Reserved for future use */
6433264273
case 0: { /* NULL */
6433364274
pMem->flags = MEM_Null;
6433464275
break;
6433564276
}
6433664277
case 1: { /* 1-byte signed integer */
64337
- pMem->u.i = (signed char)buf[0];
64278
+ pMem->u.i = ONE_BYTE_INT(buf);
6433864279
pMem->flags = MEM_Int;
6433964280
return 1;
6434064281
}
6434164282
case 2: { /* 2-byte signed integer */
64342
- i = 256*(signed char)buf[0] | buf[1];
64343
- pMem->u.i = (i64)i;
64283
+ pMem->u.i = TWO_BYTE_INT(buf);
6434464284
pMem->flags = MEM_Int;
6434564285
return 2;
6434664286
}
6434764287
case 3: { /* 3-byte signed integer */
64348
- i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
64349
- pMem->u.i = (i64)i;
64288
+ pMem->u.i = THREE_BYTE_INT(buf);
6435064289
pMem->flags = MEM_Int;
6435164290
return 3;
6435264291
}
6435364292
case 4: { /* 4-byte signed integer */
64354
- y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
64293
+ y = FOUR_BYTE_UINT(buf);
6435564294
pMem->u.i = (i64)*(int*)&y;
6435664295
pMem->flags = MEM_Int;
6435764296
return 4;
6435864297
}
6435964298
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;
64299
+ pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
6436464300
pMem->flags = MEM_Int;
6436564301
return 6;
6436664302
}
6436764303
case 6: /* 8-byte signed integer */
6436864304
case 7: { /* IEEE floating point */
@@ -64376,12 +64312,12 @@
6437664312
static const double r1 = 1.0;
6437764313
u64 t2 = t1;
6437864314
swapMixedEndianFloat(t2);
6437964315
assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
6438064316
#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];
64317
+ x = FOUR_BYTE_UINT(buf);
64318
+ y = FOUR_BYTE_UINT(buf+4);
6438364319
x = (x<<32) | y;
6438464320
if( serial_type==6 ){
6438564321
pMem->u.i = *(i64*)&x;
6438664322
pMem->flags = MEM_Int;
6438764323
}else{
@@ -64473,11 +64409,11 @@
6447364409
u32 idx; /* Offset in aKey[] to read from */
6447464410
u16 u; /* Unsigned loop counter */
6447564411
u32 szHdr;
6447664412
Mem *pMem = p->aMem;
6447764413
64478
- p->flags = 0;
64414
+ p->default_rc = 0;
6447964415
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
6448064416
idx = getVarint32(aKey, szHdr);
6448164417
d = szHdr;
6448264418
u = 0;
6448364419
while( idx<szHdr && u<p->nField && d<=nKey ){
@@ -64494,30 +64430,22 @@
6449464430
}
6449564431
assert( u<=pKeyInfo->nField + 1 );
6449664432
p->nField = u;
6449764433
}
6449864434
64435
+#if SQLITE_DEBUG
6449964436
/*
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.
64437
+** This function compares two index or table record keys in the same way
64438
+** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
64439
+** this function deserializes and compares values using the
64440
+** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
64441
+** in assert() statements to ensure that the optimized code in
64442
+** sqlite3VdbeRecordCompare() returns results with these two primitives.
6451564443
*/
64516
-SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64444
+static int vdbeRecordCompareDebug(
6451764445
int nKey1, const void *pKey1, /* Left key */
64518
- UnpackedRecord *pPKey2 /* Right key */
64446
+ const UnpackedRecord *pPKey2 /* Right key */
6451964447
){
6452064448
u32 d1; /* Offset into aKey[] of next data element */
6452164449
u32 idx1; /* Offset into aKey[] of next header element */
6452264450
u32 szHdr1; /* Number of bytes in header */
6452364451
int i = 0;
@@ -64587,28 +64515,558 @@
6458764515
** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
6458864516
*/
6458964517
assert( mem1.zMalloc==0 );
6459064518
6459164519
/* 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
-
64520
+ ** all the fields up to that point were equal. Return the the default_rc
64521
+ ** value. */
64522
+ return pPKey2->default_rc;
64523
+}
64524
+#endif
64525
+
64526
+/*
64527
+** Both *pMem1 and *pMem2 contain string values. Compare the two values
64528
+** using the collation sequence pColl. As usual, return a negative , zero
64529
+** or positive value if *pMem1 is less than, equal to or greater than
64530
+** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
64531
+*/
64532
+static int vdbeCompareMemString(
64533
+ const Mem *pMem1,
64534
+ const Mem *pMem2,
64535
+ const CollSeq *pColl
64536
+){
64537
+ if( pMem1->enc==pColl->enc ){
64538
+ /* The strings are already in the correct encoding. Call the
64539
+ ** comparison function directly */
64540
+ return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
64541
+ }else{
64542
+ int rc;
64543
+ const void *v1, *v2;
64544
+ int n1, n2;
64545
+ Mem c1;
64546
+ Mem c2;
64547
+ memset(&c1, 0, sizeof(c1));
64548
+ memset(&c2, 0, sizeof(c2));
64549
+ sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
64550
+ sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
64551
+ v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
64552
+ n1 = v1==0 ? 0 : c1.n;
64553
+ v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
64554
+ n2 = v2==0 ? 0 : c2.n;
64555
+ rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
64556
+ sqlite3VdbeMemRelease(&c1);
64557
+ sqlite3VdbeMemRelease(&c2);
64558
+ return rc;
64559
+ }
64560
+}
64561
+
64562
+/*
64563
+** Compare the values contained by the two memory cells, returning
64564
+** negative, zero or positive if pMem1 is less than, equal to, or greater
64565
+** than pMem2. Sorting order is NULL's first, followed by numbers (integers
64566
+** and reals) sorted numerically, followed by text ordered by the collating
64567
+** sequence pColl and finally blob's ordered by memcmp().
64568
+**
64569
+** Two NULL values are considered equal by this function.
64570
+*/
64571
+SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
64572
+ int rc;
64573
+ int f1, f2;
64574
+ int combined_flags;
64575
+
64576
+ f1 = pMem1->flags;
64577
+ f2 = pMem2->flags;
64578
+ combined_flags = f1|f2;
64579
+ assert( (combined_flags & MEM_RowSet)==0 );
64580
+
64581
+ /* If one value is NULL, it is less than the other. If both values
64582
+ ** are NULL, return 0.
64583
+ */
64584
+ if( combined_flags&MEM_Null ){
64585
+ return (f2&MEM_Null) - (f1&MEM_Null);
64586
+ }
64587
+
64588
+ /* If one value is a number and the other is not, the number is less.
64589
+ ** If both are numbers, compare as reals if one is a real, or as integers
64590
+ ** if both values are integers.
64591
+ */
64592
+ if( combined_flags&(MEM_Int|MEM_Real) ){
64593
+ double r1, r2;
64594
+ if( (f1 & f2 & MEM_Int)!=0 ){
64595
+ if( pMem1->u.i < pMem2->u.i ) return -1;
64596
+ if( pMem1->u.i > pMem2->u.i ) return 1;
64597
+ return 0;
64598
+ }
64599
+ if( (f1&MEM_Real)!=0 ){
64600
+ r1 = pMem1->r;
64601
+ }else if( (f1&MEM_Int)!=0 ){
64602
+ r1 = (double)pMem1->u.i;
64603
+ }else{
64604
+ return 1;
64605
+ }
64606
+ if( (f2&MEM_Real)!=0 ){
64607
+ r2 = pMem2->r;
64608
+ }else if( (f2&MEM_Int)!=0 ){
64609
+ r2 = (double)pMem2->u.i;
64610
+ }else{
64611
+ return -1;
64612
+ }
64613
+ if( r1<r2 ) return -1;
64614
+ if( r1>r2 ) return 1;
64615
+ return 0;
64616
+ }
64617
+
64618
+ /* If one value is a string and the other is a blob, the string is less.
64619
+ ** If both are strings, compare using the collating functions.
64620
+ */
64621
+ if( combined_flags&MEM_Str ){
64622
+ if( (f1 & MEM_Str)==0 ){
64623
+ return 1;
64624
+ }
64625
+ if( (f2 & MEM_Str)==0 ){
64626
+ return -1;
64627
+ }
64628
+
64629
+ assert( pMem1->enc==pMem2->enc );
64630
+ assert( pMem1->enc==SQLITE_UTF8 ||
64631
+ pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
64632
+
64633
+ /* The collation sequence must be defined at this point, even if
64634
+ ** the user deletes the collation sequence after the vdbe program is
64635
+ ** compiled (this was not always the case).
64636
+ */
64637
+ assert( !pColl || pColl->xCmp );
64638
+
64639
+ if( pColl ){
64640
+ return vdbeCompareMemString(pMem1, pMem2, pColl);
64641
+ }
64642
+ /* If a NULL pointer was passed as the collate function, fall through
64643
+ ** to the blob case and use memcmp(). */
64644
+ }
64645
+
64646
+ /* Both values must be blobs. Compare using memcmp(). */
64647
+ rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
64648
+ if( rc==0 ){
64649
+ rc = pMem1->n - pMem2->n;
64650
+ }
64651
+ return rc;
64652
+}
64653
+
64654
+
64655
+/*
64656
+** The first argument passed to this function is a serial-type that
64657
+** corresponds to an integer - all values between 1 and 9 inclusive
64658
+** except 7. The second points to a buffer containing an integer value
64659
+** serialized according to serial_type. This function deserializes
64660
+** and returns the value.
64661
+*/
64662
+static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
64663
+ u32 y;
64664
+ assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
64665
+ switch( serial_type ){
64666
+ case 0:
64667
+ case 1:
64668
+ return ONE_BYTE_INT(aKey);
64669
+ case 2:
64670
+ return TWO_BYTE_INT(aKey);
64671
+ case 3:
64672
+ return THREE_BYTE_INT(aKey);
64673
+ case 4: {
64674
+ y = FOUR_BYTE_UINT(aKey);
64675
+ return (i64)*(int*)&y;
64676
+ }
64677
+ case 5: {
64678
+ return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64679
+ }
64680
+ case 6: {
64681
+ u64 x = FOUR_BYTE_UINT(aKey);
64682
+ x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64683
+ return (i64)*(i64*)&x;
64684
+ }
64685
+ }
64686
+
64687
+ return (serial_type - 8);
64688
+}
64689
+
64690
+/*
64691
+** This function compares the two table rows or index records
64692
+** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
64693
+** or positive integer if key1 is less than, equal to or
64694
+** greater than key2. The {nKey1, pKey1} key must be a blob
64695
+** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
64696
+** key must be a parsed key such as obtained from
64697
+** sqlite3VdbeParseRecord.
64698
+**
64699
+** If argument bSkip is non-zero, it is assumed that the caller has already
64700
+** determined that the first fields of the keys are equal.
64701
+**
64702
+** Key1 and Key2 do not have to contain the same number of fields. If all
64703
+** fields that appear in both keys are equal, then pPKey2->default_rc is
64704
+** returned.
64705
+*/
64706
+SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64707
+ int nKey1, const void *pKey1, /* Left key */
64708
+ const UnpackedRecord *pPKey2, /* Right key */
64709
+ int bSkip /* If true, skip the first field */
64710
+){
64711
+ u32 d1; /* Offset into aKey[] of next data element */
64712
+ int i; /* Index of next field to compare */
64713
+ int szHdr1; /* Size of record header in bytes */
64714
+ u32 idx1; /* Offset of first type in header */
64715
+ int rc = 0; /* Return value */
64716
+ Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
64717
+ KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
64718
+ const unsigned char *aKey1 = (const unsigned char *)pKey1;
64719
+ Mem mem1;
64720
+
64721
+ /* If bSkip is true, then the caller has already determined that the first
64722
+ ** two elements in the keys are equal. Fix the various stack variables so
64723
+ ** that this routine begins comparing at the second field. */
64724
+ if( bSkip ){
64725
+ u32 s1;
64726
+ idx1 = 1 + getVarint32(&aKey1[1], s1);
64727
+ szHdr1 = aKey1[0];
64728
+ d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
64729
+ i = 1;
64730
+ pRhs++;
64731
+ }else{
64732
+ idx1 = getVarint32(aKey1, szHdr1);
64733
+ d1 = szHdr1;
64734
+ i = 0;
64735
+ }
64736
+
64737
+ VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64738
+ assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
64739
+ || CORRUPT_DB );
64740
+ assert( pPKey2->pKeyInfo->aSortOrder!=0 );
64741
+ assert( pPKey2->pKeyInfo->nField>0 );
64742
+ assert( idx1<=szHdr1 || CORRUPT_DB );
64743
+ do{
64744
+ u32 serial_type;
64745
+
64746
+ /* RHS is an integer */
64747
+ if( pRhs->flags & MEM_Int ){
64748
+ serial_type = aKey1[idx1];
64749
+ if( serial_type>=12 ){
64750
+ rc = +1;
64751
+ }else if( serial_type==0 ){
64752
+ rc = -1;
64753
+ }else if( serial_type==7 ){
64754
+ double rhs = (double)pRhs->u.i;
64755
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64756
+ if( mem1.r<rhs ){
64757
+ rc = -1;
64758
+ }else if( mem1.r>rhs ){
64759
+ rc = +1;
64760
+ }
64761
+ }else{
64762
+ i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
64763
+ i64 rhs = pRhs->u.i;
64764
+ if( lhs<rhs ){
64765
+ rc = -1;
64766
+ }else if( lhs>rhs ){
64767
+ rc = +1;
64768
+ }
64769
+ }
64770
+ }
64771
+
64772
+ /* RHS is real */
64773
+ else if( pRhs->flags & MEM_Real ){
64774
+ serial_type = aKey1[idx1];
64775
+ if( serial_type>=12 ){
64776
+ rc = +1;
64777
+ }else if( serial_type==0 ){
64778
+ rc = -1;
64779
+ }else{
64780
+ double rhs = pRhs->r;
64781
+ double lhs;
64782
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64783
+ if( serial_type==7 ){
64784
+ lhs = mem1.r;
64785
+ }else{
64786
+ lhs = (double)mem1.u.i;
64787
+ }
64788
+ if( lhs<rhs ){
64789
+ rc = -1;
64790
+ }else if( lhs>rhs ){
64791
+ rc = +1;
64792
+ }
64793
+ }
64794
+ }
64795
+
64796
+ /* RHS is a string */
64797
+ else if( pRhs->flags & MEM_Str ){
64798
+ getVarint32(&aKey1[idx1], serial_type);
64799
+ if( serial_type<12 ){
64800
+ rc = -1;
64801
+ }else if( !(serial_type & 0x01) ){
64802
+ rc = +1;
64803
+ }else{
64804
+ mem1.n = (serial_type - 12) / 2;
64805
+ if( (d1+mem1.n) > (unsigned)nKey1 ){
64806
+ rc = 1; /* Corruption */
64807
+ }else if( pKeyInfo->aColl[i] ){
64808
+ mem1.enc = pKeyInfo->enc;
64809
+ mem1.db = pKeyInfo->db;
64810
+ mem1.flags = MEM_Str;
64811
+ mem1.z = (char*)&aKey1[d1];
64812
+ rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
64813
+ }else{
64814
+ int nCmp = MIN(mem1.n, pRhs->n);
64815
+ rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64816
+ if( rc==0 ) rc = mem1.n - pRhs->n;
64817
+ }
64818
+ }
64819
+ }
64820
+
64821
+ /* RHS is a blob */
64822
+ else if( pRhs->flags & MEM_Blob ){
64823
+ getVarint32(&aKey1[idx1], serial_type);
64824
+ if( serial_type<12 || (serial_type & 0x01) ){
64825
+ rc = -1;
64826
+ }else{
64827
+ int nStr = (serial_type - 12) / 2;
64828
+ if( (d1+nStr) > (unsigned)nKey1 ){
64829
+ rc = 1; /* Corruption */
64830
+ }else{
64831
+ int nCmp = MIN(nStr, pRhs->n);
64832
+ rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64833
+ if( rc==0 ) rc = nStr - pRhs->n;
64834
+ }
64835
+ }
64836
+ }
64837
+
64838
+ /* RHS is null */
64839
+ else{
64840
+ serial_type = aKey1[idx1];
64841
+ rc = (serial_type!=0);
64842
+ }
64843
+
64844
+ if( rc!=0 ){
64845
+ if( pKeyInfo->aSortOrder[i] ){
64846
+ rc = -rc;
64847
+ }
64848
+ assert( CORRUPT_DB
64849
+ || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64850
+ || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64851
+ );
64852
+ assert( mem1.zMalloc==0 ); /* See comment below */
64853
+ return rc;
64854
+ }
64855
+
64856
+ i++;
64857
+ pRhs++;
64858
+ d1 += sqlite3VdbeSerialTypeLen(serial_type);
64859
+ idx1 += sqlite3VarintLen(serial_type);
64860
+ }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
64861
+
64862
+ /* No memory allocation is ever used on mem1. Prove this using
64863
+ ** the following assert(). If the assert() fails, it indicates a
64864
+ ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
64865
+ assert( mem1.zMalloc==0 );
64866
+
64867
+ /* rc==0 here means that one or both of the keys ran out of fields and
64868
+ ** all the fields up to that point were equal. Return the the default_rc
64869
+ ** value. */
64870
+ assert( CORRUPT_DB
64871
+ || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
64872
+ );
64873
+ return pPKey2->default_rc;
64874
+}
64875
+
64876
+/*
64877
+** This function is an optimized version of sqlite3VdbeRecordCompare()
64878
+** that (a) the first field of pPKey2 is an integer, and (b) the
64879
+** size-of-header varint at the start of (pKey1/nKey1) fits in a single
64880
+** byte (i.e. is less than 128).
64881
+*/
64882
+static int vdbeRecordCompareInt(
64883
+ int nKey1, const void *pKey1, /* Left key */
64884
+ const UnpackedRecord *pPKey2, /* Right key */
64885
+ int bSkip /* Ignored */
64886
+){
64887
+ const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
64888
+ int serial_type = ((const u8*)pKey1)[1];
64889
+ int res;
64890
+ u32 y;
64891
+ u64 x;
64892
+ i64 v = pPKey2->aMem[0].u.i;
64893
+ i64 lhs;
64894
+ UNUSED_PARAMETER(bSkip);
64895
+
64896
+ assert( bSkip==0 );
64897
+ switch( serial_type ){
64898
+ case 1: { /* 1-byte signed integer */
64899
+ lhs = ONE_BYTE_INT(aKey);
64900
+ break;
64901
+ }
64902
+ case 2: { /* 2-byte signed integer */
64903
+ lhs = TWO_BYTE_INT(aKey);
64904
+ break;
64905
+ }
64906
+ case 3: { /* 3-byte signed integer */
64907
+ lhs = THREE_BYTE_INT(aKey);
64908
+ break;
64909
+ }
64910
+ case 4: { /* 4-byte signed integer */
64911
+ y = FOUR_BYTE_UINT(aKey);
64912
+ lhs = (i64)*(int*)&y;
64913
+ break;
64914
+ }
64915
+ case 5: { /* 6-byte signed integer */
64916
+ lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64917
+ break;
64918
+ }
64919
+ case 6: { /* 8-byte signed integer */
64920
+ x = FOUR_BYTE_UINT(aKey);
64921
+ x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64922
+ lhs = *(i64*)&x;
64923
+ break;
64924
+ }
64925
+ case 8:
64926
+ lhs = 0;
64927
+ break;
64928
+ case 9:
64929
+ lhs = 1;
64930
+ break;
64931
+
64932
+ /* This case could be removed without changing the results of running
64933
+ ** this code. Including it causes gcc to generate a faster switch
64934
+ ** statement (since the range of switch targets now starts at zero and
64935
+ ** is contiguous) but does not cause any duplicate code to be generated
64936
+ ** (as gcc is clever enough to combine the two like cases). Other
64937
+ ** compilers might be similar. */
64938
+ case 0: case 7:
64939
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64940
+
64941
+ default:
64942
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64943
+ }
64944
+
64945
+ if( v>lhs ){
64946
+ res = pPKey2->r1;
64947
+ }else if( v<lhs ){
64948
+ res = pPKey2->r2;
64949
+ }else if( pPKey2->nField>1 ){
64950
+ /* The first fields of the two keys are equal. Compare the trailing
64951
+ ** fields. */
64952
+ res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
64953
+ }else{
64954
+ /* The first fields of the two keys are equal and there are no trailing
64955
+ ** fields. Return pPKey2->default_rc in this case. */
64956
+ res = pPKey2->default_rc;
64957
+ }
64958
+
64959
+ assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
64960
+ || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64961
+ || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64962
+ || CORRUPT_DB
64963
+ );
64964
+ return res;
64965
+}
64966
+
64967
+/*
64968
+** This function is an optimized version of sqlite3VdbeRecordCompare()
64969
+** that (a) the first field of pPKey2 is a string, that (b) the first field
64970
+** uses the collation sequence BINARY and (c) that the size-of-header varint
64971
+** at the start of (pKey1/nKey1) fits in a single byte.
64972
+*/
64973
+static int vdbeRecordCompareString(
64974
+ int nKey1, const void *pKey1, /* Left key */
64975
+ const UnpackedRecord *pPKey2, /* Right key */
64976
+ int bSkip
64977
+){
64978
+ const u8 *aKey1 = (const u8*)pKey1;
64979
+ int serial_type;
64980
+ int res;
64981
+ UNUSED_PARAMETER(bSkip);
64982
+
64983
+ assert( bSkip==0 );
64984
+ getVarint32(&aKey1[1], serial_type);
64985
+
64986
+ if( serial_type<12 ){
64987
+ res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
64988
+ }else if( !(serial_type & 0x01) ){
64989
+ res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
64990
+ }else{
64991
+ int nCmp;
64992
+ int nStr;
64993
+ int szHdr = aKey1[0];
64994
+
64995
+ nStr = (serial_type-12) / 2;
64996
+ if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */
64997
+ nCmp = MIN( pPKey2->aMem[0].n, nStr );
64998
+ res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
64999
+
65000
+ if( res==0 ){
65001
+ res = nStr - pPKey2->aMem[0].n;
65002
+ if( res==0 ){
65003
+ if( pPKey2->nField>1 ){
65004
+ res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
65005
+ }else{
65006
+ res = pPKey2->default_rc;
65007
+ }
65008
+ }else if( res>0 ){
65009
+ res = pPKey2->r2;
65010
+ }else{
65011
+ res = pPKey2->r1;
65012
+ }
65013
+ }else if( res>0 ){
65014
+ res = pPKey2->r2;
65015
+ }else{
65016
+ res = pPKey2->r1;
65017
+ }
65018
+ }
65019
+
65020
+ assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65021
+ || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65022
+ || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65023
+ || CORRUPT_DB
65024
+ );
65025
+ return res;
65026
+}
65027
+
65028
+/*
65029
+** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
65030
+** suitable for comparing serialized records to the unpacked record passed
65031
+** as the only argument.
65032
+*/
65033
+SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
65034
+ /* varintRecordCompareInt() and varintRecordCompareString() both assume
65035
+ ** that the size-of-header varint that occurs at the start of each record
65036
+ ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
65037
+ ** also assumes that it is safe to overread a buffer by at least the
65038
+ ** maximum possible legal header size plus 8 bytes. Because there is
65039
+ ** guaranteed to be at least 74 (but not 136) bytes of padding following each
65040
+ ** buffer passed to varintRecordCompareInt() this makes it convenient to
65041
+ ** limit the size of the header to 64 bytes in cases where the first field
65042
+ ** is an integer.
65043
+ **
65044
+ ** The easiest way to enforce this limit is to consider only records with
65045
+ ** 13 fields or less. If the first field is an integer, the maximum legal
65046
+ ** header size is (12*5 + 1 + 1) bytes. */
65047
+ if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
65048
+ int flags = p->aMem[0].flags;
65049
+ if( p->pKeyInfo->aSortOrder[0] ){
65050
+ p->r1 = 1;
65051
+ p->r2 = -1;
65052
+ }else{
65053
+ p->r1 = -1;
65054
+ p->r2 = 1;
65055
+ }
65056
+ if( (flags & MEM_Int) ){
65057
+ return vdbeRecordCompareInt;
65058
+ }
65059
+ if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0
65060
+ && p->pKeyInfo->aColl[0]==0
65061
+ ){
65062
+ return vdbeRecordCompareString;
65063
+ }
65064
+ }
65065
+
65066
+ return sqlite3VdbeRecordCompare;
65067
+}
6461065068
6461165069
/*
6461265070
** pCur points at an index entry created using the OP_MakeRecord opcode.
6461365071
** Read the rowid (the last field in the record) and store it in *rowid.
6461465072
** Return SQLITE_OK if everything works, or an error code otherwise.
@@ -64695,23 +65153,23 @@
6469565153
** omits the rowid at the end. The rowid at the end of the index entry
6469665154
** is ignored as well. Hence, this routine only compares the prefixes
6469765155
** of the keys prior to the final rowid, not the entire key.
6469865156
*/
6469965157
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 */
65158
+ VdbeCursor *pC, /* The cursor to compare against */
65159
+ const UnpackedRecord *pUnpacked, /* Unpacked version of key */
65160
+ int *res /* Write the comparison result here */
6470365161
){
6470465162
i64 nCellKey = 0;
6470565163
int rc;
6470665164
BtCursor *pCur = pC->pCursor;
6470765165
Mem m;
6470865166
6470965167
assert( sqlite3BtreeCursorIsValid(pCur) );
6471065168
VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
6471165169
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
65170
+ /* nCellKey will always be between 0 and 0xffffffff because of the way
6471365171
** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6471465172
if( nCellKey<=0 || nCellKey>0x7fffffff ){
6471565173
*res = 0;
6471665174
return SQLITE_CORRUPT_BKPT;
6471765175
}
@@ -64718,12 +65176,11 @@
6471865176
memset(&m, 0, sizeof(m));
6471965177
rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
6472065178
if( rc ){
6472165179
return rc;
6472265180
}
64723
- assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
64724
- *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
65181
+ *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
6472565182
sqlite3VdbeMemRelease(&m);
6472665183
return SQLITE_OK;
6472765184
}
6472865185
6472965186
/*
@@ -66629,11 +67086,11 @@
6662967086
** does not control the string, it might be deleted without the register
6663067087
** knowing it.
6663167088
**
6663267089
** This routine converts an ephemeral string into a dynamically allocated
6663367090
** string that the register itself controls. In other words, it
66634
-** converts an MEM_Ephem string into an MEM_Dyn string.
67091
+** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
6663567092
*/
6663667093
#define Deephemeralize(P) \
6663767094
if( ((P)->flags&MEM_Ephem)!=0 \
6663867095
&& sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
6663967096
@@ -67166,22 +67623,25 @@
6716667623
#ifdef SQLITE_DEBUG
6716767624
if( (pOp->opflags & OPFLG_IN1)!=0 ){
6716867625
assert( pOp->p1>0 );
6716967626
assert( pOp->p1<=(p->nMem-p->nCursor) );
6717067627
assert( memIsValid(&aMem[pOp->p1]) );
67628
+ assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
6717167629
REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
6717267630
}
6717367631
if( (pOp->opflags & OPFLG_IN2)!=0 ){
6717467632
assert( pOp->p2>0 );
6717567633
assert( pOp->p2<=(p->nMem-p->nCursor) );
6717667634
assert( memIsValid(&aMem[pOp->p2]) );
67635
+ assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
6717767636
REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
6717867637
}
6717967638
if( (pOp->opflags & OPFLG_IN3)!=0 ){
6718067639
assert( pOp->p3>0 );
6718167640
assert( pOp->p3<=(p->nMem-p->nCursor) );
6718267641
assert( memIsValid(&aMem[pOp->p3]) );
67642
+ assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
6718367643
REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
6718467644
}
6718567645
if( (pOp->opflags & OPFLG_OUT2)!=0 ){
6718667646
assert( pOp->p2>0 );
6718767647
assert( pOp->p2<=(p->nMem-p->nCursor) );
@@ -67279,11 +67739,11 @@
6727967739
** and then jump to address P2.
6728067740
*/
6728167741
case OP_Gosub: { /* jump */
6728267742
assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
6728367743
pIn1 = &aMem[pOp->p1];
67284
- assert( (pIn1->flags & MEM_Dyn)==0 );
67744
+ assert( VdbeMemDynamic(pIn1)==0 );
6728567745
memAboutToChange(p, pIn1);
6728667746
pIn1->flags = MEM_Int;
6728767747
pIn1->u.i = pc;
6728867748
REGISTER_TRACE(pOp->p1, pIn1);
6728967749
pc = pOp->p2 - 1;
@@ -67352,11 +67812,11 @@
6735267812
** OP_EndCoroutine, jump immediately to P2.
6735367813
*/
6735467814
case OP_Yield: { /* in1, jump */
6735567815
int pcDest;
6735667816
pIn1 = &aMem[pOp->p1];
67357
- assert( (pIn1->flags & MEM_Dyn)==0 );
67817
+ assert( VdbeMemDynamic(pIn1)==0 );
6735867818
pIn1->flags = MEM_Int;
6735967819
pcDest = (int)pIn1->u.i;
6736067820
pIn1->u.i = pc;
6736167821
REGISTER_TRACE(pOp->p1, pIn1);
6736267822
pc = pcDest;
@@ -67525,14 +67985,13 @@
6752567985
if( encoding!=SQLITE_UTF8 ){
6752667986
rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
6752767987
if( rc==SQLITE_TOOBIG ) goto too_big;
6752867988
if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
6752967989
assert( pOut->zMalloc==pOut->z );
67530
- assert( pOut->flags & MEM_Dyn );
67990
+ assert( VdbeMemDynamic(pOut)==0 );
6753167991
pOut->zMalloc = 0;
6753267992
pOut->flags |= MEM_Static;
67533
- pOut->flags &= ~MEM_Dyn;
6753467993
if( pOp->p4type==P4_DYNAMIC ){
6753567994
sqlite3DbFree(db, pOp->p4.z);
6753667995
}
6753767996
pOp->p4type = P4_DYNAMIC;
6753867997
pOp->p4.z = pOut->z;
@@ -67664,18 +68123,20 @@
6766468123
do{
6766568124
assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
6766668125
assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
6766768126
assert( memIsValid(pIn1) );
6766868127
memAboutToChange(p, pOut);
68128
+ VdbeMemRelease(pOut);
6766968129
zMalloc = pOut->zMalloc;
67670
- pOut->zMalloc = 0;
67671
- sqlite3VdbeMemMove(pOut, pIn1);
68130
+ memcpy(pOut, pIn1, sizeof(Mem));
6767268131
#ifdef SQLITE_DEBUG
6767368132
if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
6767468133
pOut->pScopyFrom += p1 - pOp->p2;
6767568134
}
6767668135
#endif
68136
+ pIn1->flags = MEM_Undefined;
68137
+ pIn1->xDel = 0;
6767768138
pIn1->zMalloc = zMalloc;
6767868139
REGISTER_TRACE(p2++, pOut);
6767968140
pIn1++;
6768068141
pOut++;
6768168142
}while( n-- );
@@ -67848,14 +68309,14 @@
6784868309
Stringify(pIn2, encoding);
6784968310
nByte = pIn1->n + pIn2->n;
6785068311
if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6785168312
goto too_big;
6785268313
}
67853
- MemSetTypeFlag(pOut, MEM_Str);
6785468314
if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
6785568315
goto no_mem;
6785668316
}
68317
+ MemSetTypeFlag(pOut, MEM_Str);
6785768318
if( pOut!=pIn2 ){
6785868319
memcpy(pOut->z, pIn2->z, pIn2->n);
6785968320
}
6786068321
memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
6786168322
pOut->z[nByte]=0;
@@ -69026,10 +69487,11 @@
6902669487
** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
6902769488
** all valid.
6902869489
*/
6902969490
assert( p2<pC->nHdrParsed );
6903069491
assert( rc==SQLITE_OK );
69492
+ assert( sqlite3VdbeCheckMemInvariants(pDest) );
6903169493
if( pC->szRow>=aOffset[p2+1] ){
6903269494
/* This is the common case where the desired content fits on the original
6903369495
** page - where the content is not on an overflow page */
6903469496
VdbeMemRelease(pDest);
6903569497
sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
@@ -69063,12 +69525,12 @@
6906369525
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
6906469526
** dynamically allocated space over to the pDest structure.
6906569527
** This prevents a memory copy. */
6906669528
if( sMem.zMalloc ){
6906769529
assert( sMem.z==sMem.zMalloc );
69068
- assert( !(pDest->flags & MEM_Dyn) );
69069
- assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
69530
+ assert( VdbeMemDynamic(pDest)==0 );
69531
+ assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
6907069532
pDest->flags &= ~(MEM_Ephem|MEM_Static);
6907169533
pDest->flags |= MEM_Term;
6907269534
pDest->z = sMem.z;
6907369535
pDest->zMalloc = sMem.zMalloc;
6907469536
}
@@ -69247,11 +69709,11 @@
6924769709
assert( i==nHdr );
6924869710
assert( j==nByte );
6924969711
6925069712
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6925169713
pOut->n = (int)nByte;
69252
- pOut->flags = MEM_Blob | MEM_Dyn;
69714
+ pOut->flags = MEM_Blob;
6925369715
pOut->xDel = 0;
6925469716
if( nZero ){
6925569717
pOut->u.nZero = nZero;
6925669718
pOut->flags |= MEM_Zero;
6925769719
}
@@ -70121,20 +70583,20 @@
7012170583
r.pKeyInfo = pC->pKeyInfo;
7012270584
r.nField = (u16)nField;
7012370585
7012470586
/* The next line of code computes as follows, only faster:
7012570587
** if( oc==OP_SeekGT || oc==OP_SeekLE ){
70126
- ** r.flags = UNPACKED_INCRKEY;
70588
+ ** r.default_rc = -1;
7012770589
** }else{
70128
- ** r.flags = 0;
70590
+ ** r.default_rc = +1;
7012970591
** }
7013070592
*/
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 );
70593
+ r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
70594
+ assert( oc!=OP_SeekGT || r.default_rc==-1 );
70595
+ assert( oc!=OP_SeekLE || r.default_rc==-1 );
70596
+ assert( oc!=OP_SeekGE || r.default_rc==+1 );
70597
+ assert( oc!=OP_SeekLT || r.default_rc==+1 );
7013670598
7013770599
r.aMem = &aMem[pOp->p3];
7013870600
#ifdef SQLITE_DEBUG
7013970601
{ int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
7014070602
#endif
@@ -70288,22 +70750,21 @@
7028870750
ExpandBlob(&r.aMem[ii]);
7028970751
#ifdef SQLITE_DEBUG
7029070752
if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
7029170753
#endif
7029270754
}
70293
- r.flags = UNPACKED_PREFIX_MATCH;
7029470755
pIdxKey = &r;
7029570756
}else{
7029670757
pIdxKey = sqlite3VdbeAllocUnpackedRecord(
7029770758
pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
7029870759
);
7029970760
if( pIdxKey==0 ) goto no_mem;
7030070761
assert( pIn3->flags & MEM_Blob );
7030170762
assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
7030270763
sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
70303
- pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
7030470764
}
70765
+ pIdxKey->default_rc = 0;
7030570766
if( pOp->opcode==OP_NoConflict ){
7030670767
/* For the OP_NoConflict opcode, take the jump if any of the
7030770768
** input fields are NULL, since any key with a NULL will not
7030870769
** conflict */
7030970770
for(ii=0; ii<r.nField; ii++){
@@ -71188,11 +71649,11 @@
7118871649
pCrsr = pC->pCursor;
7118971650
assert( pCrsr!=0 );
7119071651
assert( pOp->p5==0 );
7119171652
r.pKeyInfo = pC->pKeyInfo;
7119271653
r.nField = (u16)pOp->p3;
71193
- r.flags = UNPACKED_PREFIX_MATCH;
71654
+ r.default_rc = 0;
7119471655
r.aMem = &aMem[pOp->p2];
7119571656
#ifdef SQLITE_DEBUG
7119671657
{ int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
7119771658
#endif
7119871659
rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
@@ -71302,14 +71763,14 @@
7130271763
assert( pOp->p4type==P4_INT32 );
7130371764
r.pKeyInfo = pC->pKeyInfo;
7130471765
r.nField = (u16)pOp->p4.i;
7130571766
if( pOp->opcode<OP_IdxLT ){
7130671767
assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71307
- r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
71768
+ r.default_rc = -1;
7130871769
}else{
7130971770
assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71310
- r.flags = UNPACKED_PREFIX_MATCH;
71771
+ r.default_rc = 0;
7131171772
}
7131271773
r.aMem = &aMem[pOp->p3];
7131371774
#ifdef SQLITE_DEBUG
7131471775
{ int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
7131571776
#endif
@@ -73809,14 +74270,14 @@
7380974270
if( r2->aMem[i].flags & MEM_Null ){
7381074271
*pRes = -1;
7381174272
return;
7381274273
}
7381374274
}
73814
- r2->flags |= UNPACKED_PREFIX_MATCH;
74275
+ assert( r2->default_rc==0 );
7381574276
}
7381674277
73817
- *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
74278
+ *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
7381874279
}
7381974280
7382074281
/*
7382174282
** This function is called to compare two iterator keys when merging
7382274283
** multiple b-tree segments. Parameter iOut is the index of the aTree[]
@@ -100117,11 +100578,11 @@
100117100578
}else if( eDest!=SRT_Exists ){
100118100579
/* If the destination is an EXISTS(...) expression, the actual
100119100580
** values returned by the SELECT are not required.
100120100581
*/
100121100582
sqlite3ExprCodeExprList(pParse, pEList, regResult,
100122
- (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
100583
+ (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
100123100584
}
100124100585
100125100586
/* If the DISTINCT keyword was present on the SELECT statement
100126100587
** and this row has been seen before, then do not make this row
100127100588
** part of the result.
@@ -110767,11 +111228,11 @@
110767111228
iCol = pRec->nField - 1;
110768111229
assert( pIdx->nSample>0 );
110769111230
assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
110770111231
do{
110771111232
iTest = (iMin+i)/2;
110772
- res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
111233
+ res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
110773111234
if( res<0 ){
110774111235
iMin = iTest+1;
110775111236
}else{
110776111237
i = iTest;
110777111238
}
@@ -110782,20 +111243,20 @@
110782111243
** above found the right answer. This block serves no purpose other
110783111244
** than to invoke the asserts. */
110784111245
if( res==0 ){
110785111246
/* If (res==0) is true, then sample $i must be equal to pRec */
110786111247
assert( i<pIdx->nSample );
110787
- assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
111248
+ assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
110788111249
|| pParse->db->mallocFailed );
110789111250
}else{
110790111251
/* Otherwise, pRec must be smaller than sample $i and larger than
110791111252
** sample ($i-1). */
110792111253
assert( i==pIdx->nSample
110793
- || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
111254
+ || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
110794111255
|| pParse->db->mallocFailed );
110795111256
assert( i==0
110796
- || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
111257
+ || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
110797111258
|| pParse->db->mallocFailed );
110798111259
}
110799111260
#endif /* ifdef SQLITE_DEBUG */
110800111261
110801111262
/* At this point, aSample[i] is the first sample that is greater than
@@ -114724,11 +115185,11 @@
114724115185
114725115186
/* For a co-routine, change all OP_Column references to the table of
114726115187
** the co-routine into OP_SCopy of result contained in a register.
114727115188
** OP_Rowid becomes OP_Null.
114728115189
*/
114729
- if( pTabItem->viaCoroutine ){
115190
+ if( pTabItem->viaCoroutine && !db->mallocFailed ){
114730115191
last = sqlite3VdbeCurrentAddr(v);
114731115192
k = pLevel->addrBody;
114732115193
pOp = sqlite3VdbeGetOp(v, k);
114733115194
for(; k<last; k++, pOp++){
114734115195
if( pOp->p1!=pLevel->iTabCur ) continue;
114735115196
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -189,11 +189,11 @@
189 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
190 ** [sqlite_version()] and [sqlite_source_id()].
191 */
192 #define SQLITE_VERSION "3.8.4"
193 #define SQLITE_VERSION_NUMBER 3008004
194 #define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a"
195
196 /*
197 ** CAPI3REF: Run-Time Library Version Numbers
198 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
199 **
@@ -9334,12 +9334,15 @@
9334 #ifndef SQLITE_OMIT_TRACE
9335 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9336 #endif
9337
9338 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9339 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
9340 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
 
 
 
9341
9342 #ifndef SQLITE_OMIT_TRIGGER
9343 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
9344 #endif
9345
@@ -10950,23 +10953,23 @@
10950 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10951 ** OP_Column opcode.
10952 **
10953 ** This structure holds a record that has already been disassembled
10954 ** into its constituent fields.
 
 
 
10955 */
10956 struct UnpackedRecord {
10957 KeyInfo *pKeyInfo; /* Collation and sort-order information */
10958 u16 nField; /* Number of entries in apMem[] */
10959 u8 flags; /* Boolean settings. UNPACKED_... below */
10960 Mem *aMem; /* Values */
 
 
10961 };
10962
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
10969 /*
10970 ** Each SQL index is represented in memory by an
10971 ** instance of the following structure.
10972 **
@@ -13840,11 +13843,11 @@
13840 ** the following flags must be set to determine the memory management
13841 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
13842 ** string is \000 or \u0000 terminated
13843 */
13844 #define MEM_Term 0x0200 /* String rep is nul terminated */
13845 #define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */
13846 #define MEM_Static 0x0800 /* Mem.z points to a static string */
13847 #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
13848 #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
13849 #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
13850 #ifdef SQLITE_OMIT_INCRBLOB
@@ -14023,11 +14026,11 @@
14023 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
14024 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
14025 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14026
14027 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14028 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
14029 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14030 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14031 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14032 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14033 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
@@ -14088,10 +14091,11 @@
14088 # define sqlite3VdbeLeave(X)
14089 #endif
14090
14091 #ifdef SQLITE_DEBUG
14092 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
 
14093 #endif
14094
14095 #ifndef SQLITE_OMIT_FOREIGN_KEY
14096 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
14097 #else
@@ -21471,11 +21475,11 @@
21471 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
21472
21473 sqlite3VdbeMemRelease(pMem);
21474 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
21475 pMem->enc = desiredEnc;
21476 pMem->flags |= (MEM_Term|MEM_Dyn);
21477 pMem->z = (char*)zOut;
21478 pMem->zMalloc = pMem->z;
21479
21480 translate_out:
21481 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
@@ -21599,11 +21603,10 @@
21599 sqlite3VdbeMemRelease(&m);
21600 m.z = 0;
21601 }
21602 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
21603 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
21604 assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21605 assert( m.z || db->mallocFailed );
21606 return m.z;
21607 }
21608
21609 /*
@@ -55305,10 +55308,11 @@
55305 i64 intKey, /* The table key */
55306 int biasRight, /* If true, bias the search to the high end */
55307 int *pRes /* Write search results here */
55308 ){
55309 int rc;
 
55310
55311 assert( cursorHoldsMutex(pCur) );
55312 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55313 assert( pRes );
55314 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
@@ -55325,10 +55329,20 @@
55325 if( pCur->atLast && pCur->info.nKey<intKey ){
55326 *pRes = -1;
55327 return SQLITE_OK;
55328 }
55329 }
 
 
 
 
 
 
 
 
 
 
55330
55331 rc = moveToRoot(pCur);
55332 if( rc ){
55333 return rc;
55334 }
@@ -55410,18 +55424,18 @@
55410 if( nCell<=pPage->max1bytePayload ){
55411 /* This branch runs if the record-size field of the cell is a
55412 ** single byte varint and the record fits entirely on the main
55413 ** b-tree page. */
55414 testcase( pCell+nCell+1==pPage->aDataEnd );
55415 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
55416 }else if( !(pCell[1] & 0x80)
55417 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
55418 ){
55419 /* The record-size field is a 2 byte varint and the record
55420 ** fits entirely on the main b-tree page. */
55421 testcase( pCell+nCell+2==pPage->aDataEnd );
55422 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
55423 }else{
55424 /* The record flows over onto one or more overflow pages. In
55425 ** this case the whole cell needs to be parsed, a buffer allocated
55426 ** and accessPayload() used to retrieve the record into the
55427 ** buffer before VdbeRecordCompare() can be called. */
@@ -55438,11 +55452,11 @@
55438 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
55439 if( rc ){
55440 sqlite3_free(pCellKey);
55441 goto moveto_finish;
55442 }
55443 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
55444 sqlite3_free(pCellKey);
55445 }
55446 if( c<0 ){
55447 lwr = idx+1;
55448 }else if( c>0 ){
@@ -60001,10 +60015,46 @@
60001 ** This file contains code use to manipulate "Mem" structure. A "Mem"
60002 ** stores a single value in the VDBE. Mem is an opaque structure visible
60003 ** only within the VDBE. Interface routines refer to a Mem using the
60004 ** name sqlite_value
60005 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60006
60007 /*
60008 ** If pMem is an object with a valid string representation, this routine
60009 ** ensures the internal encoding for the string representation is
60010 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -60051,16 +60101,11 @@
60051 ** pMem->z into the new allocation. pMem must be either a string or
60052 ** blob if bPreserve is true. If bPreserve is false, any prior content
60053 ** in pMem->z is discarded.
60054 */
60055 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 );
60062 assert( (pMem->flags&MEM_RowSet)==0 );
60063
60064 /* If the bPreserve flag is set to true, then the memory cell must already
60065 ** contain a valid string or blob value. */
60066 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
@@ -60074,26 +60119,26 @@
60074 }else{
60075 sqlite3DbFree(pMem->db, pMem->zMalloc);
60076 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
60077 }
60078 if( pMem->zMalloc==0 ){
60079 sqlite3VdbeMemRelease(pMem);
60080 pMem->flags = MEM_Null;
60081 return SQLITE_NOMEM;
60082 }
60083 }
60084
60085 if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
60086 memcpy(pMem->zMalloc, pMem->z, pMem->n);
60087 }
60088 if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){
60089 assert( pMem->xDel!=SQLITE_DYNAMIC );
60090 pMem->xDel((void *)(pMem->z));
60091 }
60092
60093 pMem->z = pMem->zMalloc;
60094 pMem->flags &= ~(MEM_Ephem|MEM_Static);
60095 pMem->xDel = 0;
60096 return SQLITE_OK;
60097 }
60098
60099 /*
@@ -60258,13 +60303,13 @@
60258 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
60259 if( p->flags&MEM_Agg ){
60260 sqlite3VdbeMemFinalize(p, p->u.pDef);
60261 assert( (p->flags & MEM_Agg)==0 );
60262 sqlite3VdbeMemRelease(p);
60263 }else if( p->flags&MEM_Dyn && p->xDel ){
60264 assert( (p->flags&MEM_RowSet)==0 );
60265 assert( p->xDel!=SQLITE_DYNAMIC );
60266 p->xDel((void *)p->z);
60267 p->xDel = 0;
60268 }else if( p->flags&MEM_RowSet ){
60269 sqlite3RowSetClear(p->u.pRowSet);
60270 }else if( p->flags&MEM_Frame ){
@@ -60276,10 +60321,11 @@
60276 ** Release any memory held by the Mem. This may leave the Mem in an
60277 ** inconsistent state, for example with (Mem.z==0) and
60278 ** (Mem.memType==MEM_Str).
60279 */
60280 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
 
60281 VdbeMemRelease(p);
60282 if( p->zMalloc ){
60283 sqlite3DbFree(p->db, p->zMalloc);
60284 p->zMalloc = 0;
60285 }
@@ -60613,10 +60659,11 @@
60613
60614 assert( (pFrom->flags & MEM_RowSet)==0 );
60615 VdbeMemRelease(pTo);
60616 memcpy(pTo, pFrom, MEMCELLSIZE);
60617 pTo->flags &= ~MEM_Dyn;
 
60618
60619 if( pTo->flags&(MEM_Str|MEM_Blob) ){
60620 if( 0==(pFrom->flags&MEM_Static) ){
60621 pTo->flags |= MEM_Ephem;
60622 rc = sqlite3VdbeMemMakeWriteable(pTo);
@@ -60738,123 +60785,10 @@
60738 }
60739
60740 return SQLITE_OK;
60741 }
60742
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 /*
60857 ** Move data out of a btree key or data field and into a Mem structure.
60858 ** The data or key is taken from the entry that pCur is currently pointing
60859 ** to. offset and amt determine what portion of the data or key to retrieve.
60860 ** key is true to get the key or false to get data. The result is written
@@ -60892,11 +60826,11 @@
60892 if( offset+amt<=available ){
60893 sqlite3VdbeMemRelease(pMem);
60894 pMem->z = &zData[offset];
60895 pMem->flags = MEM_Blob|MEM_Ephem;
60896 }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 if( key ){
60901 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
60902 }else{
@@ -61010,11 +60944,10 @@
61010 if( pRec ){
61011 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
61012 if( pRec->pKeyInfo ){
61013 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
61014 assert( pRec->pKeyInfo->enc==ENC(db) );
61015 pRec->flags = UNPACKED_PREFIX_MATCH;
61016 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
61017 for(i=0; i<nCol; i++){
61018 pRec->aMem[i].flags = MEM_Null;
61019 pRec->aMem[i].memType = MEM_Null;
61020 pRec->aMem[i].db = db;
@@ -62591,10 +62524,11 @@
62591 }
62592 return;
62593 }
62594 for(pEnd=&p[N]; p<pEnd; p++){
62595 assert( (&p[1])==pEnd || p[0].db==p[1].db );
 
62596
62597 /* This block is really an inlined version of sqlite3VdbeMemRelease()
62598 ** that takes advantage of the fact that the memory cell value is
62599 ** being set to NULL after releasing any dynamic resources.
62600 **
@@ -62784,11 +62718,11 @@
62784
62785 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
62786 assert( p->db->mallocFailed );
62787 return SQLITE_ERROR;
62788 }
62789 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62790 zP4 = displayP4(pOp, pMem->z, 32);
62791 if( zP4!=pMem->z ){
62792 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
62793 }else{
62794 assert( pMem->z!=0 );
@@ -62801,11 +62735,11 @@
62801 if( p->explain==1 ){
62802 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
62803 assert( p->db->mallocFailed );
62804 return SQLITE_ERROR;
62805 }
62806 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62807 pMem->n = 2;
62808 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
62809 pMem->memType = MEM_Str;
62810 pMem->enc = SQLITE_UTF8;
62811 pMem++;
@@ -62813,11 +62747,11 @@
62813 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62814 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
62815 assert( p->db->mallocFailed );
62816 return SQLITE_ERROR;
62817 }
62818 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62819 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
62820 pMem->memType = MEM_Str;
62821 pMem->enc = SQLITE_UTF8;
62822 #else
62823 pMem->flags = MEM_Null; /* Comment */
@@ -64312,10 +64246,18 @@
64312
64313 /* NULL or constants 0 or 1 */
64314 return 0;
64315 }
64316
 
 
 
 
 
 
 
 
64317 /*
64318 ** Deserialize the data blob pointed to by buf as serial type serial_type
64319 ** and store the result in pMem. Return the number of bytes read.
64320 */
64321 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
@@ -64323,46 +64265,40 @@
64323 u32 serial_type, /* Serial type to deserialize */
64324 Mem *pMem /* Memory cell to write value into */
64325 ){
64326 u64 x;
64327 u32 y;
64328 int i;
64329 switch( serial_type ){
64330 case 10: /* Reserved for future use */
64331 case 11: /* Reserved for future use */
64332 case 0: { /* NULL */
64333 pMem->flags = MEM_Null;
64334 break;
64335 }
64336 case 1: { /* 1-byte signed integer */
64337 pMem->u.i = (signed char)buf[0];
64338 pMem->flags = MEM_Int;
64339 return 1;
64340 }
64341 case 2: { /* 2-byte signed integer */
64342 i = 256*(signed char)buf[0] | buf[1];
64343 pMem->u.i = (i64)i;
64344 pMem->flags = MEM_Int;
64345 return 2;
64346 }
64347 case 3: { /* 3-byte signed integer */
64348 i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
64349 pMem->u.i = (i64)i;
64350 pMem->flags = MEM_Int;
64351 return 3;
64352 }
64353 case 4: { /* 4-byte signed integer */
64354 y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
64355 pMem->u.i = (i64)*(int*)&y;
64356 pMem->flags = MEM_Int;
64357 return 4;
64358 }
64359 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;
64364 pMem->flags = MEM_Int;
64365 return 6;
64366 }
64367 case 6: /* 8-byte signed integer */
64368 case 7: { /* IEEE floating point */
@@ -64376,12 +64312,12 @@
64376 static const double r1 = 1.0;
64377 u64 t2 = t1;
64378 swapMixedEndianFloat(t2);
64379 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
64380 #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];
64383 x = (x<<32) | y;
64384 if( serial_type==6 ){
64385 pMem->u.i = *(i64*)&x;
64386 pMem->flags = MEM_Int;
64387 }else{
@@ -64473,11 +64409,11 @@
64473 u32 idx; /* Offset in aKey[] to read from */
64474 u16 u; /* Unsigned loop counter */
64475 u32 szHdr;
64476 Mem *pMem = p->aMem;
64477
64478 p->flags = 0;
64479 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64480 idx = getVarint32(aKey, szHdr);
64481 d = szHdr;
64482 u = 0;
64483 while( idx<szHdr && u<p->nField && d<=nKey ){
@@ -64494,30 +64430,22 @@
64494 }
64495 assert( u<=pKeyInfo->nField + 1 );
64496 p->nField = u;
64497 }
64498
 
64499 /*
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.
64515 */
64516 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64517 int nKey1, const void *pKey1, /* Left key */
64518 UnpackedRecord *pPKey2 /* Right key */
64519 ){
64520 u32 d1; /* Offset into aKey[] of next data element */
64521 u32 idx1; /* Offset into aKey[] of next header element */
64522 u32 szHdr1; /* Number of bytes in header */
64523 int i = 0;
@@ -64587,28 +64515,558 @@
64587 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
64588 */
64589 assert( mem1.zMalloc==0 );
64590
64591 /* 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64610
64611 /*
64612 ** pCur points at an index entry created using the OP_MakeRecord opcode.
64613 ** Read the rowid (the last field in the record) and store it in *rowid.
64614 ** Return SQLITE_OK if everything works, or an error code otherwise.
@@ -64695,23 +65153,23 @@
64695 ** omits the rowid at the end. The rowid at the end of the index entry
64696 ** is ignored as well. Hence, this routine only compares the prefixes
64697 ** of the keys prior to the final rowid, not the entire key.
64698 */
64699 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 */
64703 ){
64704 i64 nCellKey = 0;
64705 int rc;
64706 BtCursor *pCur = pC->pCursor;
64707 Mem m;
64708
64709 assert( sqlite3BtreeCursorIsValid(pCur) );
64710 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
64711 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
64713 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
64714 if( nCellKey<=0 || nCellKey>0x7fffffff ){
64715 *res = 0;
64716 return SQLITE_CORRUPT_BKPT;
64717 }
@@ -64718,12 +65176,11 @@
64718 memset(&m, 0, sizeof(m));
64719 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
64720 if( rc ){
64721 return rc;
64722 }
64723 assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
64724 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
64725 sqlite3VdbeMemRelease(&m);
64726 return SQLITE_OK;
64727 }
64728
64729 /*
@@ -66629,11 +67086,11 @@
66629 ** does not control the string, it might be deleted without the register
66630 ** knowing it.
66631 **
66632 ** This routine converts an ephemeral string into a dynamically allocated
66633 ** string that the register itself controls. In other words, it
66634 ** converts an MEM_Ephem string into an MEM_Dyn string.
66635 */
66636 #define Deephemeralize(P) \
66637 if( ((P)->flags&MEM_Ephem)!=0 \
66638 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
66639
@@ -67166,22 +67623,25 @@
67166 #ifdef SQLITE_DEBUG
67167 if( (pOp->opflags & OPFLG_IN1)!=0 ){
67168 assert( pOp->p1>0 );
67169 assert( pOp->p1<=(p->nMem-p->nCursor) );
67170 assert( memIsValid(&aMem[pOp->p1]) );
 
67171 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
67172 }
67173 if( (pOp->opflags & OPFLG_IN2)!=0 ){
67174 assert( pOp->p2>0 );
67175 assert( pOp->p2<=(p->nMem-p->nCursor) );
67176 assert( memIsValid(&aMem[pOp->p2]) );
 
67177 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
67178 }
67179 if( (pOp->opflags & OPFLG_IN3)!=0 ){
67180 assert( pOp->p3>0 );
67181 assert( pOp->p3<=(p->nMem-p->nCursor) );
67182 assert( memIsValid(&aMem[pOp->p3]) );
 
67183 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
67184 }
67185 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
67186 assert( pOp->p2>0 );
67187 assert( pOp->p2<=(p->nMem-p->nCursor) );
@@ -67279,11 +67739,11 @@
67279 ** and then jump to address P2.
67280 */
67281 case OP_Gosub: { /* jump */
67282 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
67283 pIn1 = &aMem[pOp->p1];
67284 assert( (pIn1->flags & MEM_Dyn)==0 );
67285 memAboutToChange(p, pIn1);
67286 pIn1->flags = MEM_Int;
67287 pIn1->u.i = pc;
67288 REGISTER_TRACE(pOp->p1, pIn1);
67289 pc = pOp->p2 - 1;
@@ -67352,11 +67812,11 @@
67352 ** OP_EndCoroutine, jump immediately to P2.
67353 */
67354 case OP_Yield: { /* in1, jump */
67355 int pcDest;
67356 pIn1 = &aMem[pOp->p1];
67357 assert( (pIn1->flags & MEM_Dyn)==0 );
67358 pIn1->flags = MEM_Int;
67359 pcDest = (int)pIn1->u.i;
67360 pIn1->u.i = pc;
67361 REGISTER_TRACE(pOp->p1, pIn1);
67362 pc = pcDest;
@@ -67525,14 +67985,13 @@
67525 if( encoding!=SQLITE_UTF8 ){
67526 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
67527 if( rc==SQLITE_TOOBIG ) goto too_big;
67528 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
67529 assert( pOut->zMalloc==pOut->z );
67530 assert( pOut->flags & MEM_Dyn );
67531 pOut->zMalloc = 0;
67532 pOut->flags |= MEM_Static;
67533 pOut->flags &= ~MEM_Dyn;
67534 if( pOp->p4type==P4_DYNAMIC ){
67535 sqlite3DbFree(db, pOp->p4.z);
67536 }
67537 pOp->p4type = P4_DYNAMIC;
67538 pOp->p4.z = pOut->z;
@@ -67664,18 +68123,20 @@
67664 do{
67665 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
67666 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
67667 assert( memIsValid(pIn1) );
67668 memAboutToChange(p, pOut);
 
67669 zMalloc = pOut->zMalloc;
67670 pOut->zMalloc = 0;
67671 sqlite3VdbeMemMove(pOut, pIn1);
67672 #ifdef SQLITE_DEBUG
67673 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
67674 pOut->pScopyFrom += p1 - pOp->p2;
67675 }
67676 #endif
 
 
67677 pIn1->zMalloc = zMalloc;
67678 REGISTER_TRACE(p2++, pOut);
67679 pIn1++;
67680 pOut++;
67681 }while( n-- );
@@ -67848,14 +68309,14 @@
67848 Stringify(pIn2, encoding);
67849 nByte = pIn1->n + pIn2->n;
67850 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67851 goto too_big;
67852 }
67853 MemSetTypeFlag(pOut, MEM_Str);
67854 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
67855 goto no_mem;
67856 }
 
67857 if( pOut!=pIn2 ){
67858 memcpy(pOut->z, pIn2->z, pIn2->n);
67859 }
67860 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
67861 pOut->z[nByte]=0;
@@ -69026,10 +69487,11 @@
69026 ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
69027 ** all valid.
69028 */
69029 assert( p2<pC->nHdrParsed );
69030 assert( rc==SQLITE_OK );
 
69031 if( pC->szRow>=aOffset[p2+1] ){
69032 /* This is the common case where the desired content fits on the original
69033 ** page - where the content is not on an overflow page */
69034 VdbeMemRelease(pDest);
69035 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
@@ -69063,12 +69525,12 @@
69063 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
69064 ** dynamically allocated space over to the pDest structure.
69065 ** This prevents a memory copy. */
69066 if( sMem.zMalloc ){
69067 assert( sMem.z==sMem.zMalloc );
69068 assert( !(pDest->flags & MEM_Dyn) );
69069 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
69070 pDest->flags &= ~(MEM_Ephem|MEM_Static);
69071 pDest->flags |= MEM_Term;
69072 pDest->z = sMem.z;
69073 pDest->zMalloc = sMem.zMalloc;
69074 }
@@ -69247,11 +69709,11 @@
69247 assert( i==nHdr );
69248 assert( j==nByte );
69249
69250 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69251 pOut->n = (int)nByte;
69252 pOut->flags = MEM_Blob | MEM_Dyn;
69253 pOut->xDel = 0;
69254 if( nZero ){
69255 pOut->u.nZero = nZero;
69256 pOut->flags |= MEM_Zero;
69257 }
@@ -70121,20 +70583,20 @@
70121 r.pKeyInfo = pC->pKeyInfo;
70122 r.nField = (u16)nField;
70123
70124 /* The next line of code computes as follows, only faster:
70125 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
70126 ** r.flags = UNPACKED_INCRKEY;
70127 ** }else{
70128 ** r.flags = 0;
70129 ** }
70130 */
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 );
70136
70137 r.aMem = &aMem[pOp->p3];
70138 #ifdef SQLITE_DEBUG
70139 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
70140 #endif
@@ -70288,22 +70750,21 @@
70288 ExpandBlob(&r.aMem[ii]);
70289 #ifdef SQLITE_DEBUG
70290 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
70291 #endif
70292 }
70293 r.flags = UNPACKED_PREFIX_MATCH;
70294 pIdxKey = &r;
70295 }else{
70296 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
70297 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
70298 );
70299 if( pIdxKey==0 ) goto no_mem;
70300 assert( pIn3->flags & MEM_Blob );
70301 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
70302 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
70303 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
70304 }
 
70305 if( pOp->opcode==OP_NoConflict ){
70306 /* For the OP_NoConflict opcode, take the jump if any of the
70307 ** input fields are NULL, since any key with a NULL will not
70308 ** conflict */
70309 for(ii=0; ii<r.nField; ii++){
@@ -71188,11 +71649,11 @@
71188 pCrsr = pC->pCursor;
71189 assert( pCrsr!=0 );
71190 assert( pOp->p5==0 );
71191 r.pKeyInfo = pC->pKeyInfo;
71192 r.nField = (u16)pOp->p3;
71193 r.flags = UNPACKED_PREFIX_MATCH;
71194 r.aMem = &aMem[pOp->p2];
71195 #ifdef SQLITE_DEBUG
71196 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71197 #endif
71198 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
@@ -71302,14 +71763,14 @@
71302 assert( pOp->p4type==P4_INT32 );
71303 r.pKeyInfo = pC->pKeyInfo;
71304 r.nField = (u16)pOp->p4.i;
71305 if( pOp->opcode<OP_IdxLT ){
71306 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71307 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
71308 }else{
71309 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71310 r.flags = UNPACKED_PREFIX_MATCH;
71311 }
71312 r.aMem = &aMem[pOp->p3];
71313 #ifdef SQLITE_DEBUG
71314 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71315 #endif
@@ -73809,14 +74270,14 @@
73809 if( r2->aMem[i].flags & MEM_Null ){
73810 *pRes = -1;
73811 return;
73812 }
73813 }
73814 r2->flags |= UNPACKED_PREFIX_MATCH;
73815 }
73816
73817 *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
73818 }
73819
73820 /*
73821 ** This function is called to compare two iterator keys when merging
73822 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
@@ -100117,11 +100578,11 @@
100117 }else if( eDest!=SRT_Exists ){
100118 /* If the destination is an EXISTS(...) expression, the actual
100119 ** values returned by the SELECT are not required.
100120 */
100121 sqlite3ExprCodeExprList(pParse, pEList, regResult,
100122 (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
100123 }
100124
100125 /* If the DISTINCT keyword was present on the SELECT statement
100126 ** and this row has been seen before, then do not make this row
100127 ** part of the result.
@@ -110767,11 +111228,11 @@
110767 iCol = pRec->nField - 1;
110768 assert( pIdx->nSample>0 );
110769 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
110770 do{
110771 iTest = (iMin+i)/2;
110772 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
110773 if( res<0 ){
110774 iMin = iTest+1;
110775 }else{
110776 i = iTest;
110777 }
@@ -110782,20 +111243,20 @@
110782 ** above found the right answer. This block serves no purpose other
110783 ** than to invoke the asserts. */
110784 if( res==0 ){
110785 /* If (res==0) is true, then sample $i must be equal to pRec */
110786 assert( i<pIdx->nSample );
110787 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
110788 || pParse->db->mallocFailed );
110789 }else{
110790 /* Otherwise, pRec must be smaller than sample $i and larger than
110791 ** sample ($i-1). */
110792 assert( i==pIdx->nSample
110793 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
110794 || pParse->db->mallocFailed );
110795 assert( i==0
110796 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
110797 || pParse->db->mallocFailed );
110798 }
110799 #endif /* ifdef SQLITE_DEBUG */
110800
110801 /* At this point, aSample[i] is the first sample that is greater than
@@ -114724,11 +115185,11 @@
114724
114725 /* For a co-routine, change all OP_Column references to the table of
114726 ** the co-routine into OP_SCopy of result contained in a register.
114727 ** OP_Rowid becomes OP_Null.
114728 */
114729 if( pTabItem->viaCoroutine ){
114730 last = sqlite3VdbeCurrentAddr(v);
114731 k = pLevel->addrBody;
114732 pOp = sqlite3VdbeGetOp(v, k);
114733 for(; k<last; k++, pOp++){
114734 if( pOp->p1!=pLevel->iTabCur ) continue;
114735
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -189,11 +189,11 @@
189 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
190 ** [sqlite_version()] and [sqlite_source_id()].
191 */
192 #define SQLITE_VERSION "3.8.4"
193 #define SQLITE_VERSION_NUMBER 3008004
194 #define SQLITE_SOURCE_ID "2014-03-04 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
195
196 /*
197 ** CAPI3REF: Run-Time Library Version Numbers
198 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
199 **
@@ -9334,12 +9334,15 @@
9334 #ifndef SQLITE_OMIT_TRACE
9335 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9336 #endif
9337
9338 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9339 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int);
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*);
9344
9345 #ifndef SQLITE_OMIT_TRIGGER
9346 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
9347 #endif
9348
@@ -10950,23 +10953,23 @@
10953 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10954 ** OP_Column opcode.
10955 **
10956 ** This structure holds a record that has already been disassembled
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().
10961 */
10962 struct UnpackedRecord {
10963 KeyInfo *pKeyInfo; /* Collation and sort-order information */
10964 u16 nField; /* Number of entries in apMem[] */
10965 i8 default_rc; /* Comparison result if keys are equal */
10966 Mem *aMem; /* Values */
10967 int r1; /* Value to return if (lhs > rhs) */
10968 int r2; /* Value to return if (rhs < lhs) */
10969 };
10970
 
 
 
 
 
10971
10972 /*
10973 ** Each SQL index is represented in memory by an
10974 ** instance of the following structure.
10975 **
@@ -13840,11 +13843,11 @@
13843 ** the following flags must be set to determine the memory management
13844 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
13845 ** string is \000 or \u0000 terminated
13846 */
13847 #define MEM_Term 0x0200 /* String rep is nul terminated */
13848 #define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
13849 #define MEM_Static 0x0800 /* Mem.z points to a static string */
13850 #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
13851 #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
13852 #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
13853 #ifdef SQLITE_OMIT_INCRBLOB
@@ -14023,11 +14026,11 @@
14026 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
14027 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
14028 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14029
14030 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14031 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*);
14032 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14033 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14034 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14035 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14036 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
@@ -14088,10 +14091,11 @@
14091 # define sqlite3VdbeLeave(X)
14092 #endif
14093
14094 #ifdef SQLITE_DEBUG
14095 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
14096 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
14097 #endif
14098
14099 #ifndef SQLITE_OMIT_FOREIGN_KEY
14100 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
14101 #else
@@ -21471,11 +21475,11 @@
21475 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
21476
21477 sqlite3VdbeMemRelease(pMem);
21478 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
21479 pMem->enc = desiredEnc;
21480 pMem->flags |= (MEM_Term);
21481 pMem->z = (char*)zOut;
21482 pMem->zMalloc = pMem->z;
21483
21484 translate_out:
21485 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
@@ -21599,11 +21603,10 @@
21603 sqlite3VdbeMemRelease(&m);
21604 m.z = 0;
21605 }
21606 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
21607 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
 
21608 assert( m.z || db->mallocFailed );
21609 return m.z;
21610 }
21611
21612 /*
@@ -55305,10 +55308,11 @@
55308 i64 intKey, /* The table key */
55309 int biasRight, /* If true, bias the search to the high end */
55310 int *pRes /* Write search results here */
55311 ){
55312 int rc;
55313 RecordCompare xRecordCompare;
55314
55315 assert( cursorHoldsMutex(pCur) );
55316 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55317 assert( pRes );
55318 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
@@ -55325,10 +55329,20 @@
55329 if( pCur->atLast && pCur->info.nKey<intKey ){
55330 *pRes = -1;
55331 return SQLITE_OK;
55332 }
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 }
55344
55345 rc = moveToRoot(pCur);
55346 if( rc ){
55347 return rc;
55348 }
@@ -55410,18 +55424,18 @@
55424 if( nCell<=pPage->max1bytePayload ){
55425 /* This branch runs if the record-size field of the cell is a
55426 ** single byte varint and the record fits entirely on the main
55427 ** b-tree page. */
55428 testcase( pCell+nCell+1==pPage->aDataEnd );
55429 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
55430 }else if( !(pCell[1] & 0x80)
55431 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
55432 ){
55433 /* The record-size field is a 2 byte varint and the record
55434 ** fits entirely on the main b-tree page. */
55435 testcase( pCell+nCell+2==pPage->aDataEnd );
55436 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
55437 }else{
55438 /* The record flows over onto one or more overflow pages. In
55439 ** this case the whole cell needs to be parsed, a buffer allocated
55440 ** and accessPayload() used to retrieve the record into the
55441 ** buffer before VdbeRecordCompare() can be called. */
@@ -55438,11 +55452,11 @@
55452 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
55453 if( rc ){
55454 sqlite3_free(pCellKey);
55455 goto moveto_finish;
55456 }
55457 c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
55458 sqlite3_free(pCellKey);
55459 }
55460 if( c<0 ){
55461 lwr = idx+1;
55462 }else if( c>0 ){
@@ -60001,10 +60015,46 @@
60015 ** This file contains code use to manipulate "Mem" structure. A "Mem"
60016 ** stores a single value in the VDBE. Mem is an opaque structure visible
60017 ** only within the VDBE. Interface routines refer to a Mem using the
60018 ** name sqlite_value
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
60056
60057 /*
60058 ** If pMem is an object with a valid string representation, this routine
60059 ** ensures the internal encoding for the string representation is
60060 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -60051,16 +60101,11 @@
60101 ** pMem->z into the new allocation. pMem must be either a string or
60102 ** blob if bPreserve is true. If bPreserve is false, any prior content
60103 ** in pMem->z is discarded.
60104 */
60105 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
60106 assert( sqlite3VdbeCheckMemInvariants(pMem) );
 
 
 
 
 
60107 assert( (pMem->flags&MEM_RowSet)==0 );
60108
60109 /* If the bPreserve flag is set to true, then the memory cell must already
60110 ** contain a valid string or blob value. */
60111 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
@@ -60074,26 +60119,26 @@
60119 }else{
60120 sqlite3DbFree(pMem->db, pMem->zMalloc);
60121 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
60122 }
60123 if( pMem->zMalloc==0 ){
60124 VdbeMemRelease(pMem);
60125 pMem->flags = MEM_Null;
60126 return SQLITE_NOMEM;
60127 }
60128 }
60129
60130 if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
60131 memcpy(pMem->zMalloc, pMem->z, pMem->n);
60132 }
60133 if( (pMem->flags&MEM_Dyn)!=0 ){
60134 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
60135 pMem->xDel((void *)(pMem->z));
60136 }
60137
60138 pMem->z = pMem->zMalloc;
60139 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
60140 pMem->xDel = 0;
60141 return SQLITE_OK;
60142 }
60143
60144 /*
@@ -60258,13 +60303,13 @@
60303 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
60304 if( p->flags&MEM_Agg ){
60305 sqlite3VdbeMemFinalize(p, p->u.pDef);
60306 assert( (p->flags & MEM_Agg)==0 );
60307 sqlite3VdbeMemRelease(p);
60308 }else if( p->flags&MEM_Dyn ){
60309 assert( (p->flags&MEM_RowSet)==0 );
60310 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
60311 p->xDel((void *)p->z);
60312 p->xDel = 0;
60313 }else if( p->flags&MEM_RowSet ){
60314 sqlite3RowSetClear(p->u.pRowSet);
60315 }else if( p->flags&MEM_Frame ){
@@ -60276,10 +60321,11 @@
60321 ** Release any memory held by the Mem. This may leave the Mem in an
60322 ** inconsistent state, for example with (Mem.z==0) and
60323 ** (Mem.memType==MEM_Str).
60324 */
60325 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
60326 assert( sqlite3VdbeCheckMemInvariants(p) );
60327 VdbeMemRelease(p);
60328 if( p->zMalloc ){
60329 sqlite3DbFree(p->db, p->zMalloc);
60330 p->zMalloc = 0;
60331 }
@@ -60613,10 +60659,11 @@
60659
60660 assert( (pFrom->flags & MEM_RowSet)==0 );
60661 VdbeMemRelease(pTo);
60662 memcpy(pTo, pFrom, MEMCELLSIZE);
60663 pTo->flags &= ~MEM_Dyn;
60664 pTo->xDel = 0;
60665
60666 if( pTo->flags&(MEM_Str|MEM_Blob) ){
60667 if( 0==(pFrom->flags&MEM_Static) ){
60668 pTo->flags |= MEM_Ephem;
60669 rc = sqlite3VdbeMemMakeWriteable(pTo);
@@ -60738,123 +60785,10 @@
60785 }
60786
60787 return SQLITE_OK;
60788 }
60789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60790 /*
60791 ** Move data out of a btree key or data field and into a Mem structure.
60792 ** The data or key is taken from the entry that pCur is currently pointing
60793 ** to. offset and amt determine what portion of the data or key to retrieve.
60794 ** key is true to get the key or false to get data. The result is written
@@ -60892,11 +60826,11 @@
60826 if( offset+amt<=available ){
60827 sqlite3VdbeMemRelease(pMem);
60828 pMem->z = &zData[offset];
60829 pMem->flags = MEM_Blob|MEM_Ephem;
60830 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
60831 pMem->flags = MEM_Blob|MEM_Term;
60832 pMem->enc = 0;
60833 pMem->memType = MEM_Blob;
60834 if( key ){
60835 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
60836 }else{
@@ -61010,11 +60944,10 @@
60944 if( pRec ){
60945 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
60946 if( pRec->pKeyInfo ){
60947 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
60948 assert( pRec->pKeyInfo->enc==ENC(db) );
 
60949 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
60950 for(i=0; i<nCol; i++){
60951 pRec->aMem[i].flags = MEM_Null;
60952 pRec->aMem[i].memType = MEM_Null;
60953 pRec->aMem[i].db = db;
@@ -62591,10 +62524,11 @@
62524 }
62525 return;
62526 }
62527 for(pEnd=&p[N]; p<pEnd; p++){
62528 assert( (&p[1])==pEnd || p[0].db==p[1].db );
62529 assert( sqlite3VdbeCheckMemInvariants(p) );
62530
62531 /* This block is really an inlined version of sqlite3VdbeMemRelease()
62532 ** that takes advantage of the fact that the memory cell value is
62533 ** being set to NULL after releasing any dynamic resources.
62534 **
@@ -62784,11 +62718,11 @@
62718
62719 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
62720 assert( p->db->mallocFailed );
62721 return SQLITE_ERROR;
62722 }
62723 pMem->flags = MEM_Str|MEM_Term;
62724 zP4 = displayP4(pOp, pMem->z, 32);
62725 if( zP4!=pMem->z ){
62726 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
62727 }else{
62728 assert( pMem->z!=0 );
@@ -62801,11 +62735,11 @@
62735 if( p->explain==1 ){
62736 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
62737 assert( p->db->mallocFailed );
62738 return SQLITE_ERROR;
62739 }
62740 pMem->flags = MEM_Str|MEM_Term;
62741 pMem->n = 2;
62742 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
62743 pMem->memType = MEM_Str;
62744 pMem->enc = SQLITE_UTF8;
62745 pMem++;
@@ -62813,11 +62747,11 @@
62747 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62748 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
62749 assert( p->db->mallocFailed );
62750 return SQLITE_ERROR;
62751 }
62752 pMem->flags = MEM_Str|MEM_Term;
62753 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
62754 pMem->memType = MEM_Str;
62755 pMem->enc = SQLITE_UTF8;
62756 #else
62757 pMem->flags = MEM_Null; /* Comment */
@@ -64312,10 +64246,18 @@
64246
64247 /* NULL or constants 0 or 1 */
64248 return 0;
64249 }
64250
64251 /* Input "x" is a sequence of unsigned characters that represent a
64252 ** big-endian integer. Return the equivalent native integer
64253 */
64254 #define ONE_BYTE_INT(x) ((i8)(x)[0])
64255 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
64256 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
64257 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
64258
64259 /*
64260 ** Deserialize the data blob pointed to by buf as serial type serial_type
64261 ** and store the result in pMem. Return the number of bytes read.
64262 */
64263 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
@@ -64323,46 +64265,40 @@
64265 u32 serial_type, /* Serial type to deserialize */
64266 Mem *pMem /* Memory cell to write value into */
64267 ){
64268 u64 x;
64269 u32 y;
 
64270 switch( serial_type ){
64271 case 10: /* Reserved for future use */
64272 case 11: /* Reserved for future use */
64273 case 0: { /* NULL */
64274 pMem->flags = MEM_Null;
64275 break;
64276 }
64277 case 1: { /* 1-byte signed integer */
64278 pMem->u.i = ONE_BYTE_INT(buf);
64279 pMem->flags = MEM_Int;
64280 return 1;
64281 }
64282 case 2: { /* 2-byte signed integer */
64283 pMem->u.i = TWO_BYTE_INT(buf);
 
64284 pMem->flags = MEM_Int;
64285 return 2;
64286 }
64287 case 3: { /* 3-byte signed integer */
64288 pMem->u.i = THREE_BYTE_INT(buf);
 
64289 pMem->flags = MEM_Int;
64290 return 3;
64291 }
64292 case 4: { /* 4-byte signed integer */
64293 y = FOUR_BYTE_UINT(buf);
64294 pMem->u.i = (i64)*(int*)&y;
64295 pMem->flags = MEM_Int;
64296 return 4;
64297 }
64298 case 5: { /* 6-byte signed integer */
64299 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
 
 
 
64300 pMem->flags = MEM_Int;
64301 return 6;
64302 }
64303 case 6: /* 8-byte signed integer */
64304 case 7: { /* IEEE floating point */
@@ -64376,12 +64312,12 @@
64312 static const double r1 = 1.0;
64313 u64 t2 = t1;
64314 swapMixedEndianFloat(t2);
64315 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
64316 #endif
64317 x = FOUR_BYTE_UINT(buf);
64318 y = FOUR_BYTE_UINT(buf+4);
64319 x = (x<<32) | y;
64320 if( serial_type==6 ){
64321 pMem->u.i = *(i64*)&x;
64322 pMem->flags = MEM_Int;
64323 }else{
@@ -64473,11 +64409,11 @@
64409 u32 idx; /* Offset in aKey[] to read from */
64410 u16 u; /* Unsigned loop counter */
64411 u32 szHdr;
64412 Mem *pMem = p->aMem;
64413
64414 p->default_rc = 0;
64415 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64416 idx = getVarint32(aKey, szHdr);
64417 d = szHdr;
64418 u = 0;
64419 while( idx<szHdr && u<p->nField && d<=nKey ){
@@ -64494,30 +64430,22 @@
64430 }
64431 assert( u<=pKeyInfo->nField + 1 );
64432 p->nField = u;
64433 }
64434
64435 #if SQLITE_DEBUG
64436 /*
64437 ** This function compares two index or table record keys in the same way
64438 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
64439 ** this function deserializes and compares values using the
64440 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
64441 ** in assert() statements to ensure that the optimized code in
64442 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
 
 
 
 
 
 
 
 
 
64443 */
64444 static int vdbeRecordCompareDebug(
64445 int nKey1, const void *pKey1, /* Left key */
64446 const UnpackedRecord *pPKey2 /* Right key */
64447 ){
64448 u32 d1; /* Offset into aKey[] of next data element */
64449 u32 idx1; /* Offset into aKey[] of next header element */
64450 u32 szHdr1; /* Number of bytes in header */
64451 int i = 0;
@@ -64587,28 +64515,558 @@
64515 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
64516 */
64517 assert( mem1.zMalloc==0 );
64518
64519 /* rc==0 here means that one of the keys ran out of fields and
64520 ** all the fields up to that point were equal. Return the the default_rc
64521 ** value. */
64522 return pPKey2->default_rc;
64523 }
64524 #endif
64525
64526 /*
64527 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
64528 ** using the collation sequence pColl. As usual, return a negative , zero
64529 ** or positive value if *pMem1 is less than, equal to or greater than
64530 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
64531 */
64532 static int vdbeCompareMemString(
64533 const Mem *pMem1,
64534 const Mem *pMem2,
64535 const CollSeq *pColl
64536 ){
64537 if( pMem1->enc==pColl->enc ){
64538 /* The strings are already in the correct encoding. Call the
64539 ** comparison function directly */
64540 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
64541 }else{
64542 int rc;
64543 const void *v1, *v2;
64544 int n1, n2;
64545 Mem c1;
64546 Mem c2;
64547 memset(&c1, 0, sizeof(c1));
64548 memset(&c2, 0, sizeof(c2));
64549 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
64550 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
64551 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
64552 n1 = v1==0 ? 0 : c1.n;
64553 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
64554 n2 = v2==0 ? 0 : c2.n;
64555 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
64556 sqlite3VdbeMemRelease(&c1);
64557 sqlite3VdbeMemRelease(&c2);
64558 return rc;
64559 }
64560 }
64561
64562 /*
64563 ** Compare the values contained by the two memory cells, returning
64564 ** negative, zero or positive if pMem1 is less than, equal to, or greater
64565 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
64566 ** and reals) sorted numerically, followed by text ordered by the collating
64567 ** sequence pColl and finally blob's ordered by memcmp().
64568 **
64569 ** Two NULL values are considered equal by this function.
64570 */
64571 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
64572 int rc;
64573 int f1, f2;
64574 int combined_flags;
64575
64576 f1 = pMem1->flags;
64577 f2 = pMem2->flags;
64578 combined_flags = f1|f2;
64579 assert( (combined_flags & MEM_RowSet)==0 );
64580
64581 /* If one value is NULL, it is less than the other. If both values
64582 ** are NULL, return 0.
64583 */
64584 if( combined_flags&MEM_Null ){
64585 return (f2&MEM_Null) - (f1&MEM_Null);
64586 }
64587
64588 /* If one value is a number and the other is not, the number is less.
64589 ** If both are numbers, compare as reals if one is a real, or as integers
64590 ** if both values are integers.
64591 */
64592 if( combined_flags&(MEM_Int|MEM_Real) ){
64593 double r1, r2;
64594 if( (f1 & f2 & MEM_Int)!=0 ){
64595 if( pMem1->u.i < pMem2->u.i ) return -1;
64596 if( pMem1->u.i > pMem2->u.i ) return 1;
64597 return 0;
64598 }
64599 if( (f1&MEM_Real)!=0 ){
64600 r1 = pMem1->r;
64601 }else if( (f1&MEM_Int)!=0 ){
64602 r1 = (double)pMem1->u.i;
64603 }else{
64604 return 1;
64605 }
64606 if( (f2&MEM_Real)!=0 ){
64607 r2 = pMem2->r;
64608 }else if( (f2&MEM_Int)!=0 ){
64609 r2 = (double)pMem2->u.i;
64610 }else{
64611 return -1;
64612 }
64613 if( r1<r2 ) return -1;
64614 if( r1>r2 ) return 1;
64615 return 0;
64616 }
64617
64618 /* If one value is a string and the other is a blob, the string is less.
64619 ** If both are strings, compare using the collating functions.
64620 */
64621 if( combined_flags&MEM_Str ){
64622 if( (f1 & MEM_Str)==0 ){
64623 return 1;
64624 }
64625 if( (f2 & MEM_Str)==0 ){
64626 return -1;
64627 }
64628
64629 assert( pMem1->enc==pMem2->enc );
64630 assert( pMem1->enc==SQLITE_UTF8 ||
64631 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
64632
64633 /* The collation sequence must be defined at this point, even if
64634 ** the user deletes the collation sequence after the vdbe program is
64635 ** compiled (this was not always the case).
64636 */
64637 assert( !pColl || pColl->xCmp );
64638
64639 if( pColl ){
64640 return vdbeCompareMemString(pMem1, pMem2, pColl);
64641 }
64642 /* If a NULL pointer was passed as the collate function, fall through
64643 ** to the blob case and use memcmp(). */
64644 }
64645
64646 /* Both values must be blobs. Compare using memcmp(). */
64647 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
64648 if( rc==0 ){
64649 rc = pMem1->n - pMem2->n;
64650 }
64651 return rc;
64652 }
64653
64654
64655 /*
64656 ** The first argument passed to this function is a serial-type that
64657 ** corresponds to an integer - all values between 1 and 9 inclusive
64658 ** except 7. The second points to a buffer containing an integer value
64659 ** serialized according to serial_type. This function deserializes
64660 ** and returns the value.
64661 */
64662 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
64663 u32 y;
64664 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
64665 switch( serial_type ){
64666 case 0:
64667 case 1:
64668 return ONE_BYTE_INT(aKey);
64669 case 2:
64670 return TWO_BYTE_INT(aKey);
64671 case 3:
64672 return THREE_BYTE_INT(aKey);
64673 case 4: {
64674 y = FOUR_BYTE_UINT(aKey);
64675 return (i64)*(int*)&y;
64676 }
64677 case 5: {
64678 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64679 }
64680 case 6: {
64681 u64 x = FOUR_BYTE_UINT(aKey);
64682 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64683 return (i64)*(i64*)&x;
64684 }
64685 }
64686
64687 return (serial_type - 8);
64688 }
64689
64690 /*
64691 ** This function compares the two table rows or index records
64692 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
64693 ** or positive integer if key1 is less than, equal to or
64694 ** greater than key2. The {nKey1, pKey1} key must be a blob
64695 ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
64696 ** key must be a parsed key such as obtained from
64697 ** sqlite3VdbeParseRecord.
64698 **
64699 ** If argument bSkip is non-zero, it is assumed that the caller has already
64700 ** determined that the first fields of the keys are equal.
64701 **
64702 ** Key1 and Key2 do not have to contain the same number of fields. If all
64703 ** fields that appear in both keys are equal, then pPKey2->default_rc is
64704 ** returned.
64705 */
64706 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64707 int nKey1, const void *pKey1, /* Left key */
64708 const UnpackedRecord *pPKey2, /* Right key */
64709 int bSkip /* If true, skip the first field */
64710 ){
64711 u32 d1; /* Offset into aKey[] of next data element */
64712 int i; /* Index of next field to compare */
64713 int szHdr1; /* Size of record header in bytes */
64714 u32 idx1; /* Offset of first type in header */
64715 int rc = 0; /* Return value */
64716 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
64717 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
64718 const unsigned char *aKey1 = (const unsigned char *)pKey1;
64719 Mem mem1;
64720
64721 /* If bSkip is true, then the caller has already determined that the first
64722 ** two elements in the keys are equal. Fix the various stack variables so
64723 ** that this routine begins comparing at the second field. */
64724 if( bSkip ){
64725 u32 s1;
64726 idx1 = 1 + getVarint32(&aKey1[1], s1);
64727 szHdr1 = aKey1[0];
64728 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
64729 i = 1;
64730 pRhs++;
64731 }else{
64732 idx1 = getVarint32(aKey1, szHdr1);
64733 d1 = szHdr1;
64734 i = 0;
64735 }
64736
64737 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64738 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
64739 || CORRUPT_DB );
64740 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
64741 assert( pPKey2->pKeyInfo->nField>0 );
64742 assert( idx1<=szHdr1 || CORRUPT_DB );
64743 do{
64744 u32 serial_type;
64745
64746 /* RHS is an integer */
64747 if( pRhs->flags & MEM_Int ){
64748 serial_type = aKey1[idx1];
64749 if( serial_type>=12 ){
64750 rc = +1;
64751 }else if( serial_type==0 ){
64752 rc = -1;
64753 }else if( serial_type==7 ){
64754 double rhs = (double)pRhs->u.i;
64755 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64756 if( mem1.r<rhs ){
64757 rc = -1;
64758 }else if( mem1.r>rhs ){
64759 rc = +1;
64760 }
64761 }else{
64762 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
64763 i64 rhs = pRhs->u.i;
64764 if( lhs<rhs ){
64765 rc = -1;
64766 }else if( lhs>rhs ){
64767 rc = +1;
64768 }
64769 }
64770 }
64771
64772 /* RHS is real */
64773 else if( pRhs->flags & MEM_Real ){
64774 serial_type = aKey1[idx1];
64775 if( serial_type>=12 ){
64776 rc = +1;
64777 }else if( serial_type==0 ){
64778 rc = -1;
64779 }else{
64780 double rhs = pRhs->r;
64781 double lhs;
64782 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64783 if( serial_type==7 ){
64784 lhs = mem1.r;
64785 }else{
64786 lhs = (double)mem1.u.i;
64787 }
64788 if( lhs<rhs ){
64789 rc = -1;
64790 }else if( lhs>rhs ){
64791 rc = +1;
64792 }
64793 }
64794 }
64795
64796 /* RHS is a string */
64797 else if( pRhs->flags & MEM_Str ){
64798 getVarint32(&aKey1[idx1], serial_type);
64799 if( serial_type<12 ){
64800 rc = -1;
64801 }else if( !(serial_type & 0x01) ){
64802 rc = +1;
64803 }else{
64804 mem1.n = (serial_type - 12) / 2;
64805 if( (d1+mem1.n) > (unsigned)nKey1 ){
64806 rc = 1; /* Corruption */
64807 }else if( pKeyInfo->aColl[i] ){
64808 mem1.enc = pKeyInfo->enc;
64809 mem1.db = pKeyInfo->db;
64810 mem1.flags = MEM_Str;
64811 mem1.z = (char*)&aKey1[d1];
64812 rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
64813 }else{
64814 int nCmp = MIN(mem1.n, pRhs->n);
64815 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64816 if( rc==0 ) rc = mem1.n - pRhs->n;
64817 }
64818 }
64819 }
64820
64821 /* RHS is a blob */
64822 else if( pRhs->flags & MEM_Blob ){
64823 getVarint32(&aKey1[idx1], serial_type);
64824 if( serial_type<12 || (serial_type & 0x01) ){
64825 rc = -1;
64826 }else{
64827 int nStr = (serial_type - 12) / 2;
64828 if( (d1+nStr) > (unsigned)nKey1 ){
64829 rc = 1; /* Corruption */
64830 }else{
64831 int nCmp = MIN(nStr, pRhs->n);
64832 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64833 if( rc==0 ) rc = nStr - pRhs->n;
64834 }
64835 }
64836 }
64837
64838 /* RHS is null */
64839 else{
64840 serial_type = aKey1[idx1];
64841 rc = (serial_type!=0);
64842 }
64843
64844 if( rc!=0 ){
64845 if( pKeyInfo->aSortOrder[i] ){
64846 rc = -rc;
64847 }
64848 assert( CORRUPT_DB
64849 || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64850 || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64851 );
64852 assert( mem1.zMalloc==0 ); /* See comment below */
64853 return rc;
64854 }
64855
64856 i++;
64857 pRhs++;
64858 d1 += sqlite3VdbeSerialTypeLen(serial_type);
64859 idx1 += sqlite3VarintLen(serial_type);
64860 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
64861
64862 /* No memory allocation is ever used on mem1. Prove this using
64863 ** the following assert(). If the assert() fails, it indicates a
64864 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
64865 assert( mem1.zMalloc==0 );
64866
64867 /* rc==0 here means that one or both of the keys ran out of fields and
64868 ** all the fields up to that point were equal. Return the the default_rc
64869 ** value. */
64870 assert( CORRUPT_DB
64871 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
64872 );
64873 return pPKey2->default_rc;
64874 }
64875
64876 /*
64877 ** This function is an optimized version of sqlite3VdbeRecordCompare()
64878 ** that (a) the first field of pPKey2 is an integer, and (b) the
64879 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
64880 ** byte (i.e. is less than 128).
64881 */
64882 static int vdbeRecordCompareInt(
64883 int nKey1, const void *pKey1, /* Left key */
64884 const UnpackedRecord *pPKey2, /* Right key */
64885 int bSkip /* Ignored */
64886 ){
64887 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
64888 int serial_type = ((const u8*)pKey1)[1];
64889 int res;
64890 u32 y;
64891 u64 x;
64892 i64 v = pPKey2->aMem[0].u.i;
64893 i64 lhs;
64894 UNUSED_PARAMETER(bSkip);
64895
64896 assert( bSkip==0 );
64897 switch( serial_type ){
64898 case 1: { /* 1-byte signed integer */
64899 lhs = ONE_BYTE_INT(aKey);
64900 break;
64901 }
64902 case 2: { /* 2-byte signed integer */
64903 lhs = TWO_BYTE_INT(aKey);
64904 break;
64905 }
64906 case 3: { /* 3-byte signed integer */
64907 lhs = THREE_BYTE_INT(aKey);
64908 break;
64909 }
64910 case 4: { /* 4-byte signed integer */
64911 y = FOUR_BYTE_UINT(aKey);
64912 lhs = (i64)*(int*)&y;
64913 break;
64914 }
64915 case 5: { /* 6-byte signed integer */
64916 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64917 break;
64918 }
64919 case 6: { /* 8-byte signed integer */
64920 x = FOUR_BYTE_UINT(aKey);
64921 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64922 lhs = *(i64*)&x;
64923 break;
64924 }
64925 case 8:
64926 lhs = 0;
64927 break;
64928 case 9:
64929 lhs = 1;
64930 break;
64931
64932 /* This case could be removed without changing the results of running
64933 ** this code. Including it causes gcc to generate a faster switch
64934 ** statement (since the range of switch targets now starts at zero and
64935 ** is contiguous) but does not cause any duplicate code to be generated
64936 ** (as gcc is clever enough to combine the two like cases). Other
64937 ** compilers might be similar. */
64938 case 0: case 7:
64939 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64940
64941 default:
64942 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64943 }
64944
64945 if( v>lhs ){
64946 res = pPKey2->r1;
64947 }else if( v<lhs ){
64948 res = pPKey2->r2;
64949 }else if( pPKey2->nField>1 ){
64950 /* The first fields of the two keys are equal. Compare the trailing
64951 ** fields. */
64952 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
64953 }else{
64954 /* The first fields of the two keys are equal and there are no trailing
64955 ** fields. Return pPKey2->default_rc in this case. */
64956 res = pPKey2->default_rc;
64957 }
64958
64959 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
64960 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64961 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64962 || CORRUPT_DB
64963 );
64964 return res;
64965 }
64966
64967 /*
64968 ** This function is an optimized version of sqlite3VdbeRecordCompare()
64969 ** that (a) the first field of pPKey2 is a string, that (b) the first field
64970 ** uses the collation sequence BINARY and (c) that the size-of-header varint
64971 ** at the start of (pKey1/nKey1) fits in a single byte.
64972 */
64973 static int vdbeRecordCompareString(
64974 int nKey1, const void *pKey1, /* Left key */
64975 const UnpackedRecord *pPKey2, /* Right key */
64976 int bSkip
64977 ){
64978 const u8 *aKey1 = (const u8*)pKey1;
64979 int serial_type;
64980 int res;
64981 UNUSED_PARAMETER(bSkip);
64982
64983 assert( bSkip==0 );
64984 getVarint32(&aKey1[1], serial_type);
64985
64986 if( serial_type<12 ){
64987 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
64988 }else if( !(serial_type & 0x01) ){
64989 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
64990 }else{
64991 int nCmp;
64992 int nStr;
64993 int szHdr = aKey1[0];
64994
64995 nStr = (serial_type-12) / 2;
64996 if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */
64997 nCmp = MIN( pPKey2->aMem[0].n, nStr );
64998 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
64999
65000 if( res==0 ){
65001 res = nStr - pPKey2->aMem[0].n;
65002 if( res==0 ){
65003 if( pPKey2->nField>1 ){
65004 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
65005 }else{
65006 res = pPKey2->default_rc;
65007 }
65008 }else if( res>0 ){
65009 res = pPKey2->r2;
65010 }else{
65011 res = pPKey2->r1;
65012 }
65013 }else if( res>0 ){
65014 res = pPKey2->r2;
65015 }else{
65016 res = pPKey2->r1;
65017 }
65018 }
65019
65020 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65021 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65022 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65023 || CORRUPT_DB
65024 );
65025 return res;
65026 }
65027
65028 /*
65029 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
65030 ** suitable for comparing serialized records to the unpacked record passed
65031 ** as the only argument.
65032 */
65033 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
65034 /* varintRecordCompareInt() and varintRecordCompareString() both assume
65035 ** that the size-of-header varint that occurs at the start of each record
65036 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
65037 ** also assumes that it is safe to overread a buffer by at least the
65038 ** maximum possible legal header size plus 8 bytes. Because there is
65039 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
65040 ** buffer passed to varintRecordCompareInt() this makes it convenient to
65041 ** limit the size of the header to 64 bytes in cases where the first field
65042 ** is an integer.
65043 **
65044 ** The easiest way to enforce this limit is to consider only records with
65045 ** 13 fields or less. If the first field is an integer, the maximum legal
65046 ** header size is (12*5 + 1 + 1) bytes. */
65047 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
65048 int flags = p->aMem[0].flags;
65049 if( p->pKeyInfo->aSortOrder[0] ){
65050 p->r1 = 1;
65051 p->r2 = -1;
65052 }else{
65053 p->r1 = -1;
65054 p->r2 = 1;
65055 }
65056 if( (flags & MEM_Int) ){
65057 return vdbeRecordCompareInt;
65058 }
65059 if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0
65060 && p->pKeyInfo->aColl[0]==0
65061 ){
65062 return vdbeRecordCompareString;
65063 }
65064 }
65065
65066 return sqlite3VdbeRecordCompare;
65067 }
65068
65069 /*
65070 ** pCur points at an index entry created using the OP_MakeRecord opcode.
65071 ** Read the rowid (the last field in the record) and store it in *rowid.
65072 ** Return SQLITE_OK if everything works, or an error code otherwise.
@@ -64695,23 +65153,23 @@
65153 ** omits the rowid at the end. The rowid at the end of the index entry
65154 ** is ignored as well. Hence, this routine only compares the prefixes
65155 ** of the keys prior to the final rowid, not the entire key.
65156 */
65157 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
65158 VdbeCursor *pC, /* The cursor to compare against */
65159 const UnpackedRecord *pUnpacked, /* Unpacked version of key */
65160 int *res /* Write the comparison result here */
65161 ){
65162 i64 nCellKey = 0;
65163 int rc;
65164 BtCursor *pCur = pC->pCursor;
65165 Mem m;
65166
65167 assert( sqlite3BtreeCursorIsValid(pCur) );
65168 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
65169 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
65170 /* nCellKey will always be between 0 and 0xffffffff because of the way
65171 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
65172 if( nCellKey<=0 || nCellKey>0x7fffffff ){
65173 *res = 0;
65174 return SQLITE_CORRUPT_BKPT;
65175 }
@@ -64718,12 +65176,11 @@
65176 memset(&m, 0, sizeof(m));
65177 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
65178 if( rc ){
65179 return rc;
65180 }
65181 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
 
65182 sqlite3VdbeMemRelease(&m);
65183 return SQLITE_OK;
65184 }
65185
65186 /*
@@ -66629,11 +67086,11 @@
67086 ** does not control the string, it might be deleted without the register
67087 ** knowing it.
67088 **
67089 ** This routine converts an ephemeral string into a dynamically allocated
67090 ** string that the register itself controls. In other words, it
67091 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
67092 */
67093 #define Deephemeralize(P) \
67094 if( ((P)->flags&MEM_Ephem)!=0 \
67095 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
67096
@@ -67166,22 +67623,25 @@
67623 #ifdef SQLITE_DEBUG
67624 if( (pOp->opflags & OPFLG_IN1)!=0 ){
67625 assert( pOp->p1>0 );
67626 assert( pOp->p1<=(p->nMem-p->nCursor) );
67627 assert( memIsValid(&aMem[pOp->p1]) );
67628 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
67629 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
67630 }
67631 if( (pOp->opflags & OPFLG_IN2)!=0 ){
67632 assert( pOp->p2>0 );
67633 assert( pOp->p2<=(p->nMem-p->nCursor) );
67634 assert( memIsValid(&aMem[pOp->p2]) );
67635 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
67636 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
67637 }
67638 if( (pOp->opflags & OPFLG_IN3)!=0 ){
67639 assert( pOp->p3>0 );
67640 assert( pOp->p3<=(p->nMem-p->nCursor) );
67641 assert( memIsValid(&aMem[pOp->p3]) );
67642 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
67643 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
67644 }
67645 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
67646 assert( pOp->p2>0 );
67647 assert( pOp->p2<=(p->nMem-p->nCursor) );
@@ -67279,11 +67739,11 @@
67739 ** and then jump to address P2.
67740 */
67741 case OP_Gosub: { /* jump */
67742 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
67743 pIn1 = &aMem[pOp->p1];
67744 assert( VdbeMemDynamic(pIn1)==0 );
67745 memAboutToChange(p, pIn1);
67746 pIn1->flags = MEM_Int;
67747 pIn1->u.i = pc;
67748 REGISTER_TRACE(pOp->p1, pIn1);
67749 pc = pOp->p2 - 1;
@@ -67352,11 +67812,11 @@
67812 ** OP_EndCoroutine, jump immediately to P2.
67813 */
67814 case OP_Yield: { /* in1, jump */
67815 int pcDest;
67816 pIn1 = &aMem[pOp->p1];
67817 assert( VdbeMemDynamic(pIn1)==0 );
67818 pIn1->flags = MEM_Int;
67819 pcDest = (int)pIn1->u.i;
67820 pIn1->u.i = pc;
67821 REGISTER_TRACE(pOp->p1, pIn1);
67822 pc = pcDest;
@@ -67525,14 +67985,13 @@
67985 if( encoding!=SQLITE_UTF8 ){
67986 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
67987 if( rc==SQLITE_TOOBIG ) goto too_big;
67988 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
67989 assert( pOut->zMalloc==pOut->z );
67990 assert( VdbeMemDynamic(pOut)==0 );
67991 pOut->zMalloc = 0;
67992 pOut->flags |= MEM_Static;
 
67993 if( pOp->p4type==P4_DYNAMIC ){
67994 sqlite3DbFree(db, pOp->p4.z);
67995 }
67996 pOp->p4type = P4_DYNAMIC;
67997 pOp->p4.z = pOut->z;
@@ -67664,18 +68123,20 @@
68123 do{
68124 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
68125 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
68126 assert( memIsValid(pIn1) );
68127 memAboutToChange(p, pOut);
68128 VdbeMemRelease(pOut);
68129 zMalloc = pOut->zMalloc;
68130 memcpy(pOut, pIn1, sizeof(Mem));
 
68131 #ifdef SQLITE_DEBUG
68132 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
68133 pOut->pScopyFrom += p1 - pOp->p2;
68134 }
68135 #endif
68136 pIn1->flags = MEM_Undefined;
68137 pIn1->xDel = 0;
68138 pIn1->zMalloc = zMalloc;
68139 REGISTER_TRACE(p2++, pOut);
68140 pIn1++;
68141 pOut++;
68142 }while( n-- );
@@ -67848,14 +68309,14 @@
68309 Stringify(pIn2, encoding);
68310 nByte = pIn1->n + pIn2->n;
68311 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
68312 goto too_big;
68313 }
 
68314 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
68315 goto no_mem;
68316 }
68317 MemSetTypeFlag(pOut, MEM_Str);
68318 if( pOut!=pIn2 ){
68319 memcpy(pOut->z, pIn2->z, pIn2->n);
68320 }
68321 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
68322 pOut->z[nByte]=0;
@@ -69026,10 +69487,11 @@
69487 ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
69488 ** all valid.
69489 */
69490 assert( p2<pC->nHdrParsed );
69491 assert( rc==SQLITE_OK );
69492 assert( sqlite3VdbeCheckMemInvariants(pDest) );
69493 if( pC->szRow>=aOffset[p2+1] ){
69494 /* This is the common case where the desired content fits on the original
69495 ** page - where the content is not on an overflow page */
69496 VdbeMemRelease(pDest);
69497 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
@@ -69063,12 +69525,12 @@
69525 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
69526 ** dynamically allocated space over to the pDest structure.
69527 ** This prevents a memory copy. */
69528 if( sMem.zMalloc ){
69529 assert( sMem.z==sMem.zMalloc );
69530 assert( VdbeMemDynamic(pDest)==0 );
69531 assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
69532 pDest->flags &= ~(MEM_Ephem|MEM_Static);
69533 pDest->flags |= MEM_Term;
69534 pDest->z = sMem.z;
69535 pDest->zMalloc = sMem.zMalloc;
69536 }
@@ -69247,11 +69709,11 @@
69709 assert( i==nHdr );
69710 assert( j==nByte );
69711
69712 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69713 pOut->n = (int)nByte;
69714 pOut->flags = MEM_Blob;
69715 pOut->xDel = 0;
69716 if( nZero ){
69717 pOut->u.nZero = nZero;
69718 pOut->flags |= MEM_Zero;
69719 }
@@ -70121,20 +70583,20 @@
70583 r.pKeyInfo = pC->pKeyInfo;
70584 r.nField = (u16)nField;
70585
70586 /* The next line of code computes as follows, only faster:
70587 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
70588 ** r.default_rc = -1;
70589 ** }else{
70590 ** r.default_rc = +1;
70591 ** }
70592 */
70593 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
70594 assert( oc!=OP_SeekGT || r.default_rc==-1 );
70595 assert( oc!=OP_SeekLE || r.default_rc==-1 );
70596 assert( oc!=OP_SeekGE || r.default_rc==+1 );
70597 assert( oc!=OP_SeekLT || r.default_rc==+1 );
70598
70599 r.aMem = &aMem[pOp->p3];
70600 #ifdef SQLITE_DEBUG
70601 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
70602 #endif
@@ -70288,22 +70750,21 @@
70750 ExpandBlob(&r.aMem[ii]);
70751 #ifdef SQLITE_DEBUG
70752 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
70753 #endif
70754 }
 
70755 pIdxKey = &r;
70756 }else{
70757 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
70758 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
70759 );
70760 if( pIdxKey==0 ) goto no_mem;
70761 assert( pIn3->flags & MEM_Blob );
70762 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
70763 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 
70764 }
70765 pIdxKey->default_rc = 0;
70766 if( pOp->opcode==OP_NoConflict ){
70767 /* For the OP_NoConflict opcode, take the jump if any of the
70768 ** input fields are NULL, since any key with a NULL will not
70769 ** conflict */
70770 for(ii=0; ii<r.nField; ii++){
@@ -71188,11 +71649,11 @@
71649 pCrsr = pC->pCursor;
71650 assert( pCrsr!=0 );
71651 assert( pOp->p5==0 );
71652 r.pKeyInfo = pC->pKeyInfo;
71653 r.nField = (u16)pOp->p3;
71654 r.default_rc = 0;
71655 r.aMem = &aMem[pOp->p2];
71656 #ifdef SQLITE_DEBUG
71657 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71658 #endif
71659 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
@@ -71302,14 +71763,14 @@
71763 assert( pOp->p4type==P4_INT32 );
71764 r.pKeyInfo = pC->pKeyInfo;
71765 r.nField = (u16)pOp->p4.i;
71766 if( pOp->opcode<OP_IdxLT ){
71767 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71768 r.default_rc = -1;
71769 }else{
71770 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71771 r.default_rc = 0;
71772 }
71773 r.aMem = &aMem[pOp->p3];
71774 #ifdef SQLITE_DEBUG
71775 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71776 #endif
@@ -73809,14 +74270,14 @@
74270 if( r2->aMem[i].flags & MEM_Null ){
74271 *pRes = -1;
74272 return;
74273 }
74274 }
74275 assert( r2->default_rc==0 );
74276 }
74277
74278 *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
74279 }
74280
74281 /*
74282 ** This function is called to compare two iterator keys when merging
74283 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
@@ -100117,11 +100578,11 @@
100578 }else if( eDest!=SRT_Exists ){
100579 /* If the destination is an EXISTS(...) expression, the actual
100580 ** values returned by the SELECT are not required.
100581 */
100582 sqlite3ExprCodeExprList(pParse, pEList, regResult,
100583 (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
100584 }
100585
100586 /* If the DISTINCT keyword was present on the SELECT statement
100587 ** and this row has been seen before, then do not make this row
100588 ** part of the result.
@@ -110767,11 +111228,11 @@
111228 iCol = pRec->nField - 1;
111229 assert( pIdx->nSample>0 );
111230 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
111231 do{
111232 iTest = (iMin+i)/2;
111233 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
111234 if( res<0 ){
111235 iMin = iTest+1;
111236 }else{
111237 i = iTest;
111238 }
@@ -110782,20 +111243,20 @@
111243 ** above found the right answer. This block serves no purpose other
111244 ** than to invoke the asserts. */
111245 if( res==0 ){
111246 /* If (res==0) is true, then sample $i must be equal to pRec */
111247 assert( i<pIdx->nSample );
111248 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
111249 || pParse->db->mallocFailed );
111250 }else{
111251 /* Otherwise, pRec must be smaller than sample $i and larger than
111252 ** sample ($i-1). */
111253 assert( i==pIdx->nSample
111254 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
111255 || pParse->db->mallocFailed );
111256 assert( i==0
111257 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
111258 || pParse->db->mallocFailed );
111259 }
111260 #endif /* ifdef SQLITE_DEBUG */
111261
111262 /* At this point, aSample[i] is the first sample that is greater than
@@ -114724,11 +115185,11 @@
115185
115186 /* For a co-routine, change all OP_Column references to the table of
115187 ** the co-routine into OP_SCopy of result contained in a register.
115188 ** OP_Rowid becomes OP_Null.
115189 */
115190 if( pTabItem->viaCoroutine && !db->mallocFailed ){
115191 last = sqlite3VdbeCurrentAddr(v);
115192 k = pLevel->addrBody;
115193 pOp = sqlite3VdbeGetOp(v, k);
115194 for(; k<last; k++, pOp++){
115195 if( pOp->p1!=pLevel->iTabCur ) continue;
115196
+692 -231
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -189,11 +189,11 @@
189189
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
190190
** [sqlite_version()] and [sqlite_source_id()].
191191
*/
192192
#define SQLITE_VERSION "3.8.4"
193193
#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 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
195195
196196
/*
197197
** CAPI3REF: Run-Time Library Version Numbers
198198
** KEYWORDS: sqlite3_version, sqlite3_sourceid
199199
**
@@ -9334,12 +9334,15 @@
93349334
#ifndef SQLITE_OMIT_TRACE
93359335
SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
93369336
#endif
93379337
93389338
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);
93409340
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*);
93419344
93429345
#ifndef SQLITE_OMIT_TRIGGER
93439346
SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
93449347
#endif
93459348
@@ -10950,23 +10953,23 @@
1095010953
** the OP_MakeRecord opcode of the VDBE and is disassembled by the
1095110954
** OP_Column opcode.
1095210955
**
1095310956
** This structure holds a record that has already been disassembled
1095410957
** into its constituent fields.
10958
+**
10959
+** The r1 and r2 member variables are only used by the optimized comparison
10960
+** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
1095510961
*/
1095610962
struct UnpackedRecord {
1095710963
KeyInfo *pKeyInfo; /* Collation and sort-order information */
1095810964
u16 nField; /* Number of entries in apMem[] */
10959
- u8 flags; /* Boolean settings. UNPACKED_... below */
10965
+ i8 default_rc; /* Comparison result if keys are equal */
1096010966
Mem *aMem; /* Values */
10967
+ int r1; /* Value to return if (lhs > rhs) */
10968
+ int r2; /* Value to return if (rhs < lhs) */
1096110969
};
1096210970
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 */
1096810971
1096910972
/*
1097010973
** Each SQL index is represented in memory by an
1097110974
** instance of the following structure.
1097210975
**
@@ -13840,11 +13843,11 @@
1384013843
** the following flags must be set to determine the memory management
1384113844
** policy for Mem.z. The MEM_Term flag tells us whether or not the
1384213845
** string is \000 or \u0000 terminated
1384313846
*/
1384413847
#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 */
1384613849
#define MEM_Static 0x0800 /* Mem.z points to a static string */
1384713850
#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
1384813851
#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
1384913852
#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
1385013853
#ifdef SQLITE_OMIT_INCRBLOB
@@ -14023,11 +14026,11 @@
1402314026
SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
1402414027
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
1402514028
SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
1402614029
1402714030
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*);
1402914032
SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
1403014033
SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1403114034
SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
1403214035
SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
1403314036
SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
@@ -14088,10 +14091,11 @@
1408814091
# define sqlite3VdbeLeave(X)
1408914092
#endif
1409014093
1409114094
#ifdef SQLITE_DEBUG
1409214095
SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
14096
+SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
1409314097
#endif
1409414098
1409514099
#ifndef SQLITE_OMIT_FOREIGN_KEY
1409614100
SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
1409714101
#else
@@ -21471,11 +21475,11 @@
2147121475
assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
2147221476
2147321477
sqlite3VdbeMemRelease(pMem);
2147421478
pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
2147521479
pMem->enc = desiredEnc;
21476
- pMem->flags |= (MEM_Term|MEM_Dyn);
21480
+ pMem->flags |= (MEM_Term);
2147721481
pMem->z = (char*)zOut;
2147821482
pMem->zMalloc = pMem->z;
2147921483
2148021484
translate_out:
2148121485
#if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
@@ -21599,11 +21603,10 @@
2159921603
sqlite3VdbeMemRelease(&m);
2160021604
m.z = 0;
2160121605
}
2160221606
assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
2160321607
assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
21604
- assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
2160521608
assert( m.z || db->mallocFailed );
2160621609
return m.z;
2160721610
}
2160821611
2160921612
/*
@@ -55305,10 +55308,11 @@
5530555308
i64 intKey, /* The table key */
5530655309
int biasRight, /* If true, bias the search to the high end */
5530755310
int *pRes /* Write search results here */
5530855311
){
5530955312
int rc;
55313
+ RecordCompare xRecordCompare;
5531055314
5531155315
assert( cursorHoldsMutex(pCur) );
5531255316
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5531355317
assert( pRes );
5531455318
assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
@@ -55325,10 +55329,20 @@
5532555329
if( pCur->atLast && pCur->info.nKey<intKey ){
5532655330
*pRes = -1;
5532755331
return SQLITE_OK;
5532855332
}
5532955333
}
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
+ }
5533055344
5533155345
rc = moveToRoot(pCur);
5533255346
if( rc ){
5533355347
return rc;
5533455348
}
@@ -55410,18 +55424,18 @@
5541055424
if( nCell<=pPage->max1bytePayload ){
5541155425
/* This branch runs if the record-size field of the cell is a
5541255426
** single byte varint and the record fits entirely on the main
5541355427
** b-tree page. */
5541455428
testcase( pCell+nCell+1==pPage->aDataEnd );
55415
- c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
55429
+ c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
5541655430
}else if( !(pCell[1] & 0x80)
5541755431
&& (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5541855432
){
5541955433
/* The record-size field is a 2 byte varint and the record
5542055434
** fits entirely on the main b-tree page. */
5542155435
testcase( pCell+nCell+2==pPage->aDataEnd );
55422
- c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
55436
+ c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
5542355437
}else{
5542455438
/* The record flows over onto one or more overflow pages. In
5542555439
** this case the whole cell needs to be parsed, a buffer allocated
5542655440
** and accessPayload() used to retrieve the record into the
5542755441
** buffer before VdbeRecordCompare() can be called. */
@@ -55438,11 +55452,11 @@
5543855452
rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
5543955453
if( rc ){
5544055454
sqlite3_free(pCellKey);
5544155455
goto moveto_finish;
5544255456
}
55443
- c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
55457
+ c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
5544455458
sqlite3_free(pCellKey);
5544555459
}
5544655460
if( c<0 ){
5544755461
lwr = idx+1;
5544855462
}else if( c>0 ){
@@ -60001,10 +60015,46 @@
6000160015
** This file contains code use to manipulate "Mem" structure. A "Mem"
6000260016
** stores a single value in the VDBE. Mem is an opaque structure visible
6000360017
** only within the VDBE. Interface routines refer to a Mem using the
6000460018
** name sqlite_value
6000560019
*/
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
+
6000660056
6000760057
/*
6000860058
** If pMem is an object with a valid string representation, this routine
6000960059
** ensures the internal encoding for the string representation is
6001060060
** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -60051,16 +60101,11 @@
6005160101
** pMem->z into the new allocation. pMem must be either a string or
6005260102
** blob if bPreserve is true. If bPreserve is false, any prior content
6005360103
** in pMem->z is discarded.
6005460104
*/
6005560105
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) );
6006260107
assert( (pMem->flags&MEM_RowSet)==0 );
6006360108
6006460109
/* If the bPreserve flag is set to true, then the memory cell must already
6006560110
** contain a valid string or blob value. */
6006660111
assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
@@ -60074,26 +60119,26 @@
6007460119
}else{
6007560120
sqlite3DbFree(pMem->db, pMem->zMalloc);
6007660121
pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
6007760122
}
6007860123
if( pMem->zMalloc==0 ){
60079
- sqlite3VdbeMemRelease(pMem);
60124
+ VdbeMemRelease(pMem);
6008060125
pMem->flags = MEM_Null;
6008160126
return SQLITE_NOMEM;
6008260127
}
6008360128
}
6008460129
6008560130
if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
6008660131
memcpy(pMem->zMalloc, pMem->z, pMem->n);
6008760132
}
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 );
6009060135
pMem->xDel((void *)(pMem->z));
6009160136
}
6009260137
6009360138
pMem->z = pMem->zMalloc;
60094
- pMem->flags &= ~(MEM_Ephem|MEM_Static);
60139
+ pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
6009560140
pMem->xDel = 0;
6009660141
return SQLITE_OK;
6009760142
}
6009860143
6009960144
/*
@@ -60258,13 +60303,13 @@
6025860303
assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
6025960304
if( p->flags&MEM_Agg ){
6026060305
sqlite3VdbeMemFinalize(p, p->u.pDef);
6026160306
assert( (p->flags & MEM_Agg)==0 );
6026260307
sqlite3VdbeMemRelease(p);
60263
- }else if( p->flags&MEM_Dyn && p->xDel ){
60308
+ }else if( p->flags&MEM_Dyn ){
6026460309
assert( (p->flags&MEM_RowSet)==0 );
60265
- assert( p->xDel!=SQLITE_DYNAMIC );
60310
+ assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
6026660311
p->xDel((void *)p->z);
6026760312
p->xDel = 0;
6026860313
}else if( p->flags&MEM_RowSet ){
6026960314
sqlite3RowSetClear(p->u.pRowSet);
6027060315
}else if( p->flags&MEM_Frame ){
@@ -60276,10 +60321,11 @@
6027660321
** Release any memory held by the Mem. This may leave the Mem in an
6027760322
** inconsistent state, for example with (Mem.z==0) and
6027860323
** (Mem.memType==MEM_Str).
6027960324
*/
6028060325
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
60326
+ assert( sqlite3VdbeCheckMemInvariants(p) );
6028160327
VdbeMemRelease(p);
6028260328
if( p->zMalloc ){
6028360329
sqlite3DbFree(p->db, p->zMalloc);
6028460330
p->zMalloc = 0;
6028560331
}
@@ -60613,10 +60659,11 @@
6061360659
6061460660
assert( (pFrom->flags & MEM_RowSet)==0 );
6061560661
VdbeMemRelease(pTo);
6061660662
memcpy(pTo, pFrom, MEMCELLSIZE);
6061760663
pTo->flags &= ~MEM_Dyn;
60664
+ pTo->xDel = 0;
6061860665
6061960666
if( pTo->flags&(MEM_Str|MEM_Blob) ){
6062060667
if( 0==(pFrom->flags&MEM_Static) ){
6062160668
pTo->flags |= MEM_Ephem;
6062260669
rc = sqlite3VdbeMemMakeWriteable(pTo);
@@ -60738,123 +60785,10 @@
6073860785
}
6073960786
6074060787
return SQLITE_OK;
6074160788
}
6074260789
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
-
6085660790
/*
6085760791
** Move data out of a btree key or data field and into a Mem structure.
6085860792
** The data or key is taken from the entry that pCur is currently pointing
6085960793
** to. offset and amt determine what portion of the data or key to retrieve.
6086060794
** key is true to get the key or false to get data. The result is written
@@ -60892,11 +60826,11 @@
6089260826
if( offset+amt<=available ){
6089360827
sqlite3VdbeMemRelease(pMem);
6089460828
pMem->z = &zData[offset];
6089560829
pMem->flags = MEM_Blob|MEM_Ephem;
6089660830
}else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
60897
- pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
60831
+ pMem->flags = MEM_Blob|MEM_Term;
6089860832
pMem->enc = 0;
6089960833
pMem->memType = MEM_Blob;
6090060834
if( key ){
6090160835
rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
6090260836
}else{
@@ -61010,11 +60944,10 @@
6101060944
if( pRec ){
6101160945
pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
6101260946
if( pRec->pKeyInfo ){
6101360947
assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
6101460948
assert( pRec->pKeyInfo->enc==ENC(db) );
61015
- pRec->flags = UNPACKED_PREFIX_MATCH;
6101660949
pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
6101760950
for(i=0; i<nCol; i++){
6101860951
pRec->aMem[i].flags = MEM_Null;
6101960952
pRec->aMem[i].memType = MEM_Null;
6102060953
pRec->aMem[i].db = db;
@@ -62591,10 +62524,11 @@
6259162524
}
6259262525
return;
6259362526
}
6259462527
for(pEnd=&p[N]; p<pEnd; p++){
6259562528
assert( (&p[1])==pEnd || p[0].db==p[1].db );
62529
+ assert( sqlite3VdbeCheckMemInvariants(p) );
6259662530
6259762531
/* This block is really an inlined version of sqlite3VdbeMemRelease()
6259862532
** that takes advantage of the fact that the memory cell value is
6259962533
** being set to NULL after releasing any dynamic resources.
6260062534
**
@@ -62784,11 +62718,11 @@
6278462718
6278562719
if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
6278662720
assert( p->db->mallocFailed );
6278762721
return SQLITE_ERROR;
6278862722
}
62789
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62723
+ pMem->flags = MEM_Str|MEM_Term;
6279062724
zP4 = displayP4(pOp, pMem->z, 32);
6279162725
if( zP4!=pMem->z ){
6279262726
sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
6279362727
}else{
6279462728
assert( pMem->z!=0 );
@@ -62801,11 +62735,11 @@
6280162735
if( p->explain==1 ){
6280262736
if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
6280362737
assert( p->db->mallocFailed );
6280462738
return SQLITE_ERROR;
6280562739
}
62806
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62740
+ pMem->flags = MEM_Str|MEM_Term;
6280762741
pMem->n = 2;
6280862742
sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
6280962743
pMem->memType = MEM_Str;
6281062744
pMem->enc = SQLITE_UTF8;
6281162745
pMem++;
@@ -62813,11 +62747,11 @@
6281362747
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
6281462748
if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
6281562749
assert( p->db->mallocFailed );
6281662750
return SQLITE_ERROR;
6281762751
}
62818
- pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62752
+ pMem->flags = MEM_Str|MEM_Term;
6281962753
pMem->n = displayComment(pOp, zP4, pMem->z, 500);
6282062754
pMem->memType = MEM_Str;
6282162755
pMem->enc = SQLITE_UTF8;
6282262756
#else
6282362757
pMem->flags = MEM_Null; /* Comment */
@@ -64312,10 +64246,18 @@
6431264246
6431364247
/* NULL or constants 0 or 1 */
6431464248
return 0;
6431564249
}
6431664250
64251
+/* Input "x" is a sequence of unsigned characters that represent a
64252
+** big-endian integer. Return the equivalent native integer
64253
+*/
64254
+#define ONE_BYTE_INT(x) ((i8)(x)[0])
64255
+#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
64256
+#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
64257
+#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
64258
+
6431764259
/*
6431864260
** Deserialize the data blob pointed to by buf as serial type serial_type
6431964261
** and store the result in pMem. Return the number of bytes read.
6432064262
*/
6432164263
SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
@@ -64323,46 +64265,40 @@
6432364265
u32 serial_type, /* Serial type to deserialize */
6432464266
Mem *pMem /* Memory cell to write value into */
6432564267
){
6432664268
u64 x;
6432764269
u32 y;
64328
- int i;
6432964270
switch( serial_type ){
6433064271
case 10: /* Reserved for future use */
6433164272
case 11: /* Reserved for future use */
6433264273
case 0: { /* NULL */
6433364274
pMem->flags = MEM_Null;
6433464275
break;
6433564276
}
6433664277
case 1: { /* 1-byte signed integer */
64337
- pMem->u.i = (signed char)buf[0];
64278
+ pMem->u.i = ONE_BYTE_INT(buf);
6433864279
pMem->flags = MEM_Int;
6433964280
return 1;
6434064281
}
6434164282
case 2: { /* 2-byte signed integer */
64342
- i = 256*(signed char)buf[0] | buf[1];
64343
- pMem->u.i = (i64)i;
64283
+ pMem->u.i = TWO_BYTE_INT(buf);
6434464284
pMem->flags = MEM_Int;
6434564285
return 2;
6434664286
}
6434764287
case 3: { /* 3-byte signed integer */
64348
- i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
64349
- pMem->u.i = (i64)i;
64288
+ pMem->u.i = THREE_BYTE_INT(buf);
6435064289
pMem->flags = MEM_Int;
6435164290
return 3;
6435264291
}
6435364292
case 4: { /* 4-byte signed integer */
64354
- y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
64293
+ y = FOUR_BYTE_UINT(buf);
6435564294
pMem->u.i = (i64)*(int*)&y;
6435664295
pMem->flags = MEM_Int;
6435764296
return 4;
6435864297
}
6435964298
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;
64299
+ pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
6436464300
pMem->flags = MEM_Int;
6436564301
return 6;
6436664302
}
6436764303
case 6: /* 8-byte signed integer */
6436864304
case 7: { /* IEEE floating point */
@@ -64376,12 +64312,12 @@
6437664312
static const double r1 = 1.0;
6437764313
u64 t2 = t1;
6437864314
swapMixedEndianFloat(t2);
6437964315
assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
6438064316
#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];
64317
+ x = FOUR_BYTE_UINT(buf);
64318
+ y = FOUR_BYTE_UINT(buf+4);
6438364319
x = (x<<32) | y;
6438464320
if( serial_type==6 ){
6438564321
pMem->u.i = *(i64*)&x;
6438664322
pMem->flags = MEM_Int;
6438764323
}else{
@@ -64473,11 +64409,11 @@
6447364409
u32 idx; /* Offset in aKey[] to read from */
6447464410
u16 u; /* Unsigned loop counter */
6447564411
u32 szHdr;
6447664412
Mem *pMem = p->aMem;
6447764413
64478
- p->flags = 0;
64414
+ p->default_rc = 0;
6447964415
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
6448064416
idx = getVarint32(aKey, szHdr);
6448164417
d = szHdr;
6448264418
u = 0;
6448364419
while( idx<szHdr && u<p->nField && d<=nKey ){
@@ -64494,30 +64430,22 @@
6449464430
}
6449564431
assert( u<=pKeyInfo->nField + 1 );
6449664432
p->nField = u;
6449764433
}
6449864434
64435
+#if SQLITE_DEBUG
6449964436
/*
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.
64437
+** This function compares two index or table record keys in the same way
64438
+** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
64439
+** this function deserializes and compares values using the
64440
+** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
64441
+** in assert() statements to ensure that the optimized code in
64442
+** sqlite3VdbeRecordCompare() returns results with these two primitives.
6451564443
*/
64516
-SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64444
+static int vdbeRecordCompareDebug(
6451764445
int nKey1, const void *pKey1, /* Left key */
64518
- UnpackedRecord *pPKey2 /* Right key */
64446
+ const UnpackedRecord *pPKey2 /* Right key */
6451964447
){
6452064448
u32 d1; /* Offset into aKey[] of next data element */
6452164449
u32 idx1; /* Offset into aKey[] of next header element */
6452264450
u32 szHdr1; /* Number of bytes in header */
6452364451
int i = 0;
@@ -64587,28 +64515,558 @@
6458764515
** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
6458864516
*/
6458964517
assert( mem1.zMalloc==0 );
6459064518
6459164519
/* 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
-
64520
+ ** all the fields up to that point were equal. Return the the default_rc
64521
+ ** value. */
64522
+ return pPKey2->default_rc;
64523
+}
64524
+#endif
64525
+
64526
+/*
64527
+** Both *pMem1 and *pMem2 contain string values. Compare the two values
64528
+** using the collation sequence pColl. As usual, return a negative , zero
64529
+** or positive value if *pMem1 is less than, equal to or greater than
64530
+** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
64531
+*/
64532
+static int vdbeCompareMemString(
64533
+ const Mem *pMem1,
64534
+ const Mem *pMem2,
64535
+ const CollSeq *pColl
64536
+){
64537
+ if( pMem1->enc==pColl->enc ){
64538
+ /* The strings are already in the correct encoding. Call the
64539
+ ** comparison function directly */
64540
+ return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
64541
+ }else{
64542
+ int rc;
64543
+ const void *v1, *v2;
64544
+ int n1, n2;
64545
+ Mem c1;
64546
+ Mem c2;
64547
+ memset(&c1, 0, sizeof(c1));
64548
+ memset(&c2, 0, sizeof(c2));
64549
+ sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
64550
+ sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
64551
+ v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
64552
+ n1 = v1==0 ? 0 : c1.n;
64553
+ v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
64554
+ n2 = v2==0 ? 0 : c2.n;
64555
+ rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
64556
+ sqlite3VdbeMemRelease(&c1);
64557
+ sqlite3VdbeMemRelease(&c2);
64558
+ return rc;
64559
+ }
64560
+}
64561
+
64562
+/*
64563
+** Compare the values contained by the two memory cells, returning
64564
+** negative, zero or positive if pMem1 is less than, equal to, or greater
64565
+** than pMem2. Sorting order is NULL's first, followed by numbers (integers
64566
+** and reals) sorted numerically, followed by text ordered by the collating
64567
+** sequence pColl and finally blob's ordered by memcmp().
64568
+**
64569
+** Two NULL values are considered equal by this function.
64570
+*/
64571
+SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
64572
+ int rc;
64573
+ int f1, f2;
64574
+ int combined_flags;
64575
+
64576
+ f1 = pMem1->flags;
64577
+ f2 = pMem2->flags;
64578
+ combined_flags = f1|f2;
64579
+ assert( (combined_flags & MEM_RowSet)==0 );
64580
+
64581
+ /* If one value is NULL, it is less than the other. If both values
64582
+ ** are NULL, return 0.
64583
+ */
64584
+ if( combined_flags&MEM_Null ){
64585
+ return (f2&MEM_Null) - (f1&MEM_Null);
64586
+ }
64587
+
64588
+ /* If one value is a number and the other is not, the number is less.
64589
+ ** If both are numbers, compare as reals if one is a real, or as integers
64590
+ ** if both values are integers.
64591
+ */
64592
+ if( combined_flags&(MEM_Int|MEM_Real) ){
64593
+ double r1, r2;
64594
+ if( (f1 & f2 & MEM_Int)!=0 ){
64595
+ if( pMem1->u.i < pMem2->u.i ) return -1;
64596
+ if( pMem1->u.i > pMem2->u.i ) return 1;
64597
+ return 0;
64598
+ }
64599
+ if( (f1&MEM_Real)!=0 ){
64600
+ r1 = pMem1->r;
64601
+ }else if( (f1&MEM_Int)!=0 ){
64602
+ r1 = (double)pMem1->u.i;
64603
+ }else{
64604
+ return 1;
64605
+ }
64606
+ if( (f2&MEM_Real)!=0 ){
64607
+ r2 = pMem2->r;
64608
+ }else if( (f2&MEM_Int)!=0 ){
64609
+ r2 = (double)pMem2->u.i;
64610
+ }else{
64611
+ return -1;
64612
+ }
64613
+ if( r1<r2 ) return -1;
64614
+ if( r1>r2 ) return 1;
64615
+ return 0;
64616
+ }
64617
+
64618
+ /* If one value is a string and the other is a blob, the string is less.
64619
+ ** If both are strings, compare using the collating functions.
64620
+ */
64621
+ if( combined_flags&MEM_Str ){
64622
+ if( (f1 & MEM_Str)==0 ){
64623
+ return 1;
64624
+ }
64625
+ if( (f2 & MEM_Str)==0 ){
64626
+ return -1;
64627
+ }
64628
+
64629
+ assert( pMem1->enc==pMem2->enc );
64630
+ assert( pMem1->enc==SQLITE_UTF8 ||
64631
+ pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
64632
+
64633
+ /* The collation sequence must be defined at this point, even if
64634
+ ** the user deletes the collation sequence after the vdbe program is
64635
+ ** compiled (this was not always the case).
64636
+ */
64637
+ assert( !pColl || pColl->xCmp );
64638
+
64639
+ if( pColl ){
64640
+ return vdbeCompareMemString(pMem1, pMem2, pColl);
64641
+ }
64642
+ /* If a NULL pointer was passed as the collate function, fall through
64643
+ ** to the blob case and use memcmp(). */
64644
+ }
64645
+
64646
+ /* Both values must be blobs. Compare using memcmp(). */
64647
+ rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
64648
+ if( rc==0 ){
64649
+ rc = pMem1->n - pMem2->n;
64650
+ }
64651
+ return rc;
64652
+}
64653
+
64654
+
64655
+/*
64656
+** The first argument passed to this function is a serial-type that
64657
+** corresponds to an integer - all values between 1 and 9 inclusive
64658
+** except 7. The second points to a buffer containing an integer value
64659
+** serialized according to serial_type. This function deserializes
64660
+** and returns the value.
64661
+*/
64662
+static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
64663
+ u32 y;
64664
+ assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
64665
+ switch( serial_type ){
64666
+ case 0:
64667
+ case 1:
64668
+ return ONE_BYTE_INT(aKey);
64669
+ case 2:
64670
+ return TWO_BYTE_INT(aKey);
64671
+ case 3:
64672
+ return THREE_BYTE_INT(aKey);
64673
+ case 4: {
64674
+ y = FOUR_BYTE_UINT(aKey);
64675
+ return (i64)*(int*)&y;
64676
+ }
64677
+ case 5: {
64678
+ return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64679
+ }
64680
+ case 6: {
64681
+ u64 x = FOUR_BYTE_UINT(aKey);
64682
+ x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64683
+ return (i64)*(i64*)&x;
64684
+ }
64685
+ }
64686
+
64687
+ return (serial_type - 8);
64688
+}
64689
+
64690
+/*
64691
+** This function compares the two table rows or index records
64692
+** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
64693
+** or positive integer if key1 is less than, equal to or
64694
+** greater than key2. The {nKey1, pKey1} key must be a blob
64695
+** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
64696
+** key must be a parsed key such as obtained from
64697
+** sqlite3VdbeParseRecord.
64698
+**
64699
+** If argument bSkip is non-zero, it is assumed that the caller has already
64700
+** determined that the first fields of the keys are equal.
64701
+**
64702
+** Key1 and Key2 do not have to contain the same number of fields. If all
64703
+** fields that appear in both keys are equal, then pPKey2->default_rc is
64704
+** returned.
64705
+*/
64706
+SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64707
+ int nKey1, const void *pKey1, /* Left key */
64708
+ const UnpackedRecord *pPKey2, /* Right key */
64709
+ int bSkip /* If true, skip the first field */
64710
+){
64711
+ u32 d1; /* Offset into aKey[] of next data element */
64712
+ int i; /* Index of next field to compare */
64713
+ int szHdr1; /* Size of record header in bytes */
64714
+ u32 idx1; /* Offset of first type in header */
64715
+ int rc = 0; /* Return value */
64716
+ Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
64717
+ KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
64718
+ const unsigned char *aKey1 = (const unsigned char *)pKey1;
64719
+ Mem mem1;
64720
+
64721
+ /* If bSkip is true, then the caller has already determined that the first
64722
+ ** two elements in the keys are equal. Fix the various stack variables so
64723
+ ** that this routine begins comparing at the second field. */
64724
+ if( bSkip ){
64725
+ u32 s1;
64726
+ idx1 = 1 + getVarint32(&aKey1[1], s1);
64727
+ szHdr1 = aKey1[0];
64728
+ d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
64729
+ i = 1;
64730
+ pRhs++;
64731
+ }else{
64732
+ idx1 = getVarint32(aKey1, szHdr1);
64733
+ d1 = szHdr1;
64734
+ i = 0;
64735
+ }
64736
+
64737
+ VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64738
+ assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
64739
+ || CORRUPT_DB );
64740
+ assert( pPKey2->pKeyInfo->aSortOrder!=0 );
64741
+ assert( pPKey2->pKeyInfo->nField>0 );
64742
+ assert( idx1<=szHdr1 || CORRUPT_DB );
64743
+ do{
64744
+ u32 serial_type;
64745
+
64746
+ /* RHS is an integer */
64747
+ if( pRhs->flags & MEM_Int ){
64748
+ serial_type = aKey1[idx1];
64749
+ if( serial_type>=12 ){
64750
+ rc = +1;
64751
+ }else if( serial_type==0 ){
64752
+ rc = -1;
64753
+ }else if( serial_type==7 ){
64754
+ double rhs = (double)pRhs->u.i;
64755
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64756
+ if( mem1.r<rhs ){
64757
+ rc = -1;
64758
+ }else if( mem1.r>rhs ){
64759
+ rc = +1;
64760
+ }
64761
+ }else{
64762
+ i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
64763
+ i64 rhs = pRhs->u.i;
64764
+ if( lhs<rhs ){
64765
+ rc = -1;
64766
+ }else if( lhs>rhs ){
64767
+ rc = +1;
64768
+ }
64769
+ }
64770
+ }
64771
+
64772
+ /* RHS is real */
64773
+ else if( pRhs->flags & MEM_Real ){
64774
+ serial_type = aKey1[idx1];
64775
+ if( serial_type>=12 ){
64776
+ rc = +1;
64777
+ }else if( serial_type==0 ){
64778
+ rc = -1;
64779
+ }else{
64780
+ double rhs = pRhs->r;
64781
+ double lhs;
64782
+ sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64783
+ if( serial_type==7 ){
64784
+ lhs = mem1.r;
64785
+ }else{
64786
+ lhs = (double)mem1.u.i;
64787
+ }
64788
+ if( lhs<rhs ){
64789
+ rc = -1;
64790
+ }else if( lhs>rhs ){
64791
+ rc = +1;
64792
+ }
64793
+ }
64794
+ }
64795
+
64796
+ /* RHS is a string */
64797
+ else if( pRhs->flags & MEM_Str ){
64798
+ getVarint32(&aKey1[idx1], serial_type);
64799
+ if( serial_type<12 ){
64800
+ rc = -1;
64801
+ }else if( !(serial_type & 0x01) ){
64802
+ rc = +1;
64803
+ }else{
64804
+ mem1.n = (serial_type - 12) / 2;
64805
+ if( (d1+mem1.n) > (unsigned)nKey1 ){
64806
+ rc = 1; /* Corruption */
64807
+ }else if( pKeyInfo->aColl[i] ){
64808
+ mem1.enc = pKeyInfo->enc;
64809
+ mem1.db = pKeyInfo->db;
64810
+ mem1.flags = MEM_Str;
64811
+ mem1.z = (char*)&aKey1[d1];
64812
+ rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
64813
+ }else{
64814
+ int nCmp = MIN(mem1.n, pRhs->n);
64815
+ rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64816
+ if( rc==0 ) rc = mem1.n - pRhs->n;
64817
+ }
64818
+ }
64819
+ }
64820
+
64821
+ /* RHS is a blob */
64822
+ else if( pRhs->flags & MEM_Blob ){
64823
+ getVarint32(&aKey1[idx1], serial_type);
64824
+ if( serial_type<12 || (serial_type & 0x01) ){
64825
+ rc = -1;
64826
+ }else{
64827
+ int nStr = (serial_type - 12) / 2;
64828
+ if( (d1+nStr) > (unsigned)nKey1 ){
64829
+ rc = 1; /* Corruption */
64830
+ }else{
64831
+ int nCmp = MIN(nStr, pRhs->n);
64832
+ rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64833
+ if( rc==0 ) rc = nStr - pRhs->n;
64834
+ }
64835
+ }
64836
+ }
64837
+
64838
+ /* RHS is null */
64839
+ else{
64840
+ serial_type = aKey1[idx1];
64841
+ rc = (serial_type!=0);
64842
+ }
64843
+
64844
+ if( rc!=0 ){
64845
+ if( pKeyInfo->aSortOrder[i] ){
64846
+ rc = -rc;
64847
+ }
64848
+ assert( CORRUPT_DB
64849
+ || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64850
+ || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64851
+ );
64852
+ assert( mem1.zMalloc==0 ); /* See comment below */
64853
+ return rc;
64854
+ }
64855
+
64856
+ i++;
64857
+ pRhs++;
64858
+ d1 += sqlite3VdbeSerialTypeLen(serial_type);
64859
+ idx1 += sqlite3VarintLen(serial_type);
64860
+ }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
64861
+
64862
+ /* No memory allocation is ever used on mem1. Prove this using
64863
+ ** the following assert(). If the assert() fails, it indicates a
64864
+ ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
64865
+ assert( mem1.zMalloc==0 );
64866
+
64867
+ /* rc==0 here means that one or both of the keys ran out of fields and
64868
+ ** all the fields up to that point were equal. Return the the default_rc
64869
+ ** value. */
64870
+ assert( CORRUPT_DB
64871
+ || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
64872
+ );
64873
+ return pPKey2->default_rc;
64874
+}
64875
+
64876
+/*
64877
+** This function is an optimized version of sqlite3VdbeRecordCompare()
64878
+** that (a) the first field of pPKey2 is an integer, and (b) the
64879
+** size-of-header varint at the start of (pKey1/nKey1) fits in a single
64880
+** byte (i.e. is less than 128).
64881
+*/
64882
+static int vdbeRecordCompareInt(
64883
+ int nKey1, const void *pKey1, /* Left key */
64884
+ const UnpackedRecord *pPKey2, /* Right key */
64885
+ int bSkip /* Ignored */
64886
+){
64887
+ const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
64888
+ int serial_type = ((const u8*)pKey1)[1];
64889
+ int res;
64890
+ u32 y;
64891
+ u64 x;
64892
+ i64 v = pPKey2->aMem[0].u.i;
64893
+ i64 lhs;
64894
+ UNUSED_PARAMETER(bSkip);
64895
+
64896
+ assert( bSkip==0 );
64897
+ switch( serial_type ){
64898
+ case 1: { /* 1-byte signed integer */
64899
+ lhs = ONE_BYTE_INT(aKey);
64900
+ break;
64901
+ }
64902
+ case 2: { /* 2-byte signed integer */
64903
+ lhs = TWO_BYTE_INT(aKey);
64904
+ break;
64905
+ }
64906
+ case 3: { /* 3-byte signed integer */
64907
+ lhs = THREE_BYTE_INT(aKey);
64908
+ break;
64909
+ }
64910
+ case 4: { /* 4-byte signed integer */
64911
+ y = FOUR_BYTE_UINT(aKey);
64912
+ lhs = (i64)*(int*)&y;
64913
+ break;
64914
+ }
64915
+ case 5: { /* 6-byte signed integer */
64916
+ lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64917
+ break;
64918
+ }
64919
+ case 6: { /* 8-byte signed integer */
64920
+ x = FOUR_BYTE_UINT(aKey);
64921
+ x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64922
+ lhs = *(i64*)&x;
64923
+ break;
64924
+ }
64925
+ case 8:
64926
+ lhs = 0;
64927
+ break;
64928
+ case 9:
64929
+ lhs = 1;
64930
+ break;
64931
+
64932
+ /* This case could be removed without changing the results of running
64933
+ ** this code. Including it causes gcc to generate a faster switch
64934
+ ** statement (since the range of switch targets now starts at zero and
64935
+ ** is contiguous) but does not cause any duplicate code to be generated
64936
+ ** (as gcc is clever enough to combine the two like cases). Other
64937
+ ** compilers might be similar. */
64938
+ case 0: case 7:
64939
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64940
+
64941
+ default:
64942
+ return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64943
+ }
64944
+
64945
+ if( v>lhs ){
64946
+ res = pPKey2->r1;
64947
+ }else if( v<lhs ){
64948
+ res = pPKey2->r2;
64949
+ }else if( pPKey2->nField>1 ){
64950
+ /* The first fields of the two keys are equal. Compare the trailing
64951
+ ** fields. */
64952
+ res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
64953
+ }else{
64954
+ /* The first fields of the two keys are equal and there are no trailing
64955
+ ** fields. Return pPKey2->default_rc in this case. */
64956
+ res = pPKey2->default_rc;
64957
+ }
64958
+
64959
+ assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
64960
+ || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64961
+ || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64962
+ || CORRUPT_DB
64963
+ );
64964
+ return res;
64965
+}
64966
+
64967
+/*
64968
+** This function is an optimized version of sqlite3VdbeRecordCompare()
64969
+** that (a) the first field of pPKey2 is a string, that (b) the first field
64970
+** uses the collation sequence BINARY and (c) that the size-of-header varint
64971
+** at the start of (pKey1/nKey1) fits in a single byte.
64972
+*/
64973
+static int vdbeRecordCompareString(
64974
+ int nKey1, const void *pKey1, /* Left key */
64975
+ const UnpackedRecord *pPKey2, /* Right key */
64976
+ int bSkip
64977
+){
64978
+ const u8 *aKey1 = (const u8*)pKey1;
64979
+ int serial_type;
64980
+ int res;
64981
+ UNUSED_PARAMETER(bSkip);
64982
+
64983
+ assert( bSkip==0 );
64984
+ getVarint32(&aKey1[1], serial_type);
64985
+
64986
+ if( serial_type<12 ){
64987
+ res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
64988
+ }else if( !(serial_type & 0x01) ){
64989
+ res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
64990
+ }else{
64991
+ int nCmp;
64992
+ int nStr;
64993
+ int szHdr = aKey1[0];
64994
+
64995
+ nStr = (serial_type-12) / 2;
64996
+ if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */
64997
+ nCmp = MIN( pPKey2->aMem[0].n, nStr );
64998
+ res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
64999
+
65000
+ if( res==0 ){
65001
+ res = nStr - pPKey2->aMem[0].n;
65002
+ if( res==0 ){
65003
+ if( pPKey2->nField>1 ){
65004
+ res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
65005
+ }else{
65006
+ res = pPKey2->default_rc;
65007
+ }
65008
+ }else if( res>0 ){
65009
+ res = pPKey2->r2;
65010
+ }else{
65011
+ res = pPKey2->r1;
65012
+ }
65013
+ }else if( res>0 ){
65014
+ res = pPKey2->r2;
65015
+ }else{
65016
+ res = pPKey2->r1;
65017
+ }
65018
+ }
65019
+
65020
+ assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65021
+ || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65022
+ || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65023
+ || CORRUPT_DB
65024
+ );
65025
+ return res;
65026
+}
65027
+
65028
+/*
65029
+** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
65030
+** suitable for comparing serialized records to the unpacked record passed
65031
+** as the only argument.
65032
+*/
65033
+SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
65034
+ /* varintRecordCompareInt() and varintRecordCompareString() both assume
65035
+ ** that the size-of-header varint that occurs at the start of each record
65036
+ ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
65037
+ ** also assumes that it is safe to overread a buffer by at least the
65038
+ ** maximum possible legal header size plus 8 bytes. Because there is
65039
+ ** guaranteed to be at least 74 (but not 136) bytes of padding following each
65040
+ ** buffer passed to varintRecordCompareInt() this makes it convenient to
65041
+ ** limit the size of the header to 64 bytes in cases where the first field
65042
+ ** is an integer.
65043
+ **
65044
+ ** The easiest way to enforce this limit is to consider only records with
65045
+ ** 13 fields or less. If the first field is an integer, the maximum legal
65046
+ ** header size is (12*5 + 1 + 1) bytes. */
65047
+ if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
65048
+ int flags = p->aMem[0].flags;
65049
+ if( p->pKeyInfo->aSortOrder[0] ){
65050
+ p->r1 = 1;
65051
+ p->r2 = -1;
65052
+ }else{
65053
+ p->r1 = -1;
65054
+ p->r2 = 1;
65055
+ }
65056
+ if( (flags & MEM_Int) ){
65057
+ return vdbeRecordCompareInt;
65058
+ }
65059
+ if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0
65060
+ && p->pKeyInfo->aColl[0]==0
65061
+ ){
65062
+ return vdbeRecordCompareString;
65063
+ }
65064
+ }
65065
+
65066
+ return sqlite3VdbeRecordCompare;
65067
+}
6461065068
6461165069
/*
6461265070
** pCur points at an index entry created using the OP_MakeRecord opcode.
6461365071
** Read the rowid (the last field in the record) and store it in *rowid.
6461465072
** Return SQLITE_OK if everything works, or an error code otherwise.
@@ -64695,23 +65153,23 @@
6469565153
** omits the rowid at the end. The rowid at the end of the index entry
6469665154
** is ignored as well. Hence, this routine only compares the prefixes
6469765155
** of the keys prior to the final rowid, not the entire key.
6469865156
*/
6469965157
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 */
65158
+ VdbeCursor *pC, /* The cursor to compare against */
65159
+ const UnpackedRecord *pUnpacked, /* Unpacked version of key */
65160
+ int *res /* Write the comparison result here */
6470365161
){
6470465162
i64 nCellKey = 0;
6470565163
int rc;
6470665164
BtCursor *pCur = pC->pCursor;
6470765165
Mem m;
6470865166
6470965167
assert( sqlite3BtreeCursorIsValid(pCur) );
6471065168
VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
6471165169
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
65170
+ /* nCellKey will always be between 0 and 0xffffffff because of the way
6471365171
** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6471465172
if( nCellKey<=0 || nCellKey>0x7fffffff ){
6471565173
*res = 0;
6471665174
return SQLITE_CORRUPT_BKPT;
6471765175
}
@@ -64718,12 +65176,11 @@
6471865176
memset(&m, 0, sizeof(m));
6471965177
rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
6472065178
if( rc ){
6472165179
return rc;
6472265180
}
64723
- assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
64724
- *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
65181
+ *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
6472565182
sqlite3VdbeMemRelease(&m);
6472665183
return SQLITE_OK;
6472765184
}
6472865185
6472965186
/*
@@ -66629,11 +67086,11 @@
6662967086
** does not control the string, it might be deleted without the register
6663067087
** knowing it.
6663167088
**
6663267089
** This routine converts an ephemeral string into a dynamically allocated
6663367090
** string that the register itself controls. In other words, it
66634
-** converts an MEM_Ephem string into an MEM_Dyn string.
67091
+** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
6663567092
*/
6663667093
#define Deephemeralize(P) \
6663767094
if( ((P)->flags&MEM_Ephem)!=0 \
6663867095
&& sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
6663967096
@@ -67166,22 +67623,25 @@
6716667623
#ifdef SQLITE_DEBUG
6716767624
if( (pOp->opflags & OPFLG_IN1)!=0 ){
6716867625
assert( pOp->p1>0 );
6716967626
assert( pOp->p1<=(p->nMem-p->nCursor) );
6717067627
assert( memIsValid(&aMem[pOp->p1]) );
67628
+ assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
6717167629
REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
6717267630
}
6717367631
if( (pOp->opflags & OPFLG_IN2)!=0 ){
6717467632
assert( pOp->p2>0 );
6717567633
assert( pOp->p2<=(p->nMem-p->nCursor) );
6717667634
assert( memIsValid(&aMem[pOp->p2]) );
67635
+ assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
6717767636
REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
6717867637
}
6717967638
if( (pOp->opflags & OPFLG_IN3)!=0 ){
6718067639
assert( pOp->p3>0 );
6718167640
assert( pOp->p3<=(p->nMem-p->nCursor) );
6718267641
assert( memIsValid(&aMem[pOp->p3]) );
67642
+ assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
6718367643
REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
6718467644
}
6718567645
if( (pOp->opflags & OPFLG_OUT2)!=0 ){
6718667646
assert( pOp->p2>0 );
6718767647
assert( pOp->p2<=(p->nMem-p->nCursor) );
@@ -67279,11 +67739,11 @@
6727967739
** and then jump to address P2.
6728067740
*/
6728167741
case OP_Gosub: { /* jump */
6728267742
assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
6728367743
pIn1 = &aMem[pOp->p1];
67284
- assert( (pIn1->flags & MEM_Dyn)==0 );
67744
+ assert( VdbeMemDynamic(pIn1)==0 );
6728567745
memAboutToChange(p, pIn1);
6728667746
pIn1->flags = MEM_Int;
6728767747
pIn1->u.i = pc;
6728867748
REGISTER_TRACE(pOp->p1, pIn1);
6728967749
pc = pOp->p2 - 1;
@@ -67352,11 +67812,11 @@
6735267812
** OP_EndCoroutine, jump immediately to P2.
6735367813
*/
6735467814
case OP_Yield: { /* in1, jump */
6735567815
int pcDest;
6735667816
pIn1 = &aMem[pOp->p1];
67357
- assert( (pIn1->flags & MEM_Dyn)==0 );
67817
+ assert( VdbeMemDynamic(pIn1)==0 );
6735867818
pIn1->flags = MEM_Int;
6735967819
pcDest = (int)pIn1->u.i;
6736067820
pIn1->u.i = pc;
6736167821
REGISTER_TRACE(pOp->p1, pIn1);
6736267822
pc = pcDest;
@@ -67525,14 +67985,13 @@
6752567985
if( encoding!=SQLITE_UTF8 ){
6752667986
rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
6752767987
if( rc==SQLITE_TOOBIG ) goto too_big;
6752867988
if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
6752967989
assert( pOut->zMalloc==pOut->z );
67530
- assert( pOut->flags & MEM_Dyn );
67990
+ assert( VdbeMemDynamic(pOut)==0 );
6753167991
pOut->zMalloc = 0;
6753267992
pOut->flags |= MEM_Static;
67533
- pOut->flags &= ~MEM_Dyn;
6753467993
if( pOp->p4type==P4_DYNAMIC ){
6753567994
sqlite3DbFree(db, pOp->p4.z);
6753667995
}
6753767996
pOp->p4type = P4_DYNAMIC;
6753867997
pOp->p4.z = pOut->z;
@@ -67664,18 +68123,20 @@
6766468123
do{
6766568124
assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
6766668125
assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
6766768126
assert( memIsValid(pIn1) );
6766868127
memAboutToChange(p, pOut);
68128
+ VdbeMemRelease(pOut);
6766968129
zMalloc = pOut->zMalloc;
67670
- pOut->zMalloc = 0;
67671
- sqlite3VdbeMemMove(pOut, pIn1);
68130
+ memcpy(pOut, pIn1, sizeof(Mem));
6767268131
#ifdef SQLITE_DEBUG
6767368132
if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
6767468133
pOut->pScopyFrom += p1 - pOp->p2;
6767568134
}
6767668135
#endif
68136
+ pIn1->flags = MEM_Undefined;
68137
+ pIn1->xDel = 0;
6767768138
pIn1->zMalloc = zMalloc;
6767868139
REGISTER_TRACE(p2++, pOut);
6767968140
pIn1++;
6768068141
pOut++;
6768168142
}while( n-- );
@@ -67848,14 +68309,14 @@
6784868309
Stringify(pIn2, encoding);
6784968310
nByte = pIn1->n + pIn2->n;
6785068311
if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6785168312
goto too_big;
6785268313
}
67853
- MemSetTypeFlag(pOut, MEM_Str);
6785468314
if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
6785568315
goto no_mem;
6785668316
}
68317
+ MemSetTypeFlag(pOut, MEM_Str);
6785768318
if( pOut!=pIn2 ){
6785868319
memcpy(pOut->z, pIn2->z, pIn2->n);
6785968320
}
6786068321
memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
6786168322
pOut->z[nByte]=0;
@@ -69026,10 +69487,11 @@
6902669487
** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
6902769488
** all valid.
6902869489
*/
6902969490
assert( p2<pC->nHdrParsed );
6903069491
assert( rc==SQLITE_OK );
69492
+ assert( sqlite3VdbeCheckMemInvariants(pDest) );
6903169493
if( pC->szRow>=aOffset[p2+1] ){
6903269494
/* This is the common case where the desired content fits on the original
6903369495
** page - where the content is not on an overflow page */
6903469496
VdbeMemRelease(pDest);
6903569497
sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
@@ -69063,12 +69525,12 @@
6906369525
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
6906469526
** dynamically allocated space over to the pDest structure.
6906569527
** This prevents a memory copy. */
6906669528
if( sMem.zMalloc ){
6906769529
assert( sMem.z==sMem.zMalloc );
69068
- assert( !(pDest->flags & MEM_Dyn) );
69069
- assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
69530
+ assert( VdbeMemDynamic(pDest)==0 );
69531
+ assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
6907069532
pDest->flags &= ~(MEM_Ephem|MEM_Static);
6907169533
pDest->flags |= MEM_Term;
6907269534
pDest->z = sMem.z;
6907369535
pDest->zMalloc = sMem.zMalloc;
6907469536
}
@@ -69247,11 +69709,11 @@
6924769709
assert( i==nHdr );
6924869710
assert( j==nByte );
6924969711
6925069712
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6925169713
pOut->n = (int)nByte;
69252
- pOut->flags = MEM_Blob | MEM_Dyn;
69714
+ pOut->flags = MEM_Blob;
6925369715
pOut->xDel = 0;
6925469716
if( nZero ){
6925569717
pOut->u.nZero = nZero;
6925669718
pOut->flags |= MEM_Zero;
6925769719
}
@@ -70121,20 +70583,20 @@
7012170583
r.pKeyInfo = pC->pKeyInfo;
7012270584
r.nField = (u16)nField;
7012370585
7012470586
/* The next line of code computes as follows, only faster:
7012570587
** if( oc==OP_SeekGT || oc==OP_SeekLE ){
70126
- ** r.flags = UNPACKED_INCRKEY;
70588
+ ** r.default_rc = -1;
7012770589
** }else{
70128
- ** r.flags = 0;
70590
+ ** r.default_rc = +1;
7012970591
** }
7013070592
*/
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 );
70593
+ r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
70594
+ assert( oc!=OP_SeekGT || r.default_rc==-1 );
70595
+ assert( oc!=OP_SeekLE || r.default_rc==-1 );
70596
+ assert( oc!=OP_SeekGE || r.default_rc==+1 );
70597
+ assert( oc!=OP_SeekLT || r.default_rc==+1 );
7013670598
7013770599
r.aMem = &aMem[pOp->p3];
7013870600
#ifdef SQLITE_DEBUG
7013970601
{ int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
7014070602
#endif
@@ -70288,22 +70750,21 @@
7028870750
ExpandBlob(&r.aMem[ii]);
7028970751
#ifdef SQLITE_DEBUG
7029070752
if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
7029170753
#endif
7029270754
}
70293
- r.flags = UNPACKED_PREFIX_MATCH;
7029470755
pIdxKey = &r;
7029570756
}else{
7029670757
pIdxKey = sqlite3VdbeAllocUnpackedRecord(
7029770758
pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
7029870759
);
7029970760
if( pIdxKey==0 ) goto no_mem;
7030070761
assert( pIn3->flags & MEM_Blob );
7030170762
assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
7030270763
sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
70303
- pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
7030470764
}
70765
+ pIdxKey->default_rc = 0;
7030570766
if( pOp->opcode==OP_NoConflict ){
7030670767
/* For the OP_NoConflict opcode, take the jump if any of the
7030770768
** input fields are NULL, since any key with a NULL will not
7030870769
** conflict */
7030970770
for(ii=0; ii<r.nField; ii++){
@@ -71188,11 +71649,11 @@
7118871649
pCrsr = pC->pCursor;
7118971650
assert( pCrsr!=0 );
7119071651
assert( pOp->p5==0 );
7119171652
r.pKeyInfo = pC->pKeyInfo;
7119271653
r.nField = (u16)pOp->p3;
71193
- r.flags = UNPACKED_PREFIX_MATCH;
71654
+ r.default_rc = 0;
7119471655
r.aMem = &aMem[pOp->p2];
7119571656
#ifdef SQLITE_DEBUG
7119671657
{ int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
7119771658
#endif
7119871659
rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
@@ -71302,14 +71763,14 @@
7130271763
assert( pOp->p4type==P4_INT32 );
7130371764
r.pKeyInfo = pC->pKeyInfo;
7130471765
r.nField = (u16)pOp->p4.i;
7130571766
if( pOp->opcode<OP_IdxLT ){
7130671767
assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71307
- r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
71768
+ r.default_rc = -1;
7130871769
}else{
7130971770
assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71310
- r.flags = UNPACKED_PREFIX_MATCH;
71771
+ r.default_rc = 0;
7131171772
}
7131271773
r.aMem = &aMem[pOp->p3];
7131371774
#ifdef SQLITE_DEBUG
7131471775
{ int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
7131571776
#endif
@@ -73809,14 +74270,14 @@
7380974270
if( r2->aMem[i].flags & MEM_Null ){
7381074271
*pRes = -1;
7381174272
return;
7381274273
}
7381374274
}
73814
- r2->flags |= UNPACKED_PREFIX_MATCH;
74275
+ assert( r2->default_rc==0 );
7381574276
}
7381674277
73817
- *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
74278
+ *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
7381874279
}
7381974280
7382074281
/*
7382174282
** This function is called to compare two iterator keys when merging
7382274283
** multiple b-tree segments. Parameter iOut is the index of the aTree[]
@@ -100117,11 +100578,11 @@
100117100578
}else if( eDest!=SRT_Exists ){
100118100579
/* If the destination is an EXISTS(...) expression, the actual
100119100580
** values returned by the SELECT are not required.
100120100581
*/
100121100582
sqlite3ExprCodeExprList(pParse, pEList, regResult,
100122
- (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
100583
+ (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
100123100584
}
100124100585
100125100586
/* If the DISTINCT keyword was present on the SELECT statement
100126100587
** and this row has been seen before, then do not make this row
100127100588
** part of the result.
@@ -110767,11 +111228,11 @@
110767111228
iCol = pRec->nField - 1;
110768111229
assert( pIdx->nSample>0 );
110769111230
assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
110770111231
do{
110771111232
iTest = (iMin+i)/2;
110772
- res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
111233
+ res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
110773111234
if( res<0 ){
110774111235
iMin = iTest+1;
110775111236
}else{
110776111237
i = iTest;
110777111238
}
@@ -110782,20 +111243,20 @@
110782111243
** above found the right answer. This block serves no purpose other
110783111244
** than to invoke the asserts. */
110784111245
if( res==0 ){
110785111246
/* If (res==0) is true, then sample $i must be equal to pRec */
110786111247
assert( i<pIdx->nSample );
110787
- assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
111248
+ assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
110788111249
|| pParse->db->mallocFailed );
110789111250
}else{
110790111251
/* Otherwise, pRec must be smaller than sample $i and larger than
110791111252
** sample ($i-1). */
110792111253
assert( i==pIdx->nSample
110793
- || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
111254
+ || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
110794111255
|| pParse->db->mallocFailed );
110795111256
assert( i==0
110796
- || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
111257
+ || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
110797111258
|| pParse->db->mallocFailed );
110798111259
}
110799111260
#endif /* ifdef SQLITE_DEBUG */
110800111261
110801111262
/* At this point, aSample[i] is the first sample that is greater than
@@ -114724,11 +115185,11 @@
114724115185
114725115186
/* For a co-routine, change all OP_Column references to the table of
114726115187
** the co-routine into OP_SCopy of result contained in a register.
114727115188
** OP_Rowid becomes OP_Null.
114728115189
*/
114729
- if( pTabItem->viaCoroutine ){
115190
+ if( pTabItem->viaCoroutine && !db->mallocFailed ){
114730115191
last = sqlite3VdbeCurrentAddr(v);
114731115192
k = pLevel->addrBody;
114732115193
pOp = sqlite3VdbeGetOp(v, k);
114733115194
for(; k<last; k++, pOp++){
114734115195
if( pOp->p1!=pLevel->iTabCur ) continue;
114735115196
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -189,11 +189,11 @@
189 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
190 ** [sqlite_version()] and [sqlite_source_id()].
191 */
192 #define SQLITE_VERSION "3.8.4"
193 #define SQLITE_VERSION_NUMBER 3008004
194 #define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a"
195
196 /*
197 ** CAPI3REF: Run-Time Library Version Numbers
198 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
199 **
@@ -9334,12 +9334,15 @@
9334 #ifndef SQLITE_OMIT_TRACE
9335 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9336 #endif
9337
9338 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9339 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
9340 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
 
 
 
9341
9342 #ifndef SQLITE_OMIT_TRIGGER
9343 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
9344 #endif
9345
@@ -10950,23 +10953,23 @@
10950 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10951 ** OP_Column opcode.
10952 **
10953 ** This structure holds a record that has already been disassembled
10954 ** into its constituent fields.
 
 
 
10955 */
10956 struct UnpackedRecord {
10957 KeyInfo *pKeyInfo; /* Collation and sort-order information */
10958 u16 nField; /* Number of entries in apMem[] */
10959 u8 flags; /* Boolean settings. UNPACKED_... below */
10960 Mem *aMem; /* Values */
 
 
10961 };
10962
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
10969 /*
10970 ** Each SQL index is represented in memory by an
10971 ** instance of the following structure.
10972 **
@@ -13840,11 +13843,11 @@
13840 ** the following flags must be set to determine the memory management
13841 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
13842 ** string is \000 or \u0000 terminated
13843 */
13844 #define MEM_Term 0x0200 /* String rep is nul terminated */
13845 #define MEM_Dyn 0x0400 /* Need to call sqliteFree() on Mem.z */
13846 #define MEM_Static 0x0800 /* Mem.z points to a static string */
13847 #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
13848 #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
13849 #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
13850 #ifdef SQLITE_OMIT_INCRBLOB
@@ -14023,11 +14026,11 @@
14023 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
14024 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
14025 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14026
14027 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14028 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
14029 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14030 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14031 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14032 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14033 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
@@ -14088,10 +14091,11 @@
14088 # define sqlite3VdbeLeave(X)
14089 #endif
14090
14091 #ifdef SQLITE_DEBUG
14092 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
 
14093 #endif
14094
14095 #ifndef SQLITE_OMIT_FOREIGN_KEY
14096 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
14097 #else
@@ -21471,11 +21475,11 @@
21471 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
21472
21473 sqlite3VdbeMemRelease(pMem);
21474 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
21475 pMem->enc = desiredEnc;
21476 pMem->flags |= (MEM_Term|MEM_Dyn);
21477 pMem->z = (char*)zOut;
21478 pMem->zMalloc = pMem->z;
21479
21480 translate_out:
21481 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
@@ -21599,11 +21603,10 @@
21599 sqlite3VdbeMemRelease(&m);
21600 m.z = 0;
21601 }
21602 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
21603 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
21604 assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21605 assert( m.z || db->mallocFailed );
21606 return m.z;
21607 }
21608
21609 /*
@@ -55305,10 +55308,11 @@
55305 i64 intKey, /* The table key */
55306 int biasRight, /* If true, bias the search to the high end */
55307 int *pRes /* Write search results here */
55308 ){
55309 int rc;
 
55310
55311 assert( cursorHoldsMutex(pCur) );
55312 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55313 assert( pRes );
55314 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
@@ -55325,10 +55329,20 @@
55325 if( pCur->atLast && pCur->info.nKey<intKey ){
55326 *pRes = -1;
55327 return SQLITE_OK;
55328 }
55329 }
 
 
 
 
 
 
 
 
 
 
55330
55331 rc = moveToRoot(pCur);
55332 if( rc ){
55333 return rc;
55334 }
@@ -55410,18 +55424,18 @@
55410 if( nCell<=pPage->max1bytePayload ){
55411 /* This branch runs if the record-size field of the cell is a
55412 ** single byte varint and the record fits entirely on the main
55413 ** b-tree page. */
55414 testcase( pCell+nCell+1==pPage->aDataEnd );
55415 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
55416 }else if( !(pCell[1] & 0x80)
55417 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
55418 ){
55419 /* The record-size field is a 2 byte varint and the record
55420 ** fits entirely on the main b-tree page. */
55421 testcase( pCell+nCell+2==pPage->aDataEnd );
55422 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
55423 }else{
55424 /* The record flows over onto one or more overflow pages. In
55425 ** this case the whole cell needs to be parsed, a buffer allocated
55426 ** and accessPayload() used to retrieve the record into the
55427 ** buffer before VdbeRecordCompare() can be called. */
@@ -55438,11 +55452,11 @@
55438 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
55439 if( rc ){
55440 sqlite3_free(pCellKey);
55441 goto moveto_finish;
55442 }
55443 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
55444 sqlite3_free(pCellKey);
55445 }
55446 if( c<0 ){
55447 lwr = idx+1;
55448 }else if( c>0 ){
@@ -60001,10 +60015,46 @@
60001 ** This file contains code use to manipulate "Mem" structure. A "Mem"
60002 ** stores a single value in the VDBE. Mem is an opaque structure visible
60003 ** only within the VDBE. Interface routines refer to a Mem using the
60004 ** name sqlite_value
60005 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60006
60007 /*
60008 ** If pMem is an object with a valid string representation, this routine
60009 ** ensures the internal encoding for the string representation is
60010 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -60051,16 +60101,11 @@
60051 ** pMem->z into the new allocation. pMem must be either a string or
60052 ** blob if bPreserve is true. If bPreserve is false, any prior content
60053 ** in pMem->z is discarded.
60054 */
60055 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 );
60062 assert( (pMem->flags&MEM_RowSet)==0 );
60063
60064 /* If the bPreserve flag is set to true, then the memory cell must already
60065 ** contain a valid string or blob value. */
60066 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
@@ -60074,26 +60119,26 @@
60074 }else{
60075 sqlite3DbFree(pMem->db, pMem->zMalloc);
60076 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
60077 }
60078 if( pMem->zMalloc==0 ){
60079 sqlite3VdbeMemRelease(pMem);
60080 pMem->flags = MEM_Null;
60081 return SQLITE_NOMEM;
60082 }
60083 }
60084
60085 if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
60086 memcpy(pMem->zMalloc, pMem->z, pMem->n);
60087 }
60088 if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){
60089 assert( pMem->xDel!=SQLITE_DYNAMIC );
60090 pMem->xDel((void *)(pMem->z));
60091 }
60092
60093 pMem->z = pMem->zMalloc;
60094 pMem->flags &= ~(MEM_Ephem|MEM_Static);
60095 pMem->xDel = 0;
60096 return SQLITE_OK;
60097 }
60098
60099 /*
@@ -60258,13 +60303,13 @@
60258 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
60259 if( p->flags&MEM_Agg ){
60260 sqlite3VdbeMemFinalize(p, p->u.pDef);
60261 assert( (p->flags & MEM_Agg)==0 );
60262 sqlite3VdbeMemRelease(p);
60263 }else if( p->flags&MEM_Dyn && p->xDel ){
60264 assert( (p->flags&MEM_RowSet)==0 );
60265 assert( p->xDel!=SQLITE_DYNAMIC );
60266 p->xDel((void *)p->z);
60267 p->xDel = 0;
60268 }else if( p->flags&MEM_RowSet ){
60269 sqlite3RowSetClear(p->u.pRowSet);
60270 }else if( p->flags&MEM_Frame ){
@@ -60276,10 +60321,11 @@
60276 ** Release any memory held by the Mem. This may leave the Mem in an
60277 ** inconsistent state, for example with (Mem.z==0) and
60278 ** (Mem.memType==MEM_Str).
60279 */
60280 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
 
60281 VdbeMemRelease(p);
60282 if( p->zMalloc ){
60283 sqlite3DbFree(p->db, p->zMalloc);
60284 p->zMalloc = 0;
60285 }
@@ -60613,10 +60659,11 @@
60613
60614 assert( (pFrom->flags & MEM_RowSet)==0 );
60615 VdbeMemRelease(pTo);
60616 memcpy(pTo, pFrom, MEMCELLSIZE);
60617 pTo->flags &= ~MEM_Dyn;
 
60618
60619 if( pTo->flags&(MEM_Str|MEM_Blob) ){
60620 if( 0==(pFrom->flags&MEM_Static) ){
60621 pTo->flags |= MEM_Ephem;
60622 rc = sqlite3VdbeMemMakeWriteable(pTo);
@@ -60738,123 +60785,10 @@
60738 }
60739
60740 return SQLITE_OK;
60741 }
60742
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 /*
60857 ** Move data out of a btree key or data field and into a Mem structure.
60858 ** The data or key is taken from the entry that pCur is currently pointing
60859 ** to. offset and amt determine what portion of the data or key to retrieve.
60860 ** key is true to get the key or false to get data. The result is written
@@ -60892,11 +60826,11 @@
60892 if( offset+amt<=available ){
60893 sqlite3VdbeMemRelease(pMem);
60894 pMem->z = &zData[offset];
60895 pMem->flags = MEM_Blob|MEM_Ephem;
60896 }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 if( key ){
60901 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
60902 }else{
@@ -61010,11 +60944,10 @@
61010 if( pRec ){
61011 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
61012 if( pRec->pKeyInfo ){
61013 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
61014 assert( pRec->pKeyInfo->enc==ENC(db) );
61015 pRec->flags = UNPACKED_PREFIX_MATCH;
61016 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
61017 for(i=0; i<nCol; i++){
61018 pRec->aMem[i].flags = MEM_Null;
61019 pRec->aMem[i].memType = MEM_Null;
61020 pRec->aMem[i].db = db;
@@ -62591,10 +62524,11 @@
62591 }
62592 return;
62593 }
62594 for(pEnd=&p[N]; p<pEnd; p++){
62595 assert( (&p[1])==pEnd || p[0].db==p[1].db );
 
62596
62597 /* This block is really an inlined version of sqlite3VdbeMemRelease()
62598 ** that takes advantage of the fact that the memory cell value is
62599 ** being set to NULL after releasing any dynamic resources.
62600 **
@@ -62784,11 +62718,11 @@
62784
62785 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
62786 assert( p->db->mallocFailed );
62787 return SQLITE_ERROR;
62788 }
62789 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62790 zP4 = displayP4(pOp, pMem->z, 32);
62791 if( zP4!=pMem->z ){
62792 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
62793 }else{
62794 assert( pMem->z!=0 );
@@ -62801,11 +62735,11 @@
62801 if( p->explain==1 ){
62802 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
62803 assert( p->db->mallocFailed );
62804 return SQLITE_ERROR;
62805 }
62806 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62807 pMem->n = 2;
62808 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
62809 pMem->memType = MEM_Str;
62810 pMem->enc = SQLITE_UTF8;
62811 pMem++;
@@ -62813,11 +62747,11 @@
62813 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62814 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
62815 assert( p->db->mallocFailed );
62816 return SQLITE_ERROR;
62817 }
62818 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62819 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
62820 pMem->memType = MEM_Str;
62821 pMem->enc = SQLITE_UTF8;
62822 #else
62823 pMem->flags = MEM_Null; /* Comment */
@@ -64312,10 +64246,18 @@
64312
64313 /* NULL or constants 0 or 1 */
64314 return 0;
64315 }
64316
 
 
 
 
 
 
 
 
64317 /*
64318 ** Deserialize the data blob pointed to by buf as serial type serial_type
64319 ** and store the result in pMem. Return the number of bytes read.
64320 */
64321 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
@@ -64323,46 +64265,40 @@
64323 u32 serial_type, /* Serial type to deserialize */
64324 Mem *pMem /* Memory cell to write value into */
64325 ){
64326 u64 x;
64327 u32 y;
64328 int i;
64329 switch( serial_type ){
64330 case 10: /* Reserved for future use */
64331 case 11: /* Reserved for future use */
64332 case 0: { /* NULL */
64333 pMem->flags = MEM_Null;
64334 break;
64335 }
64336 case 1: { /* 1-byte signed integer */
64337 pMem->u.i = (signed char)buf[0];
64338 pMem->flags = MEM_Int;
64339 return 1;
64340 }
64341 case 2: { /* 2-byte signed integer */
64342 i = 256*(signed char)buf[0] | buf[1];
64343 pMem->u.i = (i64)i;
64344 pMem->flags = MEM_Int;
64345 return 2;
64346 }
64347 case 3: { /* 3-byte signed integer */
64348 i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
64349 pMem->u.i = (i64)i;
64350 pMem->flags = MEM_Int;
64351 return 3;
64352 }
64353 case 4: { /* 4-byte signed integer */
64354 y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
64355 pMem->u.i = (i64)*(int*)&y;
64356 pMem->flags = MEM_Int;
64357 return 4;
64358 }
64359 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;
64364 pMem->flags = MEM_Int;
64365 return 6;
64366 }
64367 case 6: /* 8-byte signed integer */
64368 case 7: { /* IEEE floating point */
@@ -64376,12 +64312,12 @@
64376 static const double r1 = 1.0;
64377 u64 t2 = t1;
64378 swapMixedEndianFloat(t2);
64379 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
64380 #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];
64383 x = (x<<32) | y;
64384 if( serial_type==6 ){
64385 pMem->u.i = *(i64*)&x;
64386 pMem->flags = MEM_Int;
64387 }else{
@@ -64473,11 +64409,11 @@
64473 u32 idx; /* Offset in aKey[] to read from */
64474 u16 u; /* Unsigned loop counter */
64475 u32 szHdr;
64476 Mem *pMem = p->aMem;
64477
64478 p->flags = 0;
64479 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64480 idx = getVarint32(aKey, szHdr);
64481 d = szHdr;
64482 u = 0;
64483 while( idx<szHdr && u<p->nField && d<=nKey ){
@@ -64494,30 +64430,22 @@
64494 }
64495 assert( u<=pKeyInfo->nField + 1 );
64496 p->nField = u;
64497 }
64498
 
64499 /*
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.
64515 */
64516 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64517 int nKey1, const void *pKey1, /* Left key */
64518 UnpackedRecord *pPKey2 /* Right key */
64519 ){
64520 u32 d1; /* Offset into aKey[] of next data element */
64521 u32 idx1; /* Offset into aKey[] of next header element */
64522 u32 szHdr1; /* Number of bytes in header */
64523 int i = 0;
@@ -64587,28 +64515,558 @@
64587 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
64588 */
64589 assert( mem1.zMalloc==0 );
64590
64591 /* 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64610
64611 /*
64612 ** pCur points at an index entry created using the OP_MakeRecord opcode.
64613 ** Read the rowid (the last field in the record) and store it in *rowid.
64614 ** Return SQLITE_OK if everything works, or an error code otherwise.
@@ -64695,23 +65153,23 @@
64695 ** omits the rowid at the end. The rowid at the end of the index entry
64696 ** is ignored as well. Hence, this routine only compares the prefixes
64697 ** of the keys prior to the final rowid, not the entire key.
64698 */
64699 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 */
64703 ){
64704 i64 nCellKey = 0;
64705 int rc;
64706 BtCursor *pCur = pC->pCursor;
64707 Mem m;
64708
64709 assert( sqlite3BtreeCursorIsValid(pCur) );
64710 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
64711 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
64713 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
64714 if( nCellKey<=0 || nCellKey>0x7fffffff ){
64715 *res = 0;
64716 return SQLITE_CORRUPT_BKPT;
64717 }
@@ -64718,12 +65176,11 @@
64718 memset(&m, 0, sizeof(m));
64719 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
64720 if( rc ){
64721 return rc;
64722 }
64723 assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
64724 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
64725 sqlite3VdbeMemRelease(&m);
64726 return SQLITE_OK;
64727 }
64728
64729 /*
@@ -66629,11 +67086,11 @@
66629 ** does not control the string, it might be deleted without the register
66630 ** knowing it.
66631 **
66632 ** This routine converts an ephemeral string into a dynamically allocated
66633 ** string that the register itself controls. In other words, it
66634 ** converts an MEM_Ephem string into an MEM_Dyn string.
66635 */
66636 #define Deephemeralize(P) \
66637 if( ((P)->flags&MEM_Ephem)!=0 \
66638 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
66639
@@ -67166,22 +67623,25 @@
67166 #ifdef SQLITE_DEBUG
67167 if( (pOp->opflags & OPFLG_IN1)!=0 ){
67168 assert( pOp->p1>0 );
67169 assert( pOp->p1<=(p->nMem-p->nCursor) );
67170 assert( memIsValid(&aMem[pOp->p1]) );
 
67171 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
67172 }
67173 if( (pOp->opflags & OPFLG_IN2)!=0 ){
67174 assert( pOp->p2>0 );
67175 assert( pOp->p2<=(p->nMem-p->nCursor) );
67176 assert( memIsValid(&aMem[pOp->p2]) );
 
67177 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
67178 }
67179 if( (pOp->opflags & OPFLG_IN3)!=0 ){
67180 assert( pOp->p3>0 );
67181 assert( pOp->p3<=(p->nMem-p->nCursor) );
67182 assert( memIsValid(&aMem[pOp->p3]) );
 
67183 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
67184 }
67185 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
67186 assert( pOp->p2>0 );
67187 assert( pOp->p2<=(p->nMem-p->nCursor) );
@@ -67279,11 +67739,11 @@
67279 ** and then jump to address P2.
67280 */
67281 case OP_Gosub: { /* jump */
67282 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
67283 pIn1 = &aMem[pOp->p1];
67284 assert( (pIn1->flags & MEM_Dyn)==0 );
67285 memAboutToChange(p, pIn1);
67286 pIn1->flags = MEM_Int;
67287 pIn1->u.i = pc;
67288 REGISTER_TRACE(pOp->p1, pIn1);
67289 pc = pOp->p2 - 1;
@@ -67352,11 +67812,11 @@
67352 ** OP_EndCoroutine, jump immediately to P2.
67353 */
67354 case OP_Yield: { /* in1, jump */
67355 int pcDest;
67356 pIn1 = &aMem[pOp->p1];
67357 assert( (pIn1->flags & MEM_Dyn)==0 );
67358 pIn1->flags = MEM_Int;
67359 pcDest = (int)pIn1->u.i;
67360 pIn1->u.i = pc;
67361 REGISTER_TRACE(pOp->p1, pIn1);
67362 pc = pcDest;
@@ -67525,14 +67985,13 @@
67525 if( encoding!=SQLITE_UTF8 ){
67526 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
67527 if( rc==SQLITE_TOOBIG ) goto too_big;
67528 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
67529 assert( pOut->zMalloc==pOut->z );
67530 assert( pOut->flags & MEM_Dyn );
67531 pOut->zMalloc = 0;
67532 pOut->flags |= MEM_Static;
67533 pOut->flags &= ~MEM_Dyn;
67534 if( pOp->p4type==P4_DYNAMIC ){
67535 sqlite3DbFree(db, pOp->p4.z);
67536 }
67537 pOp->p4type = P4_DYNAMIC;
67538 pOp->p4.z = pOut->z;
@@ -67664,18 +68123,20 @@
67664 do{
67665 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
67666 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
67667 assert( memIsValid(pIn1) );
67668 memAboutToChange(p, pOut);
 
67669 zMalloc = pOut->zMalloc;
67670 pOut->zMalloc = 0;
67671 sqlite3VdbeMemMove(pOut, pIn1);
67672 #ifdef SQLITE_DEBUG
67673 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
67674 pOut->pScopyFrom += p1 - pOp->p2;
67675 }
67676 #endif
 
 
67677 pIn1->zMalloc = zMalloc;
67678 REGISTER_TRACE(p2++, pOut);
67679 pIn1++;
67680 pOut++;
67681 }while( n-- );
@@ -67848,14 +68309,14 @@
67848 Stringify(pIn2, encoding);
67849 nByte = pIn1->n + pIn2->n;
67850 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67851 goto too_big;
67852 }
67853 MemSetTypeFlag(pOut, MEM_Str);
67854 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
67855 goto no_mem;
67856 }
 
67857 if( pOut!=pIn2 ){
67858 memcpy(pOut->z, pIn2->z, pIn2->n);
67859 }
67860 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
67861 pOut->z[nByte]=0;
@@ -69026,10 +69487,11 @@
69026 ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
69027 ** all valid.
69028 */
69029 assert( p2<pC->nHdrParsed );
69030 assert( rc==SQLITE_OK );
 
69031 if( pC->szRow>=aOffset[p2+1] ){
69032 /* This is the common case where the desired content fits on the original
69033 ** page - where the content is not on an overflow page */
69034 VdbeMemRelease(pDest);
69035 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
@@ -69063,12 +69525,12 @@
69063 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
69064 ** dynamically allocated space over to the pDest structure.
69065 ** This prevents a memory copy. */
69066 if( sMem.zMalloc ){
69067 assert( sMem.z==sMem.zMalloc );
69068 assert( !(pDest->flags & MEM_Dyn) );
69069 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
69070 pDest->flags &= ~(MEM_Ephem|MEM_Static);
69071 pDest->flags |= MEM_Term;
69072 pDest->z = sMem.z;
69073 pDest->zMalloc = sMem.zMalloc;
69074 }
@@ -69247,11 +69709,11 @@
69247 assert( i==nHdr );
69248 assert( j==nByte );
69249
69250 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69251 pOut->n = (int)nByte;
69252 pOut->flags = MEM_Blob | MEM_Dyn;
69253 pOut->xDel = 0;
69254 if( nZero ){
69255 pOut->u.nZero = nZero;
69256 pOut->flags |= MEM_Zero;
69257 }
@@ -70121,20 +70583,20 @@
70121 r.pKeyInfo = pC->pKeyInfo;
70122 r.nField = (u16)nField;
70123
70124 /* The next line of code computes as follows, only faster:
70125 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
70126 ** r.flags = UNPACKED_INCRKEY;
70127 ** }else{
70128 ** r.flags = 0;
70129 ** }
70130 */
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 );
70136
70137 r.aMem = &aMem[pOp->p3];
70138 #ifdef SQLITE_DEBUG
70139 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
70140 #endif
@@ -70288,22 +70750,21 @@
70288 ExpandBlob(&r.aMem[ii]);
70289 #ifdef SQLITE_DEBUG
70290 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
70291 #endif
70292 }
70293 r.flags = UNPACKED_PREFIX_MATCH;
70294 pIdxKey = &r;
70295 }else{
70296 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
70297 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
70298 );
70299 if( pIdxKey==0 ) goto no_mem;
70300 assert( pIn3->flags & MEM_Blob );
70301 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
70302 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
70303 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
70304 }
 
70305 if( pOp->opcode==OP_NoConflict ){
70306 /* For the OP_NoConflict opcode, take the jump if any of the
70307 ** input fields are NULL, since any key with a NULL will not
70308 ** conflict */
70309 for(ii=0; ii<r.nField; ii++){
@@ -71188,11 +71649,11 @@
71188 pCrsr = pC->pCursor;
71189 assert( pCrsr!=0 );
71190 assert( pOp->p5==0 );
71191 r.pKeyInfo = pC->pKeyInfo;
71192 r.nField = (u16)pOp->p3;
71193 r.flags = UNPACKED_PREFIX_MATCH;
71194 r.aMem = &aMem[pOp->p2];
71195 #ifdef SQLITE_DEBUG
71196 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71197 #endif
71198 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
@@ -71302,14 +71763,14 @@
71302 assert( pOp->p4type==P4_INT32 );
71303 r.pKeyInfo = pC->pKeyInfo;
71304 r.nField = (u16)pOp->p4.i;
71305 if( pOp->opcode<OP_IdxLT ){
71306 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71307 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
71308 }else{
71309 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71310 r.flags = UNPACKED_PREFIX_MATCH;
71311 }
71312 r.aMem = &aMem[pOp->p3];
71313 #ifdef SQLITE_DEBUG
71314 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71315 #endif
@@ -73809,14 +74270,14 @@
73809 if( r2->aMem[i].flags & MEM_Null ){
73810 *pRes = -1;
73811 return;
73812 }
73813 }
73814 r2->flags |= UNPACKED_PREFIX_MATCH;
73815 }
73816
73817 *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
73818 }
73819
73820 /*
73821 ** This function is called to compare two iterator keys when merging
73822 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
@@ -100117,11 +100578,11 @@
100117 }else if( eDest!=SRT_Exists ){
100118 /* If the destination is an EXISTS(...) expression, the actual
100119 ** values returned by the SELECT are not required.
100120 */
100121 sqlite3ExprCodeExprList(pParse, pEList, regResult,
100122 (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
100123 }
100124
100125 /* If the DISTINCT keyword was present on the SELECT statement
100126 ** and this row has been seen before, then do not make this row
100127 ** part of the result.
@@ -110767,11 +111228,11 @@
110767 iCol = pRec->nField - 1;
110768 assert( pIdx->nSample>0 );
110769 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
110770 do{
110771 iTest = (iMin+i)/2;
110772 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
110773 if( res<0 ){
110774 iMin = iTest+1;
110775 }else{
110776 i = iTest;
110777 }
@@ -110782,20 +111243,20 @@
110782 ** above found the right answer. This block serves no purpose other
110783 ** than to invoke the asserts. */
110784 if( res==0 ){
110785 /* If (res==0) is true, then sample $i must be equal to pRec */
110786 assert( i<pIdx->nSample );
110787 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
110788 || pParse->db->mallocFailed );
110789 }else{
110790 /* Otherwise, pRec must be smaller than sample $i and larger than
110791 ** sample ($i-1). */
110792 assert( i==pIdx->nSample
110793 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
110794 || pParse->db->mallocFailed );
110795 assert( i==0
110796 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
110797 || pParse->db->mallocFailed );
110798 }
110799 #endif /* ifdef SQLITE_DEBUG */
110800
110801 /* At this point, aSample[i] is the first sample that is greater than
@@ -114724,11 +115185,11 @@
114724
114725 /* For a co-routine, change all OP_Column references to the table of
114726 ** the co-routine into OP_SCopy of result contained in a register.
114727 ** OP_Rowid becomes OP_Null.
114728 */
114729 if( pTabItem->viaCoroutine ){
114730 last = sqlite3VdbeCurrentAddr(v);
114731 k = pLevel->addrBody;
114732 pOp = sqlite3VdbeGetOp(v, k);
114733 for(; k<last; k++, pOp++){
114734 if( pOp->p1!=pLevel->iTabCur ) continue;
114735
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -189,11 +189,11 @@
189 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
190 ** [sqlite_version()] and [sqlite_source_id()].
191 */
192 #define SQLITE_VERSION "3.8.4"
193 #define SQLITE_VERSION_NUMBER 3008004
194 #define SQLITE_SOURCE_ID "2014-03-04 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
195
196 /*
197 ** CAPI3REF: Run-Time Library Version Numbers
198 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
199 **
@@ -9334,12 +9334,15 @@
9334 #ifndef SQLITE_OMIT_TRACE
9335 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9336 #endif
9337
9338 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9339 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int);
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*);
9344
9345 #ifndef SQLITE_OMIT_TRIGGER
9346 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
9347 #endif
9348
@@ -10950,23 +10953,23 @@
10953 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10954 ** OP_Column opcode.
10955 **
10956 ** This structure holds a record that has already been disassembled
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().
10961 */
10962 struct UnpackedRecord {
10963 KeyInfo *pKeyInfo; /* Collation and sort-order information */
10964 u16 nField; /* Number of entries in apMem[] */
10965 i8 default_rc; /* Comparison result if keys are equal */
10966 Mem *aMem; /* Values */
10967 int r1; /* Value to return if (lhs > rhs) */
10968 int r2; /* Value to return if (rhs < lhs) */
10969 };
10970
 
 
 
 
 
10971
10972 /*
10973 ** Each SQL index is represented in memory by an
10974 ** instance of the following structure.
10975 **
@@ -13840,11 +13843,11 @@
13843 ** the following flags must be set to determine the memory management
13844 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
13845 ** string is \000 or \u0000 terminated
13846 */
13847 #define MEM_Term 0x0200 /* String rep is nul terminated */
13848 #define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
13849 #define MEM_Static 0x0800 /* Mem.z points to a static string */
13850 #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
13851 #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
13852 #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
13853 #ifdef SQLITE_OMIT_INCRBLOB
@@ -14023,11 +14026,11 @@
14026 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
14027 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
14028 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14029
14030 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14031 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*);
14032 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14033 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14034 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14035 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14036 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
@@ -14088,10 +14091,11 @@
14091 # define sqlite3VdbeLeave(X)
14092 #endif
14093
14094 #ifdef SQLITE_DEBUG
14095 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
14096 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
14097 #endif
14098
14099 #ifndef SQLITE_OMIT_FOREIGN_KEY
14100 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
14101 #else
@@ -21471,11 +21475,11 @@
21475 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
21476
21477 sqlite3VdbeMemRelease(pMem);
21478 pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
21479 pMem->enc = desiredEnc;
21480 pMem->flags |= (MEM_Term);
21481 pMem->z = (char*)zOut;
21482 pMem->zMalloc = pMem->z;
21483
21484 translate_out:
21485 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
@@ -21599,11 +21603,10 @@
21603 sqlite3VdbeMemRelease(&m);
21604 m.z = 0;
21605 }
21606 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
21607 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
 
21608 assert( m.z || db->mallocFailed );
21609 return m.z;
21610 }
21611
21612 /*
@@ -55305,10 +55308,11 @@
55308 i64 intKey, /* The table key */
55309 int biasRight, /* If true, bias the search to the high end */
55310 int *pRes /* Write search results here */
55311 ){
55312 int rc;
55313 RecordCompare xRecordCompare;
55314
55315 assert( cursorHoldsMutex(pCur) );
55316 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55317 assert( pRes );
55318 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
@@ -55325,10 +55329,20 @@
55329 if( pCur->atLast && pCur->info.nKey<intKey ){
55330 *pRes = -1;
55331 return SQLITE_OK;
55332 }
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 }
55344
55345 rc = moveToRoot(pCur);
55346 if( rc ){
55347 return rc;
55348 }
@@ -55410,18 +55424,18 @@
55424 if( nCell<=pPage->max1bytePayload ){
55425 /* This branch runs if the record-size field of the cell is a
55426 ** single byte varint and the record fits entirely on the main
55427 ** b-tree page. */
55428 testcase( pCell+nCell+1==pPage->aDataEnd );
55429 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
55430 }else if( !(pCell[1] & 0x80)
55431 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
55432 ){
55433 /* The record-size field is a 2 byte varint and the record
55434 ** fits entirely on the main b-tree page. */
55435 testcase( pCell+nCell+2==pPage->aDataEnd );
55436 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
55437 }else{
55438 /* The record flows over onto one or more overflow pages. In
55439 ** this case the whole cell needs to be parsed, a buffer allocated
55440 ** and accessPayload() used to retrieve the record into the
55441 ** buffer before VdbeRecordCompare() can be called. */
@@ -55438,11 +55452,11 @@
55452 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
55453 if( rc ){
55454 sqlite3_free(pCellKey);
55455 goto moveto_finish;
55456 }
55457 c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
55458 sqlite3_free(pCellKey);
55459 }
55460 if( c<0 ){
55461 lwr = idx+1;
55462 }else if( c>0 ){
@@ -60001,10 +60015,46 @@
60015 ** This file contains code use to manipulate "Mem" structure. A "Mem"
60016 ** stores a single value in the VDBE. Mem is an opaque structure visible
60017 ** only within the VDBE. Interface routines refer to a Mem using the
60018 ** name sqlite_value
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
60056
60057 /*
60058 ** If pMem is an object with a valid string representation, this routine
60059 ** ensures the internal encoding for the string representation is
60060 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
@@ -60051,16 +60101,11 @@
60101 ** pMem->z into the new allocation. pMem must be either a string or
60102 ** blob if bPreserve is true. If bPreserve is false, any prior content
60103 ** in pMem->z is discarded.
60104 */
60105 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
60106 assert( sqlite3VdbeCheckMemInvariants(pMem) );
 
 
 
 
 
60107 assert( (pMem->flags&MEM_RowSet)==0 );
60108
60109 /* If the bPreserve flag is set to true, then the memory cell must already
60110 ** contain a valid string or blob value. */
60111 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
@@ -60074,26 +60119,26 @@
60119 }else{
60120 sqlite3DbFree(pMem->db, pMem->zMalloc);
60121 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
60122 }
60123 if( pMem->zMalloc==0 ){
60124 VdbeMemRelease(pMem);
60125 pMem->flags = MEM_Null;
60126 return SQLITE_NOMEM;
60127 }
60128 }
60129
60130 if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
60131 memcpy(pMem->zMalloc, pMem->z, pMem->n);
60132 }
60133 if( (pMem->flags&MEM_Dyn)!=0 ){
60134 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
60135 pMem->xDel((void *)(pMem->z));
60136 }
60137
60138 pMem->z = pMem->zMalloc;
60139 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
60140 pMem->xDel = 0;
60141 return SQLITE_OK;
60142 }
60143
60144 /*
@@ -60258,13 +60303,13 @@
60303 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
60304 if( p->flags&MEM_Agg ){
60305 sqlite3VdbeMemFinalize(p, p->u.pDef);
60306 assert( (p->flags & MEM_Agg)==0 );
60307 sqlite3VdbeMemRelease(p);
60308 }else if( p->flags&MEM_Dyn ){
60309 assert( (p->flags&MEM_RowSet)==0 );
60310 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
60311 p->xDel((void *)p->z);
60312 p->xDel = 0;
60313 }else if( p->flags&MEM_RowSet ){
60314 sqlite3RowSetClear(p->u.pRowSet);
60315 }else if( p->flags&MEM_Frame ){
@@ -60276,10 +60321,11 @@
60321 ** Release any memory held by the Mem. This may leave the Mem in an
60322 ** inconsistent state, for example with (Mem.z==0) and
60323 ** (Mem.memType==MEM_Str).
60324 */
60325 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
60326 assert( sqlite3VdbeCheckMemInvariants(p) );
60327 VdbeMemRelease(p);
60328 if( p->zMalloc ){
60329 sqlite3DbFree(p->db, p->zMalloc);
60330 p->zMalloc = 0;
60331 }
@@ -60613,10 +60659,11 @@
60659
60660 assert( (pFrom->flags & MEM_RowSet)==0 );
60661 VdbeMemRelease(pTo);
60662 memcpy(pTo, pFrom, MEMCELLSIZE);
60663 pTo->flags &= ~MEM_Dyn;
60664 pTo->xDel = 0;
60665
60666 if( pTo->flags&(MEM_Str|MEM_Blob) ){
60667 if( 0==(pFrom->flags&MEM_Static) ){
60668 pTo->flags |= MEM_Ephem;
60669 rc = sqlite3VdbeMemMakeWriteable(pTo);
@@ -60738,123 +60785,10 @@
60785 }
60786
60787 return SQLITE_OK;
60788 }
60789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60790 /*
60791 ** Move data out of a btree key or data field and into a Mem structure.
60792 ** The data or key is taken from the entry that pCur is currently pointing
60793 ** to. offset and amt determine what portion of the data or key to retrieve.
60794 ** key is true to get the key or false to get data. The result is written
@@ -60892,11 +60826,11 @@
60826 if( offset+amt<=available ){
60827 sqlite3VdbeMemRelease(pMem);
60828 pMem->z = &zData[offset];
60829 pMem->flags = MEM_Blob|MEM_Ephem;
60830 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
60831 pMem->flags = MEM_Blob|MEM_Term;
60832 pMem->enc = 0;
60833 pMem->memType = MEM_Blob;
60834 if( key ){
60835 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
60836 }else{
@@ -61010,11 +60944,10 @@
60944 if( pRec ){
60945 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
60946 if( pRec->pKeyInfo ){
60947 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
60948 assert( pRec->pKeyInfo->enc==ENC(db) );
 
60949 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
60950 for(i=0; i<nCol; i++){
60951 pRec->aMem[i].flags = MEM_Null;
60952 pRec->aMem[i].memType = MEM_Null;
60953 pRec->aMem[i].db = db;
@@ -62591,10 +62524,11 @@
62524 }
62525 return;
62526 }
62527 for(pEnd=&p[N]; p<pEnd; p++){
62528 assert( (&p[1])==pEnd || p[0].db==p[1].db );
62529 assert( sqlite3VdbeCheckMemInvariants(p) );
62530
62531 /* This block is really an inlined version of sqlite3VdbeMemRelease()
62532 ** that takes advantage of the fact that the memory cell value is
62533 ** being set to NULL after releasing any dynamic resources.
62534 **
@@ -62784,11 +62718,11 @@
62718
62719 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */
62720 assert( p->db->mallocFailed );
62721 return SQLITE_ERROR;
62722 }
62723 pMem->flags = MEM_Str|MEM_Term;
62724 zP4 = displayP4(pOp, pMem->z, 32);
62725 if( zP4!=pMem->z ){
62726 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
62727 }else{
62728 assert( pMem->z!=0 );
@@ -62801,11 +62735,11 @@
62735 if( p->explain==1 ){
62736 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
62737 assert( p->db->mallocFailed );
62738 return SQLITE_ERROR;
62739 }
62740 pMem->flags = MEM_Str|MEM_Term;
62741 pMem->n = 2;
62742 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */
62743 pMem->memType = MEM_Str;
62744 pMem->enc = SQLITE_UTF8;
62745 pMem++;
@@ -62813,11 +62747,11 @@
62747 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62748 if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
62749 assert( p->db->mallocFailed );
62750 return SQLITE_ERROR;
62751 }
62752 pMem->flags = MEM_Str|MEM_Term;
62753 pMem->n = displayComment(pOp, zP4, pMem->z, 500);
62754 pMem->memType = MEM_Str;
62755 pMem->enc = SQLITE_UTF8;
62756 #else
62757 pMem->flags = MEM_Null; /* Comment */
@@ -64312,10 +64246,18 @@
64246
64247 /* NULL or constants 0 or 1 */
64248 return 0;
64249 }
64250
64251 /* Input "x" is a sequence of unsigned characters that represent a
64252 ** big-endian integer. Return the equivalent native integer
64253 */
64254 #define ONE_BYTE_INT(x) ((i8)(x)[0])
64255 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
64256 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
64257 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
64258
64259 /*
64260 ** Deserialize the data blob pointed to by buf as serial type serial_type
64261 ** and store the result in pMem. Return the number of bytes read.
64262 */
64263 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
@@ -64323,46 +64265,40 @@
64265 u32 serial_type, /* Serial type to deserialize */
64266 Mem *pMem /* Memory cell to write value into */
64267 ){
64268 u64 x;
64269 u32 y;
 
64270 switch( serial_type ){
64271 case 10: /* Reserved for future use */
64272 case 11: /* Reserved for future use */
64273 case 0: { /* NULL */
64274 pMem->flags = MEM_Null;
64275 break;
64276 }
64277 case 1: { /* 1-byte signed integer */
64278 pMem->u.i = ONE_BYTE_INT(buf);
64279 pMem->flags = MEM_Int;
64280 return 1;
64281 }
64282 case 2: { /* 2-byte signed integer */
64283 pMem->u.i = TWO_BYTE_INT(buf);
 
64284 pMem->flags = MEM_Int;
64285 return 2;
64286 }
64287 case 3: { /* 3-byte signed integer */
64288 pMem->u.i = THREE_BYTE_INT(buf);
 
64289 pMem->flags = MEM_Int;
64290 return 3;
64291 }
64292 case 4: { /* 4-byte signed integer */
64293 y = FOUR_BYTE_UINT(buf);
64294 pMem->u.i = (i64)*(int*)&y;
64295 pMem->flags = MEM_Int;
64296 return 4;
64297 }
64298 case 5: { /* 6-byte signed integer */
64299 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
 
 
 
64300 pMem->flags = MEM_Int;
64301 return 6;
64302 }
64303 case 6: /* 8-byte signed integer */
64304 case 7: { /* IEEE floating point */
@@ -64376,12 +64312,12 @@
64312 static const double r1 = 1.0;
64313 u64 t2 = t1;
64314 swapMixedEndianFloat(t2);
64315 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
64316 #endif
64317 x = FOUR_BYTE_UINT(buf);
64318 y = FOUR_BYTE_UINT(buf+4);
64319 x = (x<<32) | y;
64320 if( serial_type==6 ){
64321 pMem->u.i = *(i64*)&x;
64322 pMem->flags = MEM_Int;
64323 }else{
@@ -64473,11 +64409,11 @@
64409 u32 idx; /* Offset in aKey[] to read from */
64410 u16 u; /* Unsigned loop counter */
64411 u32 szHdr;
64412 Mem *pMem = p->aMem;
64413
64414 p->default_rc = 0;
64415 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64416 idx = getVarint32(aKey, szHdr);
64417 d = szHdr;
64418 u = 0;
64419 while( idx<szHdr && u<p->nField && d<=nKey ){
@@ -64494,30 +64430,22 @@
64430 }
64431 assert( u<=pKeyInfo->nField + 1 );
64432 p->nField = u;
64433 }
64434
64435 #if SQLITE_DEBUG
64436 /*
64437 ** This function compares two index or table record keys in the same way
64438 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
64439 ** this function deserializes and compares values using the
64440 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
64441 ** in assert() statements to ensure that the optimized code in
64442 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
 
 
 
 
 
 
 
 
 
64443 */
64444 static int vdbeRecordCompareDebug(
64445 int nKey1, const void *pKey1, /* Left key */
64446 const UnpackedRecord *pPKey2 /* Right key */
64447 ){
64448 u32 d1; /* Offset into aKey[] of next data element */
64449 u32 idx1; /* Offset into aKey[] of next header element */
64450 u32 szHdr1; /* Number of bytes in header */
64451 int i = 0;
@@ -64587,28 +64515,558 @@
64515 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
64516 */
64517 assert( mem1.zMalloc==0 );
64518
64519 /* rc==0 here means that one of the keys ran out of fields and
64520 ** all the fields up to that point were equal. Return the the default_rc
64521 ** value. */
64522 return pPKey2->default_rc;
64523 }
64524 #endif
64525
64526 /*
64527 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
64528 ** using the collation sequence pColl. As usual, return a negative , zero
64529 ** or positive value if *pMem1 is less than, equal to or greater than
64530 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
64531 */
64532 static int vdbeCompareMemString(
64533 const Mem *pMem1,
64534 const Mem *pMem2,
64535 const CollSeq *pColl
64536 ){
64537 if( pMem1->enc==pColl->enc ){
64538 /* The strings are already in the correct encoding. Call the
64539 ** comparison function directly */
64540 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
64541 }else{
64542 int rc;
64543 const void *v1, *v2;
64544 int n1, n2;
64545 Mem c1;
64546 Mem c2;
64547 memset(&c1, 0, sizeof(c1));
64548 memset(&c2, 0, sizeof(c2));
64549 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
64550 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
64551 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
64552 n1 = v1==0 ? 0 : c1.n;
64553 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
64554 n2 = v2==0 ? 0 : c2.n;
64555 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
64556 sqlite3VdbeMemRelease(&c1);
64557 sqlite3VdbeMemRelease(&c2);
64558 return rc;
64559 }
64560 }
64561
64562 /*
64563 ** Compare the values contained by the two memory cells, returning
64564 ** negative, zero or positive if pMem1 is less than, equal to, or greater
64565 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
64566 ** and reals) sorted numerically, followed by text ordered by the collating
64567 ** sequence pColl and finally blob's ordered by memcmp().
64568 **
64569 ** Two NULL values are considered equal by this function.
64570 */
64571 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
64572 int rc;
64573 int f1, f2;
64574 int combined_flags;
64575
64576 f1 = pMem1->flags;
64577 f2 = pMem2->flags;
64578 combined_flags = f1|f2;
64579 assert( (combined_flags & MEM_RowSet)==0 );
64580
64581 /* If one value is NULL, it is less than the other. If both values
64582 ** are NULL, return 0.
64583 */
64584 if( combined_flags&MEM_Null ){
64585 return (f2&MEM_Null) - (f1&MEM_Null);
64586 }
64587
64588 /* If one value is a number and the other is not, the number is less.
64589 ** If both are numbers, compare as reals if one is a real, or as integers
64590 ** if both values are integers.
64591 */
64592 if( combined_flags&(MEM_Int|MEM_Real) ){
64593 double r1, r2;
64594 if( (f1 & f2 & MEM_Int)!=0 ){
64595 if( pMem1->u.i < pMem2->u.i ) return -1;
64596 if( pMem1->u.i > pMem2->u.i ) return 1;
64597 return 0;
64598 }
64599 if( (f1&MEM_Real)!=0 ){
64600 r1 = pMem1->r;
64601 }else if( (f1&MEM_Int)!=0 ){
64602 r1 = (double)pMem1->u.i;
64603 }else{
64604 return 1;
64605 }
64606 if( (f2&MEM_Real)!=0 ){
64607 r2 = pMem2->r;
64608 }else if( (f2&MEM_Int)!=0 ){
64609 r2 = (double)pMem2->u.i;
64610 }else{
64611 return -1;
64612 }
64613 if( r1<r2 ) return -1;
64614 if( r1>r2 ) return 1;
64615 return 0;
64616 }
64617
64618 /* If one value is a string and the other is a blob, the string is less.
64619 ** If both are strings, compare using the collating functions.
64620 */
64621 if( combined_flags&MEM_Str ){
64622 if( (f1 & MEM_Str)==0 ){
64623 return 1;
64624 }
64625 if( (f2 & MEM_Str)==0 ){
64626 return -1;
64627 }
64628
64629 assert( pMem1->enc==pMem2->enc );
64630 assert( pMem1->enc==SQLITE_UTF8 ||
64631 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
64632
64633 /* The collation sequence must be defined at this point, even if
64634 ** the user deletes the collation sequence after the vdbe program is
64635 ** compiled (this was not always the case).
64636 */
64637 assert( !pColl || pColl->xCmp );
64638
64639 if( pColl ){
64640 return vdbeCompareMemString(pMem1, pMem2, pColl);
64641 }
64642 /* If a NULL pointer was passed as the collate function, fall through
64643 ** to the blob case and use memcmp(). */
64644 }
64645
64646 /* Both values must be blobs. Compare using memcmp(). */
64647 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
64648 if( rc==0 ){
64649 rc = pMem1->n - pMem2->n;
64650 }
64651 return rc;
64652 }
64653
64654
64655 /*
64656 ** The first argument passed to this function is a serial-type that
64657 ** corresponds to an integer - all values between 1 and 9 inclusive
64658 ** except 7. The second points to a buffer containing an integer value
64659 ** serialized according to serial_type. This function deserializes
64660 ** and returns the value.
64661 */
64662 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
64663 u32 y;
64664 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
64665 switch( serial_type ){
64666 case 0:
64667 case 1:
64668 return ONE_BYTE_INT(aKey);
64669 case 2:
64670 return TWO_BYTE_INT(aKey);
64671 case 3:
64672 return THREE_BYTE_INT(aKey);
64673 case 4: {
64674 y = FOUR_BYTE_UINT(aKey);
64675 return (i64)*(int*)&y;
64676 }
64677 case 5: {
64678 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64679 }
64680 case 6: {
64681 u64 x = FOUR_BYTE_UINT(aKey);
64682 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64683 return (i64)*(i64*)&x;
64684 }
64685 }
64686
64687 return (serial_type - 8);
64688 }
64689
64690 /*
64691 ** This function compares the two table rows or index records
64692 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
64693 ** or positive integer if key1 is less than, equal to or
64694 ** greater than key2. The {nKey1, pKey1} key must be a blob
64695 ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2
64696 ** key must be a parsed key such as obtained from
64697 ** sqlite3VdbeParseRecord.
64698 **
64699 ** If argument bSkip is non-zero, it is assumed that the caller has already
64700 ** determined that the first fields of the keys are equal.
64701 **
64702 ** Key1 and Key2 do not have to contain the same number of fields. If all
64703 ** fields that appear in both keys are equal, then pPKey2->default_rc is
64704 ** returned.
64705 */
64706 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64707 int nKey1, const void *pKey1, /* Left key */
64708 const UnpackedRecord *pPKey2, /* Right key */
64709 int bSkip /* If true, skip the first field */
64710 ){
64711 u32 d1; /* Offset into aKey[] of next data element */
64712 int i; /* Index of next field to compare */
64713 int szHdr1; /* Size of record header in bytes */
64714 u32 idx1; /* Offset of first type in header */
64715 int rc = 0; /* Return value */
64716 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
64717 KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
64718 const unsigned char *aKey1 = (const unsigned char *)pKey1;
64719 Mem mem1;
64720
64721 /* If bSkip is true, then the caller has already determined that the first
64722 ** two elements in the keys are equal. Fix the various stack variables so
64723 ** that this routine begins comparing at the second field. */
64724 if( bSkip ){
64725 u32 s1;
64726 idx1 = 1 + getVarint32(&aKey1[1], s1);
64727 szHdr1 = aKey1[0];
64728 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
64729 i = 1;
64730 pRhs++;
64731 }else{
64732 idx1 = getVarint32(aKey1, szHdr1);
64733 d1 = szHdr1;
64734 i = 0;
64735 }
64736
64737 VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64738 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
64739 || CORRUPT_DB );
64740 assert( pPKey2->pKeyInfo->aSortOrder!=0 );
64741 assert( pPKey2->pKeyInfo->nField>0 );
64742 assert( idx1<=szHdr1 || CORRUPT_DB );
64743 do{
64744 u32 serial_type;
64745
64746 /* RHS is an integer */
64747 if( pRhs->flags & MEM_Int ){
64748 serial_type = aKey1[idx1];
64749 if( serial_type>=12 ){
64750 rc = +1;
64751 }else if( serial_type==0 ){
64752 rc = -1;
64753 }else if( serial_type==7 ){
64754 double rhs = (double)pRhs->u.i;
64755 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64756 if( mem1.r<rhs ){
64757 rc = -1;
64758 }else if( mem1.r>rhs ){
64759 rc = +1;
64760 }
64761 }else{
64762 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
64763 i64 rhs = pRhs->u.i;
64764 if( lhs<rhs ){
64765 rc = -1;
64766 }else if( lhs>rhs ){
64767 rc = +1;
64768 }
64769 }
64770 }
64771
64772 /* RHS is real */
64773 else if( pRhs->flags & MEM_Real ){
64774 serial_type = aKey1[idx1];
64775 if( serial_type>=12 ){
64776 rc = +1;
64777 }else if( serial_type==0 ){
64778 rc = -1;
64779 }else{
64780 double rhs = pRhs->r;
64781 double lhs;
64782 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64783 if( serial_type==7 ){
64784 lhs = mem1.r;
64785 }else{
64786 lhs = (double)mem1.u.i;
64787 }
64788 if( lhs<rhs ){
64789 rc = -1;
64790 }else if( lhs>rhs ){
64791 rc = +1;
64792 }
64793 }
64794 }
64795
64796 /* RHS is a string */
64797 else if( pRhs->flags & MEM_Str ){
64798 getVarint32(&aKey1[idx1], serial_type);
64799 if( serial_type<12 ){
64800 rc = -1;
64801 }else if( !(serial_type & 0x01) ){
64802 rc = +1;
64803 }else{
64804 mem1.n = (serial_type - 12) / 2;
64805 if( (d1+mem1.n) > (unsigned)nKey1 ){
64806 rc = 1; /* Corruption */
64807 }else if( pKeyInfo->aColl[i] ){
64808 mem1.enc = pKeyInfo->enc;
64809 mem1.db = pKeyInfo->db;
64810 mem1.flags = MEM_Str;
64811 mem1.z = (char*)&aKey1[d1];
64812 rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
64813 }else{
64814 int nCmp = MIN(mem1.n, pRhs->n);
64815 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64816 if( rc==0 ) rc = mem1.n - pRhs->n;
64817 }
64818 }
64819 }
64820
64821 /* RHS is a blob */
64822 else if( pRhs->flags & MEM_Blob ){
64823 getVarint32(&aKey1[idx1], serial_type);
64824 if( serial_type<12 || (serial_type & 0x01) ){
64825 rc = -1;
64826 }else{
64827 int nStr = (serial_type - 12) / 2;
64828 if( (d1+nStr) > (unsigned)nKey1 ){
64829 rc = 1; /* Corruption */
64830 }else{
64831 int nCmp = MIN(nStr, pRhs->n);
64832 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64833 if( rc==0 ) rc = nStr - pRhs->n;
64834 }
64835 }
64836 }
64837
64838 /* RHS is null */
64839 else{
64840 serial_type = aKey1[idx1];
64841 rc = (serial_type!=0);
64842 }
64843
64844 if( rc!=0 ){
64845 if( pKeyInfo->aSortOrder[i] ){
64846 rc = -rc;
64847 }
64848 assert( CORRUPT_DB
64849 || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64850 || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64851 );
64852 assert( mem1.zMalloc==0 ); /* See comment below */
64853 return rc;
64854 }
64855
64856 i++;
64857 pRhs++;
64858 d1 += sqlite3VdbeSerialTypeLen(serial_type);
64859 idx1 += sqlite3VarintLen(serial_type);
64860 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
64861
64862 /* No memory allocation is ever used on mem1. Prove this using
64863 ** the following assert(). If the assert() fails, it indicates a
64864 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
64865 assert( mem1.zMalloc==0 );
64866
64867 /* rc==0 here means that one or both of the keys ran out of fields and
64868 ** all the fields up to that point were equal. Return the the default_rc
64869 ** value. */
64870 assert( CORRUPT_DB
64871 || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
64872 );
64873 return pPKey2->default_rc;
64874 }
64875
64876 /*
64877 ** This function is an optimized version of sqlite3VdbeRecordCompare()
64878 ** that (a) the first field of pPKey2 is an integer, and (b) the
64879 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
64880 ** byte (i.e. is less than 128).
64881 */
64882 static int vdbeRecordCompareInt(
64883 int nKey1, const void *pKey1, /* Left key */
64884 const UnpackedRecord *pPKey2, /* Right key */
64885 int bSkip /* Ignored */
64886 ){
64887 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
64888 int serial_type = ((const u8*)pKey1)[1];
64889 int res;
64890 u32 y;
64891 u64 x;
64892 i64 v = pPKey2->aMem[0].u.i;
64893 i64 lhs;
64894 UNUSED_PARAMETER(bSkip);
64895
64896 assert( bSkip==0 );
64897 switch( serial_type ){
64898 case 1: { /* 1-byte signed integer */
64899 lhs = ONE_BYTE_INT(aKey);
64900 break;
64901 }
64902 case 2: { /* 2-byte signed integer */
64903 lhs = TWO_BYTE_INT(aKey);
64904 break;
64905 }
64906 case 3: { /* 3-byte signed integer */
64907 lhs = THREE_BYTE_INT(aKey);
64908 break;
64909 }
64910 case 4: { /* 4-byte signed integer */
64911 y = FOUR_BYTE_UINT(aKey);
64912 lhs = (i64)*(int*)&y;
64913 break;
64914 }
64915 case 5: { /* 6-byte signed integer */
64916 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64917 break;
64918 }
64919 case 6: { /* 8-byte signed integer */
64920 x = FOUR_BYTE_UINT(aKey);
64921 x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64922 lhs = *(i64*)&x;
64923 break;
64924 }
64925 case 8:
64926 lhs = 0;
64927 break;
64928 case 9:
64929 lhs = 1;
64930 break;
64931
64932 /* This case could be removed without changing the results of running
64933 ** this code. Including it causes gcc to generate a faster switch
64934 ** statement (since the range of switch targets now starts at zero and
64935 ** is contiguous) but does not cause any duplicate code to be generated
64936 ** (as gcc is clever enough to combine the two like cases). Other
64937 ** compilers might be similar. */
64938 case 0: case 7:
64939 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64940
64941 default:
64942 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
64943 }
64944
64945 if( v>lhs ){
64946 res = pPKey2->r1;
64947 }else if( v<lhs ){
64948 res = pPKey2->r2;
64949 }else if( pPKey2->nField>1 ){
64950 /* The first fields of the two keys are equal. Compare the trailing
64951 ** fields. */
64952 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
64953 }else{
64954 /* The first fields of the two keys are equal and there are no trailing
64955 ** fields. Return pPKey2->default_rc in this case. */
64956 res = pPKey2->default_rc;
64957 }
64958
64959 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
64960 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64961 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64962 || CORRUPT_DB
64963 );
64964 return res;
64965 }
64966
64967 /*
64968 ** This function is an optimized version of sqlite3VdbeRecordCompare()
64969 ** that (a) the first field of pPKey2 is a string, that (b) the first field
64970 ** uses the collation sequence BINARY and (c) that the size-of-header varint
64971 ** at the start of (pKey1/nKey1) fits in a single byte.
64972 */
64973 static int vdbeRecordCompareString(
64974 int nKey1, const void *pKey1, /* Left key */
64975 const UnpackedRecord *pPKey2, /* Right key */
64976 int bSkip
64977 ){
64978 const u8 *aKey1 = (const u8*)pKey1;
64979 int serial_type;
64980 int res;
64981 UNUSED_PARAMETER(bSkip);
64982
64983 assert( bSkip==0 );
64984 getVarint32(&aKey1[1], serial_type);
64985
64986 if( serial_type<12 ){
64987 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
64988 }else if( !(serial_type & 0x01) ){
64989 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
64990 }else{
64991 int nCmp;
64992 int nStr;
64993 int szHdr = aKey1[0];
64994
64995 nStr = (serial_type-12) / 2;
64996 if( (szHdr + nStr) > nKey1 ) return 0; /* Corruption */
64997 nCmp = MIN( pPKey2->aMem[0].n, nStr );
64998 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
64999
65000 if( res==0 ){
65001 res = nStr - pPKey2->aMem[0].n;
65002 if( res==0 ){
65003 if( pPKey2->nField>1 ){
65004 res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
65005 }else{
65006 res = pPKey2->default_rc;
65007 }
65008 }else if( res>0 ){
65009 res = pPKey2->r2;
65010 }else{
65011 res = pPKey2->r1;
65012 }
65013 }else if( res>0 ){
65014 res = pPKey2->r2;
65015 }else{
65016 res = pPKey2->r1;
65017 }
65018 }
65019
65020 assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65021 || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65022 || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65023 || CORRUPT_DB
65024 );
65025 return res;
65026 }
65027
65028 /*
65029 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
65030 ** suitable for comparing serialized records to the unpacked record passed
65031 ** as the only argument.
65032 */
65033 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
65034 /* varintRecordCompareInt() and varintRecordCompareString() both assume
65035 ** that the size-of-header varint that occurs at the start of each record
65036 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
65037 ** also assumes that it is safe to overread a buffer by at least the
65038 ** maximum possible legal header size plus 8 bytes. Because there is
65039 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
65040 ** buffer passed to varintRecordCompareInt() this makes it convenient to
65041 ** limit the size of the header to 64 bytes in cases where the first field
65042 ** is an integer.
65043 **
65044 ** The easiest way to enforce this limit is to consider only records with
65045 ** 13 fields or less. If the first field is an integer, the maximum legal
65046 ** header size is (12*5 + 1 + 1) bytes. */
65047 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
65048 int flags = p->aMem[0].flags;
65049 if( p->pKeyInfo->aSortOrder[0] ){
65050 p->r1 = 1;
65051 p->r2 = -1;
65052 }else{
65053 p->r1 = -1;
65054 p->r2 = 1;
65055 }
65056 if( (flags & MEM_Int) ){
65057 return vdbeRecordCompareInt;
65058 }
65059 if( (flags & (MEM_Int|MEM_Real|MEM_Null|MEM_Blob))==0
65060 && p->pKeyInfo->aColl[0]==0
65061 ){
65062 return vdbeRecordCompareString;
65063 }
65064 }
65065
65066 return sqlite3VdbeRecordCompare;
65067 }
65068
65069 /*
65070 ** pCur points at an index entry created using the OP_MakeRecord opcode.
65071 ** Read the rowid (the last field in the record) and store it in *rowid.
65072 ** Return SQLITE_OK if everything works, or an error code otherwise.
@@ -64695,23 +65153,23 @@
65153 ** omits the rowid at the end. The rowid at the end of the index entry
65154 ** is ignored as well. Hence, this routine only compares the prefixes
65155 ** of the keys prior to the final rowid, not the entire key.
65156 */
65157 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
65158 VdbeCursor *pC, /* The cursor to compare against */
65159 const UnpackedRecord *pUnpacked, /* Unpacked version of key */
65160 int *res /* Write the comparison result here */
65161 ){
65162 i64 nCellKey = 0;
65163 int rc;
65164 BtCursor *pCur = pC->pCursor;
65165 Mem m;
65166
65167 assert( sqlite3BtreeCursorIsValid(pCur) );
65168 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
65169 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
65170 /* nCellKey will always be between 0 and 0xffffffff because of the way
65171 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
65172 if( nCellKey<=0 || nCellKey>0x7fffffff ){
65173 *res = 0;
65174 return SQLITE_CORRUPT_BKPT;
65175 }
@@ -64718,12 +65176,11 @@
65176 memset(&m, 0, sizeof(m));
65177 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
65178 if( rc ){
65179 return rc;
65180 }
65181 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
 
65182 sqlite3VdbeMemRelease(&m);
65183 return SQLITE_OK;
65184 }
65185
65186 /*
@@ -66629,11 +67086,11 @@
67086 ** does not control the string, it might be deleted without the register
67087 ** knowing it.
67088 **
67089 ** This routine converts an ephemeral string into a dynamically allocated
67090 ** string that the register itself controls. In other words, it
67091 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
67092 */
67093 #define Deephemeralize(P) \
67094 if( ((P)->flags&MEM_Ephem)!=0 \
67095 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
67096
@@ -67166,22 +67623,25 @@
67623 #ifdef SQLITE_DEBUG
67624 if( (pOp->opflags & OPFLG_IN1)!=0 ){
67625 assert( pOp->p1>0 );
67626 assert( pOp->p1<=(p->nMem-p->nCursor) );
67627 assert( memIsValid(&aMem[pOp->p1]) );
67628 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
67629 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
67630 }
67631 if( (pOp->opflags & OPFLG_IN2)!=0 ){
67632 assert( pOp->p2>0 );
67633 assert( pOp->p2<=(p->nMem-p->nCursor) );
67634 assert( memIsValid(&aMem[pOp->p2]) );
67635 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
67636 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
67637 }
67638 if( (pOp->opflags & OPFLG_IN3)!=0 ){
67639 assert( pOp->p3>0 );
67640 assert( pOp->p3<=(p->nMem-p->nCursor) );
67641 assert( memIsValid(&aMem[pOp->p3]) );
67642 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
67643 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
67644 }
67645 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
67646 assert( pOp->p2>0 );
67647 assert( pOp->p2<=(p->nMem-p->nCursor) );
@@ -67279,11 +67739,11 @@
67739 ** and then jump to address P2.
67740 */
67741 case OP_Gosub: { /* jump */
67742 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
67743 pIn1 = &aMem[pOp->p1];
67744 assert( VdbeMemDynamic(pIn1)==0 );
67745 memAboutToChange(p, pIn1);
67746 pIn1->flags = MEM_Int;
67747 pIn1->u.i = pc;
67748 REGISTER_TRACE(pOp->p1, pIn1);
67749 pc = pOp->p2 - 1;
@@ -67352,11 +67812,11 @@
67812 ** OP_EndCoroutine, jump immediately to P2.
67813 */
67814 case OP_Yield: { /* in1, jump */
67815 int pcDest;
67816 pIn1 = &aMem[pOp->p1];
67817 assert( VdbeMemDynamic(pIn1)==0 );
67818 pIn1->flags = MEM_Int;
67819 pcDest = (int)pIn1->u.i;
67820 pIn1->u.i = pc;
67821 REGISTER_TRACE(pOp->p1, pIn1);
67822 pc = pcDest;
@@ -67525,14 +67985,13 @@
67985 if( encoding!=SQLITE_UTF8 ){
67986 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
67987 if( rc==SQLITE_TOOBIG ) goto too_big;
67988 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
67989 assert( pOut->zMalloc==pOut->z );
67990 assert( VdbeMemDynamic(pOut)==0 );
67991 pOut->zMalloc = 0;
67992 pOut->flags |= MEM_Static;
 
67993 if( pOp->p4type==P4_DYNAMIC ){
67994 sqlite3DbFree(db, pOp->p4.z);
67995 }
67996 pOp->p4type = P4_DYNAMIC;
67997 pOp->p4.z = pOut->z;
@@ -67664,18 +68123,20 @@
68123 do{
68124 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
68125 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
68126 assert( memIsValid(pIn1) );
68127 memAboutToChange(p, pOut);
68128 VdbeMemRelease(pOut);
68129 zMalloc = pOut->zMalloc;
68130 memcpy(pOut, pIn1, sizeof(Mem));
 
68131 #ifdef SQLITE_DEBUG
68132 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
68133 pOut->pScopyFrom += p1 - pOp->p2;
68134 }
68135 #endif
68136 pIn1->flags = MEM_Undefined;
68137 pIn1->xDel = 0;
68138 pIn1->zMalloc = zMalloc;
68139 REGISTER_TRACE(p2++, pOut);
68140 pIn1++;
68141 pOut++;
68142 }while( n-- );
@@ -67848,14 +68309,14 @@
68309 Stringify(pIn2, encoding);
68310 nByte = pIn1->n + pIn2->n;
68311 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
68312 goto too_big;
68313 }
 
68314 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
68315 goto no_mem;
68316 }
68317 MemSetTypeFlag(pOut, MEM_Str);
68318 if( pOut!=pIn2 ){
68319 memcpy(pOut->z, pIn2->z, pIn2->n);
68320 }
68321 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
68322 pOut->z[nByte]=0;
@@ -69026,10 +69487,11 @@
69487 ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
69488 ** all valid.
69489 */
69490 assert( p2<pC->nHdrParsed );
69491 assert( rc==SQLITE_OK );
69492 assert( sqlite3VdbeCheckMemInvariants(pDest) );
69493 if( pC->szRow>=aOffset[p2+1] ){
69494 /* This is the common case where the desired content fits on the original
69495 ** page - where the content is not on an overflow page */
69496 VdbeMemRelease(pDest);
69497 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
@@ -69063,12 +69525,12 @@
69525 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
69526 ** dynamically allocated space over to the pDest structure.
69527 ** This prevents a memory copy. */
69528 if( sMem.zMalloc ){
69529 assert( sMem.z==sMem.zMalloc );
69530 assert( VdbeMemDynamic(pDest)==0 );
69531 assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
69532 pDest->flags &= ~(MEM_Ephem|MEM_Static);
69533 pDest->flags |= MEM_Term;
69534 pDest->z = sMem.z;
69535 pDest->zMalloc = sMem.zMalloc;
69536 }
@@ -69247,11 +69709,11 @@
69709 assert( i==nHdr );
69710 assert( j==nByte );
69711
69712 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69713 pOut->n = (int)nByte;
69714 pOut->flags = MEM_Blob;
69715 pOut->xDel = 0;
69716 if( nZero ){
69717 pOut->u.nZero = nZero;
69718 pOut->flags |= MEM_Zero;
69719 }
@@ -70121,20 +70583,20 @@
70583 r.pKeyInfo = pC->pKeyInfo;
70584 r.nField = (u16)nField;
70585
70586 /* The next line of code computes as follows, only faster:
70587 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
70588 ** r.default_rc = -1;
70589 ** }else{
70590 ** r.default_rc = +1;
70591 ** }
70592 */
70593 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
70594 assert( oc!=OP_SeekGT || r.default_rc==-1 );
70595 assert( oc!=OP_SeekLE || r.default_rc==-1 );
70596 assert( oc!=OP_SeekGE || r.default_rc==+1 );
70597 assert( oc!=OP_SeekLT || r.default_rc==+1 );
70598
70599 r.aMem = &aMem[pOp->p3];
70600 #ifdef SQLITE_DEBUG
70601 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
70602 #endif
@@ -70288,22 +70750,21 @@
70750 ExpandBlob(&r.aMem[ii]);
70751 #ifdef SQLITE_DEBUG
70752 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
70753 #endif
70754 }
 
70755 pIdxKey = &r;
70756 }else{
70757 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
70758 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
70759 );
70760 if( pIdxKey==0 ) goto no_mem;
70761 assert( pIn3->flags & MEM_Blob );
70762 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
70763 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
 
70764 }
70765 pIdxKey->default_rc = 0;
70766 if( pOp->opcode==OP_NoConflict ){
70767 /* For the OP_NoConflict opcode, take the jump if any of the
70768 ** input fields are NULL, since any key with a NULL will not
70769 ** conflict */
70770 for(ii=0; ii<r.nField; ii++){
@@ -71188,11 +71649,11 @@
71649 pCrsr = pC->pCursor;
71650 assert( pCrsr!=0 );
71651 assert( pOp->p5==0 );
71652 r.pKeyInfo = pC->pKeyInfo;
71653 r.nField = (u16)pOp->p3;
71654 r.default_rc = 0;
71655 r.aMem = &aMem[pOp->p2];
71656 #ifdef SQLITE_DEBUG
71657 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71658 #endif
71659 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
@@ -71302,14 +71763,14 @@
71763 assert( pOp->p4type==P4_INT32 );
71764 r.pKeyInfo = pC->pKeyInfo;
71765 r.nField = (u16)pOp->p4.i;
71766 if( pOp->opcode<OP_IdxLT ){
71767 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71768 r.default_rc = -1;
71769 }else{
71770 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71771 r.default_rc = 0;
71772 }
71773 r.aMem = &aMem[pOp->p3];
71774 #ifdef SQLITE_DEBUG
71775 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71776 #endif
@@ -73809,14 +74270,14 @@
74270 if( r2->aMem[i].flags & MEM_Null ){
74271 *pRes = -1;
74272 return;
74273 }
74274 }
74275 assert( r2->default_rc==0 );
74276 }
74277
74278 *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
74279 }
74280
74281 /*
74282 ** This function is called to compare two iterator keys when merging
74283 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
@@ -100117,11 +100578,11 @@
100578 }else if( eDest!=SRT_Exists ){
100579 /* If the destination is an EXISTS(...) expression, the actual
100580 ** values returned by the SELECT are not required.
100581 */
100582 sqlite3ExprCodeExprList(pParse, pEList, regResult,
100583 (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
100584 }
100585
100586 /* If the DISTINCT keyword was present on the SELECT statement
100587 ** and this row has been seen before, then do not make this row
100588 ** part of the result.
@@ -110767,11 +111228,11 @@
111228 iCol = pRec->nField - 1;
111229 assert( pIdx->nSample>0 );
111230 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
111231 do{
111232 iTest = (iMin+i)/2;
111233 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
111234 if( res<0 ){
111235 iMin = iTest+1;
111236 }else{
111237 i = iTest;
111238 }
@@ -110782,20 +111243,20 @@
111243 ** above found the right answer. This block serves no purpose other
111244 ** than to invoke the asserts. */
111245 if( res==0 ){
111246 /* If (res==0) is true, then sample $i must be equal to pRec */
111247 assert( i<pIdx->nSample );
111248 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
111249 || pParse->db->mallocFailed );
111250 }else{
111251 /* Otherwise, pRec must be smaller than sample $i and larger than
111252 ** sample ($i-1). */
111253 assert( i==pIdx->nSample
111254 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
111255 || pParse->db->mallocFailed );
111256 assert( i==0
111257 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
111258 || pParse->db->mallocFailed );
111259 }
111260 #endif /* ifdef SQLITE_DEBUG */
111261
111262 /* At this point, aSample[i] is the first sample that is greater than
@@ -114724,11 +115185,11 @@
115185
115186 /* For a co-routine, change all OP_Column references to the table of
115187 ** the co-routine into OP_SCopy of result contained in a register.
115188 ** OP_Rowid becomes OP_Null.
115189 */
115190 if( pTabItem->viaCoroutine && !db->mallocFailed ){
115191 last = sqlite3VdbeCurrentAddr(v);
115192 k = pLevel->addrBody;
115193 pOp = sqlite3VdbeGetOp(v, k);
115194 for(; k<last; k++, pOp++){
115195 if( pOp->p1!=pLevel->iTabCur ) continue;
115196
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.8.4"
111111
#define SQLITE_VERSION_NUMBER 3008004
112
-#define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a"
112
+#define SQLITE_SOURCE_ID "2014-03-04 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
118118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.4"
111 #define SQLITE_VERSION_NUMBER 3008004
112 #define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.4"
111 #define SQLITE_VERSION_NUMBER 3008004
112 #define SQLITE_SOURCE_ID "2014-03-04 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.8.4"
111111
#define SQLITE_VERSION_NUMBER 3008004
112
-#define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a"
112
+#define SQLITE_SOURCE_ID "2014-03-04 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
118118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.4"
111 #define SQLITE_VERSION_NUMBER 3008004
112 #define SQLITE_SOURCE_ID "2014-02-27 15:04:13 a6690400235705ecc0d1a60dacff6ad5fb1f944a"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.4"
111 #define SQLITE_VERSION_NUMBER 3008004
112 #define SQLITE_SOURCE_ID "2014-03-04 04:12:56 3325ad5bdc2f81f63b556d6f4d0589d89b142b2b"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button