Fossil SCM

Update the built-in SQLite to the latest 3.30.0 alpha version, for testing.

drh 2019-09-21 17:50 trunk
Commit 44900415b10f342581193f21e51b08ef3575e7d929f19777a8cc9990f79a58ba
3 files changed +17 -3 +401 -160 +20 -3
+17 -3
--- src/shell.c
+++ src/shell.c
@@ -12131,10 +12131,12 @@
1213112131
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1213212132
".recover Recover as much data as possible from corrupt db.",
1213312133
" --freelist-corrupt Assume the freelist is corrupt",
1213412134
" --recovery-db NAME Store recovery metadata in database file NAME",
1213512135
" --lost-and-found TABLE Alternative name for the lost-and-found table",
12136
+ " --no-rowids Do not attempt to recover rowid values",
12137
+ " that are not also INTEGER PRIMARY KEYs",
1213612138
#endif
1213712139
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
1213812140
".save FILE Write in-memory database into FILE",
1213912141
".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
1214012142
".schema ?PATTERN? Show the CREATE statements matching PATTERN",
@@ -15135,10 +15137,11 @@
1513515137
int i;
1513615138
int nOrphan = -1;
1513715139
RecoverTable *pOrphan = 0;
1513815140
1513915141
int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
15142
+ int bRowids = 1; /* 0 if --no-rowids */
1514015143
for(i=1; i<nArg; i++){
1514115144
char *z = azArg[i];
1514215145
int n;
1514315146
if( z[0]=='-' && z[1]=='-' ) z++;
1514415147
n = strlen30(z);
@@ -15150,10 +15153,13 @@
1515015153
zRecoveryDb = azArg[i];
1515115154
}else
1515215155
if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
1515315156
i++;
1515415157
zLostAndFound = azArg[i];
15158
+ }else
15159
+ if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
15160
+ bRowids = 0;
1515515161
}
1515615162
else{
1515715163
utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
1515815164
showHelp(pState->out, azArg[0]);
1515915165
return 1;
@@ -15315,12 +15321,15 @@
1531515321
pLoop = 0;
1531615322
1531715323
shellPrepare(pState->db, &rc,
1531815324
"SELECT pgno FROM recovery.map WHERE root=?", &pPages
1531915325
);
15326
+
1532015327
shellPrepare(pState->db, &rc,
15321
- "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')"
15328
+ "SELECT max(field), group_concat(shell_escape_crnl(quote"
15329
+ "(case when (? AND field<0) then NULL else value end)"
15330
+ "), ', ')"
1532215331
", min(field) "
1532315332
"FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
1532415333
"GROUP BY cell", &pCells
1532515334
);
1532615335
@@ -15351,15 +15360,20 @@
1535115360
1535215361
if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
1535315362
raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
1535415363
}
1535515364
sqlite3_bind_int(pPages, 1, iRoot);
15356
- sqlite3_bind_int(pCells, 2, pTab->iPk);
15365
+ if( bRowids==0 && pTab->iPk<0 ){
15366
+ sqlite3_bind_int(pCells, 1, 1);
15367
+ }else{
15368
+ sqlite3_bind_int(pCells, 1, 0);
15369
+ }
15370
+ sqlite3_bind_int(pCells, 3, pTab->iPk);
1535715371
1535815372
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
1535915373
int iPgno = sqlite3_column_int(pPages, 0);
15360
- sqlite3_bind_int(pCells, 1, iPgno);
15374
+ sqlite3_bind_int(pCells, 2, iPgno);
1536115375
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
1536215376
int nField = sqlite3_column_int(pCells, 0);
1536315377
int iMin = sqlite3_column_int(pCells, 2);
1536415378
const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
1536515379
1536615380
--- src/shell.c
+++ src/shell.c
@@ -12131,10 +12131,12 @@
12131 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12132 ".recover Recover as much data as possible from corrupt db.",
12133 " --freelist-corrupt Assume the freelist is corrupt",
12134 " --recovery-db NAME Store recovery metadata in database file NAME",
12135 " --lost-and-found TABLE Alternative name for the lost-and-found table",
 
 
12136 #endif
12137 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
12138 ".save FILE Write in-memory database into FILE",
12139 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
12140 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
@@ -15135,10 +15137,11 @@
15135 int i;
15136 int nOrphan = -1;
15137 RecoverTable *pOrphan = 0;
15138
15139 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
 
15140 for(i=1; i<nArg; i++){
15141 char *z = azArg[i];
15142 int n;
15143 if( z[0]=='-' && z[1]=='-' ) z++;
15144 n = strlen30(z);
@@ -15150,10 +15153,13 @@
15150 zRecoveryDb = azArg[i];
15151 }else
15152 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
15153 i++;
15154 zLostAndFound = azArg[i];
 
 
 
15155 }
15156 else{
15157 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
15158 showHelp(pState->out, azArg[0]);
15159 return 1;
@@ -15315,12 +15321,15 @@
15315 pLoop = 0;
15316
15317 shellPrepare(pState->db, &rc,
15318 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
15319 );
 
15320 shellPrepare(pState->db, &rc,
15321 "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')"
 
 
15322 ", min(field) "
15323 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
15324 "GROUP BY cell", &pCells
15325 );
15326
@@ -15351,15 +15360,20 @@
15351
15352 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
15353 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
15354 }
15355 sqlite3_bind_int(pPages, 1, iRoot);
15356 sqlite3_bind_int(pCells, 2, pTab->iPk);
 
 
 
 
 
15357
15358 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
15359 int iPgno = sqlite3_column_int(pPages, 0);
15360 sqlite3_bind_int(pCells, 1, iPgno);
15361 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
15362 int nField = sqlite3_column_int(pCells, 0);
15363 int iMin = sqlite3_column_int(pCells, 2);
15364 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
15365
15366
--- src/shell.c
+++ src/shell.c
@@ -12131,10 +12131,12 @@
12131 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12132 ".recover Recover as much data as possible from corrupt db.",
12133 " --freelist-corrupt Assume the freelist is corrupt",
12134 " --recovery-db NAME Store recovery metadata in database file NAME",
12135 " --lost-and-found TABLE Alternative name for the lost-and-found table",
12136 " --no-rowids Do not attempt to recover rowid values",
12137 " that are not also INTEGER PRIMARY KEYs",
12138 #endif
12139 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
12140 ".save FILE Write in-memory database into FILE",
12141 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
12142 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
@@ -15135,10 +15137,11 @@
15137 int i;
15138 int nOrphan = -1;
15139 RecoverTable *pOrphan = 0;
15140
15141 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
15142 int bRowids = 1; /* 0 if --no-rowids */
15143 for(i=1; i<nArg; i++){
15144 char *z = azArg[i];
15145 int n;
15146 if( z[0]=='-' && z[1]=='-' ) z++;
15147 n = strlen30(z);
@@ -15150,10 +15153,13 @@
15153 zRecoveryDb = azArg[i];
15154 }else
15155 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
15156 i++;
15157 zLostAndFound = azArg[i];
15158 }else
15159 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
15160 bRowids = 0;
15161 }
15162 else{
15163 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
15164 showHelp(pState->out, azArg[0]);
15165 return 1;
@@ -15315,12 +15321,15 @@
15321 pLoop = 0;
15322
15323 shellPrepare(pState->db, &rc,
15324 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
15325 );
15326
15327 shellPrepare(pState->db, &rc,
15328 "SELECT max(field), group_concat(shell_escape_crnl(quote"
15329 "(case when (? AND field<0) then NULL else value end)"
15330 "), ', ')"
15331 ", min(field) "
15332 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
15333 "GROUP BY cell", &pCells
15334 );
15335
@@ -15351,15 +15360,20 @@
15360
15361 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
15362 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
15363 }
15364 sqlite3_bind_int(pPages, 1, iRoot);
15365 if( bRowids==0 && pTab->iPk<0 ){
15366 sqlite3_bind_int(pCells, 1, 1);
15367 }else{
15368 sqlite3_bind_int(pCells, 1, 0);
15369 }
15370 sqlite3_bind_int(pCells, 3, pTab->iPk);
15371
15372 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
15373 int iPgno = sqlite3_column_int(pPages, 0);
15374 sqlite3_bind_int(pCells, 2, iPgno);
15375 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
15376 int nField = sqlite3_column_int(pCells, 0);
15377 int iMin = sqlite3_column_int(pCells, 2);
15378 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
15379
15380
+401 -160
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1165,11 +1165,11 @@
11651165
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
11661166
** [sqlite_version()] and [sqlite_source_id()].
11671167
*/
11681168
#define SQLITE_VERSION "3.30.0"
11691169
#define SQLITE_VERSION_NUMBER 3030000
1170
-#define SQLITE_SOURCE_ID "2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2d897d"
1170
+#define SQLITE_SOURCE_ID "2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f044cbd"
11711171
11721172
/*
11731173
** CAPI3REF: Run-Time Library Version Numbers
11741174
** KEYWORDS: sqlite3_version sqlite3_sourceid
11751175
**
@@ -5900,13 +5900,16 @@
59005900
** the same inputs within a single SQL statement. Most SQL functions are
59015901
** deterministic. The built-in [random()] SQL function is an example of a
59025902
** function that is not deterministic. The SQLite query planner is able to
59035903
** perform additional optimizations on deterministic functions, so use
59045904
** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5905
+**
59055906
** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
59065907
** flag, which if present prevents the function from being invoked from
5907
-** within VIEWs or TRIGGERs.
5908
+** within VIEWs or TRIGGERs. For security reasons, the [SQLITE_DIRECTONLY]
5909
+** flag is recommended for any application-defined SQL function that has
5910
+** side-effects.
59085911
**
59095912
** ^(The fifth parameter is an arbitrary pointer. The implementation of the
59105913
** function can gain access to this pointer using [sqlite3_user_data()].)^
59115914
**
59125915
** ^The sixth, seventh and eighth parameters passed to the three
@@ -6026,14 +6029,28 @@
60266029
** The SQLITE_DETERMINISTIC flag means that the new function will always
60276030
** maps the same inputs into the same output. The abs() function is
60286031
** deterministic, for example, but randomblob() is not.
60296032
**
60306033
** The SQLITE_DIRECTONLY flag means that the function may only be invoked
6031
-** from top-level SQL, and cannot be used in VIEWs or TRIGGERs.
6034
+** from top-level SQL, and cannot be used in VIEWs or TRIGGERs. This is
6035
+** a security feature which is recommended for all
6036
+** [application-defined SQL functions] that have side-effects. This flag
6037
+** prevents an attacker from adding triggers and views to a schema then
6038
+** tricking a high-privilege application into causing unintended side-effects
6039
+** while performing ordinary queries.
6040
+**
6041
+** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
6042
+** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6043
+** Specifying this flag makes no difference for scalar or aggregate user
6044
+** functions. However, if it is not specified for a user-defined window
6045
+** function, then any sub-types belonging to arguments passed to the window
6046
+** function may be discarded before the window function is called (i.e.
6047
+** sqlite3_value_subtype() will always return 0).
60326048
*/
60336049
#define SQLITE_DETERMINISTIC 0x000000800
60346050
#define SQLITE_DIRECTONLY 0x000080000
6051
+#define SQLITE_SUBTYPE 0x000100000
60356052
60366053
/*
60376054
** CAPI3REF: Deprecated Functions
60386055
** DEPRECATED
60396056
**
@@ -16700,10 +16717,11 @@
1670016717
#define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
1670116718
#define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
1670216719
#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
1670316720
#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
1670416721
#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
16722
+#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
1670516723
1670616724
/*
1670716725
** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
1670816726
** used to create the initializers for the FuncDef structures.
1670916727
**
@@ -18614,21 +18632,23 @@
1861418632
Window **ppThis; /* Pointer to this object in Select.pWin list */
1861518633
Window *pNextWin; /* Next window function belonging to this SELECT */
1861618634
Expr *pFilter; /* The FILTER expression */
1861718635
FuncDef *pFunc; /* The function */
1861818636
int iEphCsr; /* Partition buffer or Peer buffer */
18619
- int regAccum;
18620
- int regResult;
18637
+ int regAccum; /* Accumulator */
18638
+ int regResult; /* Interim result */
1862118639
int csrApp; /* Function cursor (used by min/max) */
1862218640
int regApp; /* Function register (also used by min/max) */
1862318641
int regPart; /* Array of registers for PARTITION BY values */
1862418642
Expr *pOwner; /* Expression object this window is attached to */
1862518643
int nBufferCol; /* Number of columns in buffer table */
1862618644
int iArgCol; /* Offset of first argument for this function */
1862718645
int regOne; /* Register containing constant value 1 */
1862818646
int regStartRowid;
1862918647
int regEndRowid;
18648
+ u8 bExprArgs; /* Defer evaluation of window function arguments
18649
+ ** due to the SQLITE_SUBTYPE flag */
1863018650
};
1863118651
1863218652
#ifndef SQLITE_OMIT_WINDOWFUNC
1863318653
SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
1863418654
SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
@@ -29214,11 +29234,17 @@
2921429234
sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
2921529235
break;
2921629236
}
2921729237
2921829238
case TK_COLLATE: {
29219
- sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
29239
+ /* COLLATE operators without the EP_Collate flag are intended to
29240
+ ** emulate collation associated with a table column. Explicit
29241
+ ** COLLATE operators that appear in the original SQL always have
29242
+ ** the EP_Collate bit set */
29243
+ sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
29244
+ !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
29245
+ pExpr->u.zToken, zFlgs);
2922029246
sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
2922129247
break;
2922229248
}
2922329249
2922429250
case TK_AGG_FUNCTION:
@@ -74843,11 +74869,17 @@
7484374869
testcase( bPreserve && pMem->z==0 );
7484474870
7484574871
assert( pMem->szMalloc==0
7484674872
|| pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
7484774873
if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
74848
- pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
74874
+ if( pMem->db ){
74875
+ pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
74876
+ }else{
74877
+ pMem->zMalloc = sqlite3Realloc(pMem->z, n);
74878
+ if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
74879
+ pMem->z = pMem->zMalloc;
74880
+ }
7484974881
bPreserve = 0;
7485074882
}else{
7485174883
if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
7485274884
pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
7485374885
}
@@ -84279,10 +84311,11 @@
8427984311
assert( (f & (MEM_Static|MEM_Dyn))==0 );
8428084312
}else{
8428184313
c = 's';
8428284314
}
8428384315
*(zCsr++) = c;
84316
+ *(zCsr++) = 'x';
8428484317
sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
8428584318
zCsr += sqlite3Strlen30(zCsr);
8428684319
for(i=0; i<25 && i<pMem->n; i++){
8428784320
sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
8428884321
zCsr += sqlite3Strlen30(zCsr);
@@ -85027,11 +85060,10 @@
8502785060
** as the P1 parameter.
8502885061
*/
8502985062
case OP_String8: { /* same as TK_STRING, out2 */
8503085063
assert( pOp->p4.z!=0 );
8503185064
pOut = out2Prerelease(p, pOp);
85032
- pOp->opcode = OP_String;
8503385065
pOp->p1 = sqlite3Strlen30(pOp->p4.z);
8503485066
8503585067
#ifndef SQLITE_OMIT_UTF16
8503685068
if( encoding!=SQLITE_UTF8 ){
8503785069
rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
@@ -85051,10 +85083,11 @@
8505185083
}
8505285084
#endif
8505385085
if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
8505485086
goto too_big;
8505585087
}
85088
+ pOp->opcode = OP_String;
8505685089
assert( rc==SQLITE_OK );
8505785090
/* Fall through to the next case, OP_String */
8505885091
}
8505985092
8506085093
/* Opcode: String P1 P2 P3 P4 P5
@@ -100317,10 +100350,11 @@
100317100350
if( addrOnce ){
100318100351
sqlite3VdbeJumpHere(v, addrOnce);
100319100352
/* Subroutine return */
100320100353
sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
100321100354
sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
100355
+ sqlite3ClearTempRegCache(pParse);
100322100356
}
100323100357
}
100324100358
#endif /* SQLITE_OMIT_SUBQUERY */
100325100359
100326100360
/*
@@ -100427,10 +100461,11 @@
100427100461
sqlite3VdbeJumpHere(v, addrOnce);
100428100462
100429100463
/* Subroutine return */
100430100464
sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
100431100465
sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
100466
+ sqlite3ClearTempRegCache(pParse);
100432100467
}
100433100468
100434100469
return rReg;
100435100470
}
100436100471
#endif /* SQLITE_OMIT_SUBQUERY */
@@ -103058,10 +103093,15 @@
103058103093
}
103059103094
}
103060103095
103061103096
/*
103062103097
** Mark all temporary registers as being unavailable for reuse.
103098
+**
103099
+** Always invoke this procedure after coding a subroutine or co-routine
103100
+** that might be invoked from other parts of the code, to ensure that
103101
+** the sub/co-routine does not use registers in common with the code that
103102
+** invokes the sub/co-routine.
103063103103
*/
103064103104
SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
103065103105
pParse->nTempReg = 0;
103066103106
pParse->nRangeReg = 0;
103067103107
}
@@ -114049,10 +114089,12 @@
114049114089
int nNeedle;
114050114090
int typeHaystack, typeNeedle;
114051114091
int N = 1;
114052114092
int isText;
114053114093
unsigned char firstChar;
114094
+ sqlite3_value *pC1 = 0;
114095
+ sqlite3_value *pC2 = 0;
114054114096
114055114097
UNUSED_PARAMETER(argc);
114056114098
typeHaystack = sqlite3_value_type(argv[0]);
114057114099
typeNeedle = sqlite3_value_type(argv[1]);
114058114100
if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
@@ -114061,16 +114103,26 @@
114061114103
if( nNeedle>0 ){
114062114104
if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
114063114105
zHaystack = sqlite3_value_blob(argv[0]);
114064114106
zNeedle = sqlite3_value_blob(argv[1]);
114065114107
isText = 0;
114066
- }else{
114108
+ }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
114067114109
zHaystack = sqlite3_value_text(argv[0]);
114068114110
zNeedle = sqlite3_value_text(argv[1]);
114069114111
isText = 1;
114112
+ }else{
114113
+ pC1 = sqlite3_value_dup(argv[0]);
114114
+ zHaystack = sqlite3_value_text(pC1);
114115
+ if( zHaystack==0 ) goto endInstrOOM;
114116
+ nHaystack = sqlite3_value_bytes(pC1);
114117
+ pC2 = sqlite3_value_dup(argv[1]);
114118
+ zNeedle = sqlite3_value_text(pC2);
114119
+ if( zNeedle==0 ) goto endInstrOOM;
114120
+ nNeedle = sqlite3_value_bytes(pC2);
114121
+ isText = 1;
114070114122
}
114071
- if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
114123
+ if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
114072114124
firstChar = zNeedle[0];
114073114125
while( nNeedle<=nHaystack
114074114126
&& (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
114075114127
){
114076114128
N++;
@@ -114080,10 +114132,17 @@
114080114132
}while( isText && (zHaystack[0]&0xc0)==0x80 );
114081114133
}
114082114134
if( nNeedle>nHaystack ) N = 0;
114083114135
}
114084114136
sqlite3_result_int(context, N);
114137
+endInstr:
114138
+ sqlite3_value_free(pC1);
114139
+ sqlite3_value_free(pC2);
114140
+ return;
114141
+endInstrOOM:
114142
+ sqlite3_result_error_nomem(context);
114143
+ goto endInstr;
114085114144
}
114086114145
114087114146
/*
114088114147
** Implementation of the printf() function.
114089114148
*/
@@ -128983,10 +129042,22 @@
128983129042
pNew->iRightJoinTable = pExpr->iRightJoinTable;
128984129043
ExprSetProperty(pNew, EP_FromJoin);
128985129044
}
128986129045
sqlite3ExprDelete(db, pExpr);
128987129046
pExpr = pNew;
129047
+
129048
+ /* Ensure that the expression now has an implicit collation sequence,
129049
+ ** just as it did when it was a column of a view or sub-query. */
129050
+ if( pExpr ){
129051
+ if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
129052
+ CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
129053
+ pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
129054
+ (pColl ? pColl->zName : "BINARY")
129055
+ );
129056
+ }
129057
+ ExprClearProperty(pExpr, EP_Collate);
129058
+ }
128988129059
}
128989129060
}
128990129061
}else{
128991129062
if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
128992129063
pExpr->iTable = pSubst->iNewTable;
@@ -130849,10 +130920,23 @@
130849130920
ExprList *pList = pF->pExpr->x.pList;
130850130921
assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
130851130922
assert( !IsWindowFunc(pF->pExpr) );
130852130923
if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
130853130924
Expr *pFilter = pF->pExpr->y.pWin->pFilter;
130925
+ if( pAggInfo->nAccumulator
130926
+ && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
130927
+ ){
130928
+ if( regHit==0 ) regHit = ++pParse->nMem;
130929
+ /* If this is the first row of the group (regAcc==0), clear the
130930
+ ** "magnet" register regHit so that the accumulator registers
130931
+ ** are populated if the FILTER clause jumps over the the
130932
+ ** invocation of min() or max() altogether. Or, if this is not
130933
+ ** the first row (regAcc==1), set the magnet register so that the
130934
+ ** accumulators are not populated unless the min()/max() is invoked and
130935
+ ** indicates that they should be. */
130936
+ sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit);
130937
+ }
130854130938
addrNext = sqlite3VdbeMakeLabel(pParse);
130855130939
sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
130856130940
}
130857130941
if( pList ){
130858130942
nArg = pList->nExpr;
@@ -130899,10 +130983,11 @@
130899130983
addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
130900130984
}
130901130985
for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
130902130986
sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
130903130987
}
130988
+
130904130989
pAggInfo->directMode = 0;
130905130990
if( addrHitTest ){
130906130991
sqlite3VdbeJumpHere(v, addrHitTest);
130907130992
}
130908130993
}
@@ -131702,27 +131787,39 @@
131702131787
for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
131703131788
pItem->u.x.iAlias = 0;
131704131789
}
131705131790
assert( 66==sqlite3LogEst(100) );
131706131791
if( p->nSelectRow>66 ) p->nSelectRow = 66;
131792
+
131793
+ /* If there is both a GROUP BY and an ORDER BY clause and they are
131794
+ ** identical, then it may be possible to disable the ORDER BY clause
131795
+ ** on the grounds that the GROUP BY will cause elements to come out
131796
+ ** in the correct order. It also may not - the GROUP BY might use a
131797
+ ** database index that causes rows to be grouped together as required
131798
+ ** but not actually sorted. Either way, record the fact that the
131799
+ ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
131800
+ ** variable. */
131801
+ if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
131802
+ int ii;
131803
+ /* The GROUP BY processing doesn't care whether rows are delivered in
131804
+ ** ASC or DESC order - only that each group is returned contiguously.
131805
+ ** So set the ASC/DESC flags in the GROUP BY to match those in the
131806
+ ** ORDER BY to maximize the chances of rows being delivered in an
131807
+ ** order that makes the ORDER BY redundant. */
131808
+ for(ii=0; ii<pGroupBy->nExpr; ii++){
131809
+ u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
131810
+ pGroupBy->a[ii].sortFlags = sortFlags;
131811
+ }
131812
+ if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
131813
+ orderByGrp = 1;
131814
+ }
131815
+ }
131707131816
}else{
131708131817
assert( 0==sqlite3LogEst(1) );
131709131818
p->nSelectRow = 0;
131710131819
}
131711131820
131712
- /* If there is both a GROUP BY and an ORDER BY clause and they are
131713
- ** identical, then it may be possible to disable the ORDER BY clause
131714
- ** on the grounds that the GROUP BY will cause elements to come out
131715
- ** in the correct order. It also may not - the GROUP BY might use a
131716
- ** database index that causes rows to be grouped together as required
131717
- ** but not actually sorted. Either way, record the fact that the
131718
- ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
131719
- ** variable. */
131720
- if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
131721
- orderByGrp = 1;
131722
- }
131723
-
131724131821
/* Create a label to jump to when we want to abort the query */
131725131822
addrEnd = sqlite3VdbeMakeLabel(pParse);
131726131823
131727131824
/* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
131728131825
** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
@@ -132074,17 +132171,22 @@
132074132171
}else
132075132172
#endif /* SQLITE_OMIT_BTREECOUNT */
132076132173
{
132077132174
int regAcc = 0; /* "populate accumulators" flag */
132078132175
132079
- /* If there are accumulator registers but no min() or max() functions,
132080
- ** allocate register regAcc. Register regAcc will contain 0 the first
132081
- ** time the inner loop runs, and 1 thereafter. The code generated
132082
- ** by updateAccumulator() only updates the accumulator registers if
132083
- ** regAcc contains 0. */
132176
+ /* If there are accumulator registers but no min() or max() functions
132177
+ ** without FILTER clauses, allocate register regAcc. Register regAcc
132178
+ ** will contain 0 the first time the inner loop runs, and 1 thereafter.
132179
+ ** The code generated by updateAccumulator() uses this to ensure
132180
+ ** that the accumulator registers are (a) updated only once if
132181
+ ** there are no min() or max functions or (b) always updated for the
132182
+ ** first row visited by the aggregate, so that they are updated at
132183
+ ** least once even if the FILTER clause means the min() or max()
132184
+ ** function visits zero rows. */
132084132185
if( sAggInfo.nAccumulator ){
132085132186
for(i=0; i<sAggInfo.nFunc; i++){
132187
+ if( ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_WinFunc) ) continue;
132086132188
if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
132087132189
}
132088132190
if( i==sAggInfo.nFunc ){
132089132191
regAcc = ++pParse->nMem;
132090132192
sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
@@ -139778,22 +139880,27 @@
139778139880
** LIKE optimization. See, for example:
139779139881
** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
139780139882
** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
139781139883
** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
139782139884
** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
139885
+ ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
139783139886
*/
139784139887
if( pLeft->op!=TK_COLUMN
139785139888
|| sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
139786139889
|| IsVirtual(pLeft->y.pTab) /* Value might be numeric */
139787139890
){
139788139891
int isNum;
139789139892
double rDummy;
139790139893
isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139791139894
if( isNum<=0 ){
139792
- zNew[iTo-1]++;
139793
- isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139794
- zNew[iTo-1]--;
139895
+ if( iTo==1 && zNew[0]=='-' ){
139896
+ isNum = +1;
139897
+ }else{
139898
+ zNew[iTo-1]++;
139899
+ isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139900
+ zNew[iTo-1]--;
139901
+ }
139795139902
}
139796139903
if( isNum>0 ){
139797139904
sqlite3ExprDelete(db, pPrefix);
139798139905
sqlite3ValueFree(pVal);
139799139906
return 0;
@@ -143384,11 +143491,11 @@
143384143491
WhereLoop *pLoop, /* The loop to adjust downward */
143385143492
LogEst nRow /* Number of rows in the entire table */
143386143493
){
143387143494
WhereTerm *pTerm, *pX;
143388143495
Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
143389
- int i, j, k;
143496
+ int i, j;
143390143497
LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
143391143498
143392143499
assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
143393143500
for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
143394143501
assert( pTerm!=0 );
@@ -143410,10 +143517,11 @@
143410143517
/* In the absence of explicit truth probabilities, use heuristics to
143411143518
** guess a reasonable truth probability. */
143412143519
pLoop->nOut--;
143413143520
if( pTerm->eOperator&(WO_EQ|WO_IS) ){
143414143521
Expr *pRight = pTerm->pExpr->pRight;
143522
+ int k = 0;
143415143523
testcase( pTerm->pExpr->op==TK_IS );
143416143524
if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
143417143525
k = 10;
143418143526
}else{
143419143527
k = 20;
@@ -147493,12 +147601,19 @@
147493147601
/* Append the arguments passed to each window function to the
147494147602
** sub-select expression list. Also allocate two registers for each
147495147603
** window function - one for the accumulator, another for interim
147496147604
** results. */
147497147605
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147498
- pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
147499
- pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList, 0);
147606
+ ExprList *pArgs = pWin->pOwner->x.pList;
147607
+ if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
147608
+ selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
147609
+ pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
147610
+ pWin->bExprArgs = 1;
147611
+ }else{
147612
+ pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
147613
+ pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
147614
+ }
147500147615
if( pWin->pFilter ){
147501147616
Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
147502147617
pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
147503147618
}
147504147619
pWin->regAccum = ++pParse->nMem;
@@ -147960,11 +148075,11 @@
147960148075
Vdbe *v = sqlite3GetVdbe(pParse);
147961148076
Window *pWin;
147962148077
for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147963148078
FuncDef *pFunc = pWin->pFunc;
147964148079
int regArg;
147965
- int nArg = windowArgCount(pWin);
148080
+ int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
147966148081
int i;
147967148082
147968148083
assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
147969148084
147970148085
for(i=0; i<nArg; i++){
@@ -148002,17 +148117,32 @@
148002148117
sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
148003148118
}else if( pFunc->xSFunc!=noopStepFunc ){
148004148119
int addrIf = 0;
148005148120
if( pWin->pFilter ){
148006148121
int regTmp;
148007
- assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
148008
- assert( nArg || pWin->pOwner->x.pList==0 );
148122
+ assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
148123
+ assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
148009148124
regTmp = sqlite3GetTempReg(pParse);
148010148125
sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
148011148126
addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
148012148127
VdbeCoverage(v);
148013148128
sqlite3ReleaseTempReg(pParse, regTmp);
148129
+ }
148130
+ if( pWin->bExprArgs ){
148131
+ int iStart = sqlite3VdbeCurrentAddr(v);
148132
+ VdbeOp *pOp, *pEnd;
148133
+
148134
+ nArg = pWin->pOwner->x.pList->nExpr;
148135
+ regArg = sqlite3GetTempRange(pParse, nArg);
148136
+ sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
148137
+
148138
+ pEnd = sqlite3VdbeGetOp(v, -1);
148139
+ for(pOp=sqlite3VdbeGetOp(v, iStart); pOp<=pEnd; pOp++){
148140
+ if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
148141
+ pOp->p1 = csr;
148142
+ }
148143
+ }
148014148144
}
148015148145
if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
148016148146
CollSeq *pColl;
148017148147
assert( nArg>0 );
148018148148
pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
@@ -148020,10 +148150,13 @@
148020148150
}
148021148151
sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
148022148152
bInverse, regArg, pWin->regAccum);
148023148153
sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
148024148154
sqlite3VdbeChangeP5(v, (u8)nArg);
148155
+ if( pWin->bExprArgs ){
148156
+ sqlite3ReleaseTempRange(pParse, regArg, nArg);
148157
+ }
148025148158
if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
148026148159
}
148027148160
}
148028148161
}
148029148162
@@ -148718,10 +148851,11 @@
148718148851
Window *pNew = 0;
148719148852
if( ALWAYS(p) ){
148720148853
pNew = sqlite3DbMallocZero(db, sizeof(Window));
148721148854
if( pNew ){
148722148855
pNew->zName = sqlite3DbStrDup(db, p->zName);
148856
+ pNew->zBase = sqlite3DbStrDup(db, p->zBase);
148723148857
pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
148724148858
pNew->pFunc = p->pFunc;
148725148859
pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
148726148860
pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
148727148861
pNew->eFrmType = p->eFrmType;
@@ -148730,10 +148864,11 @@
148730148864
pNew->eExclude = p->eExclude;
148731148865
pNew->regResult = p->regResult;
148732148866
pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
148733148867
pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
148734148868
pNew->pOwner = pOwner;
148869
+ pNew->bImplicitFrame = p->bImplicitFrame;
148735148870
}
148736148871
}
148737148872
return pNew;
148738148873
}
148739148874
@@ -149273,11 +149408,11 @@
149273149408
if( regEnd ){
149274149409
sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
149275149410
windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
149276149411
}
149277149412
149278
- if( pMWin->eStart==pMWin->eEnd && regStart ){
149413
+ if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
149279149414
int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
149280149415
int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
149281149416
VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
149282149417
VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
149283149418
windowAggFinal(&s, 0);
@@ -157734,11 +157869,11 @@
157734157869
return SQLITE_MISUSE_BKPT;
157735157870
}
157736157871
157737157872
assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
157738157873
assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
157739
- extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY);
157874
+ extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|SQLITE_SUBTYPE);
157740157875
enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
157741157876
157742157877
#ifndef SQLITE_OMIT_UTF16
157743157878
/* If SQLITE_UTF16 is specified as the encoding type, transform this
157744157879
** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
@@ -164202,11 +164337,12 @@
164202164337
iWrite = iVal - *piPrev;
164203164338
}else{
164204164339
iWrite = *piPrev - iVal;
164205164340
}
164206164341
assert( *pbFirst || *piPrev==0 );
164207
- assert( *pbFirst==0 || iWrite>0 );
164342
+ assert_fts3_nc( *pbFirst==0 || iWrite>0 );
164343
+ assert( *pbFirst==0 || iWrite>=0 );
164208164344
*pp += sqlite3Fts3PutVarint(*pp, iWrite);
164209164345
*piPrev = iVal;
164210164346
*pbFirst = 1;
164211164347
}
164212164348
@@ -164308,10 +164444,12 @@
164308164444
}else{
164309164445
fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
164310164446
fts3PoslistCopy(&p, &p2);
164311164447
fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
164312164448
}
164449
+
164450
+ assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
164313164451
}
164314164452
164315164453
if( rc!=SQLITE_OK ){
164316164454
sqlite3_free(aOut);
164317164455
p = aOut = 0;
@@ -181914,11 +182052,11 @@
181914182052
pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
181915182053
if( pStr ){
181916182054
if( pStr->zBuf==0 ){
181917182055
jsonInit(pStr, ctx);
181918182056
jsonAppendChar(pStr, '[');
181919
- }else{
182057
+ }else if( pStr->nUsed>1 ){
181920182058
jsonAppendChar(pStr, ',');
181921182059
pStr->pCtx = ctx;
181922182060
}
181923182061
jsonAppendValue(pStr, argv[0]);
181924182062
}
@@ -181962,13 +182100,15 @@
181962182100
static void jsonGroupInverse(
181963182101
sqlite3_context *ctx,
181964182102
int argc,
181965182103
sqlite3_value **argv
181966182104
){
181967
- int i;
182105
+ unsigned int i;
181968182106
int inStr = 0;
182107
+ int nNest = 0;
181969182108
char *z;
182109
+ char c;
181970182110
JsonString *pStr;
181971182111
UNUSED_PARAM(argc);
181972182112
UNUSED_PARAM(argv);
181973182113
pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
181974182114
#ifdef NEVER
@@ -181975,16 +182115,22 @@
181975182115
/* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
181976182116
** always have been called to initalize it */
181977182117
if( NEVER(!pStr) ) return;
181978182118
#endif
181979182119
z = pStr->zBuf;
181980
- for(i=1; z[i]!=',' || inStr; i++){
181981
- assert( i<pStr->nUsed );
181982
- if( z[i]=='"' ){
182120
+ for(i=1; (c = z[i])!=',' || inStr || nNest; i++){
182121
+ if( i>=pStr->nUsed ){
182122
+ pStr->nUsed = 1;
182123
+ return;
182124
+ }
182125
+ if( c=='"' ){
181983182126
inStr = !inStr;
181984
- }else if( z[i]=='\\' ){
182127
+ }else if( c=='\\' ){
181985182128
i++;
182129
+ }else if( !inStr ){
182130
+ if( c=='{' || c=='[' ) nNest++;
182131
+ if( c=='}' || c==']' ) nNest--;
181986182132
}
181987182133
}
181988182134
pStr->nUsed -= i;
181989182135
memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
181990182136
}
@@ -182010,11 +182156,11 @@
182010182156
pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
182011182157
if( pStr ){
182012182158
if( pStr->zBuf==0 ){
182013182159
jsonInit(pStr, ctx);
182014182160
jsonAppendChar(pStr, '{');
182015
- }else{
182161
+ }else if( pStr->nUsed>1 ){
182016182162
jsonAppendChar(pStr, ',');
182017182163
pStr->pCtx = ctx;
182018182164
}
182019182165
z = (const char*)sqlite3_value_text(argv[0]);
182020182166
n = (u32)sqlite3_value_bytes(argv[0]);
@@ -182598,18 +182744,18 @@
182598182744
{ "json_tree", &jsonTreeModule },
182599182745
};
182600182746
#endif
182601182747
for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
182602182748
rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
182603
- SQLITE_UTF8 | SQLITE_DETERMINISTIC,
182749
+ SQLITE_UTF8 | SQLITE_DETERMINISTIC,
182604182750
(void*)&aFunc[i].flag,
182605182751
aFunc[i].xFunc, 0, 0);
182606182752
}
182607182753
#ifndef SQLITE_OMIT_WINDOWFUNC
182608182754
for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
182609182755
rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
182610
- SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
182756
+ SQLITE_SUBTYPE | SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
182611182757
aAgg[i].xStep, aAgg[i].xFinal,
182612182758
aAgg[i].xValue, jsonGroupInverse, 0);
182613182759
}
182614182760
#endif
182615182761
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -203607,10 +203753,11 @@
203607203753
static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
203608203754
static int sqlite3Fts5ExprEof(Fts5Expr*);
203609203755
static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
203610203756
203611203757
static void sqlite3Fts5ExprFree(Fts5Expr*);
203758
+static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
203612203759
203613203760
/* Called during startup to register a UDF with SQLite */
203614203761
static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
203615203762
203616203763
static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
@@ -207014,11 +207161,11 @@
207014207161
assert( zSql || rc==SQLITE_NOMEM );
207015207162
if( zSql ){
207016207163
rc = sqlite3_declare_vtab(pConfig->db, zSql);
207017207164
sqlite3_free(zSql);
207018207165
}
207019
-
207166
+
207020207167
return rc;
207021207168
}
207022207169
207023207170
/*
207024207171
** Tokenize the text passed via the second and third arguments.
@@ -207601,10 +207748,46 @@
207601207748
sqlite3Fts5ParseNodeFree(p->pRoot);
207602207749
sqlite3_free(p->apExprPhrase);
207603207750
sqlite3_free(p);
207604207751
}
207605207752
}
207753
+
207754
+static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
207755
+ Fts5Parse sParse;
207756
+ memset(&sParse, 0, sizeof(sParse));
207757
+
207758
+ if( *pp1 ){
207759
+ Fts5Expr *p1 = *pp1;
207760
+ int nPhrase = p1->nPhrase + p2->nPhrase;
207761
+
207762
+ p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
207763
+ p2->pRoot = 0;
207764
+
207765
+ if( sParse.rc==SQLITE_OK ){
207766
+ Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
207767
+ p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
207768
+ );
207769
+ if( ap==0 ){
207770
+ sParse.rc = SQLITE_NOMEM;
207771
+ }else{
207772
+ int i;
207773
+ memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
207774
+ for(i=0; i<p2->nPhrase; i++){
207775
+ ap[i] = p2->apExprPhrase[i];
207776
+ }
207777
+ p1->nPhrase = nPhrase;
207778
+ p1->apExprPhrase = ap;
207779
+ }
207780
+ }
207781
+ sqlite3_free(p2->apExprPhrase);
207782
+ sqlite3_free(p2);
207783
+ }else{
207784
+ *pp1 = p2;
207785
+ }
207786
+
207787
+ return sParse.rc;
207788
+}
207606207789
207607207790
/*
207608207791
** Argument pTerm must be a synonym iterator. Return the current rowid
207609207792
** that it points to.
207610207793
*/
@@ -211421,11 +211604,11 @@
211421211604
}
211422211605
211423211606
static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
211424211607
Fts5Data *pRet = fts5DataRead(p, iRowid);
211425211608
if( pRet ){
211426
- if( pRet->szLeaf>pRet->nn ){
211609
+ if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
211427211610
p->rc = FTS5_CORRUPT;
211428211611
fts5DataRelease(pRet);
211429211612
pRet = 0;
211430211613
}
211431211614
}
@@ -217765,20 +217948,42 @@
217765217948
217766217949
/*
217767217950
** Implementation of the xBestIndex method for FTS5 tables. Within the
217768217951
** WHERE constraint, it searches for the following:
217769217952
**
217770
-** 1. A MATCH constraint against the special column.
217953
+** 1. A MATCH constraint against the table column.
217771217954
** 2. A MATCH constraint against the "rank" column.
217772
-** 3. An == constraint against the rowid column.
217773
-** 4. A < or <= constraint against the rowid column.
217774
-** 5. A > or >= constraint against the rowid column.
217955
+** 3. A MATCH constraint against some other column.
217956
+** 4. An == constraint against the rowid column.
217957
+** 5. A < or <= constraint against the rowid column.
217958
+** 6. A > or >= constraint against the rowid column.
217775217959
**
217776
-** Within the ORDER BY, either:
217960
+** Within the ORDER BY, the following are supported:
217777217961
**
217778217962
** 5. ORDER BY rank [ASC|DESC]
217779217963
** 6. ORDER BY rowid [ASC|DESC]
217964
+**
217965
+** Information for the xFilter call is passed via both the idxNum and
217966
+** idxStr variables. Specifically, idxNum is a bitmask of the following
217967
+** flags used to encode the ORDER BY clause:
217968
+**
217969
+** FTS5_BI_ORDER_RANK
217970
+** FTS5_BI_ORDER_ROWID
217971
+** FTS5_BI_ORDER_DESC
217972
+**
217973
+** idxStr is used to encode data from the WHERE clause. For each argument
217974
+** passed to the xFilter method, the following is appended to idxStr:
217975
+**
217976
+** Match against table column: "m"
217977
+** Match against rank column: "r"
217978
+** Match against other column: "<column-number>"
217979
+** Equality constraint against the rowid: "="
217980
+** A < or <= against the rowid: "<"
217981
+** A > or >= against the rowid: ">"
217982
+**
217983
+** This function ensures that there is at most one "r" or "=". And that if
217984
+** there exists an "=" then there is no "<" or ">".
217780217985
**
217781217986
** Costs are assigned as follows:
217782217987
**
217783217988
** a) If an unusable MATCH operator is present in the WHERE clause, the
217784217989
** cost is unconditionally set to 1e50 (a really big number).
@@ -217803,36 +218008,22 @@
217803218008
static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
217804218009
Fts5Table *pTab = (Fts5Table*)pVTab;
217805218010
Fts5Config *pConfig = pTab->pConfig;
217806218011
const int nCol = pConfig->nCol;
217807218012
int idxFlags = 0; /* Parameter passed through to xFilter() */
217808
- int bHasMatch;
217809
- int iNext;
217810218013
int i;
217811218014
217812
- struct Constraint {
217813
- int op; /* Mask against sqlite3_index_constraint.op */
217814
- int fts5op; /* FTS5 mask for idxFlags */
217815
- int iCol; /* 0==rowid, 1==tbl, 2==rank */
217816
- int omit; /* True to omit this if found */
217817
- int iConsIndex; /* Index in pInfo->aConstraint[] */
217818
- } aConstraint[] = {
217819
- {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
217820
- FTS5_BI_MATCH, 1, 1, -1},
217821
- {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
217822
- FTS5_BI_RANK, 2, 1, -1},
217823
- {SQLITE_INDEX_CONSTRAINT_EQ, FTS5_BI_ROWID_EQ, 0, 0, -1},
217824
- {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE,
217825
- FTS5_BI_ROWID_LE, 0, 0, -1},
217826
- {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE,
217827
- FTS5_BI_ROWID_GE, 0, 0, -1},
217828
- };
217829
-
217830
- int aColMap[3];
217831
- aColMap[0] = -1;
217832
- aColMap[1] = nCol;
217833
- aColMap[2] = nCol+1;
218015
+ char *idxStr;
218016
+ int iIdxStr = 0;
218017
+ int iCons = 0;
218018
+
218019
+ int bSeenEq = 0;
218020
+ int bSeenGt = 0;
218021
+ int bSeenLt = 0;
218022
+ int bSeenMatch = 0;
218023
+ int bSeenRank = 0;
218024
+
217834218025
217835218026
assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
217836218027
assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
217837218028
assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
217838218029
assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
@@ -217843,44 +218034,82 @@
217843218034
"recursively defined fts5 content table"
217844218035
);
217845218036
return SQLITE_ERROR;
217846218037
}
217847218038
217848
- /* Set idxFlags flags for all WHERE clause terms that will be used. */
218039
+ idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 6 + 1);
218040
+ if( idxStr==0 ) return SQLITE_NOMEM;
218041
+ pInfo->idxStr = idxStr;
218042
+ pInfo->needToFreeIdxStr = 1;
218043
+
217849218044
for(i=0; i<pInfo->nConstraint; i++){
217850218045
struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
217851218046
int iCol = p->iColumn;
217852
-
217853
- if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)
217854
- || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol==nCol)
218047
+ if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
218048
+ || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
217855218049
){
217856218050
/* A MATCH operator or equivalent */
217857
- if( p->usable ){
217858
- idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
217859
- aConstraint[0].iConsIndex = i;
217860
- }else{
218051
+ if( p->usable==0 || iCol<0 ){
217861218052
/* As there exists an unusable MATCH constraint this is an
217862218053
** unusable plan. Set a prohibitively high cost. */
217863218054
pInfo->estimatedCost = 1e50;
218055
+ assert( iIdxStr < pInfo->nConstraint*6 + 1 );
218056
+ idxStr[iIdxStr] = 0;
217864218057
return SQLITE_OK;
217865
- }
217866
- }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH ){
217867
- int j;
217868
- for(j=1; j<ArraySize(aConstraint); j++){
217869
- struct Constraint *pC = &aConstraint[j];
217870
- if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
217871
- pC->iConsIndex = i;
217872
- idxFlags |= pC->fts5op;
218058
+ }else{
218059
+ if( iCol==nCol+1 ){
218060
+ if( bSeenRank ) continue;
218061
+ idxStr[iIdxStr++] = 'r';
218062
+ bSeenRank = 1;
218063
+ }else{
218064
+ bSeenMatch = 1;
218065
+ idxStr[iIdxStr++] = 'm';
218066
+ if( iCol<nCol ){
218067
+ sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
218068
+ idxStr += strlen(&idxStr[iIdxStr]);
218069
+ assert( idxStr[iIdxStr]=='\0' );
218070
+ }
218071
+ }
218072
+ pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218073
+ pInfo->aConstraintUsage[i].omit = 1;
218074
+ }
218075
+ }
218076
+ else if( p->usable && bSeenEq==0
218077
+ && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0
218078
+ ){
218079
+ idxStr[iIdxStr++] = '=';
218080
+ bSeenEq = 1;
218081
+ pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218082
+ }
218083
+ }
218084
+
218085
+ if( bSeenEq==0 ){
218086
+ for(i=0; i<pInfo->nConstraint; i++){
218087
+ struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
218088
+ if( p->iColumn<0 && p->usable ){
218089
+ int op = p->op;
218090
+ if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
218091
+ if( bSeenLt ) continue;
218092
+ idxStr[iIdxStr++] = '<';
218093
+ pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218094
+ bSeenLt = 1;
218095
+ }else
218096
+ if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
218097
+ if( bSeenGt ) continue;
218098
+ idxStr[iIdxStr++] = '>';
218099
+ pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218100
+ bSeenGt = 1;
217873218101
}
217874218102
}
217875218103
}
217876218104
}
218105
+ idxStr[iIdxStr] = '\0';
217877218106
217878218107
/* Set idxFlags flags for the ORDER BY clause */
217879218108
if( pInfo->nOrderBy==1 ){
217880218109
int iSort = pInfo->aOrderBy[0].iColumn;
217881
- if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
218110
+ if( iSort==(pConfig->nCol+1) && bSeenMatch ){
217882218111
idxFlags |= FTS5_BI_ORDER_RANK;
217883218112
}else if( iSort==-1 ){
217884218113
idxFlags |= FTS5_BI_ORDER_ROWID;
217885218114
}
217886218115
if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
@@ -217890,30 +218119,19 @@
217890218119
}
217891218120
}
217892218121
}
217893218122
217894218123
/* Calculate the estimated cost based on the flags set in idxFlags. */
217895
- bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
217896
- if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
217897
- pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
217898
- if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
217899
- }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
217900
- pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
217901
- }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
217902
- pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
218124
+ if( bSeenEq ){
218125
+ pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
218126
+ if( bSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
218127
+ }else if( bSeenLt && bSeenGt ){
218128
+ pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
218129
+ }else if( bSeenLt || bSeenGt ){
218130
+ pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
217903218131
}else{
217904
- pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
217905
- }
217906
-
217907
- /* Assign argvIndex values to each constraint in use. */
217908
- iNext = 1;
217909
- for(i=0; i<ArraySize(aConstraint); i++){
217910
- struct Constraint *pC = &aConstraint[i];
217911
- if( pC->iConsIndex>=0 ){
217912
- pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
217913
- pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
217914
- }
218132
+ pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
217915218133
}
217916218134
217917218135
pInfo->idxNum = idxFlags;
217918218136
return SQLITE_OK;
217919218137
}
@@ -218432,31 +218650,29 @@
218432218650
** 3. A full-table scan.
218433218651
*/
218434218652
static int fts5FilterMethod(
218435218653
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
218436218654
int idxNum, /* Strategy index */
218437
- const char *zUnused, /* Unused */
218655
+ const char *idxStr, /* Unused */
218438218656
int nVal, /* Number of elements in apVal */
218439218657
sqlite3_value **apVal /* Arguments for the indexing scheme */
218440218658
){
218441218659
Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
218442218660
Fts5Config *pConfig = pTab->p.pConfig;
218443218661
Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
218444218662
int rc = SQLITE_OK; /* Error code */
218445
- int iVal = 0; /* Counter for apVal[] */
218446218663
int bDesc; /* True if ORDER BY [rank|rowid] DESC */
218447218664
int bOrderByRank; /* True if ORDER BY rank */
218448
- sqlite3_value *pMatch = 0; /* <tbl> MATCH ? expression (or NULL) */
218449218665
sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
218450218666
sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
218451218667
sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
218452218668
sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
218453218669
int iCol; /* Column on LHS of MATCH operator */
218454218670
char **pzErrmsg = pConfig->pzErrmsg;
218455
-
218456
- UNUSED_PARAM(zUnused);
218457
- UNUSED_PARAM(nVal);
218671
+ int i;
218672
+ int iIdxStr = 0;
218673
+ Fts5Expr *pExpr = 0;
218458218674
218459218675
if( pCsr->ePlan ){
218460218676
fts5FreeCursorComponents(pCsr);
218461218677
memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
218462218678
}
@@ -218465,27 +218681,64 @@
218465218681
assert( pCsr->pExpr==0 );
218466218682
assert( pCsr->csrflags==0 );
218467218683
assert( pCsr->pRank==0 );
218468218684
assert( pCsr->zRank==0 );
218469218685
assert( pCsr->zRankArgs==0 );
218686
+ assert( pTab->pSortCsr==0 || nVal==0 );
218470218687
218471218688
assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
218472218689
pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
218473218690
218474
- /* Decode the arguments passed through to this function.
218475
- **
218476
- ** Note: The following set of if(...) statements must be in the same
218477
- ** order as the corresponding entries in the struct at the top of
218478
- ** fts5BestIndexMethod(). */
218479
- if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
218480
- if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
218481
- if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
218482
- if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
218483
- if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
218484
- iCol = (idxNum>>16);
218485
- assert( iCol>=0 && iCol<=pConfig->nCol );
218486
- assert( iVal==nVal );
218691
+ /* Decode the arguments passed through to this function. */
218692
+ for(i=0; i<nVal; i++){
218693
+ switch( idxStr[iIdxStr++] ){
218694
+ case 'r':
218695
+ pRank = apVal[i];
218696
+ break;
218697
+ case 'm': {
218698
+ const char *zText = (const char*)sqlite3_value_text(apVal[i]);
218699
+ if( zText==0 ) zText = "";
218700
+
218701
+ if( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ){
218702
+ iCol = 0;
218703
+ do{
218704
+ iCol = iCol*10 + (idxStr[iIdxStr]-'0');
218705
+ iIdxStr++;
218706
+ }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
218707
+ }else{
218708
+ iCol = pConfig->nCol;
218709
+ }
218710
+
218711
+ if( zText[0]=='*' ){
218712
+ /* The user has issued a query of the form "MATCH '*...'". This
218713
+ ** indicates that the MATCH expression is not a full text query,
218714
+ ** but a request for an internal parameter. */
218715
+ rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
218716
+ goto filter_out;
218717
+ }else{
218718
+ char **pzErr = &pTab->p.base.zErrMsg;
218719
+ rc = sqlite3Fts5ExprNew(pConfig, iCol, zText, &pExpr, pzErr);
218720
+ if( rc==SQLITE_OK ){
218721
+ rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
218722
+ pExpr = 0;
218723
+ }
218724
+ if( rc!=SQLITE_OK ) goto filter_out;
218725
+ }
218726
+
218727
+ break;
218728
+ }
218729
+ case '=':
218730
+ pRowidEq = apVal[i];
218731
+ break;
218732
+ case '<':
218733
+ pRowidLe = apVal[i];
218734
+ break;
218735
+ default: assert( idxStr[iIdxStr-1]=='>' );
218736
+ pRowidGe = apVal[i];
218737
+ break;
218738
+ }
218739
+ }
218487218740
bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
218488218741
pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
218489218742
218490218743
/* Set the cursor upper and lower rowid limits. Only some strategies
218491218744
** actually use them. This is ok, as the xBestIndex() method leaves the
@@ -218508,11 +218761,11 @@
218508218761
** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
218509218762
** return results to the user for this query. The current cursor
218510218763
** (pCursor) is used to execute the query issued by function
218511218764
** fts5CursorFirstSorted() above. */
218512218765
assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
218513
- assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
218766
+ assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
218514218767
assert( pCsr->iLastRowid==LARGEST_INT64 );
218515218768
assert( pCsr->iFirstRowid==SMALLEST_INT64 );
218516218769
if( pTab->pSortCsr->bDesc ){
218517218770
pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
218518218771
pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
@@ -218521,33 +218774,19 @@
218521218774
pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
218522218775
}
218523218776
pCsr->ePlan = FTS5_PLAN_SOURCE;
218524218777
pCsr->pExpr = pTab->pSortCsr->pExpr;
218525218778
rc = fts5CursorFirst(pTab, pCsr, bDesc);
218526
- }else if( pMatch ){
218527
- const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
218528
- if( zExpr==0 ) zExpr = "";
218529
-
218779
+ }else if( pCsr->pExpr ){
218530218780
rc = fts5CursorParseRank(pConfig, pCsr, pRank);
218531218781
if( rc==SQLITE_OK ){
218532
- if( zExpr[0]=='*' ){
218533
- /* The user has issued a query of the form "MATCH '*...'". This
218534
- ** indicates that the MATCH expression is not a full text query,
218535
- ** but a request for an internal parameter. */
218536
- rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
218537
- }else{
218538
- char **pzErr = &pTab->p.base.zErrMsg;
218539
- rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
218540
- if( rc==SQLITE_OK ){
218541
- if( bOrderByRank ){
218542
- pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
218543
- rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
218544
- }else{
218545
- pCsr->ePlan = FTS5_PLAN_MATCH;
218546
- rc = fts5CursorFirst(pTab, pCsr, bDesc);
218547
- }
218548
- }
218782
+ if( bOrderByRank ){
218783
+ pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
218784
+ rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
218785
+ }else{
218786
+ pCsr->ePlan = FTS5_PLAN_MATCH;
218787
+ rc = fts5CursorFirst(pTab, pCsr, bDesc);
218549218788
}
218550218789
}
218551218790
}else if( pConfig->zContent==0 ){
218552218791
*pConfig->pzErrmsg = sqlite3_mprintf(
218553218792
"%s: table does not support scanning", pConfig->zName
@@ -218560,19 +218799,21 @@
218560218799
rc = sqlite3Fts5StorageStmt(
218561218800
pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
218562218801
);
218563218802
if( rc==SQLITE_OK ){
218564218803
if( pCsr->ePlan==FTS5_PLAN_ROWID ){
218565
- sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
218804
+ sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
218566218805
}else{
218567218806
sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
218568218807
sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
218569218808
}
218570218809
rc = fts5NextMethod(pCursor);
218571218810
}
218572218811
}
218573218812
218813
+ filter_out:
218814
+ sqlite3Fts5ExprFree(pExpr);
218574218815
pConfig->pzErrmsg = pzErrmsg;
218575218816
return rc;
218576218817
}
218577218818
218578218819
/*
@@ -219955,11 +220196,11 @@
219955220196
int nArg, /* Number of args */
219956220197
sqlite3_value **apUnused /* Function arguments */
219957220198
){
219958220199
assert( nArg==0 );
219959220200
UNUSED_PARAM2(nArg, apUnused);
219960
- sqlite3_result_text(pCtx, "fts5: 2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2d897d", -1, SQLITE_TRANSIENT);
220201
+ sqlite3_result_text(pCtx, "fts5: 2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f044cbd", -1, SQLITE_TRANSIENT);
219961220202
}
219962220203
219963220204
/*
219964220205
** Return true if zName is the extension on one of the shadow tables used
219965220206
** by this module.
@@ -224723,12 +224964,12 @@
224723224964
}
224724224965
#endif /* SQLITE_CORE */
224725224966
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
224726224967
224727224968
/************** End of stmt.c ************************************************/
224728
-#if __LINE__!=224728
224969
+#if __LINE__!=224969
224729224970
#undef SQLITE_SOURCE_ID
224730
-#define SQLITE_SOURCE_ID "2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2dalt2"
224971
+#define SQLITE_SOURCE_ID "2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f04alt2"
224731224972
#endif
224732224973
/* Return the source-id for this library */
224733224974
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
224734224975
/************************** End of sqlite3.c ******************************/
224735224976
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1165,11 +1165,11 @@
1165 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1166 ** [sqlite_version()] and [sqlite_source_id()].
1167 */
1168 #define SQLITE_VERSION "3.30.0"
1169 #define SQLITE_VERSION_NUMBER 3030000
1170 #define SQLITE_SOURCE_ID "2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2d897d"
1171
1172 /*
1173 ** CAPI3REF: Run-Time Library Version Numbers
1174 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1175 **
@@ -5900,13 +5900,16 @@
5900 ** the same inputs within a single SQL statement. Most SQL functions are
5901 ** deterministic. The built-in [random()] SQL function is an example of a
5902 ** function that is not deterministic. The SQLite query planner is able to
5903 ** perform additional optimizations on deterministic functions, so use
5904 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
 
5905 ** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5906 ** flag, which if present prevents the function from being invoked from
5907 ** within VIEWs or TRIGGERs.
 
 
5908 **
5909 ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5910 ** function can gain access to this pointer using [sqlite3_user_data()].)^
5911 **
5912 ** ^The sixth, seventh and eighth parameters passed to the three
@@ -6026,14 +6029,28 @@
6026 ** The SQLITE_DETERMINISTIC flag means that the new function will always
6027 ** maps the same inputs into the same output. The abs() function is
6028 ** deterministic, for example, but randomblob() is not.
6029 **
6030 ** The SQLITE_DIRECTONLY flag means that the function may only be invoked
6031 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs.
 
 
 
 
 
 
 
 
 
 
 
 
 
6032 */
6033 #define SQLITE_DETERMINISTIC 0x000000800
6034 #define SQLITE_DIRECTONLY 0x000080000
 
6035
6036 /*
6037 ** CAPI3REF: Deprecated Functions
6038 ** DEPRECATED
6039 **
@@ -16700,10 +16717,11 @@
16700 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
16701 #define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
16702 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
16703 #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
16704 #define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
 
16705
16706 /*
16707 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
16708 ** used to create the initializers for the FuncDef structures.
16709 **
@@ -18614,21 +18632,23 @@
18614 Window **ppThis; /* Pointer to this object in Select.pWin list */
18615 Window *pNextWin; /* Next window function belonging to this SELECT */
18616 Expr *pFilter; /* The FILTER expression */
18617 FuncDef *pFunc; /* The function */
18618 int iEphCsr; /* Partition buffer or Peer buffer */
18619 int regAccum;
18620 int regResult;
18621 int csrApp; /* Function cursor (used by min/max) */
18622 int regApp; /* Function register (also used by min/max) */
18623 int regPart; /* Array of registers for PARTITION BY values */
18624 Expr *pOwner; /* Expression object this window is attached to */
18625 int nBufferCol; /* Number of columns in buffer table */
18626 int iArgCol; /* Offset of first argument for this function */
18627 int regOne; /* Register containing constant value 1 */
18628 int regStartRowid;
18629 int regEndRowid;
 
 
18630 };
18631
18632 #ifndef SQLITE_OMIT_WINDOWFUNC
18633 SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
18634 SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
@@ -29214,11 +29234,17 @@
29214 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29215 break;
29216 }
29217
29218 case TK_COLLATE: {
29219 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
 
 
 
 
 
 
29220 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29221 break;
29222 }
29223
29224 case TK_AGG_FUNCTION:
@@ -74843,11 +74869,17 @@
74843 testcase( bPreserve && pMem->z==0 );
74844
74845 assert( pMem->szMalloc==0
74846 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
74847 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
74848 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
 
 
 
 
 
 
74849 bPreserve = 0;
74850 }else{
74851 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74852 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
74853 }
@@ -84279,10 +84311,11 @@
84279 assert( (f & (MEM_Static|MEM_Dyn))==0 );
84280 }else{
84281 c = 's';
84282 }
84283 *(zCsr++) = c;
 
84284 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
84285 zCsr += sqlite3Strlen30(zCsr);
84286 for(i=0; i<25 && i<pMem->n; i++){
84287 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
84288 zCsr += sqlite3Strlen30(zCsr);
@@ -85027,11 +85060,10 @@
85027 ** as the P1 parameter.
85028 */
85029 case OP_String8: { /* same as TK_STRING, out2 */
85030 assert( pOp->p4.z!=0 );
85031 pOut = out2Prerelease(p, pOp);
85032 pOp->opcode = OP_String;
85033 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
85034
85035 #ifndef SQLITE_OMIT_UTF16
85036 if( encoding!=SQLITE_UTF8 ){
85037 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
@@ -85051,10 +85083,11 @@
85051 }
85052 #endif
85053 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
85054 goto too_big;
85055 }
 
85056 assert( rc==SQLITE_OK );
85057 /* Fall through to the next case, OP_String */
85058 }
85059
85060 /* Opcode: String P1 P2 P3 P4 P5
@@ -100317,10 +100350,11 @@
100317 if( addrOnce ){
100318 sqlite3VdbeJumpHere(v, addrOnce);
100319 /* Subroutine return */
100320 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
100321 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
 
100322 }
100323 }
100324 #endif /* SQLITE_OMIT_SUBQUERY */
100325
100326 /*
@@ -100427,10 +100461,11 @@
100427 sqlite3VdbeJumpHere(v, addrOnce);
100428
100429 /* Subroutine return */
100430 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
100431 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
 
100432 }
100433
100434 return rReg;
100435 }
100436 #endif /* SQLITE_OMIT_SUBQUERY */
@@ -103058,10 +103093,15 @@
103058 }
103059 }
103060
103061 /*
103062 ** Mark all temporary registers as being unavailable for reuse.
 
 
 
 
 
103063 */
103064 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
103065 pParse->nTempReg = 0;
103066 pParse->nRangeReg = 0;
103067 }
@@ -114049,10 +114089,12 @@
114049 int nNeedle;
114050 int typeHaystack, typeNeedle;
114051 int N = 1;
114052 int isText;
114053 unsigned char firstChar;
 
 
114054
114055 UNUSED_PARAMETER(argc);
114056 typeHaystack = sqlite3_value_type(argv[0]);
114057 typeNeedle = sqlite3_value_type(argv[1]);
114058 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
@@ -114061,16 +114103,26 @@
114061 if( nNeedle>0 ){
114062 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
114063 zHaystack = sqlite3_value_blob(argv[0]);
114064 zNeedle = sqlite3_value_blob(argv[1]);
114065 isText = 0;
114066 }else{
114067 zHaystack = sqlite3_value_text(argv[0]);
114068 zNeedle = sqlite3_value_text(argv[1]);
114069 isText = 1;
 
 
 
 
 
 
 
 
 
 
114070 }
114071 if( zNeedle==0 || (nHaystack && zHaystack==0) ) return;
114072 firstChar = zNeedle[0];
114073 while( nNeedle<=nHaystack
114074 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
114075 ){
114076 N++;
@@ -114080,10 +114132,17 @@
114080 }while( isText && (zHaystack[0]&0xc0)==0x80 );
114081 }
114082 if( nNeedle>nHaystack ) N = 0;
114083 }
114084 sqlite3_result_int(context, N);
 
 
 
 
 
 
 
114085 }
114086
114087 /*
114088 ** Implementation of the printf() function.
114089 */
@@ -128983,10 +129042,22 @@
128983 pNew->iRightJoinTable = pExpr->iRightJoinTable;
128984 ExprSetProperty(pNew, EP_FromJoin);
128985 }
128986 sqlite3ExprDelete(db, pExpr);
128987 pExpr = pNew;
 
 
 
 
 
 
 
 
 
 
 
 
128988 }
128989 }
128990 }else{
128991 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
128992 pExpr->iTable = pSubst->iNewTable;
@@ -130849,10 +130920,23 @@
130849 ExprList *pList = pF->pExpr->x.pList;
130850 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
130851 assert( !IsWindowFunc(pF->pExpr) );
130852 if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
130853 Expr *pFilter = pF->pExpr->y.pWin->pFilter;
 
 
 
 
 
 
 
 
 
 
 
 
 
130854 addrNext = sqlite3VdbeMakeLabel(pParse);
130855 sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
130856 }
130857 if( pList ){
130858 nArg = pList->nExpr;
@@ -130899,10 +130983,11 @@
130899 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
130900 }
130901 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
130902 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
130903 }
 
130904 pAggInfo->directMode = 0;
130905 if( addrHitTest ){
130906 sqlite3VdbeJumpHere(v, addrHitTest);
130907 }
130908 }
@@ -131702,27 +131787,39 @@
131702 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
131703 pItem->u.x.iAlias = 0;
131704 }
131705 assert( 66==sqlite3LogEst(100) );
131706 if( p->nSelectRow>66 ) p->nSelectRow = 66;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131707 }else{
131708 assert( 0==sqlite3LogEst(1) );
131709 p->nSelectRow = 0;
131710 }
131711
131712 /* If there is both a GROUP BY and an ORDER BY clause and they are
131713 ** identical, then it may be possible to disable the ORDER BY clause
131714 ** on the grounds that the GROUP BY will cause elements to come out
131715 ** in the correct order. It also may not - the GROUP BY might use a
131716 ** database index that causes rows to be grouped together as required
131717 ** but not actually sorted. Either way, record the fact that the
131718 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
131719 ** variable. */
131720 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
131721 orderByGrp = 1;
131722 }
131723
131724 /* Create a label to jump to when we want to abort the query */
131725 addrEnd = sqlite3VdbeMakeLabel(pParse);
131726
131727 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
131728 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
@@ -132074,17 +132171,22 @@
132074 }else
132075 #endif /* SQLITE_OMIT_BTREECOUNT */
132076 {
132077 int regAcc = 0; /* "populate accumulators" flag */
132078
132079 /* If there are accumulator registers but no min() or max() functions,
132080 ** allocate register regAcc. Register regAcc will contain 0 the first
132081 ** time the inner loop runs, and 1 thereafter. The code generated
132082 ** by updateAccumulator() only updates the accumulator registers if
132083 ** regAcc contains 0. */
 
 
 
 
132084 if( sAggInfo.nAccumulator ){
132085 for(i=0; i<sAggInfo.nFunc; i++){
 
132086 if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
132087 }
132088 if( i==sAggInfo.nFunc ){
132089 regAcc = ++pParse->nMem;
132090 sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
@@ -139778,22 +139880,27 @@
139778 ** LIKE optimization. See, for example:
139779 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
139780 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
139781 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
139782 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
 
139783 */
139784 if( pLeft->op!=TK_COLUMN
139785 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
139786 || IsVirtual(pLeft->y.pTab) /* Value might be numeric */
139787 ){
139788 int isNum;
139789 double rDummy;
139790 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139791 if( isNum<=0 ){
139792 zNew[iTo-1]++;
139793 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139794 zNew[iTo-1]--;
 
 
 
 
139795 }
139796 if( isNum>0 ){
139797 sqlite3ExprDelete(db, pPrefix);
139798 sqlite3ValueFree(pVal);
139799 return 0;
@@ -143384,11 +143491,11 @@
143384 WhereLoop *pLoop, /* The loop to adjust downward */
143385 LogEst nRow /* Number of rows in the entire table */
143386 ){
143387 WhereTerm *pTerm, *pX;
143388 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
143389 int i, j, k;
143390 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
143391
143392 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
143393 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
143394 assert( pTerm!=0 );
@@ -143410,10 +143517,11 @@
143410 /* In the absence of explicit truth probabilities, use heuristics to
143411 ** guess a reasonable truth probability. */
143412 pLoop->nOut--;
143413 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
143414 Expr *pRight = pTerm->pExpr->pRight;
 
143415 testcase( pTerm->pExpr->op==TK_IS );
143416 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
143417 k = 10;
143418 }else{
143419 k = 20;
@@ -147493,12 +147601,19 @@
147493 /* Append the arguments passed to each window function to the
147494 ** sub-select expression list. Also allocate two registers for each
147495 ** window function - one for the accumulator, another for interim
147496 ** results. */
147497 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147498 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
147499 pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList, 0);
 
 
 
 
 
 
 
147500 if( pWin->pFilter ){
147501 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
147502 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
147503 }
147504 pWin->regAccum = ++pParse->nMem;
@@ -147960,11 +148075,11 @@
147960 Vdbe *v = sqlite3GetVdbe(pParse);
147961 Window *pWin;
147962 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147963 FuncDef *pFunc = pWin->pFunc;
147964 int regArg;
147965 int nArg = windowArgCount(pWin);
147966 int i;
147967
147968 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
147969
147970 for(i=0; i<nArg; i++){
@@ -148002,17 +148117,32 @@
148002 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
148003 }else if( pFunc->xSFunc!=noopStepFunc ){
148004 int addrIf = 0;
148005 if( pWin->pFilter ){
148006 int regTmp;
148007 assert( nArg==0 || nArg==pWin->pOwner->x.pList->nExpr );
148008 assert( nArg || pWin->pOwner->x.pList==0 );
148009 regTmp = sqlite3GetTempReg(pParse);
148010 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
148011 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
148012 VdbeCoverage(v);
148013 sqlite3ReleaseTempReg(pParse, regTmp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148014 }
148015 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
148016 CollSeq *pColl;
148017 assert( nArg>0 );
148018 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
@@ -148020,10 +148150,13 @@
148020 }
148021 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
148022 bInverse, regArg, pWin->regAccum);
148023 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
148024 sqlite3VdbeChangeP5(v, (u8)nArg);
 
 
 
148025 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
148026 }
148027 }
148028 }
148029
@@ -148718,10 +148851,11 @@
148718 Window *pNew = 0;
148719 if( ALWAYS(p) ){
148720 pNew = sqlite3DbMallocZero(db, sizeof(Window));
148721 if( pNew ){
148722 pNew->zName = sqlite3DbStrDup(db, p->zName);
 
148723 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
148724 pNew->pFunc = p->pFunc;
148725 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
148726 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
148727 pNew->eFrmType = p->eFrmType;
@@ -148730,10 +148864,11 @@
148730 pNew->eExclude = p->eExclude;
148731 pNew->regResult = p->regResult;
148732 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
148733 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
148734 pNew->pOwner = pOwner;
 
148735 }
148736 }
148737 return pNew;
148738 }
148739
@@ -149273,11 +149408,11 @@
149273 if( regEnd ){
149274 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
149275 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
149276 }
149277
149278 if( pMWin->eStart==pMWin->eEnd && regStart ){
149279 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
149280 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
149281 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
149282 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
149283 windowAggFinal(&s, 0);
@@ -157734,11 +157869,11 @@
157734 return SQLITE_MISUSE_BKPT;
157735 }
157736
157737 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
157738 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
157739 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY);
157740 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
157741
157742 #ifndef SQLITE_OMIT_UTF16
157743 /* If SQLITE_UTF16 is specified as the encoding type, transform this
157744 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
@@ -164202,11 +164337,12 @@
164202 iWrite = iVal - *piPrev;
164203 }else{
164204 iWrite = *piPrev - iVal;
164205 }
164206 assert( *pbFirst || *piPrev==0 );
164207 assert( *pbFirst==0 || iWrite>0 );
 
164208 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
164209 *piPrev = iVal;
164210 *pbFirst = 1;
164211 }
164212
@@ -164308,10 +164444,12 @@
164308 }else{
164309 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
164310 fts3PoslistCopy(&p, &p2);
164311 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
164312 }
 
 
164313 }
164314
164315 if( rc!=SQLITE_OK ){
164316 sqlite3_free(aOut);
164317 p = aOut = 0;
@@ -181914,11 +182052,11 @@
181914 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
181915 if( pStr ){
181916 if( pStr->zBuf==0 ){
181917 jsonInit(pStr, ctx);
181918 jsonAppendChar(pStr, '[');
181919 }else{
181920 jsonAppendChar(pStr, ',');
181921 pStr->pCtx = ctx;
181922 }
181923 jsonAppendValue(pStr, argv[0]);
181924 }
@@ -181962,13 +182100,15 @@
181962 static void jsonGroupInverse(
181963 sqlite3_context *ctx,
181964 int argc,
181965 sqlite3_value **argv
181966 ){
181967 int i;
181968 int inStr = 0;
 
181969 char *z;
 
181970 JsonString *pStr;
181971 UNUSED_PARAM(argc);
181972 UNUSED_PARAM(argv);
181973 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
181974 #ifdef NEVER
@@ -181975,16 +182115,22 @@
181975 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
181976 ** always have been called to initalize it */
181977 if( NEVER(!pStr) ) return;
181978 #endif
181979 z = pStr->zBuf;
181980 for(i=1; z[i]!=',' || inStr; i++){
181981 assert( i<pStr->nUsed );
181982 if( z[i]=='"' ){
 
 
 
181983 inStr = !inStr;
181984 }else if( z[i]=='\\' ){
181985 i++;
 
 
 
181986 }
181987 }
181988 pStr->nUsed -= i;
181989 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
181990 }
@@ -182010,11 +182156,11 @@
182010 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
182011 if( pStr ){
182012 if( pStr->zBuf==0 ){
182013 jsonInit(pStr, ctx);
182014 jsonAppendChar(pStr, '{');
182015 }else{
182016 jsonAppendChar(pStr, ',');
182017 pStr->pCtx = ctx;
182018 }
182019 z = (const char*)sqlite3_value_text(argv[0]);
182020 n = (u32)sqlite3_value_bytes(argv[0]);
@@ -182598,18 +182744,18 @@
182598 { "json_tree", &jsonTreeModule },
182599 };
182600 #endif
182601 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
182602 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
182603 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
182604 (void*)&aFunc[i].flag,
182605 aFunc[i].xFunc, 0, 0);
182606 }
182607 #ifndef SQLITE_OMIT_WINDOWFUNC
182608 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
182609 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
182610 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
182611 aAgg[i].xStep, aAgg[i].xFinal,
182612 aAgg[i].xValue, jsonGroupInverse, 0);
182613 }
182614 #endif
182615 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -203607,10 +203753,11 @@
203607 static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
203608 static int sqlite3Fts5ExprEof(Fts5Expr*);
203609 static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
203610
203611 static void sqlite3Fts5ExprFree(Fts5Expr*);
 
203612
203613 /* Called during startup to register a UDF with SQLite */
203614 static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
203615
203616 static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
@@ -207014,11 +207161,11 @@
207014 assert( zSql || rc==SQLITE_NOMEM );
207015 if( zSql ){
207016 rc = sqlite3_declare_vtab(pConfig->db, zSql);
207017 sqlite3_free(zSql);
207018 }
207019
207020 return rc;
207021 }
207022
207023 /*
207024 ** Tokenize the text passed via the second and third arguments.
@@ -207601,10 +207748,46 @@
207601 sqlite3Fts5ParseNodeFree(p->pRoot);
207602 sqlite3_free(p->apExprPhrase);
207603 sqlite3_free(p);
207604 }
207605 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207606
207607 /*
207608 ** Argument pTerm must be a synonym iterator. Return the current rowid
207609 ** that it points to.
207610 */
@@ -211421,11 +211604,11 @@
211421 }
211422
211423 static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
211424 Fts5Data *pRet = fts5DataRead(p, iRowid);
211425 if( pRet ){
211426 if( pRet->szLeaf>pRet->nn ){
211427 p->rc = FTS5_CORRUPT;
211428 fts5DataRelease(pRet);
211429 pRet = 0;
211430 }
211431 }
@@ -217765,20 +217948,42 @@
217765
217766 /*
217767 ** Implementation of the xBestIndex method for FTS5 tables. Within the
217768 ** WHERE constraint, it searches for the following:
217769 **
217770 ** 1. A MATCH constraint against the special column.
217771 ** 2. A MATCH constraint against the "rank" column.
217772 ** 3. An == constraint against the rowid column.
217773 ** 4. A < or <= constraint against the rowid column.
217774 ** 5. A > or >= constraint against the rowid column.
 
217775 **
217776 ** Within the ORDER BY, either:
217777 **
217778 ** 5. ORDER BY rank [ASC|DESC]
217779 ** 6. ORDER BY rowid [ASC|DESC]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217780 **
217781 ** Costs are assigned as follows:
217782 **
217783 ** a) If an unusable MATCH operator is present in the WHERE clause, the
217784 ** cost is unconditionally set to 1e50 (a really big number).
@@ -217803,36 +218008,22 @@
217803 static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
217804 Fts5Table *pTab = (Fts5Table*)pVTab;
217805 Fts5Config *pConfig = pTab->pConfig;
217806 const int nCol = pConfig->nCol;
217807 int idxFlags = 0; /* Parameter passed through to xFilter() */
217808 int bHasMatch;
217809 int iNext;
217810 int i;
217811
217812 struct Constraint {
217813 int op; /* Mask against sqlite3_index_constraint.op */
217814 int fts5op; /* FTS5 mask for idxFlags */
217815 int iCol; /* 0==rowid, 1==tbl, 2==rank */
217816 int omit; /* True to omit this if found */
217817 int iConsIndex; /* Index in pInfo->aConstraint[] */
217818 } aConstraint[] = {
217819 {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
217820 FTS5_BI_MATCH, 1, 1, -1},
217821 {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ,
217822 FTS5_BI_RANK, 2, 1, -1},
217823 {SQLITE_INDEX_CONSTRAINT_EQ, FTS5_BI_ROWID_EQ, 0, 0, -1},
217824 {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE,
217825 FTS5_BI_ROWID_LE, 0, 0, -1},
217826 {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE,
217827 FTS5_BI_ROWID_GE, 0, 0, -1},
217828 };
217829
217830 int aColMap[3];
217831 aColMap[0] = -1;
217832 aColMap[1] = nCol;
217833 aColMap[2] = nCol+1;
217834
217835 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
217836 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
217837 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
217838 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
@@ -217843,44 +218034,82 @@
217843 "recursively defined fts5 content table"
217844 );
217845 return SQLITE_ERROR;
217846 }
217847
217848 /* Set idxFlags flags for all WHERE clause terms that will be used. */
 
 
 
 
217849 for(i=0; i<pInfo->nConstraint; i++){
217850 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
217851 int iCol = p->iColumn;
217852
217853 if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)
217854 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol==nCol)
217855 ){
217856 /* A MATCH operator or equivalent */
217857 if( p->usable ){
217858 idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
217859 aConstraint[0].iConsIndex = i;
217860 }else{
217861 /* As there exists an unusable MATCH constraint this is an
217862 ** unusable plan. Set a prohibitively high cost. */
217863 pInfo->estimatedCost = 1e50;
 
 
217864 return SQLITE_OK;
217865 }
217866 }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH ){
217867 int j;
217868 for(j=1; j<ArraySize(aConstraint); j++){
217869 struct Constraint *pC = &aConstraint[j];
217870 if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
217871 pC->iConsIndex = i;
217872 idxFlags |= pC->fts5op;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217873 }
217874 }
217875 }
217876 }
 
217877
217878 /* Set idxFlags flags for the ORDER BY clause */
217879 if( pInfo->nOrderBy==1 ){
217880 int iSort = pInfo->aOrderBy[0].iColumn;
217881 if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
217882 idxFlags |= FTS5_BI_ORDER_RANK;
217883 }else if( iSort==-1 ){
217884 idxFlags |= FTS5_BI_ORDER_ROWID;
217885 }
217886 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
@@ -217890,30 +218119,19 @@
217890 }
217891 }
217892 }
217893
217894 /* Calculate the estimated cost based on the flags set in idxFlags. */
217895 bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
217896 if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
217897 pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
217898 if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
217899 }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
217900 pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
217901 }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
217902 pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
217903 }else{
217904 pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
217905 }
217906
217907 /* Assign argvIndex values to each constraint in use. */
217908 iNext = 1;
217909 for(i=0; i<ArraySize(aConstraint); i++){
217910 struct Constraint *pC = &aConstraint[i];
217911 if( pC->iConsIndex>=0 ){
217912 pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
217913 pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
217914 }
217915 }
217916
217917 pInfo->idxNum = idxFlags;
217918 return SQLITE_OK;
217919 }
@@ -218432,31 +218650,29 @@
218432 ** 3. A full-table scan.
218433 */
218434 static int fts5FilterMethod(
218435 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
218436 int idxNum, /* Strategy index */
218437 const char *zUnused, /* Unused */
218438 int nVal, /* Number of elements in apVal */
218439 sqlite3_value **apVal /* Arguments for the indexing scheme */
218440 ){
218441 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
218442 Fts5Config *pConfig = pTab->p.pConfig;
218443 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
218444 int rc = SQLITE_OK; /* Error code */
218445 int iVal = 0; /* Counter for apVal[] */
218446 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
218447 int bOrderByRank; /* True if ORDER BY rank */
218448 sqlite3_value *pMatch = 0; /* <tbl> MATCH ? expression (or NULL) */
218449 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
218450 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
218451 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
218452 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
218453 int iCol; /* Column on LHS of MATCH operator */
218454 char **pzErrmsg = pConfig->pzErrmsg;
218455
218456 UNUSED_PARAM(zUnused);
218457 UNUSED_PARAM(nVal);
218458
218459 if( pCsr->ePlan ){
218460 fts5FreeCursorComponents(pCsr);
218461 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
218462 }
@@ -218465,27 +218681,64 @@
218465 assert( pCsr->pExpr==0 );
218466 assert( pCsr->csrflags==0 );
218467 assert( pCsr->pRank==0 );
218468 assert( pCsr->zRank==0 );
218469 assert( pCsr->zRankArgs==0 );
 
218470
218471 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
218472 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
218473
218474 /* Decode the arguments passed through to this function.
218475 **
218476 ** Note: The following set of if(...) statements must be in the same
218477 ** order as the corresponding entries in the struct at the top of
218478 ** fts5BestIndexMethod(). */
218479 if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
218480 if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
218481 if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
218482 if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
218483 if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
218484 iCol = (idxNum>>16);
218485 assert( iCol>=0 && iCol<=pConfig->nCol );
218486 assert( iVal==nVal );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218487 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
218488 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
218489
218490 /* Set the cursor upper and lower rowid limits. Only some strategies
218491 ** actually use them. This is ok, as the xBestIndex() method leaves the
@@ -218508,11 +218761,11 @@
218508 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
218509 ** return results to the user for this query. The current cursor
218510 ** (pCursor) is used to execute the query issued by function
218511 ** fts5CursorFirstSorted() above. */
218512 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
218513 assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
218514 assert( pCsr->iLastRowid==LARGEST_INT64 );
218515 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
218516 if( pTab->pSortCsr->bDesc ){
218517 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
218518 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
@@ -218521,33 +218774,19 @@
218521 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
218522 }
218523 pCsr->ePlan = FTS5_PLAN_SOURCE;
218524 pCsr->pExpr = pTab->pSortCsr->pExpr;
218525 rc = fts5CursorFirst(pTab, pCsr, bDesc);
218526 }else if( pMatch ){
218527 const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
218528 if( zExpr==0 ) zExpr = "";
218529
218530 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
218531 if( rc==SQLITE_OK ){
218532 if( zExpr[0]=='*' ){
218533 /* The user has issued a query of the form "MATCH '*...'". This
218534 ** indicates that the MATCH expression is not a full text query,
218535 ** but a request for an internal parameter. */
218536 rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
218537 }else{
218538 char **pzErr = &pTab->p.base.zErrMsg;
218539 rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
218540 if( rc==SQLITE_OK ){
218541 if( bOrderByRank ){
218542 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
218543 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
218544 }else{
218545 pCsr->ePlan = FTS5_PLAN_MATCH;
218546 rc = fts5CursorFirst(pTab, pCsr, bDesc);
218547 }
218548 }
218549 }
218550 }
218551 }else if( pConfig->zContent==0 ){
218552 *pConfig->pzErrmsg = sqlite3_mprintf(
218553 "%s: table does not support scanning", pConfig->zName
@@ -218560,19 +218799,21 @@
218560 rc = sqlite3Fts5StorageStmt(
218561 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
218562 );
218563 if( rc==SQLITE_OK ){
218564 if( pCsr->ePlan==FTS5_PLAN_ROWID ){
218565 sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
218566 }else{
218567 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
218568 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
218569 }
218570 rc = fts5NextMethod(pCursor);
218571 }
218572 }
218573
 
 
218574 pConfig->pzErrmsg = pzErrmsg;
218575 return rc;
218576 }
218577
218578 /*
@@ -219955,11 +220196,11 @@
219955 int nArg, /* Number of args */
219956 sqlite3_value **apUnused /* Function arguments */
219957 ){
219958 assert( nArg==0 );
219959 UNUSED_PARAM2(nArg, apUnused);
219960 sqlite3_result_text(pCtx, "fts5: 2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2d897d", -1, SQLITE_TRANSIENT);
219961 }
219962
219963 /*
219964 ** Return true if zName is the extension on one of the shadow tables used
219965 ** by this module.
@@ -224723,12 +224964,12 @@
224723 }
224724 #endif /* SQLITE_CORE */
224725 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
224726
224727 /************** End of stmt.c ************************************************/
224728 #if __LINE__!=224728
224729 #undef SQLITE_SOURCE_ID
224730 #define SQLITE_SOURCE_ID "2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2dalt2"
224731 #endif
224732 /* Return the source-id for this library */
224733 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
224734 /************************** End of sqlite3.c ******************************/
224735
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1165,11 +1165,11 @@
1165 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
1166 ** [sqlite_version()] and [sqlite_source_id()].
1167 */
1168 #define SQLITE_VERSION "3.30.0"
1169 #define SQLITE_VERSION_NUMBER 3030000
1170 #define SQLITE_SOURCE_ID "2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f044cbd"
1171
1172 /*
1173 ** CAPI3REF: Run-Time Library Version Numbers
1174 ** KEYWORDS: sqlite3_version sqlite3_sourceid
1175 **
@@ -5900,13 +5900,16 @@
5900 ** the same inputs within a single SQL statement. Most SQL functions are
5901 ** deterministic. The built-in [random()] SQL function is an example of a
5902 ** function that is not deterministic. The SQLite query planner is able to
5903 ** perform additional optimizations on deterministic functions, so use
5904 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5905 **
5906 ** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5907 ** flag, which if present prevents the function from being invoked from
5908 ** within VIEWs or TRIGGERs. For security reasons, the [SQLITE_DIRECTONLY]
5909 ** flag is recommended for any application-defined SQL function that has
5910 ** side-effects.
5911 **
5912 ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
5913 ** function can gain access to this pointer using [sqlite3_user_data()].)^
5914 **
5915 ** ^The sixth, seventh and eighth parameters passed to the three
@@ -6026,14 +6029,28 @@
6029 ** The SQLITE_DETERMINISTIC flag means that the new function will always
6030 ** maps the same inputs into the same output. The abs() function is
6031 ** deterministic, for example, but randomblob() is not.
6032 **
6033 ** The SQLITE_DIRECTONLY flag means that the function may only be invoked
6034 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs. This is
6035 ** a security feature which is recommended for all
6036 ** [application-defined SQL functions] that have side-effects. This flag
6037 ** prevents an attacker from adding triggers and views to a schema then
6038 ** tricking a high-privilege application into causing unintended side-effects
6039 ** while performing ordinary queries.
6040 **
6041 ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
6042 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6043 ** Specifying this flag makes no difference for scalar or aggregate user
6044 ** functions. However, if it is not specified for a user-defined window
6045 ** function, then any sub-types belonging to arguments passed to the window
6046 ** function may be discarded before the window function is called (i.e.
6047 ** sqlite3_value_subtype() will always return 0).
6048 */
6049 #define SQLITE_DETERMINISTIC 0x000000800
6050 #define SQLITE_DIRECTONLY 0x000080000
6051 #define SQLITE_SUBTYPE 0x000100000
6052
6053 /*
6054 ** CAPI3REF: Deprecated Functions
6055 ** DEPRECATED
6056 **
@@ -16700,10 +16717,11 @@
16717 #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
16718 #define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
16719 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
16720 #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
16721 #define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
16722 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
16723
16724 /*
16725 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
16726 ** used to create the initializers for the FuncDef structures.
16727 **
@@ -18614,21 +18632,23 @@
18632 Window **ppThis; /* Pointer to this object in Select.pWin list */
18633 Window *pNextWin; /* Next window function belonging to this SELECT */
18634 Expr *pFilter; /* The FILTER expression */
18635 FuncDef *pFunc; /* The function */
18636 int iEphCsr; /* Partition buffer or Peer buffer */
18637 int regAccum; /* Accumulator */
18638 int regResult; /* Interim result */
18639 int csrApp; /* Function cursor (used by min/max) */
18640 int regApp; /* Function register (also used by min/max) */
18641 int regPart; /* Array of registers for PARTITION BY values */
18642 Expr *pOwner; /* Expression object this window is attached to */
18643 int nBufferCol; /* Number of columns in buffer table */
18644 int iArgCol; /* Offset of first argument for this function */
18645 int regOne; /* Register containing constant value 1 */
18646 int regStartRowid;
18647 int regEndRowid;
18648 u8 bExprArgs; /* Defer evaluation of window function arguments
18649 ** due to the SQLITE_SUBTYPE flag */
18650 };
18651
18652 #ifndef SQLITE_OMIT_WINDOWFUNC
18653 SQLITE_PRIVATE void sqlite3WindowDelete(sqlite3*, Window*);
18654 SQLITE_PRIVATE void sqlite3WindowUnlinkFromSelect(Window*);
@@ -29214,11 +29234,17 @@
29234 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29235 break;
29236 }
29237
29238 case TK_COLLATE: {
29239 /* COLLATE operators without the EP_Collate flag are intended to
29240 ** emulate collation associated with a table column. Explicit
29241 ** COLLATE operators that appear in the original SQL always have
29242 ** the EP_Collate bit set */
29243 sqlite3TreeViewLine(pView, "%sCOLLATE %Q%s",
29244 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
29245 pExpr->u.zToken, zFlgs);
29246 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
29247 break;
29248 }
29249
29250 case TK_AGG_FUNCTION:
@@ -74843,11 +74869,17 @@
74869 testcase( bPreserve && pMem->z==0 );
74870
74871 assert( pMem->szMalloc==0
74872 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
74873 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
74874 if( pMem->db ){
74875 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
74876 }else{
74877 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
74878 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
74879 pMem->z = pMem->zMalloc;
74880 }
74881 bPreserve = 0;
74882 }else{
74883 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
74884 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
74885 }
@@ -84279,10 +84311,11 @@
84311 assert( (f & (MEM_Static|MEM_Dyn))==0 );
84312 }else{
84313 c = 's';
84314 }
84315 *(zCsr++) = c;
84316 *(zCsr++) = 'x';
84317 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
84318 zCsr += sqlite3Strlen30(zCsr);
84319 for(i=0; i<25 && i<pMem->n; i++){
84320 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
84321 zCsr += sqlite3Strlen30(zCsr);
@@ -85027,11 +85060,10 @@
85060 ** as the P1 parameter.
85061 */
85062 case OP_String8: { /* same as TK_STRING, out2 */
85063 assert( pOp->p4.z!=0 );
85064 pOut = out2Prerelease(p, pOp);
 
85065 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
85066
85067 #ifndef SQLITE_OMIT_UTF16
85068 if( encoding!=SQLITE_UTF8 ){
85069 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
@@ -85051,10 +85083,11 @@
85083 }
85084 #endif
85085 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
85086 goto too_big;
85087 }
85088 pOp->opcode = OP_String;
85089 assert( rc==SQLITE_OK );
85090 /* Fall through to the next case, OP_String */
85091 }
85092
85093 /* Opcode: String P1 P2 P3 P4 P5
@@ -100317,10 +100350,11 @@
100350 if( addrOnce ){
100351 sqlite3VdbeJumpHere(v, addrOnce);
100352 /* Subroutine return */
100353 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
100354 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
100355 sqlite3ClearTempRegCache(pParse);
100356 }
100357 }
100358 #endif /* SQLITE_OMIT_SUBQUERY */
100359
100360 /*
@@ -100427,10 +100461,11 @@
100461 sqlite3VdbeJumpHere(v, addrOnce);
100462
100463 /* Subroutine return */
100464 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
100465 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
100466 sqlite3ClearTempRegCache(pParse);
100467 }
100468
100469 return rReg;
100470 }
100471 #endif /* SQLITE_OMIT_SUBQUERY */
@@ -103058,10 +103093,15 @@
103093 }
103094 }
103095
103096 /*
103097 ** Mark all temporary registers as being unavailable for reuse.
103098 **
103099 ** Always invoke this procedure after coding a subroutine or co-routine
103100 ** that might be invoked from other parts of the code, to ensure that
103101 ** the sub/co-routine does not use registers in common with the code that
103102 ** invokes the sub/co-routine.
103103 */
103104 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
103105 pParse->nTempReg = 0;
103106 pParse->nRangeReg = 0;
103107 }
@@ -114049,10 +114089,12 @@
114089 int nNeedle;
114090 int typeHaystack, typeNeedle;
114091 int N = 1;
114092 int isText;
114093 unsigned char firstChar;
114094 sqlite3_value *pC1 = 0;
114095 sqlite3_value *pC2 = 0;
114096
114097 UNUSED_PARAMETER(argc);
114098 typeHaystack = sqlite3_value_type(argv[0]);
114099 typeNeedle = sqlite3_value_type(argv[1]);
114100 if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
@@ -114061,16 +114103,26 @@
114103 if( nNeedle>0 ){
114104 if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
114105 zHaystack = sqlite3_value_blob(argv[0]);
114106 zNeedle = sqlite3_value_blob(argv[1]);
114107 isText = 0;
114108 }else if( typeHaystack!=SQLITE_BLOB && typeNeedle!=SQLITE_BLOB ){
114109 zHaystack = sqlite3_value_text(argv[0]);
114110 zNeedle = sqlite3_value_text(argv[1]);
114111 isText = 1;
114112 }else{
114113 pC1 = sqlite3_value_dup(argv[0]);
114114 zHaystack = sqlite3_value_text(pC1);
114115 if( zHaystack==0 ) goto endInstrOOM;
114116 nHaystack = sqlite3_value_bytes(pC1);
114117 pC2 = sqlite3_value_dup(argv[1]);
114118 zNeedle = sqlite3_value_text(pC2);
114119 if( zNeedle==0 ) goto endInstrOOM;
114120 nNeedle = sqlite3_value_bytes(pC2);
114121 isText = 1;
114122 }
114123 if( zNeedle==0 || (nHaystack && zHaystack==0) ) goto endInstrOOM;
114124 firstChar = zNeedle[0];
114125 while( nNeedle<=nHaystack
114126 && (zHaystack[0]!=firstChar || memcmp(zHaystack, zNeedle, nNeedle)!=0)
114127 ){
114128 N++;
@@ -114080,10 +114132,17 @@
114132 }while( isText && (zHaystack[0]&0xc0)==0x80 );
114133 }
114134 if( nNeedle>nHaystack ) N = 0;
114135 }
114136 sqlite3_result_int(context, N);
114137 endInstr:
114138 sqlite3_value_free(pC1);
114139 sqlite3_value_free(pC2);
114140 return;
114141 endInstrOOM:
114142 sqlite3_result_error_nomem(context);
114143 goto endInstr;
114144 }
114145
114146 /*
114147 ** Implementation of the printf() function.
114148 */
@@ -128983,10 +129042,22 @@
129042 pNew->iRightJoinTable = pExpr->iRightJoinTable;
129043 ExprSetProperty(pNew, EP_FromJoin);
129044 }
129045 sqlite3ExprDelete(db, pExpr);
129046 pExpr = pNew;
129047
129048 /* Ensure that the expression now has an implicit collation sequence,
129049 ** just as it did when it was a column of a view or sub-query. */
129050 if( pExpr ){
129051 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
129052 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
129053 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
129054 (pColl ? pColl->zName : "BINARY")
129055 );
129056 }
129057 ExprClearProperty(pExpr, EP_Collate);
129058 }
129059 }
129060 }
129061 }else{
129062 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
129063 pExpr->iTable = pSubst->iNewTable;
@@ -130849,10 +130920,23 @@
130920 ExprList *pList = pF->pExpr->x.pList;
130921 assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
130922 assert( !IsWindowFunc(pF->pExpr) );
130923 if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
130924 Expr *pFilter = pF->pExpr->y.pWin->pFilter;
130925 if( pAggInfo->nAccumulator
130926 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
130927 ){
130928 if( regHit==0 ) regHit = ++pParse->nMem;
130929 /* If this is the first row of the group (regAcc==0), clear the
130930 ** "magnet" register regHit so that the accumulator registers
130931 ** are populated if the FILTER clause jumps over the the
130932 ** invocation of min() or max() altogether. Or, if this is not
130933 ** the first row (regAcc==1), set the magnet register so that the
130934 ** accumulators are not populated unless the min()/max() is invoked and
130935 ** indicates that they should be. */
130936 sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit);
130937 }
130938 addrNext = sqlite3VdbeMakeLabel(pParse);
130939 sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
130940 }
130941 if( pList ){
130942 nArg = pList->nExpr;
@@ -130899,10 +130983,11 @@
130983 addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
130984 }
130985 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
130986 sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
130987 }
130988
130989 pAggInfo->directMode = 0;
130990 if( addrHitTest ){
130991 sqlite3VdbeJumpHere(v, addrHitTest);
130992 }
130993 }
@@ -131702,27 +131787,39 @@
131787 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
131788 pItem->u.x.iAlias = 0;
131789 }
131790 assert( 66==sqlite3LogEst(100) );
131791 if( p->nSelectRow>66 ) p->nSelectRow = 66;
131792
131793 /* If there is both a GROUP BY and an ORDER BY clause and they are
131794 ** identical, then it may be possible to disable the ORDER BY clause
131795 ** on the grounds that the GROUP BY will cause elements to come out
131796 ** in the correct order. It also may not - the GROUP BY might use a
131797 ** database index that causes rows to be grouped together as required
131798 ** but not actually sorted. Either way, record the fact that the
131799 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
131800 ** variable. */
131801 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
131802 int ii;
131803 /* The GROUP BY processing doesn't care whether rows are delivered in
131804 ** ASC or DESC order - only that each group is returned contiguously.
131805 ** So set the ASC/DESC flags in the GROUP BY to match those in the
131806 ** ORDER BY to maximize the chances of rows being delivered in an
131807 ** order that makes the ORDER BY redundant. */
131808 for(ii=0; ii<pGroupBy->nExpr; ii++){
131809 u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
131810 pGroupBy->a[ii].sortFlags = sortFlags;
131811 }
131812 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
131813 orderByGrp = 1;
131814 }
131815 }
131816 }else{
131817 assert( 0==sqlite3LogEst(1) );
131818 p->nSelectRow = 0;
131819 }
131820
 
 
 
 
 
 
 
 
 
 
 
 
131821 /* Create a label to jump to when we want to abort the query */
131822 addrEnd = sqlite3VdbeMakeLabel(pParse);
131823
131824 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
131825 ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
@@ -132074,17 +132171,22 @@
132171 }else
132172 #endif /* SQLITE_OMIT_BTREECOUNT */
132173 {
132174 int regAcc = 0; /* "populate accumulators" flag */
132175
132176 /* If there are accumulator registers but no min() or max() functions
132177 ** without FILTER clauses, allocate register regAcc. Register regAcc
132178 ** will contain 0 the first time the inner loop runs, and 1 thereafter.
132179 ** The code generated by updateAccumulator() uses this to ensure
132180 ** that the accumulator registers are (a) updated only once if
132181 ** there are no min() or max functions or (b) always updated for the
132182 ** first row visited by the aggregate, so that they are updated at
132183 ** least once even if the FILTER clause means the min() or max()
132184 ** function visits zero rows. */
132185 if( sAggInfo.nAccumulator ){
132186 for(i=0; i<sAggInfo.nFunc; i++){
132187 if( ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_WinFunc) ) continue;
132188 if( sAggInfo.aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ) break;
132189 }
132190 if( i==sAggInfo.nFunc ){
132191 regAcc = ++pParse->nMem;
132192 sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc);
@@ -139778,22 +139880,27 @@
139880 ** LIKE optimization. See, for example:
139881 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
139882 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
139883 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
139884 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
139885 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
139886 */
139887 if( pLeft->op!=TK_COLUMN
139888 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
139889 || IsVirtual(pLeft->y.pTab) /* Value might be numeric */
139890 ){
139891 int isNum;
139892 double rDummy;
139893 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139894 if( isNum<=0 ){
139895 if( iTo==1 && zNew[0]=='-' ){
139896 isNum = +1;
139897 }else{
139898 zNew[iTo-1]++;
139899 isNum = sqlite3AtoF(zNew, &rDummy, iTo, SQLITE_UTF8);
139900 zNew[iTo-1]--;
139901 }
139902 }
139903 if( isNum>0 ){
139904 sqlite3ExprDelete(db, pPrefix);
139905 sqlite3ValueFree(pVal);
139906 return 0;
@@ -143384,11 +143491,11 @@
143491 WhereLoop *pLoop, /* The loop to adjust downward */
143492 LogEst nRow /* Number of rows in the entire table */
143493 ){
143494 WhereTerm *pTerm, *pX;
143495 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
143496 int i, j;
143497 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
143498
143499 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
143500 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
143501 assert( pTerm!=0 );
@@ -143410,10 +143517,11 @@
143517 /* In the absence of explicit truth probabilities, use heuristics to
143518 ** guess a reasonable truth probability. */
143519 pLoop->nOut--;
143520 if( pTerm->eOperator&(WO_EQ|WO_IS) ){
143521 Expr *pRight = pTerm->pExpr->pRight;
143522 int k = 0;
143523 testcase( pTerm->pExpr->op==TK_IS );
143524 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
143525 k = 10;
143526 }else{
143527 k = 20;
@@ -147493,12 +147601,19 @@
147601 /* Append the arguments passed to each window function to the
147602 ** sub-select expression list. Also allocate two registers for each
147603 ** window function - one for the accumulator, another for interim
147604 ** results. */
147605 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
147606 ExprList *pArgs = pWin->pOwner->x.pList;
147607 if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
147608 selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
147609 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
147610 pWin->bExprArgs = 1;
147611 }else{
147612 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
147613 pSublist = exprListAppendList(pParse, pSublist, pArgs, 0);
147614 }
147615 if( pWin->pFilter ){
147616 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
147617 pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
147618 }
147619 pWin->regAccum = ++pParse->nMem;
@@ -147960,11 +148075,11 @@
148075 Vdbe *v = sqlite3GetVdbe(pParse);
148076 Window *pWin;
148077 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
148078 FuncDef *pFunc = pWin->pFunc;
148079 int regArg;
148080 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
148081 int i;
148082
148083 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
148084
148085 for(i=0; i<nArg; i++){
@@ -148002,17 +148117,32 @@
148117 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
148118 }else if( pFunc->xSFunc!=noopStepFunc ){
148119 int addrIf = 0;
148120 if( pWin->pFilter ){
148121 int regTmp;
148122 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
148123 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
148124 regTmp = sqlite3GetTempReg(pParse);
148125 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
148126 addrIf = sqlite3VdbeAddOp3(v, OP_IfNot, regTmp, 0, 1);
148127 VdbeCoverage(v);
148128 sqlite3ReleaseTempReg(pParse, regTmp);
148129 }
148130 if( pWin->bExprArgs ){
148131 int iStart = sqlite3VdbeCurrentAddr(v);
148132 VdbeOp *pOp, *pEnd;
148133
148134 nArg = pWin->pOwner->x.pList->nExpr;
148135 regArg = sqlite3GetTempRange(pParse, nArg);
148136 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
148137
148138 pEnd = sqlite3VdbeGetOp(v, -1);
148139 for(pOp=sqlite3VdbeGetOp(v, iStart); pOp<=pEnd; pOp++){
148140 if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
148141 pOp->p1 = csr;
148142 }
148143 }
148144 }
148145 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
148146 CollSeq *pColl;
148147 assert( nArg>0 );
148148 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
@@ -148020,10 +148150,13 @@
148150 }
148151 sqlite3VdbeAddOp3(v, bInverse? OP_AggInverse : OP_AggStep,
148152 bInverse, regArg, pWin->regAccum);
148153 sqlite3VdbeAppendP4(v, pFunc, P4_FUNCDEF);
148154 sqlite3VdbeChangeP5(v, (u8)nArg);
148155 if( pWin->bExprArgs ){
148156 sqlite3ReleaseTempRange(pParse, regArg, nArg);
148157 }
148158 if( addrIf ) sqlite3VdbeJumpHere(v, addrIf);
148159 }
148160 }
148161 }
148162
@@ -148718,10 +148851,11 @@
148851 Window *pNew = 0;
148852 if( ALWAYS(p) ){
148853 pNew = sqlite3DbMallocZero(db, sizeof(Window));
148854 if( pNew ){
148855 pNew->zName = sqlite3DbStrDup(db, p->zName);
148856 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
148857 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
148858 pNew->pFunc = p->pFunc;
148859 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
148860 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
148861 pNew->eFrmType = p->eFrmType;
@@ -148730,10 +148864,11 @@
148864 pNew->eExclude = p->eExclude;
148865 pNew->regResult = p->regResult;
148866 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
148867 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
148868 pNew->pOwner = pOwner;
148869 pNew->bImplicitFrame = p->bImplicitFrame;
148870 }
148871 }
148872 return pNew;
148873 }
148874
@@ -149273,11 +149408,11 @@
149408 if( regEnd ){
149409 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
149410 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE ? 3 : 0));
149411 }
149412
149413 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
149414 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
149415 int addrGe = sqlite3VdbeAddOp3(v, op, regStart, 0, regEnd);
149416 VdbeCoverageNeverNullIf(v, op==OP_Ge); /* NeverNull because bound <expr> */
149417 VdbeCoverageNeverNullIf(v, op==OP_Le); /* values previously checked */
149418 windowAggFinal(&s, 0);
@@ -157734,11 +157869,11 @@
157869 return SQLITE_MISUSE_BKPT;
157870 }
157871
157872 assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
157873 assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
157874 extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|SQLITE_SUBTYPE);
157875 enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
157876
157877 #ifndef SQLITE_OMIT_UTF16
157878 /* If SQLITE_UTF16 is specified as the encoding type, transform this
157879 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
@@ -164202,11 +164337,12 @@
164337 iWrite = iVal - *piPrev;
164338 }else{
164339 iWrite = *piPrev - iVal;
164340 }
164341 assert( *pbFirst || *piPrev==0 );
164342 assert_fts3_nc( *pbFirst==0 || iWrite>0 );
164343 assert( *pbFirst==0 || iWrite>=0 );
164344 *pp += sqlite3Fts3PutVarint(*pp, iWrite);
164345 *piPrev = iVal;
164346 *pbFirst = 1;
164347 }
164348
@@ -164308,10 +164444,12 @@
164444 }else{
164445 fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
164446 fts3PoslistCopy(&p, &p2);
164447 fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
164448 }
164449
164450 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
164451 }
164452
164453 if( rc!=SQLITE_OK ){
164454 sqlite3_free(aOut);
164455 p = aOut = 0;
@@ -181914,11 +182052,11 @@
182052 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
182053 if( pStr ){
182054 if( pStr->zBuf==0 ){
182055 jsonInit(pStr, ctx);
182056 jsonAppendChar(pStr, '[');
182057 }else if( pStr->nUsed>1 ){
182058 jsonAppendChar(pStr, ',');
182059 pStr->pCtx = ctx;
182060 }
182061 jsonAppendValue(pStr, argv[0]);
182062 }
@@ -181962,13 +182100,15 @@
182100 static void jsonGroupInverse(
182101 sqlite3_context *ctx,
182102 int argc,
182103 sqlite3_value **argv
182104 ){
182105 unsigned int i;
182106 int inStr = 0;
182107 int nNest = 0;
182108 char *z;
182109 char c;
182110 JsonString *pStr;
182111 UNUSED_PARAM(argc);
182112 UNUSED_PARAM(argv);
182113 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
182114 #ifdef NEVER
@@ -181975,16 +182115,22 @@
182115 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
182116 ** always have been called to initalize it */
182117 if( NEVER(!pStr) ) return;
182118 #endif
182119 z = pStr->zBuf;
182120 for(i=1; (c = z[i])!=',' || inStr || nNest; i++){
182121 if( i>=pStr->nUsed ){
182122 pStr->nUsed = 1;
182123 return;
182124 }
182125 if( c=='"' ){
182126 inStr = !inStr;
182127 }else if( c=='\\' ){
182128 i++;
182129 }else if( !inStr ){
182130 if( c=='{' || c=='[' ) nNest++;
182131 if( c=='}' || c==']' ) nNest--;
182132 }
182133 }
182134 pStr->nUsed -= i;
182135 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
182136 }
@@ -182010,11 +182156,11 @@
182156 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
182157 if( pStr ){
182158 if( pStr->zBuf==0 ){
182159 jsonInit(pStr, ctx);
182160 jsonAppendChar(pStr, '{');
182161 }else if( pStr->nUsed>1 ){
182162 jsonAppendChar(pStr, ',');
182163 pStr->pCtx = ctx;
182164 }
182165 z = (const char*)sqlite3_value_text(argv[0]);
182166 n = (u32)sqlite3_value_bytes(argv[0]);
@@ -182598,18 +182744,18 @@
182744 { "json_tree", &jsonTreeModule },
182745 };
182746 #endif
182747 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
182748 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
182749 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
182750 (void*)&aFunc[i].flag,
182751 aFunc[i].xFunc, 0, 0);
182752 }
182753 #ifndef SQLITE_OMIT_WINDOWFUNC
182754 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
182755 rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
182756 SQLITE_SUBTYPE | SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
182757 aAgg[i].xStep, aAgg[i].xFinal,
182758 aAgg[i].xValue, jsonGroupInverse, 0);
182759 }
182760 #endif
182761 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -203607,10 +203753,11 @@
203753 static int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
203754 static int sqlite3Fts5ExprEof(Fts5Expr*);
203755 static i64 sqlite3Fts5ExprRowid(Fts5Expr*);
203756
203757 static void sqlite3Fts5ExprFree(Fts5Expr*);
203758 static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
203759
203760 /* Called during startup to register a UDF with SQLite */
203761 static int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
203762
203763 static int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
@@ -207014,11 +207161,11 @@
207161 assert( zSql || rc==SQLITE_NOMEM );
207162 if( zSql ){
207163 rc = sqlite3_declare_vtab(pConfig->db, zSql);
207164 sqlite3_free(zSql);
207165 }
207166
207167 return rc;
207168 }
207169
207170 /*
207171 ** Tokenize the text passed via the second and third arguments.
@@ -207601,10 +207748,46 @@
207748 sqlite3Fts5ParseNodeFree(p->pRoot);
207749 sqlite3_free(p->apExprPhrase);
207750 sqlite3_free(p);
207751 }
207752 }
207753
207754 static int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
207755 Fts5Parse sParse;
207756 memset(&sParse, 0, sizeof(sParse));
207757
207758 if( *pp1 ){
207759 Fts5Expr *p1 = *pp1;
207760 int nPhrase = p1->nPhrase + p2->nPhrase;
207761
207762 p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
207763 p2->pRoot = 0;
207764
207765 if( sParse.rc==SQLITE_OK ){
207766 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
207767 p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
207768 );
207769 if( ap==0 ){
207770 sParse.rc = SQLITE_NOMEM;
207771 }else{
207772 int i;
207773 memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
207774 for(i=0; i<p2->nPhrase; i++){
207775 ap[i] = p2->apExprPhrase[i];
207776 }
207777 p1->nPhrase = nPhrase;
207778 p1->apExprPhrase = ap;
207779 }
207780 }
207781 sqlite3_free(p2->apExprPhrase);
207782 sqlite3_free(p2);
207783 }else{
207784 *pp1 = p2;
207785 }
207786
207787 return sParse.rc;
207788 }
207789
207790 /*
207791 ** Argument pTerm must be a synonym iterator. Return the current rowid
207792 ** that it points to.
207793 */
@@ -211421,11 +211604,11 @@
211604 }
211605
211606 static Fts5Data *fts5LeafRead(Fts5Index *p, i64 iRowid){
211607 Fts5Data *pRet = fts5DataRead(p, iRowid);
211608 if( pRet ){
211609 if( pRet->nn<4 || pRet->szLeaf>pRet->nn ){
211610 p->rc = FTS5_CORRUPT;
211611 fts5DataRelease(pRet);
211612 pRet = 0;
211613 }
211614 }
@@ -217765,20 +217948,42 @@
217948
217949 /*
217950 ** Implementation of the xBestIndex method for FTS5 tables. Within the
217951 ** WHERE constraint, it searches for the following:
217952 **
217953 ** 1. A MATCH constraint against the table column.
217954 ** 2. A MATCH constraint against the "rank" column.
217955 ** 3. A MATCH constraint against some other column.
217956 ** 4. An == constraint against the rowid column.
217957 ** 5. A < or <= constraint against the rowid column.
217958 ** 6. A > or >= constraint against the rowid column.
217959 **
217960 ** Within the ORDER BY, the following are supported:
217961 **
217962 ** 5. ORDER BY rank [ASC|DESC]
217963 ** 6. ORDER BY rowid [ASC|DESC]
217964 **
217965 ** Information for the xFilter call is passed via both the idxNum and
217966 ** idxStr variables. Specifically, idxNum is a bitmask of the following
217967 ** flags used to encode the ORDER BY clause:
217968 **
217969 ** FTS5_BI_ORDER_RANK
217970 ** FTS5_BI_ORDER_ROWID
217971 ** FTS5_BI_ORDER_DESC
217972 **
217973 ** idxStr is used to encode data from the WHERE clause. For each argument
217974 ** passed to the xFilter method, the following is appended to idxStr:
217975 **
217976 ** Match against table column: "m"
217977 ** Match against rank column: "r"
217978 ** Match against other column: "<column-number>"
217979 ** Equality constraint against the rowid: "="
217980 ** A < or <= against the rowid: "<"
217981 ** A > or >= against the rowid: ">"
217982 **
217983 ** This function ensures that there is at most one "r" or "=". And that if
217984 ** there exists an "=" then there is no "<" or ">".
217985 **
217986 ** Costs are assigned as follows:
217987 **
217988 ** a) If an unusable MATCH operator is present in the WHERE clause, the
217989 ** cost is unconditionally set to 1e50 (a really big number).
@@ -217803,36 +218008,22 @@
218008 static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
218009 Fts5Table *pTab = (Fts5Table*)pVTab;
218010 Fts5Config *pConfig = pTab->pConfig;
218011 const int nCol = pConfig->nCol;
218012 int idxFlags = 0; /* Parameter passed through to xFilter() */
 
 
218013 int i;
218014
218015 char *idxStr;
218016 int iIdxStr = 0;
218017 int iCons = 0;
218018
218019 int bSeenEq = 0;
218020 int bSeenGt = 0;
218021 int bSeenLt = 0;
218022 int bSeenMatch = 0;
218023 int bSeenRank = 0;
218024
 
 
 
 
 
 
 
 
 
 
 
 
218025
218026 assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
218027 assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
218028 assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
218029 assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
@@ -217843,44 +218034,82 @@
218034 "recursively defined fts5 content table"
218035 );
218036 return SQLITE_ERROR;
218037 }
218038
218039 idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 6 + 1);
218040 if( idxStr==0 ) return SQLITE_NOMEM;
218041 pInfo->idxStr = idxStr;
218042 pInfo->needToFreeIdxStr = 1;
218043
218044 for(i=0; i<pInfo->nConstraint; i++){
218045 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
218046 int iCol = p->iColumn;
218047 if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
218048 || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
 
218049 ){
218050 /* A MATCH operator or equivalent */
218051 if( p->usable==0 || iCol<0 ){
 
 
 
218052 /* As there exists an unusable MATCH constraint this is an
218053 ** unusable plan. Set a prohibitively high cost. */
218054 pInfo->estimatedCost = 1e50;
218055 assert( iIdxStr < pInfo->nConstraint*6 + 1 );
218056 idxStr[iIdxStr] = 0;
218057 return SQLITE_OK;
218058 }else{
218059 if( iCol==nCol+1 ){
218060 if( bSeenRank ) continue;
218061 idxStr[iIdxStr++] = 'r';
218062 bSeenRank = 1;
218063 }else{
218064 bSeenMatch = 1;
218065 idxStr[iIdxStr++] = 'm';
218066 if( iCol<nCol ){
218067 sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
218068 idxStr += strlen(&idxStr[iIdxStr]);
218069 assert( idxStr[iIdxStr]=='\0' );
218070 }
218071 }
218072 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218073 pInfo->aConstraintUsage[i].omit = 1;
218074 }
218075 }
218076 else if( p->usable && bSeenEq==0
218077 && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0
218078 ){
218079 idxStr[iIdxStr++] = '=';
218080 bSeenEq = 1;
218081 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218082 }
218083 }
218084
218085 if( bSeenEq==0 ){
218086 for(i=0; i<pInfo->nConstraint; i++){
218087 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
218088 if( p->iColumn<0 && p->usable ){
218089 int op = p->op;
218090 if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
218091 if( bSeenLt ) continue;
218092 idxStr[iIdxStr++] = '<';
218093 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218094 bSeenLt = 1;
218095 }else
218096 if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
218097 if( bSeenGt ) continue;
218098 idxStr[iIdxStr++] = '>';
218099 pInfo->aConstraintUsage[i].argvIndex = ++iCons;
218100 bSeenGt = 1;
218101 }
218102 }
218103 }
218104 }
218105 idxStr[iIdxStr] = '\0';
218106
218107 /* Set idxFlags flags for the ORDER BY clause */
218108 if( pInfo->nOrderBy==1 ){
218109 int iSort = pInfo->aOrderBy[0].iColumn;
218110 if( iSort==(pConfig->nCol+1) && bSeenMatch ){
218111 idxFlags |= FTS5_BI_ORDER_RANK;
218112 }else if( iSort==-1 ){
218113 idxFlags |= FTS5_BI_ORDER_ROWID;
218114 }
218115 if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
@@ -217890,30 +218119,19 @@
218119 }
218120 }
218121 }
218122
218123 /* Calculate the estimated cost based on the flags set in idxFlags. */
218124 if( bSeenEq ){
218125 pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
218126 if( bSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
218127 }else if( bSeenLt && bSeenGt ){
218128 pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
218129 }else if( bSeenLt || bSeenGt ){
218130 pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
 
218131 }else{
218132 pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
 
 
 
 
 
 
 
 
 
 
218133 }
218134
218135 pInfo->idxNum = idxFlags;
218136 return SQLITE_OK;
218137 }
@@ -218432,31 +218650,29 @@
218650 ** 3. A full-table scan.
218651 */
218652 static int fts5FilterMethod(
218653 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
218654 int idxNum, /* Strategy index */
218655 const char *idxStr, /* Unused */
218656 int nVal, /* Number of elements in apVal */
218657 sqlite3_value **apVal /* Arguments for the indexing scheme */
218658 ){
218659 Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
218660 Fts5Config *pConfig = pTab->p.pConfig;
218661 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
218662 int rc = SQLITE_OK; /* Error code */
 
218663 int bDesc; /* True if ORDER BY [rank|rowid] DESC */
218664 int bOrderByRank; /* True if ORDER BY rank */
 
218665 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
218666 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
218667 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
218668 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
218669 int iCol; /* Column on LHS of MATCH operator */
218670 char **pzErrmsg = pConfig->pzErrmsg;
218671 int i;
218672 int iIdxStr = 0;
218673 Fts5Expr *pExpr = 0;
218674
218675 if( pCsr->ePlan ){
218676 fts5FreeCursorComponents(pCsr);
218677 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
218678 }
@@ -218465,27 +218681,64 @@
218681 assert( pCsr->pExpr==0 );
218682 assert( pCsr->csrflags==0 );
218683 assert( pCsr->pRank==0 );
218684 assert( pCsr->zRank==0 );
218685 assert( pCsr->zRankArgs==0 );
218686 assert( pTab->pSortCsr==0 || nVal==0 );
218687
218688 assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
218689 pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
218690
218691 /* Decode the arguments passed through to this function. */
218692 for(i=0; i<nVal; i++){
218693 switch( idxStr[iIdxStr++] ){
218694 case 'r':
218695 pRank = apVal[i];
218696 break;
218697 case 'm': {
218698 const char *zText = (const char*)sqlite3_value_text(apVal[i]);
218699 if( zText==0 ) zText = "";
218700
218701 if( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ){
218702 iCol = 0;
218703 do{
218704 iCol = iCol*10 + (idxStr[iIdxStr]-'0');
218705 iIdxStr++;
218706 }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
218707 }else{
218708 iCol = pConfig->nCol;
218709 }
218710
218711 if( zText[0]=='*' ){
218712 /* The user has issued a query of the form "MATCH '*...'". This
218713 ** indicates that the MATCH expression is not a full text query,
218714 ** but a request for an internal parameter. */
218715 rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
218716 goto filter_out;
218717 }else{
218718 char **pzErr = &pTab->p.base.zErrMsg;
218719 rc = sqlite3Fts5ExprNew(pConfig, iCol, zText, &pExpr, pzErr);
218720 if( rc==SQLITE_OK ){
218721 rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
218722 pExpr = 0;
218723 }
218724 if( rc!=SQLITE_OK ) goto filter_out;
218725 }
218726
218727 break;
218728 }
218729 case '=':
218730 pRowidEq = apVal[i];
218731 break;
218732 case '<':
218733 pRowidLe = apVal[i];
218734 break;
218735 default: assert( idxStr[iIdxStr-1]=='>' );
218736 pRowidGe = apVal[i];
218737 break;
218738 }
218739 }
218740 bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
218741 pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
218742
218743 /* Set the cursor upper and lower rowid limits. Only some strategies
218744 ** actually use them. This is ok, as the xBestIndex() method leaves the
@@ -218508,11 +218761,11 @@
218761 ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
218762 ** return results to the user for this query. The current cursor
218763 ** (pCursor) is used to execute the query issued by function
218764 ** fts5CursorFirstSorted() above. */
218765 assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
218766 assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
218767 assert( pCsr->iLastRowid==LARGEST_INT64 );
218768 assert( pCsr->iFirstRowid==SMALLEST_INT64 );
218769 if( pTab->pSortCsr->bDesc ){
218770 pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
218771 pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
@@ -218521,33 +218774,19 @@
218774 pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
218775 }
218776 pCsr->ePlan = FTS5_PLAN_SOURCE;
218777 pCsr->pExpr = pTab->pSortCsr->pExpr;
218778 rc = fts5CursorFirst(pTab, pCsr, bDesc);
218779 }else if( pCsr->pExpr ){
 
 
 
218780 rc = fts5CursorParseRank(pConfig, pCsr, pRank);
218781 if( rc==SQLITE_OK ){
218782 if( bOrderByRank ){
218783 pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
218784 rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
218785 }else{
218786 pCsr->ePlan = FTS5_PLAN_MATCH;
218787 rc = fts5CursorFirst(pTab, pCsr, bDesc);
 
 
 
 
 
 
 
 
 
 
 
218788 }
218789 }
218790 }else if( pConfig->zContent==0 ){
218791 *pConfig->pzErrmsg = sqlite3_mprintf(
218792 "%s: table does not support scanning", pConfig->zName
@@ -218560,19 +218799,21 @@
218799 rc = sqlite3Fts5StorageStmt(
218800 pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
218801 );
218802 if( rc==SQLITE_OK ){
218803 if( pCsr->ePlan==FTS5_PLAN_ROWID ){
218804 sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
218805 }else{
218806 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
218807 sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
218808 }
218809 rc = fts5NextMethod(pCursor);
218810 }
218811 }
218812
218813 filter_out:
218814 sqlite3Fts5ExprFree(pExpr);
218815 pConfig->pzErrmsg = pzErrmsg;
218816 return rc;
218817 }
218818
218819 /*
@@ -219955,11 +220196,11 @@
220196 int nArg, /* Number of args */
220197 sqlite3_value **apUnused /* Function arguments */
220198 ){
220199 assert( nArg==0 );
220200 UNUSED_PARAM2(nArg, apUnused);
220201 sqlite3_result_text(pCtx, "fts5: 2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f044cbd", -1, SQLITE_TRANSIENT);
220202 }
220203
220204 /*
220205 ** Return true if zName is the extension on one of the shadow tables used
220206 ** by this module.
@@ -224723,12 +224964,12 @@
224964 }
224965 #endif /* SQLITE_CORE */
224966 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
224967
224968 /************** End of stmt.c ************************************************/
224969 #if __LINE__!=224969
224970 #undef SQLITE_SOURCE_ID
224971 #define SQLITE_SOURCE_ID "2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f04alt2"
224972 #endif
224973 /* Return the source-id for this library */
224974 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
224975 /************************** End of sqlite3.c ******************************/
224976
+20 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123123
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124124
** [sqlite_version()] and [sqlite_source_id()].
125125
*/
126126
#define SQLITE_VERSION "3.30.0"
127127
#define SQLITE_VERSION_NUMBER 3030000
128
-#define SQLITE_SOURCE_ID "2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2d897d"
128
+#define SQLITE_SOURCE_ID "2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f044cbd"
129129
130130
/*
131131
** CAPI3REF: Run-Time Library Version Numbers
132132
** KEYWORDS: sqlite3_version sqlite3_sourceid
133133
**
@@ -4858,13 +4858,16 @@
48584858
** the same inputs within a single SQL statement. Most SQL functions are
48594859
** deterministic. The built-in [random()] SQL function is an example of a
48604860
** function that is not deterministic. The SQLite query planner is able to
48614861
** perform additional optimizations on deterministic functions, so use
48624862
** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
4863
+**
48634864
** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
48644865
** flag, which if present prevents the function from being invoked from
4865
-** within VIEWs or TRIGGERs.
4866
+** within VIEWs or TRIGGERs. For security reasons, the [SQLITE_DIRECTONLY]
4867
+** flag is recommended for any application-defined SQL function that has
4868
+** side-effects.
48664869
**
48674870
** ^(The fifth parameter is an arbitrary pointer. The implementation of the
48684871
** function can gain access to this pointer using [sqlite3_user_data()].)^
48694872
**
48704873
** ^The sixth, seventh and eighth parameters passed to the three
@@ -4984,14 +4987,28 @@
49844987
** The SQLITE_DETERMINISTIC flag means that the new function will always
49854988
** maps the same inputs into the same output. The abs() function is
49864989
** deterministic, for example, but randomblob() is not.
49874990
**
49884991
** The SQLITE_DIRECTONLY flag means that the function may only be invoked
4989
-** from top-level SQL, and cannot be used in VIEWs or TRIGGERs.
4992
+** from top-level SQL, and cannot be used in VIEWs or TRIGGERs. This is
4993
+** a security feature which is recommended for all
4994
+** [application-defined SQL functions] that have side-effects. This flag
4995
+** prevents an attacker from adding triggers and views to a schema then
4996
+** tricking a high-privilege application into causing unintended side-effects
4997
+** while performing ordinary queries.
4998
+**
4999
+** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5000
+** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5001
+** Specifying this flag makes no difference for scalar or aggregate user
5002
+** functions. However, if it is not specified for a user-defined window
5003
+** function, then any sub-types belonging to arguments passed to the window
5004
+** function may be discarded before the window function is called (i.e.
5005
+** sqlite3_value_subtype() will always return 0).
49905006
*/
49915007
#define SQLITE_DETERMINISTIC 0x000000800
49925008
#define SQLITE_DIRECTONLY 0x000080000
5009
+#define SQLITE_SUBTYPE 0x000100000
49935010
49945011
/*
49955012
** CAPI3REF: Deprecated Functions
49965013
** DEPRECATED
49975014
**
49985015
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.30.0"
127 #define SQLITE_VERSION_NUMBER 3030000
128 #define SQLITE_SOURCE_ID "2019-09-03 16:23:41 3044cf6917ea8324175fc91657e9a5978af9748f72e1914bc361753f0b2d897d"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -4858,13 +4858,16 @@
4858 ** the same inputs within a single SQL statement. Most SQL functions are
4859 ** deterministic. The built-in [random()] SQL function is an example of a
4860 ** function that is not deterministic. The SQLite query planner is able to
4861 ** perform additional optimizations on deterministic functions, so use
4862 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
 
4863 ** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
4864 ** flag, which if present prevents the function from being invoked from
4865 ** within VIEWs or TRIGGERs.
 
 
4866 **
4867 ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
4868 ** function can gain access to this pointer using [sqlite3_user_data()].)^
4869 **
4870 ** ^The sixth, seventh and eighth parameters passed to the three
@@ -4984,14 +4987,28 @@
4984 ** The SQLITE_DETERMINISTIC flag means that the new function will always
4985 ** maps the same inputs into the same output. The abs() function is
4986 ** deterministic, for example, but randomblob() is not.
4987 **
4988 ** The SQLITE_DIRECTONLY flag means that the function may only be invoked
4989 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs.
 
 
 
 
 
 
 
 
 
 
 
 
 
4990 */
4991 #define SQLITE_DETERMINISTIC 0x000000800
4992 #define SQLITE_DIRECTONLY 0x000080000
 
4993
4994 /*
4995 ** CAPI3REF: Deprecated Functions
4996 ** DEPRECATED
4997 **
4998
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -123,11 +123,11 @@
123 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124 ** [sqlite_version()] and [sqlite_source_id()].
125 */
126 #define SQLITE_VERSION "3.30.0"
127 #define SQLITE_VERSION_NUMBER 3030000
128 #define SQLITE_SOURCE_ID "2019-09-21 17:31:03 8ea1dc727d391b15d0c4fa858ff68d5b8a63dde46408f24027dac8d28f044cbd"
129
130 /*
131 ** CAPI3REF: Run-Time Library Version Numbers
132 ** KEYWORDS: sqlite3_version sqlite3_sourceid
133 **
@@ -4858,13 +4858,16 @@
4858 ** the same inputs within a single SQL statement. Most SQL functions are
4859 ** deterministic. The built-in [random()] SQL function is an example of a
4860 ** function that is not deterministic. The SQLite query planner is able to
4861 ** perform additional optimizations on deterministic functions, so use
4862 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
4863 **
4864 ** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
4865 ** flag, which if present prevents the function from being invoked from
4866 ** within VIEWs or TRIGGERs. For security reasons, the [SQLITE_DIRECTONLY]
4867 ** flag is recommended for any application-defined SQL function that has
4868 ** side-effects.
4869 **
4870 ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
4871 ** function can gain access to this pointer using [sqlite3_user_data()].)^
4872 **
4873 ** ^The sixth, seventh and eighth parameters passed to the three
@@ -4984,14 +4987,28 @@
4987 ** The SQLITE_DETERMINISTIC flag means that the new function will always
4988 ** maps the same inputs into the same output. The abs() function is
4989 ** deterministic, for example, but randomblob() is not.
4990 **
4991 ** The SQLITE_DIRECTONLY flag means that the function may only be invoked
4992 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs. This is
4993 ** a security feature which is recommended for all
4994 ** [application-defined SQL functions] that have side-effects. This flag
4995 ** prevents an attacker from adding triggers and views to a schema then
4996 ** tricking a high-privilege application into causing unintended side-effects
4997 ** while performing ordinary queries.
4998 **
4999 ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5000 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5001 ** Specifying this flag makes no difference for scalar or aggregate user
5002 ** functions. However, if it is not specified for a user-defined window
5003 ** function, then any sub-types belonging to arguments passed to the window
5004 ** function may be discarded before the window function is called (i.e.
5005 ** sqlite3_value_subtype() will always return 0).
5006 */
5007 #define SQLITE_DETERMINISTIC 0x000000800
5008 #define SQLITE_DIRECTONLY 0x000080000
5009 #define SQLITE_SUBTYPE 0x000100000
5010
5011 /*
5012 ** CAPI3REF: Deprecated Functions
5013 ** DEPRECATED
5014 **
5015

Keyboard Shortcuts

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