| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.28.0. By combining all the individual C code files into this |
| 3 | +** version 3.29.0. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -886,10 +886,15 @@ |
| 886 | 886 | #pragma warning(disable : 4306) |
| 887 | 887 | #pragma warning(disable : 4702) |
| 888 | 888 | #pragma warning(disable : 4706) |
| 889 | 889 | #endif /* defined(_MSC_VER) */ |
| 890 | 890 | |
| 891 | +#if defined(_MSC_VER) && !defined(_WIN64) |
| 892 | +#undef SQLITE_4_BYTE_ALIGNED_MALLOC |
| 893 | +#define SQLITE_4_BYTE_ALIGNED_MALLOC |
| 894 | +#endif /* defined(_MSC_VER) && !defined(_WIN64) */ |
| 895 | + |
| 891 | 896 | #endif /* SQLITE_MSVC_H */ |
| 892 | 897 | |
| 893 | 898 | /************** End of msvc.h ************************************************/ |
| 894 | 899 | /************** Continuing where we left off in sqliteInt.h ******************/ |
| 895 | 900 | |
| | @@ -1160,13 +1165,13 @@ |
| 1160 | 1165 | ** |
| 1161 | 1166 | ** See also: [sqlite3_libversion()], |
| 1162 | 1167 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 1163 | 1168 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 1164 | 1169 | */ |
| 1165 | | -#define SQLITE_VERSION "3.28.0" |
| 1166 | | -#define SQLITE_VERSION_NUMBER 3028000 |
| 1167 | | -#define SQLITE_SOURCE_ID "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50" |
| 1170 | +#define SQLITE_VERSION "3.29.0" |
| 1171 | +#define SQLITE_VERSION_NUMBER 3029000 |
| 1172 | +#define SQLITE_SOURCE_ID "2019-05-10 17:54:58 956ca2a452aa3707bca553007a7ef221af3d4f6b0af747d17070926e000f2362" |
| 1168 | 1173 | |
| 1169 | 1174 | /* |
| 1170 | 1175 | ** CAPI3REF: Run-Time Library Version Numbers |
| 1171 | 1176 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 1172 | 1177 | ** |
| | @@ -8356,11 +8361,12 @@ |
| 8356 | 8361 | #define SQLITE_TESTCTRL_BYTEORDER 22 |
| 8357 | 8362 | #define SQLITE_TESTCTRL_ISINIT 23 |
| 8358 | 8363 | #define SQLITE_TESTCTRL_SORTER_MMAP 24 |
| 8359 | 8364 | #define SQLITE_TESTCTRL_IMPOSTER 25 |
| 8360 | 8365 | #define SQLITE_TESTCTRL_PARSER_COVERAGE 26 |
| 8361 | | -#define SQLITE_TESTCTRL_LAST 26 /* Largest TESTCTRL */ |
| 8366 | +#define SQLITE_TESTCTRL_RESULT_INTREAL 27 |
| 8367 | +#define SQLITE_TESTCTRL_LAST 27 /* Largest TESTCTRL */ |
| 8362 | 8368 | |
| 8363 | 8369 | /* |
| 8364 | 8370 | ** CAPI3REF: SQL Keyword Checking |
| 8365 | 8371 | ** |
| 8366 | 8372 | ** These routines provide access to the set of SQL language keywords |
| | @@ -17451,10 +17457,12 @@ |
| 17451 | 17457 | #define EP_Leaf 0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */ |
| 17452 | 17458 | #define EP_WinFunc 0x1000000 /* TK_FUNCTION with Expr.y.pWin set */ |
| 17453 | 17459 | #define EP_Subrtn 0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */ |
| 17454 | 17460 | #define EP_Quoted 0x4000000 /* TK_ID was originally quoted */ |
| 17455 | 17461 | #define EP_Static 0x8000000 /* Held in memory not obtained from malloc() */ |
| 17462 | +#define EP_IsTrue 0x10000000 /* Always has boolean value of TRUE */ |
| 17463 | +#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */ |
| 17456 | 17464 | |
| 17457 | 17465 | /* |
| 17458 | 17466 | ** The EP_Propagate mask is a set of properties that automatically propagate |
| 17459 | 17467 | ** upwards into parent nodes. |
| 17460 | 17468 | */ |
| | @@ -17466,10 +17474,12 @@ |
| 17466 | 17474 | */ |
| 17467 | 17475 | #define ExprHasProperty(E,P) (((E)->flags&(P))!=0) |
| 17468 | 17476 | #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P)) |
| 17469 | 17477 | #define ExprSetProperty(E,P) (E)->flags|=(P) |
| 17470 | 17478 | #define ExprClearProperty(E,P) (E)->flags&=~(P) |
| 17479 | +#define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue) |
| 17480 | +#define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse) |
| 17471 | 17481 | |
| 17472 | 17482 | /* The ExprSetVVAProperty() macro is used for Verification, Validation, |
| 17473 | 17483 | ** and Accreditation only. It works like ExprSetProperty() during VVA |
| 17474 | 17484 | ** processes but is a no-op for delivery. |
| 17475 | 17485 | */ |
| | @@ -18775,11 +18785,12 @@ |
| 18775 | 18785 | SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int); |
| 18776 | 18786 | SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*); |
| 18777 | 18787 | SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*); |
| 18778 | 18788 | SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*); |
| 18779 | 18789 | SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*); |
| 18780 | | -SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*); |
| 18790 | +SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*); |
| 18791 | +SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr*); |
| 18781 | 18792 | SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int); |
| 18782 | 18793 | SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32); |
| 18783 | 18794 | SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*); |
| 18784 | 18795 | SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*); |
| 18785 | 18796 | SQLITE_PRIVATE ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*); |
| | @@ -19188,10 +19199,13 @@ |
| 19188 | 19199 | SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8); |
| 19189 | 19200 | SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8, |
| 19190 | 19201 | void(*)(void*)); |
| 19191 | 19202 | SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*); |
| 19192 | 19203 | SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*); |
| 19204 | +#ifndef SQLITE_UNTESTABLE |
| 19205 | +SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context*); |
| 19206 | +#endif |
| 19193 | 19207 | SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *); |
| 19194 | 19208 | #ifndef SQLITE_OMIT_UTF16 |
| 19195 | 19209 | SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8); |
| 19196 | 19210 | #endif |
| 19197 | 19211 | SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **); |
| | @@ -20178,16 +20192,16 @@ |
| 20178 | 20192 | #define MEM_Null 0x0001 /* Value is NULL (or a pointer) */ |
| 20179 | 20193 | #define MEM_Str 0x0002 /* Value is a string */ |
| 20180 | 20194 | #define MEM_Int 0x0004 /* Value is an integer */ |
| 20181 | 20195 | #define MEM_Real 0x0008 /* Value is a real number */ |
| 20182 | 20196 | #define MEM_Blob 0x0010 /* Value is a BLOB */ |
| 20183 | | -#define MEM_AffMask 0x001f /* Mask of affinity bits */ |
| 20184 | | -#define MEM_FromBind 0x0020 /* Value originates from sqlite3_bind() */ |
| 20185 | | -/* Available 0x0040 */ |
| 20197 | +#define MEM_IntReal 0x0020 /* MEM_Int that stringifies like MEM_Real */ |
| 20198 | +#define MEM_AffMask 0x003f /* Mask of affinity bits */ |
| 20199 | +#define MEM_FromBind 0x0040 /* Value originates from sqlite3_bind() */ |
| 20186 | 20200 | #define MEM_Undefined 0x0080 /* Value is undefined */ |
| 20187 | 20201 | #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */ |
| 20188 | | -#define MEM_TypeMask 0xc1df /* Mask of type bits */ |
| 20202 | +#define MEM_TypeMask 0xc1bf /* Mask of type bits */ |
| 20189 | 20203 | |
| 20190 | 20204 | |
| 20191 | 20205 | /* Whenever Mem contains a valid string or blob representation, one of |
| 20192 | 20206 | ** the following flags must be set to determine the memory management |
| 20193 | 20207 | ** policy for Mem.z. The MEM_Term flag tells us whether or not the |
| | @@ -30210,13 +30224,11 @@ |
| 30210 | 30224 | ** strings, and stuff like that. |
| 30211 | 30225 | ** |
| 30212 | 30226 | */ |
| 30213 | 30227 | /* #include "sqliteInt.h" */ |
| 30214 | 30228 | /* #include <stdarg.h> */ |
| 30215 | | -#if HAVE_ISNAN || SQLITE_HAVE_ISNAN |
| 30216 | | -# include <math.h> |
| 30217 | | -#endif |
| 30229 | +#include <math.h> |
| 30218 | 30230 | |
| 30219 | 30231 | /* |
| 30220 | 30232 | ** Routine needed to support the testcase() macro. |
| 30221 | 30233 | */ |
| 30222 | 30234 | #ifdef SQLITE_COVERAGE_TEST |
| | @@ -30515,16 +30527,22 @@ |
| 30515 | 30527 | } |
| 30516 | 30528 | return sqlite3StrICmp(zLeft, zRight); |
| 30517 | 30529 | } |
| 30518 | 30530 | SQLITE_PRIVATE int sqlite3StrICmp(const char *zLeft, const char *zRight){ |
| 30519 | 30531 | unsigned char *a, *b; |
| 30520 | | - int c; |
| 30532 | + int c, x; |
| 30521 | 30533 | a = (unsigned char *)zLeft; |
| 30522 | 30534 | b = (unsigned char *)zRight; |
| 30523 | 30535 | for(;;){ |
| 30524 | | - c = (int)UpperToLower[*a] - (int)UpperToLower[*b]; |
| 30525 | | - if( c || *a==0 ) break; |
| 30536 | + c = *a; |
| 30537 | + x = *b; |
| 30538 | + if( c==x ){ |
| 30539 | + if( c==0 ) break; |
| 30540 | + }else{ |
| 30541 | + c = (int)UpperToLower[c] - (int)UpperToLower[x]; |
| 30542 | + if( c ) break; |
| 30543 | + } |
| 30526 | 30544 | a++; |
| 30527 | 30545 | b++; |
| 30528 | 30546 | } |
| 30529 | 30547 | return c; |
| 30530 | 30548 | } |
| | @@ -31105,36 +31123,26 @@ |
| 31105 | 31123 | ** Return the number of bytes read. The value is stored in *v. |
| 31106 | 31124 | */ |
| 31107 | 31125 | SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ |
| 31108 | 31126 | u32 a,b,s; |
| 31109 | 31127 | |
| 31110 | | - a = *p; |
| 31111 | | - /* a: p0 (unmasked) */ |
| 31112 | | - if (!(a&0x80)) |
| 31113 | | - { |
| 31114 | | - *v = a; |
| 31128 | + if( ((signed char*)p)[0]>=0 ){ |
| 31129 | + *v = *p; |
| 31115 | 31130 | return 1; |
| 31116 | 31131 | } |
| 31117 | | - |
| 31118 | | - p++; |
| 31119 | | - b = *p; |
| 31120 | | - /* b: p1 (unmasked) */ |
| 31121 | | - if (!(b&0x80)) |
| 31122 | | - { |
| 31123 | | - a &= 0x7f; |
| 31124 | | - a = a<<7; |
| 31125 | | - a |= b; |
| 31126 | | - *v = a; |
| 31132 | + if( ((signed char*)p)[1]>=0 ){ |
| 31133 | + *v = ((u32)(p[0]&0x7f)<<7) | p[1]; |
| 31127 | 31134 | return 2; |
| 31128 | 31135 | } |
| 31129 | 31136 | |
| 31130 | 31137 | /* Verify that constants are precomputed correctly */ |
| 31131 | 31138 | assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); |
| 31132 | 31139 | assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); |
| 31133 | 31140 | |
| 31134 | | - p++; |
| 31135 | | - a = a<<14; |
| 31141 | + a = ((u32)p[0])<<14; |
| 31142 | + b = p[1]; |
| 31143 | + p += 2; |
| 31136 | 31144 | a |= *p; |
| 31137 | 31145 | /* a: p0<<14 | p2 (unmasked) */ |
| 31138 | 31146 | if (!(a&0x80)) |
| 31139 | 31147 | { |
| 31140 | 31148 | a &= SLOT_2_0; |
| | @@ -61220,13 +61228,13 @@ |
| 61220 | 61228 | if( rc!=SQLITE_OK ){ |
| 61221 | 61229 | return rc; |
| 61222 | 61230 | } |
| 61223 | 61231 | nCollide = HASHTABLE_NSLOT; |
| 61224 | 61232 | for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){ |
| 61225 | | - u32 iFrame = sLoc.aHash[iKey] + sLoc.iZero; |
| 61226 | | - if( iFrame<=iLast && iFrame>=pWal->minFrame |
| 61227 | | - && sLoc.aPgno[sLoc.aHash[iKey]]==pgno ){ |
| 61233 | + u32 iH = sLoc.aHash[iKey]; |
| 61234 | + u32 iFrame = iH + sLoc.iZero; |
| 61235 | + if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){ |
| 61228 | 61236 | assert( iFrame>iRead || CORRUPT_DB ); |
| 61229 | 61237 | iRead = iFrame; |
| 61230 | 61238 | } |
| 61231 | 61239 | if( (nCollide--)==0 ){ |
| 61232 | 61240 | return SQLITE_CORRUPT_BKPT; |
| | @@ -64814,11 +64822,11 @@ |
| 64814 | 64822 | ** and the reserved space is zero (the usual value for reserved space) |
| 64815 | 64823 | ** then the cell content offset of an empty page wants to be 65536. |
| 64816 | 64824 | ** However, that integer is too large to be stored in a 2-byte unsigned |
| 64817 | 64825 | ** integer, so a value of 0 is used in its place. */ |
| 64818 | 64826 | top = get2byte(&data[hdr+5]); |
| 64819 | | - assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ |
| 64827 | + assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */ |
| 64820 | 64828 | if( gap>top ){ |
| 64821 | 64829 | if( top==0 && pPage->pBt->usableSize==65536 ){ |
| 64822 | 64830 | top = 65536; |
| 64823 | 64831 | }else{ |
| 64824 | 64832 | return SQLITE_CORRUPT_PAGE(pPage); |
| | @@ -65111,11 +65119,11 @@ |
| 65111 | 65119 | ** the cell-content area. If this is greater than the usable-size |
| 65112 | 65120 | ** of the page, then the page must be corrupted. This check also |
| 65113 | 65121 | ** serves to verify that the offset to the start of the cell-content |
| 65114 | 65122 | ** area, according to the page header, lies within the page. |
| 65115 | 65123 | */ |
| 65116 | | - if( nFree>usableSize ){ |
| 65124 | + if( nFree>usableSize || nFree<iCellFirst ){ |
| 65117 | 65125 | return SQLITE_CORRUPT_PAGE(pPage); |
| 65118 | 65126 | } |
| 65119 | 65127 | pPage->nFree = (u16)(nFree - iCellFirst); |
| 65120 | 65128 | return SQLITE_OK; |
| 65121 | 65129 | } |
| | @@ -67338,10 +67346,22 @@ |
| 67338 | 67346 | } |
| 67339 | 67347 | sqlite3BtreeLeave(pBtree); |
| 67340 | 67348 | } |
| 67341 | 67349 | return rc; |
| 67342 | 67350 | } |
| 67351 | + |
| 67352 | +/* |
| 67353 | +** Set the pBt->nPage field correctly, according to the current |
| 67354 | +** state of the database. Assume pBt->pPage1 is valid. |
| 67355 | +*/ |
| 67356 | +static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){ |
| 67357 | + int nPage = get4byte(&pPage1->aData[28]); |
| 67358 | + testcase( nPage==0 ); |
| 67359 | + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 67360 | + testcase( pBt->nPage!=nPage ); |
| 67361 | + pBt->nPage = nPage; |
| 67362 | +} |
| 67343 | 67363 | |
| 67344 | 67364 | /* |
| 67345 | 67365 | ** Rollback the transaction in progress. |
| 67346 | 67366 | ** |
| 67347 | 67367 | ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). |
| | @@ -67384,15 +67404,11 @@ |
| 67384 | 67404 | |
| 67385 | 67405 | /* The rollback may have destroyed the pPage1->aData value. So |
| 67386 | 67406 | ** call btreeGetPage() on page 1 again to make |
| 67387 | 67407 | ** sure pPage1->aData is set correctly. */ |
| 67388 | 67408 | if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
| 67389 | | - int nPage = get4byte(28+(u8*)pPage1->aData); |
| 67390 | | - testcase( nPage==0 ); |
| 67391 | | - if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 67392 | | - testcase( pBt->nPage!=nPage ); |
| 67393 | | - pBt->nPage = nPage; |
| 67409 | + btreeSetNPage(pBt, pPage1); |
| 67394 | 67410 | releasePageOne(pPage1); |
| 67395 | 67411 | } |
| 67396 | 67412 | assert( countValidCursors(pBt, 1)==0 ); |
| 67397 | 67413 | pBt->inTransaction = TRANS_READ; |
| 67398 | 67414 | btreeClearHasContent(pBt); |
| | @@ -67468,16 +67484,15 @@ |
| 67468 | 67484 | if( rc==SQLITE_OK ){ |
| 67469 | 67485 | if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ |
| 67470 | 67486 | pBt->nPage = 0; |
| 67471 | 67487 | } |
| 67472 | 67488 | rc = newDatabase(pBt); |
| 67473 | | - pBt->nPage = get4byte(28 + pBt->pPage1->aData); |
| 67489 | + btreeSetNPage(pBt, pBt->pPage1); |
| 67474 | 67490 | |
| 67475 | | - /* The database size was written into the offset 28 of the header |
| 67476 | | - ** when the transaction started, so we know that the value at offset |
| 67477 | | - ** 28 is nonzero. */ |
| 67478 | | - assert( pBt->nPage>0 ); |
| 67491 | + /* pBt->nPage might be zero if the database was corrupt when |
| 67492 | + ** the transaction was started. Otherwise, it must be at least 1. */ |
| 67493 | + assert( CORRUPT_DB || pBt->nPage>0 ); |
| 67479 | 67494 | } |
| 67480 | 67495 | sqlite3BtreeLeave(p); |
| 67481 | 67496 | } |
| 67482 | 67497 | return rc; |
| 67483 | 67498 | } |
| | @@ -68481,10 +68496,11 @@ |
| 68481 | 68496 | assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); |
| 68482 | 68497 | } |
| 68483 | 68498 | assert( pCur->ix==pCur->pPage->nCell-1 ); |
| 68484 | 68499 | assert( pCur->pPage->leaf ); |
| 68485 | 68500 | #endif |
| 68501 | + *pRes = 0; |
| 68486 | 68502 | return SQLITE_OK; |
| 68487 | 68503 | } |
| 68488 | 68504 | |
| 68489 | 68505 | rc = moveToRoot(pCur); |
| 68490 | 68506 | if( rc==SQLITE_OK ){ |
| | @@ -70823,10 +70839,11 @@ |
| 70823 | 70839 | int limit = pOld->nCell; |
| 70824 | 70840 | u8 *aData = pOld->aData; |
| 70825 | 70841 | u16 maskPage = pOld->maskPage; |
| 70826 | 70842 | u8 *piCell = aData + pOld->cellOffset; |
| 70827 | 70843 | u8 *piEnd; |
| 70844 | + VVA_ONLY( int nCellAtStart = b.nCell; ) |
| 70828 | 70845 | |
| 70829 | 70846 | /* Verify that all sibling pages are of the same "type" (table-leaf, |
| 70830 | 70847 | ** table-interior, index-leaf, or index-interior). |
| 70831 | 70848 | */ |
| 70832 | 70849 | if( pOld->aData[0]!=apOld[0]->aData[0] ){ |
| | @@ -70851,10 +70868,14 @@ |
| 70851 | 70868 | ** long be able to find the cells if a pointer to each cell is not saved |
| 70852 | 70869 | ** first. |
| 70853 | 70870 | */ |
| 70854 | 70871 | memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow)); |
| 70855 | 70872 | if( pOld->nOverflow>0 ){ |
| 70873 | + if( limit<pOld->aiOvfl[0] ){ |
| 70874 | + rc = SQLITE_CORRUPT_BKPT; |
| 70875 | + goto balance_cleanup; |
| 70876 | + } |
| 70856 | 70877 | limit = pOld->aiOvfl[0]; |
| 70857 | 70878 | for(j=0; j<limit; j++){ |
| 70858 | 70879 | b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); |
| 70859 | 70880 | piCell += 2; |
| 70860 | 70881 | b.nCell++; |
| | @@ -70870,10 +70891,11 @@ |
| 70870 | 70891 | assert( b.nCell<nMaxCells ); |
| 70871 | 70892 | b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); |
| 70872 | 70893 | piCell += 2; |
| 70873 | 70894 | b.nCell++; |
| 70874 | 70895 | } |
| 70896 | + assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) ); |
| 70875 | 70897 | |
| 70876 | 70898 | cntOld[i] = b.nCell; |
| 70877 | 70899 | if( i<nOld-1 && !leafData){ |
| 70878 | 70900 | u16 sz = (u16)szNew[i]; |
| 70879 | 70901 | u8 *pTemp; |
| | @@ -71170,10 +71192,11 @@ |
| 71170 | 71192 | for(i=0; i<b.nCell; i++){ |
| 71171 | 71193 | u8 *pCell = b.apCell[i]; |
| 71172 | 71194 | while( i==cntOldNext ){ |
| 71173 | 71195 | iOld++; |
| 71174 | 71196 | assert( iOld<nNew || iOld<nOld ); |
| 71197 | + assert( iOld>=0 && iOld<NB ); |
| 71175 | 71198 | pOld = iOld<nNew ? apNew[iOld] : apOld[iOld]; |
| 71176 | 71199 | cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; |
| 71177 | 71200 | } |
| 71178 | 71201 | if( i==cntNew[iNew] ){ |
| 71179 | 71202 | pNew = apNew[++iNew]; |
| | @@ -73890,11 +73913,11 @@ |
| 73890 | 73913 | ** then the backup cannot proceed. |
| 73891 | 73914 | */ |
| 73892 | 73915 | if( nSrcReserve!=nDestReserve ){ |
| 73893 | 73916 | u32 newPgsz = nSrcPgsz; |
| 73894 | 73917 | rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve); |
| 73895 | | - if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY; |
| 73918 | + if( rc==SQLITE_OK && newPgsz!=(u32)nSrcPgsz ) rc = SQLITE_READONLY; |
| 73896 | 73919 | } |
| 73897 | 73920 | #endif |
| 73898 | 73921 | |
| 73899 | 73922 | /* This loop runs once for each destination page spanned by the source |
| 73900 | 73923 | ** page. For each iteration, variable iOff is set to the byte offset |
| | @@ -74437,10 +74460,15 @@ |
| 74437 | 74460 | ** name sqlite_value |
| 74438 | 74461 | */ |
| 74439 | 74462 | /* #include "sqliteInt.h" */ |
| 74440 | 74463 | /* #include "vdbeInt.h" */ |
| 74441 | 74464 | |
| 74465 | +/* True if X is a power of two. 0 is considered a power of two here. |
| 74466 | +** In other words, return true if X has at most one bit set. |
| 74467 | +*/ |
| 74468 | +#define ISPOWEROF2(X) (((X)&((X)-1))==0) |
| 74469 | + |
| 74442 | 74470 | #ifdef SQLITE_DEBUG |
| 74443 | 74471 | /* |
| 74444 | 74472 | ** Check invariants on a Mem object. |
| 74445 | 74473 | ** |
| 74446 | 74474 | ** This routine is intended for use inside of assert() statements, like |
| | @@ -74456,12 +74484,12 @@ |
| 74456 | 74484 | ** ensure that if Mem.szMalloc>0 then it is safe to do |
| 74457 | 74485 | ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn. |
| 74458 | 74486 | ** That saves a few cycles in inner loops. */ |
| 74459 | 74487 | assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); |
| 74460 | 74488 | |
| 74461 | | - /* Cannot be both MEM_Int and MEM_Real at the same time */ |
| 74462 | | - assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); |
| 74489 | + /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */ |
| 74490 | + assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) ); |
| 74463 | 74491 | |
| 74464 | 74492 | if( p->flags & MEM_Null ){ |
| 74465 | 74493 | /* Cannot be both MEM_Null and some other type */ |
| 74466 | 74494 | assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 ); |
| 74467 | 74495 | |
| | @@ -74510,10 +74538,29 @@ |
| 74510 | 74538 | ); |
| 74511 | 74539 | } |
| 74512 | 74540 | return 1; |
| 74513 | 74541 | } |
| 74514 | 74542 | #endif |
| 74543 | + |
| 74544 | +/* |
| 74545 | +** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal |
| 74546 | +** into a buffer. |
| 74547 | +*/ |
| 74548 | +static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){ |
| 74549 | + StrAccum acc; |
| 74550 | + assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) ); |
| 74551 | + sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0); |
| 74552 | + if( p->flags & MEM_Int ){ |
| 74553 | + sqlite3_str_appendf(&acc, "%lld", p->u.i); |
| 74554 | + }else if( p->flags & MEM_IntReal ){ |
| 74555 | + sqlite3_str_appendf(&acc, "%!.15g", (double)p->u.i); |
| 74556 | + }else{ |
| 74557 | + sqlite3_str_appendf(&acc, "%!.15g", p->u.r); |
| 74558 | + } |
| 74559 | + assert( acc.zText==zBuf && acc.mxAlloc<=0 ); |
| 74560 | + zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */ |
| 74561 | +} |
| 74515 | 74562 | |
| 74516 | 74563 | #ifdef SQLITE_DEBUG |
| 74517 | 74564 | /* |
| 74518 | 74565 | ** Check that string value of pMem agrees with its integer or real value. |
| 74519 | 74566 | ** |
| | @@ -74536,16 +74583,12 @@ |
| 74536 | 74583 | SQLITE_PRIVATE int sqlite3VdbeMemConsistentDualRep(Mem *p){ |
| 74537 | 74584 | char zBuf[100]; |
| 74538 | 74585 | char *z; |
| 74539 | 74586 | int i, j, incr; |
| 74540 | 74587 | if( (p->flags & MEM_Str)==0 ) return 1; |
| 74541 | | - if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1; |
| 74542 | | - if( p->flags & MEM_Int ){ |
| 74543 | | - sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i); |
| 74544 | | - }else{ |
| 74545 | | - sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r); |
| 74546 | | - } |
| 74588 | + if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1; |
| 74589 | + vdbeMemRenderNum(sizeof(zBuf), zBuf, p); |
| 74547 | 74590 | z = p->z; |
| 74548 | 74591 | i = j = 0; |
| 74549 | 74592 | incr = 1; |
| 74550 | 74593 | if( p->enc!=SQLITE_UTF8 ){ |
| 74551 | 74594 | incr = 2; |
| | @@ -74653,12 +74696,12 @@ |
| 74653 | 74696 | ** If pMem->zMalloc already meets or exceeds the requested size, this |
| 74654 | 74697 | ** routine is a no-op. |
| 74655 | 74698 | ** |
| 74656 | 74699 | ** Any prior string or blob content in the pMem object may be discarded. |
| 74657 | 74700 | ** The pMem->xDel destructor is called, if it exists. Though MEM_Str |
| 74658 | | -** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null |
| 74659 | | -** values are preserved. |
| 74701 | +** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal, |
| 74702 | +** and MEM_Null values are preserved. |
| 74660 | 74703 | ** |
| 74661 | 74704 | ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) |
| 74662 | 74705 | ** if unable to complete the resizing. |
| 74663 | 74706 | */ |
| 74664 | 74707 | SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ |
| | @@ -74667,24 +74710,30 @@ |
| 74667 | 74710 | if( pMem->szMalloc<szNew ){ |
| 74668 | 74711 | return sqlite3VdbeMemGrow(pMem, szNew, 0); |
| 74669 | 74712 | } |
| 74670 | 74713 | assert( (pMem->flags & MEM_Dyn)==0 ); |
| 74671 | 74714 | pMem->z = pMem->zMalloc; |
| 74672 | | - pMem->flags &= (MEM_Null|MEM_Int|MEM_Real); |
| 74715 | + pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal); |
| 74673 | 74716 | return SQLITE_OK; |
| 74674 | 74717 | } |
| 74675 | 74718 | |
| 74676 | 74719 | /* |
| 74677 | 74720 | ** It is already known that pMem contains an unterminated string. |
| 74678 | 74721 | ** Add the zero terminator. |
| 74722 | +** |
| 74723 | +** Three bytes of zero are added. In this way, there is guaranteed |
| 74724 | +** to be a double-zero byte at an even byte boundary in order to |
| 74725 | +** terminate a UTF16 string, even if the initial size of the buffer |
| 74726 | +** is an odd number of bytes. |
| 74679 | 74727 | */ |
| 74680 | 74728 | static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){ |
| 74681 | | - if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ |
| 74729 | + if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){ |
| 74682 | 74730 | return SQLITE_NOMEM_BKPT; |
| 74683 | 74731 | } |
| 74684 | 74732 | pMem->z[pMem->n] = 0; |
| 74685 | 74733 | pMem->z[pMem->n+1] = 0; |
| 74734 | + pMem->z[pMem->n+2] = 0; |
| 74686 | 74735 | pMem->flags |= MEM_Term; |
| 74687 | 74736 | return SQLITE_OK; |
| 74688 | 74737 | } |
| 74689 | 74738 | |
| 74690 | 74739 | /* |
| | @@ -74754,57 +74803,45 @@ |
| 74754 | 74803 | return vdbeMemAddTerminator(pMem); |
| 74755 | 74804 | } |
| 74756 | 74805 | } |
| 74757 | 74806 | |
| 74758 | 74807 | /* |
| 74759 | | -** Add MEM_Str to the set of representations for the given Mem. Numbers |
| 74760 | | -** are converted using sqlite3_snprintf(). Converting a BLOB to a string |
| 74761 | | -** is a no-op. |
| 74808 | +** Add MEM_Str to the set of representations for the given Mem. This |
| 74809 | +** routine is only called if pMem is a number of some kind, not a NULL |
| 74810 | +** or a BLOB. |
| 74762 | 74811 | ** |
| 74763 | | -** Existing representations MEM_Int and MEM_Real are invalidated if |
| 74764 | | -** bForce is true but are retained if bForce is false. |
| 74812 | +** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated |
| 74813 | +** if bForce is true but are retained if bForce is false. |
| 74765 | 74814 | ** |
| 74766 | 74815 | ** A MEM_Null value will never be passed to this function. This function is |
| 74767 | 74816 | ** used for converting values to text for returning to the user (i.e. via |
| 74768 | 74817 | ** sqlite3_value_text()), or for ensuring that values to be used as btree |
| 74769 | 74818 | ** keys are strings. In the former case a NULL pointer is returned the |
| 74770 | 74819 | ** user and the latter is an internal programming error. |
| 74771 | 74820 | */ |
| 74772 | 74821 | SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){ |
| 74773 | | - int fg = pMem->flags; |
| 74774 | 74822 | const int nByte = 32; |
| 74775 | 74823 | |
| 74776 | 74824 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 74777 | | - assert( !(fg&MEM_Zero) ); |
| 74778 | | - assert( !(fg&(MEM_Str|MEM_Blob)) ); |
| 74779 | | - assert( fg&(MEM_Int|MEM_Real) ); |
| 74825 | + assert( !(pMem->flags&MEM_Zero) ); |
| 74826 | + assert( !(pMem->flags&(MEM_Str|MEM_Blob)) ); |
| 74827 | + assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) ); |
| 74780 | 74828 | assert( !sqlite3VdbeMemIsRowSet(pMem) ); |
| 74781 | 74829 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 74782 | 74830 | |
| 74783 | 74831 | |
| 74784 | 74832 | if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
| 74785 | 74833 | pMem->enc = 0; |
| 74786 | 74834 | return SQLITE_NOMEM_BKPT; |
| 74787 | 74835 | } |
| 74788 | 74836 | |
| 74789 | | - /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8 |
| 74790 | | - ** string representation of the value. Then, if the required encoding |
| 74791 | | - ** is UTF-16le or UTF-16be do a translation. |
| 74792 | | - ** |
| 74793 | | - ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. |
| 74794 | | - */ |
| 74795 | | - if( fg & MEM_Int ){ |
| 74796 | | - sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); |
| 74797 | | - }else{ |
| 74798 | | - assert( fg & MEM_Real ); |
| 74799 | | - sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r); |
| 74800 | | - } |
| 74837 | + vdbeMemRenderNum(nByte, pMem->z, pMem); |
| 74801 | 74838 | assert( pMem->z!=0 ); |
| 74802 | 74839 | pMem->n = sqlite3Strlen30NN(pMem->z); |
| 74803 | 74840 | pMem->enc = SQLITE_UTF8; |
| 74804 | 74841 | pMem->flags |= MEM_Str|MEM_Term; |
| 74805 | | - if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real); |
| 74842 | + if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal); |
| 74806 | 74843 | sqlite3VdbeChangeEncoding(pMem, enc); |
| 74807 | 74844 | return SQLITE_OK; |
| 74808 | 74845 | } |
| 74809 | 74846 | |
| 74810 | 74847 | /* |
| | @@ -74974,11 +75011,12 @@ |
| 74974 | 75011 | SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){ |
| 74975 | 75012 | int flags; |
| 74976 | 75013 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 74977 | 75014 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 74978 | 75015 | flags = pMem->flags; |
| 74979 | | - if( flags & MEM_Int ){ |
| 75016 | + if( flags & (MEM_Int|MEM_IntReal) ){ |
| 75017 | + testcase( flags & MEM_IntReal ); |
| 74980 | 75018 | return pMem->u.i; |
| 74981 | 75019 | }else if( flags & MEM_Real ){ |
| 74982 | 75020 | return doubleToInt64(pMem->u.r); |
| 74983 | 75021 | }else if( flags & (MEM_Str|MEM_Blob) ){ |
| 74984 | 75022 | assert( pMem->z || pMem->n==0 ); |
| | @@ -75003,11 +75041,12 @@ |
| 75003 | 75041 | SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){ |
| 75004 | 75042 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 75005 | 75043 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 75006 | 75044 | if( pMem->flags & MEM_Real ){ |
| 75007 | 75045 | return pMem->u.r; |
| 75008 | | - }else if( pMem->flags & MEM_Int ){ |
| 75046 | + }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 75047 | + testcase( pMem->flags & MEM_IntReal ); |
| 75009 | 75048 | return (double)pMem->u.i; |
| 75010 | 75049 | }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
| 75011 | 75050 | return memRealValue(pMem); |
| 75012 | 75051 | }else{ |
| 75013 | 75052 | /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| | @@ -75018,11 +75057,12 @@ |
| 75018 | 75057 | /* |
| 75019 | 75058 | ** Return 1 if pMem represents true, and return 0 if pMem represents false. |
| 75020 | 75059 | ** Return the value ifNull if pMem is NULL. |
| 75021 | 75060 | */ |
| 75022 | 75061 | SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){ |
| 75023 | | - if( pMem->flags & MEM_Int ) return pMem->u.i!=0; |
| 75062 | + testcase( pMem->flags & MEM_IntReal ); |
| 75063 | + if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0; |
| 75024 | 75064 | if( pMem->flags & MEM_Null ) return ifNull; |
| 75025 | 75065 | return sqlite3VdbeRealValue(pMem)!=0.0; |
| 75026 | 75066 | } |
| 75027 | 75067 | |
| 75028 | 75068 | /* |
| | @@ -75091,19 +75131,23 @@ |
| 75091 | 75131 | double r2 = (double)i; |
| 75092 | 75132 | return memcmp(&r1, &r2, sizeof(r1))==0; |
| 75093 | 75133 | } |
| 75094 | 75134 | |
| 75095 | 75135 | /* |
| 75096 | | -** Convert pMem so that it has types MEM_Real or MEM_Int or both. |
| 75136 | +** Convert pMem so that it has type MEM_Real or MEM_Int. |
| 75097 | 75137 | ** Invalidate any prior representations. |
| 75098 | 75138 | ** |
| 75099 | 75139 | ** Every effort is made to force the conversion, even if the input |
| 75100 | 75140 | ** is a string that does not look completely like a number. Convert |
| 75101 | 75141 | ** as much of the string as we can and ignore the rest. |
| 75102 | 75142 | */ |
| 75103 | 75143 | SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){ |
| 75104 | | - if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){ |
| 75144 | + testcase( pMem->flags & MEM_Int ); |
| 75145 | + testcase( pMem->flags & MEM_Real ); |
| 75146 | + testcase( pMem->flags & MEM_IntReal ); |
| 75147 | + testcase( pMem->flags & MEM_Null ); |
| 75148 | + if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){ |
| 75105 | 75149 | int rc; |
| 75106 | 75150 | assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); |
| 75107 | 75151 | assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 75108 | 75152 | rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc); |
| 75109 | 75153 | if( rc==0 ){ |
| | @@ -75117,11 +75161,11 @@ |
| 75117 | 75161 | }else{ |
| 75118 | 75162 | MemSetTypeFlag(pMem, MEM_Real); |
| 75119 | 75163 | } |
| 75120 | 75164 | } |
| 75121 | 75165 | } |
| 75122 | | - assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 ); |
| 75166 | + assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 ); |
| 75123 | 75167 | pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero); |
| 75124 | 75168 | return SQLITE_OK; |
| 75125 | 75169 | } |
| 75126 | 75170 | |
| 75127 | 75171 | /* |
| | @@ -75160,11 +75204,11 @@ |
| 75160 | 75204 | assert( aff==SQLITE_AFF_TEXT ); |
| 75161 | 75205 | assert( MEM_Str==(MEM_Blob>>3) ); |
| 75162 | 75206 | pMem->flags |= (pMem->flags&MEM_Blob)>>3; |
| 75163 | 75207 | sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); |
| 75164 | 75208 | assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); |
| 75165 | | - pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); |
| 75209 | + pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero); |
| 75166 | 75210 | break; |
| 75167 | 75211 | } |
| 75168 | 75212 | } |
| 75169 | 75213 | } |
| 75170 | 75214 | |
| | @@ -75344,11 +75388,11 @@ |
| 75344 | 75388 | ** A significant change would indicated a missed call to this |
| 75345 | 75389 | ** function for pX. Minor changes, such as adding or removing a |
| 75346 | 75390 | ** dual type, are allowed, as long as the underlying value is the |
| 75347 | 75391 | ** same. */ |
| 75348 | 75392 | u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags; |
| 75349 | | - assert( (mFlags&MEM_Int)==0 || pMem->u.i==pX->u.i ); |
| 75393 | + assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i ); |
| 75350 | 75394 | assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r ); |
| 75351 | 75395 | assert( (mFlags&MEM_Str)==0 || (pMem->n==pX->n && pMem->z==pX->z) ); |
| 75352 | 75396 | assert( (mFlags&MEM_Blob)==0 || sqlite3BlobCompare(pMem,pX)==0 ); |
| 75353 | 75397 | |
| 75354 | 75398 | /* pMem is the register that is changing. But also mark pX as |
| | @@ -75907,11 +75951,16 @@ |
| 75907 | 75951 | if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){ |
| 75908 | 75952 | sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); |
| 75909 | 75953 | }else{ |
| 75910 | 75954 | sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); |
| 75911 | 75955 | } |
| 75912 | | - if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; |
| 75956 | + assert( (pVal->flags & MEM_IntReal)==0 ); |
| 75957 | + if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){ |
| 75958 | + testcase( pVal->flags & MEM_Int ); |
| 75959 | + testcase( pVal->flags & MEM_Real ); |
| 75960 | + pVal->flags &= ~MEM_Str; |
| 75961 | + } |
| 75913 | 75962 | if( enc!=SQLITE_UTF8 ){ |
| 75914 | 75963 | rc = sqlite3VdbeChangeEncoding(pVal, enc); |
| 75915 | 75964 | } |
| 75916 | 75965 | }else if( op==TK_UMINUS ) { |
| 75917 | 75966 | /* This branch happens for multiple negative signs. Ex: -(-5) */ |
| | @@ -75930,11 +75979,11 @@ |
| 75930 | 75979 | sqlite3ValueApplyAffinity(pVal, affinity, enc); |
| 75931 | 75980 | } |
| 75932 | 75981 | }else if( op==TK_NULL ){ |
| 75933 | 75982 | pVal = valueNew(db, pCtx); |
| 75934 | 75983 | if( pVal==0 ) goto no_mem; |
| 75935 | | - sqlite3VdbeMemNumerify(pVal); |
| 75984 | + sqlite3VdbeMemSetNull(pVal); |
| 75936 | 75985 | } |
| 75937 | 75986 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 75938 | 75987 | else if( op==TK_BLOB ){ |
| 75939 | 75988 | int nVal; |
| 75940 | 75989 | assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); |
| | @@ -77847,11 +77896,11 @@ |
| 77847 | 77896 | } |
| 77848 | 77897 | case P4_MEM: { |
| 77849 | 77898 | Mem *pMem = pOp->p4.pMem; |
| 77850 | 77899 | if( pMem->flags & MEM_Str ){ |
| 77851 | 77900 | zP4 = pMem->z; |
| 77852 | | - }else if( pMem->flags & MEM_Int ){ |
| 77901 | + }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 77853 | 77902 | sqlite3_str_appendf(&x, "%lld", pMem->u.i); |
| 77854 | 77903 | }else if( pMem->flags & MEM_Real ){ |
| 77855 | 77904 | sqlite3_str_appendf(&x, "%.16g", pMem->u.r); |
| 77856 | 77905 | }else if( pMem->flags & MEM_Null ){ |
| 77857 | 77906 | zP4 = "NULL"; |
| | @@ -79209,11 +79258,11 @@ |
| 79209 | 79258 | } |
| 79210 | 79259 | } |
| 79211 | 79260 | } |
| 79212 | 79261 | |
| 79213 | 79262 | /* Check for immediate foreign key violations. */ |
| 79214 | | - if( p->rc==SQLITE_OK ){ |
| 79263 | + if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
| 79215 | 79264 | sqlite3VdbeCheckFk(p, 0); |
| 79216 | 79265 | } |
| 79217 | 79266 | |
| 79218 | 79267 | /* If the auto-commit flag is set and this is the only active writer |
| 79219 | 79268 | ** VM, then we do either a commit or rollback of the current transaction. |
| | @@ -79735,10 +79784,12 @@ |
| 79735 | 79784 | ** of SQLite will not understand those serial types. |
| 79736 | 79785 | */ |
| 79737 | 79786 | |
| 79738 | 79787 | /* |
| 79739 | 79788 | ** Return the serial-type for the value stored in pMem. |
| 79789 | +** |
| 79790 | +** This routine might convert a large MEM_IntReal value into MEM_Real. |
| 79740 | 79791 | */ |
| 79741 | 79792 | SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ |
| 79742 | 79793 | int flags = pMem->flags; |
| 79743 | 79794 | u32 n; |
| 79744 | 79795 | |
| | @@ -79745,15 +79796,17 @@ |
| 79745 | 79796 | assert( pLen!=0 ); |
| 79746 | 79797 | if( flags&MEM_Null ){ |
| 79747 | 79798 | *pLen = 0; |
| 79748 | 79799 | return 0; |
| 79749 | 79800 | } |
| 79750 | | - if( flags&MEM_Int ){ |
| 79801 | + if( flags&(MEM_Int|MEM_IntReal) ){ |
| 79751 | 79802 | /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 79752 | 79803 | # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
| 79753 | 79804 | i64 i = pMem->u.i; |
| 79754 | 79805 | u64 u; |
| 79806 | + testcase( flags & MEM_Int ); |
| 79807 | + testcase( flags & MEM_IntReal ); |
| 79755 | 79808 | if( i<0 ){ |
| 79756 | 79809 | u = ~i; |
| 79757 | 79810 | }else{ |
| 79758 | 79811 | u = i; |
| 79759 | 79812 | } |
| | @@ -79769,10 +79822,19 @@ |
| 79769 | 79822 | if( u<=32767 ){ *pLen = 2; return 2; } |
| 79770 | 79823 | if( u<=8388607 ){ *pLen = 3; return 3; } |
| 79771 | 79824 | if( u<=2147483647 ){ *pLen = 4; return 4; } |
| 79772 | 79825 | if( u<=MAX_6BYTE ){ *pLen = 6; return 5; } |
| 79773 | 79826 | *pLen = 8; |
| 79827 | + if( flags&MEM_IntReal ){ |
| 79828 | + /* If the value is IntReal and is going to take up 8 bytes to store |
| 79829 | + ** as an integer, then we might as well make it an 8-byte floating |
| 79830 | + ** point value */ |
| 79831 | + pMem->u.r = (double)pMem->u.i; |
| 79832 | + pMem->flags &= ~MEM_IntReal; |
| 79833 | + pMem->flags |= MEM_Real; |
| 79834 | + return 7; |
| 79835 | + } |
| 79774 | 79836 | return 6; |
| 79775 | 79837 | } |
| 79776 | 79838 | if( flags&MEM_Real ){ |
| 79777 | 79839 | *pLen = 8; |
| 79778 | 79840 | return 7; |
| | @@ -80424,30 +80486,43 @@ |
| 80424 | 80486 | return (f2&MEM_Null) - (f1&MEM_Null); |
| 80425 | 80487 | } |
| 80426 | 80488 | |
| 80427 | 80489 | /* At least one of the two values is a number |
| 80428 | 80490 | */ |
| 80429 | | - if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 80430 | | - if( (f1 & f2 & MEM_Int)!=0 ){ |
| 80491 | + if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){ |
| 80492 | + testcase( combined_flags & MEM_Int ); |
| 80493 | + testcase( combined_flags & MEM_Real ); |
| 80494 | + testcase( combined_flags & MEM_IntReal ); |
| 80495 | + if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){ |
| 80496 | + testcase( f1 & f2 & MEM_Int ); |
| 80497 | + testcase( f1 & f2 & MEM_IntReal ); |
| 80431 | 80498 | if( pMem1->u.i < pMem2->u.i ) return -1; |
| 80432 | 80499 | if( pMem1->u.i > pMem2->u.i ) return +1; |
| 80433 | 80500 | return 0; |
| 80434 | 80501 | } |
| 80435 | 80502 | if( (f1 & f2 & MEM_Real)!=0 ){ |
| 80436 | 80503 | if( pMem1->u.r < pMem2->u.r ) return -1; |
| 80437 | 80504 | if( pMem1->u.r > pMem2->u.r ) return +1; |
| 80438 | 80505 | return 0; |
| 80439 | 80506 | } |
| 80440 | | - if( (f1&MEM_Int)!=0 ){ |
| 80507 | + if( (f1&(MEM_Int|MEM_IntReal))!=0 ){ |
| 80508 | + testcase( f1 & MEM_Int ); |
| 80509 | + testcase( f1 & MEM_IntReal ); |
| 80441 | 80510 | if( (f2&MEM_Real)!=0 ){ |
| 80442 | 80511 | return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r); |
| 80512 | + }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){ |
| 80513 | + if( pMem1->u.i < pMem2->u.i ) return -1; |
| 80514 | + if( pMem1->u.i > pMem2->u.i ) return +1; |
| 80515 | + return 0; |
| 80443 | 80516 | }else{ |
| 80444 | 80517 | return -1; |
| 80445 | 80518 | } |
| 80446 | 80519 | } |
| 80447 | 80520 | if( (f1&MEM_Real)!=0 ){ |
| 80448 | | - if( (f2&MEM_Int)!=0 ){ |
| 80521 | + if( (f2&(MEM_Int|MEM_IntReal))!=0 ){ |
| 80522 | + testcase( f2 & MEM_Int ); |
| 80523 | + testcase( f2 & MEM_IntReal ); |
| 80449 | 80524 | return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r); |
| 80450 | 80525 | }else{ |
| 80451 | 80526 | return -1; |
| 80452 | 80527 | } |
| 80453 | 80528 | } |
| | @@ -80592,11 +80667,13 @@ |
| 80592 | 80667 | assert( idx1<=szHdr1 || CORRUPT_DB ); |
| 80593 | 80668 | do{ |
| 80594 | 80669 | u32 serial_type; |
| 80595 | 80670 | |
| 80596 | 80671 | /* RHS is an integer */ |
| 80597 | | - if( pRhs->flags & MEM_Int ){ |
| 80672 | + if( pRhs->flags & (MEM_Int|MEM_IntReal) ){ |
| 80673 | + testcase( pRhs->flags & MEM_Int ); |
| 80674 | + testcase( pRhs->flags & MEM_IntReal ); |
| 80598 | 80675 | serial_type = aKey1[idx1]; |
| 80599 | 80676 | testcase( serial_type==12 ); |
| 80600 | 80677 | if( serial_type>=10 ){ |
| 80601 | 80678 | rc = +1; |
| 80602 | 80679 | }else if( serial_type==0 ){ |
| | @@ -80937,11 +81014,13 @@ |
| 80937 | 81014 | return vdbeRecordCompareInt; |
| 80938 | 81015 | } |
| 80939 | 81016 | testcase( flags & MEM_Real ); |
| 80940 | 81017 | testcase( flags & MEM_Null ); |
| 80941 | 81018 | testcase( flags & MEM_Blob ); |
| 80942 | | - if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){ |
| 81019 | + if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0 |
| 81020 | + && p->pKeyInfo->aColl[0]==0 |
| 81021 | + ){ |
| 80943 | 81022 | assert( flags & MEM_Str ); |
| 80944 | 81023 | return vdbeRecordCompareString; |
| 80945 | 81024 | } |
| 80946 | 81025 | } |
| 80947 | 81026 | |
| | @@ -81527,43 +81606,90 @@ |
| 81527 | 81606 | ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating |
| 81528 | 81607 | ** point number string BLOB NULL |
| 81529 | 81608 | */ |
| 81530 | 81609 | SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){ |
| 81531 | 81610 | static const u8 aType[] = { |
| 81532 | | - SQLITE_BLOB, /* 0x00 */ |
| 81533 | | - SQLITE_NULL, /* 0x01 */ |
| 81534 | | - SQLITE_TEXT, /* 0x02 */ |
| 81535 | | - SQLITE_NULL, /* 0x03 */ |
| 81536 | | - SQLITE_INTEGER, /* 0x04 */ |
| 81537 | | - SQLITE_NULL, /* 0x05 */ |
| 81538 | | - SQLITE_INTEGER, /* 0x06 */ |
| 81539 | | - SQLITE_NULL, /* 0x07 */ |
| 81540 | | - SQLITE_FLOAT, /* 0x08 */ |
| 81541 | | - SQLITE_NULL, /* 0x09 */ |
| 81542 | | - SQLITE_FLOAT, /* 0x0a */ |
| 81543 | | - SQLITE_NULL, /* 0x0b */ |
| 81544 | | - SQLITE_INTEGER, /* 0x0c */ |
| 81545 | | - SQLITE_NULL, /* 0x0d */ |
| 81546 | | - SQLITE_INTEGER, /* 0x0e */ |
| 81547 | | - SQLITE_NULL, /* 0x0f */ |
| 81548 | | - SQLITE_BLOB, /* 0x10 */ |
| 81549 | | - SQLITE_NULL, /* 0x11 */ |
| 81550 | | - SQLITE_TEXT, /* 0x12 */ |
| 81551 | | - SQLITE_NULL, /* 0x13 */ |
| 81552 | | - SQLITE_INTEGER, /* 0x14 */ |
| 81553 | | - SQLITE_NULL, /* 0x15 */ |
| 81554 | | - SQLITE_INTEGER, /* 0x16 */ |
| 81555 | | - SQLITE_NULL, /* 0x17 */ |
| 81556 | | - SQLITE_FLOAT, /* 0x18 */ |
| 81557 | | - SQLITE_NULL, /* 0x19 */ |
| 81558 | | - SQLITE_FLOAT, /* 0x1a */ |
| 81559 | | - SQLITE_NULL, /* 0x1b */ |
| 81560 | | - SQLITE_INTEGER, /* 0x1c */ |
| 81561 | | - SQLITE_NULL, /* 0x1d */ |
| 81562 | | - SQLITE_INTEGER, /* 0x1e */ |
| 81563 | | - SQLITE_NULL, /* 0x1f */ |
| 81564 | | - }; |
| 81611 | + SQLITE_BLOB, /* 0x00 (not possible) */ |
| 81612 | + SQLITE_NULL, /* 0x01 NULL */ |
| 81613 | + SQLITE_TEXT, /* 0x02 TEXT */ |
| 81614 | + SQLITE_NULL, /* 0x03 (not possible) */ |
| 81615 | + SQLITE_INTEGER, /* 0x04 INTEGER */ |
| 81616 | + SQLITE_NULL, /* 0x05 (not possible) */ |
| 81617 | + SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ |
| 81618 | + SQLITE_NULL, /* 0x07 (not possible) */ |
| 81619 | + SQLITE_FLOAT, /* 0x08 FLOAT */ |
| 81620 | + SQLITE_NULL, /* 0x09 (not possible) */ |
| 81621 | + SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ |
| 81622 | + SQLITE_NULL, /* 0x0b (not possible) */ |
| 81623 | + SQLITE_INTEGER, /* 0x0c (not possible) */ |
| 81624 | + SQLITE_NULL, /* 0x0d (not possible) */ |
| 81625 | + SQLITE_INTEGER, /* 0x0e (not possible) */ |
| 81626 | + SQLITE_NULL, /* 0x0f (not possible) */ |
| 81627 | + SQLITE_BLOB, /* 0x10 BLOB */ |
| 81628 | + SQLITE_NULL, /* 0x11 (not possible) */ |
| 81629 | + SQLITE_TEXT, /* 0x12 (not possible) */ |
| 81630 | + SQLITE_NULL, /* 0x13 (not possible) */ |
| 81631 | + SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ |
| 81632 | + SQLITE_NULL, /* 0x15 (not possible) */ |
| 81633 | + SQLITE_INTEGER, /* 0x16 (not possible) */ |
| 81634 | + SQLITE_NULL, /* 0x17 (not possible) */ |
| 81635 | + SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ |
| 81636 | + SQLITE_NULL, /* 0x19 (not possible) */ |
| 81637 | + SQLITE_FLOAT, /* 0x1a (not possible) */ |
| 81638 | + SQLITE_NULL, /* 0x1b (not possible) */ |
| 81639 | + SQLITE_INTEGER, /* 0x1c (not possible) */ |
| 81640 | + SQLITE_NULL, /* 0x1d (not possible) */ |
| 81641 | + SQLITE_INTEGER, /* 0x1e (not possible) */ |
| 81642 | + SQLITE_NULL, /* 0x1f (not possible) */ |
| 81643 | + SQLITE_FLOAT, /* 0x20 INTREAL */ |
| 81644 | + SQLITE_NULL, /* 0x21 (not possible) */ |
| 81645 | + SQLITE_TEXT, /* 0x22 INTREAL + TEXT */ |
| 81646 | + SQLITE_NULL, /* 0x23 (not possible) */ |
| 81647 | + SQLITE_FLOAT, /* 0x24 (not possible) */ |
| 81648 | + SQLITE_NULL, /* 0x25 (not possible) */ |
| 81649 | + SQLITE_FLOAT, /* 0x26 (not possible) */ |
| 81650 | + SQLITE_NULL, /* 0x27 (not possible) */ |
| 81651 | + SQLITE_FLOAT, /* 0x28 (not possible) */ |
| 81652 | + SQLITE_NULL, /* 0x29 (not possible) */ |
| 81653 | + SQLITE_FLOAT, /* 0x2a (not possible) */ |
| 81654 | + SQLITE_NULL, /* 0x2b (not possible) */ |
| 81655 | + SQLITE_FLOAT, /* 0x2c (not possible) */ |
| 81656 | + SQLITE_NULL, /* 0x2d (not possible) */ |
| 81657 | + SQLITE_FLOAT, /* 0x2e (not possible) */ |
| 81658 | + SQLITE_NULL, /* 0x2f (not possible) */ |
| 81659 | + SQLITE_BLOB, /* 0x30 (not possible) */ |
| 81660 | + SQLITE_NULL, /* 0x31 (not possible) */ |
| 81661 | + SQLITE_TEXT, /* 0x32 (not possible) */ |
| 81662 | + SQLITE_NULL, /* 0x33 (not possible) */ |
| 81663 | + SQLITE_FLOAT, /* 0x34 (not possible) */ |
| 81664 | + SQLITE_NULL, /* 0x35 (not possible) */ |
| 81665 | + SQLITE_FLOAT, /* 0x36 (not possible) */ |
| 81666 | + SQLITE_NULL, /* 0x37 (not possible) */ |
| 81667 | + SQLITE_FLOAT, /* 0x38 (not possible) */ |
| 81668 | + SQLITE_NULL, /* 0x39 (not possible) */ |
| 81669 | + SQLITE_FLOAT, /* 0x3a (not possible) */ |
| 81670 | + SQLITE_NULL, /* 0x3b (not possible) */ |
| 81671 | + SQLITE_FLOAT, /* 0x3c (not possible) */ |
| 81672 | + SQLITE_NULL, /* 0x3d (not possible) */ |
| 81673 | + SQLITE_FLOAT, /* 0x3e (not possible) */ |
| 81674 | + SQLITE_NULL, /* 0x3f (not possible) */ |
| 81675 | + }; |
| 81676 | +#ifdef SQLITE_DEBUG |
| 81677 | + { |
| 81678 | + int eType = SQLITE_BLOB; |
| 81679 | + if( pVal->flags & MEM_Null ){ |
| 81680 | + eType = SQLITE_NULL; |
| 81681 | + }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ |
| 81682 | + eType = SQLITE_FLOAT; |
| 81683 | + }else if( pVal->flags & MEM_Int ){ |
| 81684 | + eType = SQLITE_INTEGER; |
| 81685 | + }else if( pVal->flags & MEM_Str ){ |
| 81686 | + eType = SQLITE_TEXT; |
| 81687 | + } |
| 81688 | + assert( eType == aType[pVal->flags&MEM_AffMask] ); |
| 81689 | + } |
| 81690 | +#endif |
| 81565 | 81691 | return aType[pVal->flags&MEM_AffMask]; |
| 81566 | 81692 | } |
| 81567 | 81693 | |
| 81568 | 81694 | /* Return true if a parameter to xUpdate represents an unchanged column */ |
| 81569 | 81695 | SQLITE_API int sqlite3_value_nochange(sqlite3_value *pVal){ |
| | @@ -81808,10 +81934,25 @@ |
| 81808 | 81934 | assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 81809 | 81935 | sqlite3VdbeMemSetNull(pCtx->pOut); |
| 81810 | 81936 | pCtx->isError = SQLITE_NOMEM_BKPT; |
| 81811 | 81937 | sqlite3OomFault(pCtx->pOut->db); |
| 81812 | 81938 | } |
| 81939 | + |
| 81940 | +#ifndef SQLITE_UNTESTABLE |
| 81941 | +/* Force the INT64 value currently stored as the result to be |
| 81942 | +** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL |
| 81943 | +** test-control. |
| 81944 | +*/ |
| 81945 | +SQLITE_PRIVATE void sqlite3ResultIntReal(sqlite3_context *pCtx){ |
| 81946 | + assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); |
| 81947 | + if( pCtx->pOut->flags & MEM_Int ){ |
| 81948 | + pCtx->pOut->flags &= ~MEM_Int; |
| 81949 | + pCtx->pOut->flags |= MEM_IntReal; |
| 81950 | + } |
| 81951 | +} |
| 81952 | +#endif |
| 81953 | + |
| 81813 | 81954 | |
| 81814 | 81955 | /* |
| 81815 | 81956 | ** This function is called after a transaction has been committed. It |
| 81816 | 81957 | ** invokes callbacks registered with sqlite3_wal_hook() as required. |
| 81817 | 81958 | */ |
| | @@ -83095,11 +83236,13 @@ |
| 83095 | 83236 | if( iIdx==p->pTab->iPKey ){ |
| 83096 | 83237 | sqlite3VdbeMemSetInt64(pMem, p->iKey1); |
| 83097 | 83238 | }else if( iIdx>=p->pUnpacked->nField ){ |
| 83098 | 83239 | *ppValue = (sqlite3_value *)columnNullValue(); |
| 83099 | 83240 | }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ |
| 83100 | | - if( pMem->flags & MEM_Int ){ |
| 83241 | + if( pMem->flags & (MEM_Int|MEM_IntReal) ){ |
| 83242 | + testcase( pMem->flags & MEM_Int ); |
| 83243 | + testcase( pMem->flags & MEM_IntReal ); |
| 83101 | 83244 | sqlite3VdbeMemRealify(pMem); |
| 83102 | 83245 | } |
| 83103 | 83246 | } |
| 83104 | 83247 | |
| 83105 | 83248 | preupdate_old_out: |
| | @@ -83414,11 +83557,11 @@ |
| 83414 | 83557 | nextIndex = idx + 1; |
| 83415 | 83558 | assert( idx>0 && idx<=p->nVar ); |
| 83416 | 83559 | pVar = &p->aVar[idx-1]; |
| 83417 | 83560 | if( pVar->flags & MEM_Null ){ |
| 83418 | 83561 | sqlite3_str_append(&out, "NULL", 4); |
| 83419 | | - }else if( pVar->flags & MEM_Int ){ |
| 83562 | + }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){ |
| 83420 | 83563 | sqlite3_str_appendf(&out, "%lld", pVar->u.i); |
| 83421 | 83564 | }else if( pVar->flags & MEM_Real ){ |
| 83422 | 83565 | sqlite3_str_appendf(&out, "%!.15g", pVar->u.r); |
| 83423 | 83566 | }else if( pVar->flags & MEM_Str ){ |
| 83424 | 83567 | int nOut; /* Number of bytes of the string text to include in output */ |
| | @@ -83676,18 +83819,10 @@ |
| 83676 | 83819 | sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, |
| 83677 | 83820 | iSrcLine&0xffffff, I, M); |
| 83678 | 83821 | } |
| 83679 | 83822 | #endif |
| 83680 | 83823 | |
| 83681 | | -/* |
| 83682 | | -** Convert the given register into a string if it isn't one |
| 83683 | | -** already. Return non-zero if a malloc() fails. |
| 83684 | | -*/ |
| 83685 | | -#define Stringify(P, enc) \ |
| 83686 | | - if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ |
| 83687 | | - { goto no_mem; } |
| 83688 | | - |
| 83689 | 83824 | /* |
| 83690 | 83825 | ** An ephemeral string value (signified by the MEM_Ephem flag) contains |
| 83691 | 83826 | ** a pointer to a dynamically allocated string where some other entity |
| 83692 | 83827 | ** is responsible for deallocating that string. Because the register |
| 83693 | 83828 | ** does not control the string, it might be deleted without the register |
| | @@ -83745,11 +83880,11 @@ |
| 83745 | 83880 | if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/ |
| 83746 | 83881 | /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag |
| 83747 | 83882 | ** is clear. Otherwise, if this is an ephemeral cursor created by |
| 83748 | 83883 | ** OP_OpenDup, the cursor will not be closed and will still be part |
| 83749 | 83884 | ** of a BtShared.pCursor list. */ |
| 83750 | | - p->apCsr[iCur]->isEphemeral = 0; |
| 83885 | + if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0; |
| 83751 | 83886 | sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); |
| 83752 | 83887 | p->apCsr[iCur] = 0; |
| 83753 | 83888 | } |
| 83754 | 83889 | if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ |
| 83755 | 83890 | p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; |
| | @@ -83784,11 +83919,11 @@ |
| 83784 | 83919 | */ |
| 83785 | 83920 | static void applyNumericAffinity(Mem *pRec, int bTryForInt){ |
| 83786 | 83921 | double rValue; |
| 83787 | 83922 | i64 iValue; |
| 83788 | 83923 | u8 enc = pRec->enc; |
| 83789 | | - assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); |
| 83924 | + assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str ); |
| 83790 | 83925 | if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; |
| 83791 | 83926 | if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ |
| 83792 | 83927 | pRec->u.i = iValue; |
| 83793 | 83928 | pRec->flags |= MEM_Int; |
| 83794 | 83929 | }else{ |
| | @@ -83841,15 +83976,18 @@ |
| 83841 | 83976 | ** representation (blob and NULL do not get converted) but no string |
| 83842 | 83977 | ** representation. It would be harmless to repeat the conversion if |
| 83843 | 83978 | ** there is already a string rep, but it is pointless to waste those |
| 83844 | 83979 | ** CPU cycles. */ |
| 83845 | 83980 | if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/ |
| 83846 | | - if( (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 83981 | + if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){ |
| 83982 | + testcase( pRec->flags & MEM_Int ); |
| 83983 | + testcase( pRec->flags & MEM_Real ); |
| 83984 | + testcase( pRec->flags & MEM_IntReal ); |
| 83847 | 83985 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 83848 | 83986 | } |
| 83849 | 83987 | } |
| 83850 | | - pRec->flags &= ~(MEM_Real|MEM_Int); |
| 83988 | + pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal); |
| 83851 | 83989 | } |
| 83852 | 83990 | } |
| 83853 | 83991 | |
| 83854 | 83992 | /* |
| 83855 | 83993 | ** Try to convert the type of a function argument or a result column |
| | @@ -83884,11 +84022,11 @@ |
| 83884 | 84022 | ** interpret as a string if we want to). Compute its corresponding |
| 83885 | 84023 | ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields |
| 83886 | 84024 | ** accordingly. |
| 83887 | 84025 | */ |
| 83888 | 84026 | static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ |
| 83889 | | - assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); |
| 84027 | + assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ); |
| 83890 | 84028 | assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); |
| 83891 | 84029 | ExpandBlob(pMem); |
| 83892 | 84030 | if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ |
| 83893 | 84031 | return 0; |
| 83894 | 84032 | } |
| | @@ -83904,14 +84042,19 @@ |
| 83904 | 84042 | ** |
| 83905 | 84043 | ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. |
| 83906 | 84044 | ** But it does set pMem->u.r and pMem->u.i appropriately. |
| 83907 | 84045 | */ |
| 83908 | 84046 | static u16 numericType(Mem *pMem){ |
| 83909 | | - if( pMem->flags & (MEM_Int|MEM_Real) ){ |
| 83910 | | - return pMem->flags & (MEM_Int|MEM_Real); |
| 84047 | + if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){ |
| 84048 | + testcase( pMem->flags & MEM_Int ); |
| 84049 | + testcase( pMem->flags & MEM_Real ); |
| 84050 | + testcase( pMem->flags & MEM_IntReal ); |
| 84051 | + return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal); |
| 83911 | 84052 | } |
| 83912 | 84053 | if( pMem->flags & (MEM_Str|MEM_Blob) ){ |
| 84054 | + testcase( pMem->flags & MEM_Str ); |
| 84055 | + testcase( pMem->flags & MEM_Blob ); |
| 83913 | 84056 | return computeNumericType(pMem); |
| 83914 | 84057 | } |
| 83915 | 84058 | return 0; |
| 83916 | 84059 | } |
| 83917 | 84060 | |
| | @@ -84003,10 +84146,12 @@ |
| 84003 | 84146 | printf(" undefined"); |
| 84004 | 84147 | }else if( p->flags & MEM_Null ){ |
| 84005 | 84148 | printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL"); |
| 84006 | 84149 | }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ |
| 84007 | 84150 | printf(" si:%lld", p->u.i); |
| 84151 | + }else if( (p->flags & (MEM_IntReal))!=0 ){ |
| 84152 | + printf(" ir:%lld", p->u.i); |
| 84008 | 84153 | }else if( p->flags & MEM_Int ){ |
| 84009 | 84154 | printf(" i:%lld", p->u.i); |
| 84010 | 84155 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 84011 | 84156 | }else if( p->flags & MEM_Real ){ |
| 84012 | 84157 | printf(" r:%g", p->u.r); |
| | @@ -85033,23 +85178,42 @@ |
| 85033 | 85178 | ** It is illegal for P1 and P3 to be the same register. Sometimes, |
| 85034 | 85179 | ** if P3 is the same register as P2, the implementation is able |
| 85035 | 85180 | ** to avoid a memcpy(). |
| 85036 | 85181 | */ |
| 85037 | 85182 | case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ |
| 85038 | | - i64 nByte; |
| 85183 | + i64 nByte; /* Total size of the output string or blob */ |
| 85184 | + u16 flags1; /* Initial flags for P1 */ |
| 85185 | + u16 flags2; /* Initial flags for P2 */ |
| 85039 | 85186 | |
| 85040 | 85187 | pIn1 = &aMem[pOp->p1]; |
| 85041 | 85188 | pIn2 = &aMem[pOp->p2]; |
| 85042 | 85189 | pOut = &aMem[pOp->p3]; |
| 85190 | + testcase( pIn1==pIn2 ); |
| 85191 | + testcase( pOut==pIn2 ); |
| 85043 | 85192 | assert( pIn1!=pOut ); |
| 85044 | | - if( (pIn1->flags | pIn2->flags) & MEM_Null ){ |
| 85193 | + flags1 = pIn1->flags; |
| 85194 | + testcase( flags1 & MEM_Null ); |
| 85195 | + testcase( pIn2->flags & MEM_Null ); |
| 85196 | + if( (flags1 | pIn2->flags) & MEM_Null ){ |
| 85045 | 85197 | sqlite3VdbeMemSetNull(pOut); |
| 85046 | 85198 | break; |
| 85047 | 85199 | } |
| 85048 | | - if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; |
| 85049 | | - Stringify(pIn1, encoding); |
| 85050 | | - Stringify(pIn2, encoding); |
| 85200 | + if( (flags1 & (MEM_Str|MEM_Blob))==0 ){ |
| 85201 | + if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem; |
| 85202 | + flags1 = pIn1->flags & ~MEM_Str; |
| 85203 | + }else if( (flags1 & MEM_Zero)!=0 ){ |
| 85204 | + if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem; |
| 85205 | + flags1 = pIn1->flags & ~MEM_Str; |
| 85206 | + } |
| 85207 | + flags2 = pIn2->flags; |
| 85208 | + if( (flags2 & (MEM_Str|MEM_Blob))==0 ){ |
| 85209 | + if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem; |
| 85210 | + flags2 = pIn2->flags & ~MEM_Str; |
| 85211 | + }else if( (flags2 & MEM_Zero)!=0 ){ |
| 85212 | + if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem; |
| 85213 | + flags2 = pIn2->flags & ~MEM_Str; |
| 85214 | + } |
| 85051 | 85215 | nByte = pIn1->n + pIn2->n; |
| 85052 | 85216 | if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 85053 | 85217 | goto too_big; |
| 85054 | 85218 | } |
| 85055 | 85219 | if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ |
| | @@ -85056,12 +85220,16 @@ |
| 85056 | 85220 | goto no_mem; |
| 85057 | 85221 | } |
| 85058 | 85222 | MemSetTypeFlag(pOut, MEM_Str); |
| 85059 | 85223 | if( pOut!=pIn2 ){ |
| 85060 | 85224 | memcpy(pOut->z, pIn2->z, pIn2->n); |
| 85225 | + assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) ); |
| 85226 | + pIn2->flags = flags2; |
| 85061 | 85227 | } |
| 85062 | 85228 | memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); |
| 85229 | + assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 85230 | + pIn1->flags = flags1; |
| 85063 | 85231 | pOut->z[nByte]=0; |
| 85064 | 85232 | pOut->z[nByte+1] = 0; |
| 85065 | 85233 | pOut->flags |= MEM_Term; |
| 85066 | 85234 | pOut->n = (int)nByte; |
| 85067 | 85235 | pOut->enc = encoding; |
| | @@ -85183,11 +85351,11 @@ |
| 85183 | 85351 | if( sqlite3IsNaN(rB) ){ |
| 85184 | 85352 | goto arithmetic_result_is_null; |
| 85185 | 85353 | } |
| 85186 | 85354 | pOut->u.r = rB; |
| 85187 | 85355 | MemSetTypeFlag(pOut, MEM_Real); |
| 85188 | | - if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ |
| 85356 | + if( ((type1|type2)&(MEM_Real|MEM_IntReal))==0 && !bIntint ){ |
| 85189 | 85357 | sqlite3VdbeIntegerAffinity(pOut); |
| 85190 | 85358 | } |
| 85191 | 85359 | #endif |
| 85192 | 85360 | } |
| 85193 | 85361 | break; |
| | @@ -85354,11 +85522,13 @@ |
| 85354 | 85522 | ** integers, for space efficiency, but after extraction we want them |
| 85355 | 85523 | ** to have only a real value. |
| 85356 | 85524 | */ |
| 85357 | 85525 | case OP_RealAffinity: { /* in1 */ |
| 85358 | 85526 | pIn1 = &aMem[pOp->p1]; |
| 85359 | | - if( pIn1->flags & MEM_Int ){ |
| 85527 | + if( pIn1->flags & (MEM_Int|MEM_IntReal) ){ |
| 85528 | + testcase( pIn1->flags & MEM_Int ); |
| 85529 | + testcase( pIn1->flags & MEM_IntReal ); |
| 85360 | 85530 | sqlite3VdbeMemRealify(pIn1); |
| 85361 | 85531 | } |
| 85362 | 85532 | break; |
| 85363 | 85533 | } |
| 85364 | 85534 | #endif |
| | @@ -85546,21 +85716,21 @@ |
| 85546 | 85716 | }else{ |
| 85547 | 85717 | /* Neither operand is NULL. Do a comparison. */ |
| 85548 | 85718 | affinity = pOp->p5 & SQLITE_AFF_MASK; |
| 85549 | 85719 | if( affinity>=SQLITE_AFF_NUMERIC ){ |
| 85550 | 85720 | if( (flags1 | flags3)&MEM_Str ){ |
| 85551 | | - if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 85721 | + if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
| 85552 | 85722 | applyNumericAffinity(pIn1,0); |
| 85553 | 85723 | assert( flags3==pIn3->flags ); |
| 85554 | 85724 | /* testcase( flags3!=pIn3->flags ); |
| 85555 | 85725 | ** this used to be possible with pIn1==pIn3, but not since |
| 85556 | 85726 | ** the column cache was removed. The following assignment |
| 85557 | 85727 | ** is essentially a no-op. But, it provides defense-in-depth |
| 85558 | 85728 | ** in case our analysis is incorrect, so it is left in. */ |
| 85559 | 85729 | flags3 = pIn3->flags; |
| 85560 | 85730 | } |
| 85561 | | - if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 85731 | + if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){ |
| 85562 | 85732 | applyNumericAffinity(pIn3,0); |
| 85563 | 85733 | } |
| 85564 | 85734 | } |
| 85565 | 85735 | /* Handle the common case of integer comparison here, as an |
| 85566 | 85736 | ** optimization, to avoid a call to sqlite3MemCompare() */ |
| | @@ -85569,21 +85739,23 @@ |
| 85569 | 85739 | if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; } |
| 85570 | 85740 | res = 0; |
| 85571 | 85741 | goto compare_op; |
| 85572 | 85742 | } |
| 85573 | 85743 | }else if( affinity==SQLITE_AFF_TEXT ){ |
| 85574 | | - if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){ |
| 85744 | + if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
| 85575 | 85745 | testcase( pIn1->flags & MEM_Int ); |
| 85576 | 85746 | testcase( pIn1->flags & MEM_Real ); |
| 85747 | + testcase( pIn1->flags & MEM_IntReal ); |
| 85577 | 85748 | sqlite3VdbeMemStringify(pIn1, encoding, 1); |
| 85578 | 85749 | testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) ); |
| 85579 | 85750 | flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask); |
| 85580 | 85751 | assert( pIn1!=pIn3 ); |
| 85581 | 85752 | } |
| 85582 | | - if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){ |
| 85753 | + if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){ |
| 85583 | 85754 | testcase( pIn3->flags & MEM_Int ); |
| 85584 | 85755 | testcase( pIn3->flags & MEM_Real ); |
| 85756 | + testcase( pIn3->flags & MEM_IntReal ); |
| 85585 | 85757 | sqlite3VdbeMemStringify(pIn3, encoding, 1); |
| 85586 | 85758 | testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) ); |
| 85587 | 85759 | flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask); |
| 85588 | 85760 | } |
| 85589 | 85761 | } |
| | @@ -86335,16 +86507,25 @@ |
| 86335 | 86507 | zAffinity = pOp->p4.z; |
| 86336 | 86508 | assert( zAffinity!=0 ); |
| 86337 | 86509 | assert( pOp->p2>0 ); |
| 86338 | 86510 | assert( zAffinity[pOp->p2]==0 ); |
| 86339 | 86511 | pIn1 = &aMem[pOp->p1]; |
| 86340 | | - do{ |
| 86512 | + while( 1 /*edit-by-break*/ ){ |
| 86341 | 86513 | assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] ); |
| 86342 | 86514 | assert( memIsValid(pIn1) ); |
| 86343 | | - applyAffinity(pIn1, *(zAffinity++), encoding); |
| 86515 | + applyAffinity(pIn1, zAffinity[0], encoding); |
| 86516 | + if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){ |
| 86517 | + /* When applying REAL affinity, if the result is still MEM_Int, |
| 86518 | + ** indicate that REAL is actually desired */ |
| 86519 | + pIn1->flags |= MEM_IntReal; |
| 86520 | + pIn1->flags &= ~MEM_Int; |
| 86521 | + } |
| 86522 | + REGISTER_TRACE((int)(pIn1-aMem), pIn1); |
| 86523 | + zAffinity++; |
| 86524 | + if( zAffinity[0]==0 ) break; |
| 86344 | 86525 | pIn1++; |
| 86345 | | - }while( zAffinity[0] ); |
| 86526 | + } |
| 86346 | 86527 | break; |
| 86347 | 86528 | } |
| 86348 | 86529 | |
| 86349 | 86530 | /* Opcode: MakeRecord P1 P2 P3 P4 * |
| 86350 | 86531 | ** Synopsis: r[P3]=mkrec(r[P1@P2]) |
| | @@ -86361,11 +86542,10 @@ |
| 86361 | 86542 | ** macros defined in sqliteInt.h. |
| 86362 | 86543 | ** |
| 86363 | 86544 | ** If P4 is NULL then all index fields have the affinity BLOB. |
| 86364 | 86545 | */ |
| 86365 | 86546 | case OP_MakeRecord: { |
| 86366 | | - u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 86367 | 86547 | Mem *pRec; /* The new record */ |
| 86368 | 86548 | u64 nData; /* Number of bytes of data space */ |
| 86369 | 86549 | int nHdr; /* Number of bytes of header space */ |
| 86370 | 86550 | i64 nByte; /* Data space required for this record */ |
| 86371 | 86551 | i64 nZero; /* Number of zero bytes at the end of the record */ |
| | @@ -86374,13 +86554,13 @@ |
| 86374 | 86554 | Mem *pData0; /* First field to be combined into the record */ |
| 86375 | 86555 | Mem *pLast; /* Last field of the record */ |
| 86376 | 86556 | int nField; /* Number of fields in the record */ |
| 86377 | 86557 | char *zAffinity; /* The affinity string for the record */ |
| 86378 | 86558 | int file_format; /* File format to use for encoding */ |
| 86379 | | - int i; /* Space used in zNewRecord[] header */ |
| 86380 | | - int j; /* Space used in zNewRecord[] content */ |
| 86381 | 86559 | u32 len; /* Length of a field */ |
| 86560 | + u8 *zHdr; /* Where to write next byte of the header */ |
| 86561 | + u8 *zPayload; /* Where to write next byte of the payload */ |
| 86382 | 86562 | |
| 86383 | 86563 | /* Assuming the record contains N fields, the record format looks |
| 86384 | 86564 | ** like this: |
| 86385 | 86565 | ** |
| 86386 | 86566 | ** ------------------------------------------------------------------------ |
| | @@ -86415,11 +86595,14 @@ |
| 86415 | 86595 | */ |
| 86416 | 86596 | assert( pData0<=pLast ); |
| 86417 | 86597 | if( zAffinity ){ |
| 86418 | 86598 | pRec = pData0; |
| 86419 | 86599 | do{ |
| 86420 | | - applyAffinity(pRec++, *(zAffinity++), encoding); |
| 86600 | + applyAffinity(pRec, zAffinity[0], encoding); |
| 86601 | + REGISTER_TRACE((int)(pRec-aMem), pRec); |
| 86602 | + zAffinity++; |
| 86603 | + pRec++; |
| 86421 | 86604 | assert( zAffinity[0]==0 || pRec<=pLast ); |
| 86422 | 86605 | }while( zAffinity[0] ); |
| 86423 | 86606 | } |
| 86424 | 86607 | |
| 86425 | 86608 | #ifdef SQLITE_ENABLE_NULL_TRIM |
| | @@ -86503,38 +86686,38 @@ |
| 86503 | 86686 | } |
| 86504 | 86687 | if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ |
| 86505 | 86688 | goto no_mem; |
| 86506 | 86689 | } |
| 86507 | 86690 | } |
| 86508 | | - zNewRecord = (u8 *)pOut->z; |
| 86691 | + pOut->n = (int)nByte; |
| 86692 | + pOut->flags = MEM_Blob; |
| 86693 | + if( nZero ){ |
| 86694 | + pOut->u.nZero = nZero; |
| 86695 | + pOut->flags |= MEM_Zero; |
| 86696 | + } |
| 86697 | + UPDATE_MAX_BLOBSIZE(pOut); |
| 86698 | + zHdr = (u8 *)pOut->z; |
| 86699 | + zPayload = zHdr + nHdr; |
| 86509 | 86700 | |
| 86510 | 86701 | /* Write the record */ |
| 86511 | | - i = putVarint32(zNewRecord, nHdr); |
| 86512 | | - j = nHdr; |
| 86702 | + zHdr += putVarint32(zHdr, nHdr); |
| 86513 | 86703 | assert( pData0<=pLast ); |
| 86514 | 86704 | pRec = pData0; |
| 86515 | 86705 | do{ |
| 86516 | 86706 | serial_type = pRec->uTemp; |
| 86517 | 86707 | /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more |
| 86518 | 86708 | ** additional varints, one per column. */ |
| 86519 | | - i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ |
| 86709 | + zHdr += putVarint32(zHdr, serial_type); /* serial type */ |
| 86520 | 86710 | /* EVIDENCE-OF: R-64536-51728 The values for each column in the record |
| 86521 | 86711 | ** immediately follow the header. */ |
| 86522 | | - j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ |
| 86712 | + zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */ |
| 86523 | 86713 | }while( (++pRec)<=pLast ); |
| 86524 | | - assert( i==nHdr ); |
| 86525 | | - assert( j==nByte ); |
| 86714 | + assert( nHdr==(int)(zHdr - (u8*)pOut->z) ); |
| 86715 | + assert( nByte==(int)(zPayload - (u8*)pOut->z) ); |
| 86526 | 86716 | |
| 86527 | 86717 | assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) ); |
| 86528 | | - pOut->n = (int)nByte; |
| 86529 | | - pOut->flags = MEM_Blob; |
| 86530 | | - if( nZero ){ |
| 86531 | | - pOut->u.nZero = nZero; |
| 86532 | | - pOut->flags |= MEM_Zero; |
| 86533 | | - } |
| 86534 | 86718 | REGISTER_TRACE(pOp->p3, pOut); |
| 86535 | | - UPDATE_MAX_BLOBSIZE(pOut); |
| 86536 | 86719 | break; |
| 86537 | 86720 | } |
| 86538 | 86721 | |
| 86539 | 86722 | /* Opcode: Count P1 P2 * * * |
| 86540 | 86723 | ** Synopsis: r[P2]=count() |
| | @@ -86560,12 +86743,13 @@ |
| 86560 | 86743 | #endif |
| 86561 | 86744 | |
| 86562 | 86745 | /* Opcode: Savepoint P1 * * P4 * |
| 86563 | 86746 | ** |
| 86564 | 86747 | ** Open, release or rollback the savepoint named by parameter P4, depending |
| 86565 | | -** on the value of P1. To open a new savepoint, P1==0. To release (commit) an |
| 86566 | | -** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. |
| 86748 | +** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN). |
| 86749 | +** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE). |
| 86750 | +** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK). |
| 86567 | 86751 | */ |
| 86568 | 86752 | case OP_Savepoint: { |
| 86569 | 86753 | int p1; /* Value of P1 operand */ |
| 86570 | 86754 | char *zName; /* Name of savepoint */ |
| 86571 | 86755 | int nName; |
| | @@ -86629,10 +86813,11 @@ |
| 86629 | 86813 | pNew->nDeferredCons = db->nDeferredCons; |
| 86630 | 86814 | pNew->nDeferredImmCons = db->nDeferredImmCons; |
| 86631 | 86815 | } |
| 86632 | 86816 | } |
| 86633 | 86817 | }else{ |
| 86818 | + assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK ); |
| 86634 | 86819 | iSavepoint = 0; |
| 86635 | 86820 | |
| 86636 | 86821 | /* Find the named savepoint. If there is no such savepoint, then an |
| 86637 | 86822 | ** an error is returned to the user. */ |
| 86638 | 86823 | for( |
| | @@ -86682,10 +86867,11 @@ |
| 86682 | 86867 | SQLITE_ABORT_ROLLBACK, |
| 86683 | 86868 | isSchemaChange==0); |
| 86684 | 86869 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 86685 | 86870 | } |
| 86686 | 86871 | }else{ |
| 86872 | + assert( p1==SAVEPOINT_RELEASE ); |
| 86687 | 86873 | isSchemaChange = 0; |
| 86688 | 86874 | } |
| 86689 | 86875 | for(ii=0; ii<db->nDb; ii++){ |
| 86690 | 86876 | rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); |
| 86691 | 86877 | if( rc!=SQLITE_OK ){ |
| | @@ -86718,10 +86904,11 @@ |
| 86718 | 86904 | sqlite3DbFree(db, pSavepoint); |
| 86719 | 86905 | if( !isTransaction ){ |
| 86720 | 86906 | db->nSavepoint--; |
| 86721 | 86907 | } |
| 86722 | 86908 | }else{ |
| 86909 | + assert( p1==SAVEPOINT_ROLLBACK ); |
| 86723 | 86910 | db->nDeferredCons = pSavepoint->nDeferredCons; |
| 86724 | 86911 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
| 86725 | 86912 | } |
| 86726 | 86913 | |
| 86727 | 86914 | if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
| | @@ -87256,11 +87443,14 @@ |
| 87256 | 87443 | assert( pOp->p2>=0 ); |
| 87257 | 87444 | pCx = p->apCsr[pOp->p1]; |
| 87258 | 87445 | if( pCx ){ |
| 87259 | 87446 | /* If the ephermeral table is already open, erase all existing content |
| 87260 | 87447 | ** so that the table is empty again, rather than creating a new table. */ |
| 87261 | | - rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0); |
| 87448 | + assert( pCx->isEphemeral ); |
| 87449 | + if( pCx->pBtx ){ |
| 87450 | + rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0); |
| 87451 | + } |
| 87262 | 87452 | }else{ |
| 87263 | 87453 | pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE); |
| 87264 | 87454 | if( pCx==0 ) goto no_mem; |
| 87265 | 87455 | pCx->nullRow = 1; |
| 87266 | 87456 | pCx->isEphemeral = 1; |
| | @@ -87533,24 +87723,28 @@ |
| 87533 | 87723 | |
| 87534 | 87724 | /* The input value in P3 might be of any type: integer, real, string, |
| 87535 | 87725 | ** blob, or NULL. But it needs to be an integer before we can do |
| 87536 | 87726 | ** the seek, so convert it. */ |
| 87537 | 87727 | pIn3 = &aMem[pOp->p3]; |
| 87538 | | - if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ |
| 87728 | + if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){ |
| 87539 | 87729 | applyNumericAffinity(pIn3, 0); |
| 87540 | 87730 | } |
| 87541 | 87731 | iKey = sqlite3VdbeIntValue(pIn3); |
| 87542 | 87732 | |
| 87543 | 87733 | /* If the P3 value could not be converted into an integer without |
| 87544 | 87734 | ** loss of information, then special processing is required... */ |
| 87545 | | - if( (pIn3->flags & MEM_Int)==0 ){ |
| 87735 | + if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ |
| 87546 | 87736 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 87547 | | - /* If the P3 value cannot be converted into any kind of a number, |
| 87548 | | - ** then the seek is not possible, so jump to P2 */ |
| 87549 | | - VdbeBranchTaken(1,2); goto jump_to_p2; |
| 87550 | | - break; |
| 87551 | | - } |
| 87737 | + if( (pIn3->flags & MEM_Null) || oc>=OP_SeekGE ){ |
| 87738 | + VdbeBranchTaken(1,2); goto jump_to_p2; |
| 87739 | + break; |
| 87740 | + }else{ |
| 87741 | + rc = sqlite3BtreeLast(pC->uc.pCursor, &res); |
| 87742 | + if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 87743 | + goto seek_not_found; |
| 87744 | + } |
| 87745 | + }else |
| 87552 | 87746 | |
| 87553 | 87747 | /* If the approximation iKey is larger than the actual real search |
| 87554 | 87748 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| 87555 | 87749 | ** is 4.9 and the integer approximation 5: |
| 87556 | 87750 | ** |
| | @@ -87570,11 +87764,11 @@ |
| 87570 | 87764 | assert( OP_SeekLE==(OP_SeekLT+1) ); |
| 87571 | 87765 | assert( OP_SeekGT==(OP_SeekGE+1) ); |
| 87572 | 87766 | assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); |
| 87573 | 87767 | if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; |
| 87574 | 87768 | } |
| 87575 | | - } |
| 87769 | + } |
| 87576 | 87770 | rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res); |
| 87577 | 87771 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
| 87578 | 87772 | if( rc!=SQLITE_OK ){ |
| 87579 | 87773 | goto abort_due_to_error; |
| 87580 | 87774 | } |
| | @@ -87925,11 +88119,13 @@ |
| 87925 | 88119 | BtCursor *pCrsr; |
| 87926 | 88120 | int res; |
| 87927 | 88121 | u64 iKey; |
| 87928 | 88122 | |
| 87929 | 88123 | pIn3 = &aMem[pOp->p3]; |
| 87930 | | - if( (pIn3->flags & MEM_Int)==0 ){ |
| 88124 | + testcase( pIn3->flags & MEM_Int ); |
| 88125 | + testcase( pIn3->flags & MEM_IntReal ); |
| 88126 | + if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){ |
| 87931 | 88127 | /* Make sure pIn3->u.i contains a valid integer representation of |
| 87932 | 88128 | ** the key value, but do not change the datatype of the register, as |
| 87933 | 88129 | ** other parts of the perpared statement might be depending on the |
| 87934 | 88130 | ** current datatype. */ |
| 87935 | 88131 | u16 origFlags = pIn3->flags; |
| | @@ -95977,11 +96173,13 @@ |
| 95977 | 96173 | sqlite3WalkExprList(pWalker, pList); |
| 95978 | 96174 | if( is_agg ){ |
| 95979 | 96175 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 95980 | 96176 | if( pExpr->y.pWin ){ |
| 95981 | 96177 | Select *pSel = pNC->pWinSelect; |
| 95982 | | - sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); |
| 96178 | + if( IN_RENAME_OBJECT==0 ){ |
| 96179 | + sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef); |
| 96180 | + } |
| 95983 | 96181 | sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition); |
| 95984 | 96182 | sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy); |
| 95985 | 96183 | sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter); |
| 95986 | 96184 | if( 0==pSel->pWin |
| 95987 | 96185 | || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) |
| | @@ -97660,11 +97858,11 @@ |
| 97660 | 97858 | memset(pNew, 0, sizeof(Expr)); |
| 97661 | 97859 | pNew->op = (u8)op; |
| 97662 | 97860 | pNew->iAgg = -1; |
| 97663 | 97861 | if( pToken ){ |
| 97664 | 97862 | if( nExtra==0 ){ |
| 97665 | | - pNew->flags |= EP_IntValue|EP_Leaf; |
| 97863 | + pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse); |
| 97666 | 97864 | pNew->u.iValue = iValue; |
| 97667 | 97865 | }else{ |
| 97668 | 97866 | pNew->u.zToken = (char*)&pNew[1]; |
| 97669 | 97867 | assert( pToken->z!=0 || pToken->n==0 ); |
| 97670 | 97868 | if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); |
| | @@ -97737,24 +97935,20 @@ |
| 97737 | 97935 | int op, /* Expression opcode */ |
| 97738 | 97936 | Expr *pLeft, /* Left operand */ |
| 97739 | 97937 | Expr *pRight /* Right operand */ |
| 97740 | 97938 | ){ |
| 97741 | 97939 | Expr *p; |
| 97742 | | - if( op==TK_AND && pParse->nErr==0 && !IN_RENAME_OBJECT ){ |
| 97743 | | - /* Take advantage of short-circuit false optimization for AND */ |
| 97744 | | - p = sqlite3ExprAnd(pParse->db, pLeft, pRight); |
| 97745 | | - }else{ |
| 97746 | | - p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); |
| 97747 | | - if( p ){ |
| 97748 | | - memset(p, 0, sizeof(Expr)); |
| 97749 | | - p->op = op & 0xff; |
| 97750 | | - p->iAgg = -1; |
| 97751 | | - } |
| 97940 | + p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); |
| 97941 | + if( p ){ |
| 97942 | + memset(p, 0, sizeof(Expr)); |
| 97943 | + p->op = op & 0xff; |
| 97944 | + p->iAgg = -1; |
| 97752 | 97945 | sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); |
| 97753 | | - } |
| 97754 | | - if( p ) { |
| 97755 | 97946 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
| 97947 | + }else{ |
| 97948 | + sqlite3ExprDelete(pParse->db, pLeft); |
| 97949 | + sqlite3ExprDelete(pParse->db, pRight); |
| 97756 | 97950 | } |
| 97757 | 97951 | return p; |
| 97758 | 97952 | } |
| 97759 | 97953 | |
| 97760 | 97954 | /* |
| | @@ -97771,58 +97965,32 @@ |
| 97771 | 97965 | sqlite3SelectDelete(pParse->db, pSelect); |
| 97772 | 97966 | } |
| 97773 | 97967 | } |
| 97774 | 97968 | |
| 97775 | 97969 | |
| 97776 | | -/* |
| 97777 | | -** If the expression is always either TRUE or FALSE (respectively), |
| 97778 | | -** then return 1. If one cannot determine the truth value of the |
| 97779 | | -** expression at compile-time return 0. |
| 97780 | | -** |
| 97781 | | -** This is an optimization. If is OK to return 0 here even if |
| 97782 | | -** the expression really is always false or false (a false negative). |
| 97783 | | -** But it is a bug to return 1 if the expression might have different |
| 97784 | | -** boolean values in different circumstances (a false positive.) |
| 97785 | | -** |
| 97786 | | -** Note that if the expression is part of conditional for a |
| 97787 | | -** LEFT JOIN, then we cannot determine at compile-time whether or not |
| 97788 | | -** is it true or false, so always return 0. |
| 97789 | | -*/ |
| 97790 | | -static int exprAlwaysTrue(Expr *p){ |
| 97791 | | - int v = 0; |
| 97792 | | - if( ExprHasProperty(p, EP_FromJoin) ) return 0; |
| 97793 | | - if( !sqlite3ExprIsInteger(p, &v) ) return 0; |
| 97794 | | - return v!=0; |
| 97795 | | -} |
| 97796 | | -static int exprAlwaysFalse(Expr *p){ |
| 97797 | | - int v = 0; |
| 97798 | | - if( ExprHasProperty(p, EP_FromJoin) ) return 0; |
| 97799 | | - if( !sqlite3ExprIsInteger(p, &v) ) return 0; |
| 97800 | | - return v==0; |
| 97801 | | -} |
| 97802 | | - |
| 97803 | 97970 | /* |
| 97804 | 97971 | ** Join two expressions using an AND operator. If either expression is |
| 97805 | 97972 | ** NULL, then just return the other expression. |
| 97806 | 97973 | ** |
| 97807 | 97974 | ** If one side or the other of the AND is known to be false, then instead |
| 97808 | 97975 | ** of returning an AND expression, just return a constant expression with |
| 97809 | 97976 | ** a value of false. |
| 97810 | 97977 | */ |
| 97811 | | -SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ |
| 97812 | | - if( pLeft==0 ){ |
| 97978 | +SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){ |
| 97979 | + sqlite3 *db = pParse->db; |
| 97980 | + if( pLeft==0 ){ |
| 97813 | 97981 | return pRight; |
| 97814 | 97982 | }else if( pRight==0 ){ |
| 97815 | 97983 | return pLeft; |
| 97816 | | - }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ |
| 97984 | + }else if( pParse->nErr || IN_RENAME_OBJECT ){ |
| 97985 | + return sqlite3PExpr(pParse, TK_AND, pLeft, pRight); |
| 97986 | + }else if( ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight) ){ |
| 97817 | 97987 | sqlite3ExprDelete(db, pLeft); |
| 97818 | 97988 | sqlite3ExprDelete(db, pRight); |
| 97819 | 97989 | return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); |
| 97820 | 97990 | }else{ |
| 97821 | | - Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); |
| 97822 | | - sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); |
| 97823 | | - return pNew; |
| 97991 | + return sqlite3PExpr(pParse, TK_AND, pLeft, pRight); |
| 97824 | 97992 | } |
| 97825 | 97993 | } |
| 97826 | 97994 | |
| 97827 | 97995 | /* |
| 97828 | 97996 | ** Construct a new expression node for a function with multiple |
| | @@ -98708,10 +98876,11 @@ |
| 98708 | 98876 | if( !ExprHasProperty(pExpr, EP_Quoted) |
| 98709 | 98877 | && (sqlite3StrICmp(pExpr->u.zToken, "true")==0 |
| 98710 | 98878 | || sqlite3StrICmp(pExpr->u.zToken, "false")==0) |
| 98711 | 98879 | ){ |
| 98712 | 98880 | pExpr->op = TK_TRUEFALSE; |
| 98881 | + ExprSetProperty(pExpr, pExpr->u.zToken[4]==0 ? EP_IsTrue : EP_IsFalse); |
| 98713 | 98882 | return 1; |
| 98714 | 98883 | } |
| 98715 | 98884 | return 0; |
| 98716 | 98885 | } |
| 98717 | 98886 | |
| | @@ -98723,10 +98892,37 @@ |
| 98723 | 98892 | assert( pExpr->op==TK_TRUEFALSE ); |
| 98724 | 98893 | assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0 |
| 98725 | 98894 | || sqlite3StrICmp(pExpr->u.zToken,"false")==0 ); |
| 98726 | 98895 | return pExpr->u.zToken[4]==0; |
| 98727 | 98896 | } |
| 98897 | + |
| 98898 | +/* |
| 98899 | +** If pExpr is an AND or OR expression, try to simplify it by eliminating |
| 98900 | +** terms that are always true or false. Return the simplified expression. |
| 98901 | +** Or return the original expression if no simplification is possible. |
| 98902 | +** |
| 98903 | +** Examples: |
| 98904 | +** |
| 98905 | +** (x<10) AND true => (x<10) |
| 98906 | +** (x<10) AND false => false |
| 98907 | +** (x<10) AND (y=22 OR false) => (x<10) AND (y=22) |
| 98908 | +** (x<10) AND (y=22 OR true) => (x<10) |
| 98909 | +** (y=22) OR true => true |
| 98910 | +*/ |
| 98911 | +SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){ |
| 98912 | + assert( pExpr!=0 ); |
| 98913 | + if( pExpr->op==TK_AND || pExpr->op==TK_OR ){ |
| 98914 | + Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight); |
| 98915 | + Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft); |
| 98916 | + if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){ |
| 98917 | + pExpr = pExpr->op==TK_AND ? pRight : pLeft; |
| 98918 | + }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){ |
| 98919 | + pExpr = pExpr->op==TK_AND ? pLeft : pRight; |
| 98920 | + } |
| 98921 | + } |
| 98922 | + return pExpr; |
| 98923 | +} |
| 98728 | 98924 | |
| 98729 | 98925 | |
| 98730 | 98926 | /* |
| 98731 | 98927 | ** These routines are Walker callbacks used to check expressions to |
| 98732 | 98928 | ** see if they are "constant" for some definition of constant. The |
| | @@ -98968,11 +99164,11 @@ |
| 98968 | 99164 | ** in *pValue. If the expression is not an integer or if it is too big |
| 98969 | 99165 | ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
| 98970 | 99166 | */ |
| 98971 | 99167 | SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
| 98972 | 99168 | int rc = 0; |
| 98973 | | - if( p==0 ) return 0; /* Can only happen following on OOM */ |
| 99169 | + if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */ |
| 98974 | 99170 | |
| 98975 | 99171 | /* If an expression is an integer literal that fits in a signed 32-bit |
| 98976 | 99172 | ** integer, then the EP_IntValue flag will have already been set */ |
| 98977 | 99173 | assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 |
| 98978 | 99174 | || sqlite3GetInt32(p->u.zToken, &rc)==0 ); |
| | @@ -101315,22 +101511,27 @@ |
| 101315 | 101511 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
| 101316 | 101512 | if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
| 101317 | 101513 | if( NEVER(pExpr==0) ) return; /* No way this can happen */ |
| 101318 | 101514 | op = pExpr->op; |
| 101319 | 101515 | switch( op ){ |
| 101320 | | - case TK_AND: { |
| 101321 | | - int d2 = sqlite3VdbeMakeLabel(pParse); |
| 101322 | | - testcase( jumpIfNull==0 ); |
| 101323 | | - sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); |
| 101324 | | - sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101325 | | - sqlite3VdbeResolveLabel(v, d2); |
| 101326 | | - break; |
| 101327 | | - } |
| 101516 | + case TK_AND: |
| 101328 | 101517 | case TK_OR: { |
| 101329 | | - testcase( jumpIfNull==0 ); |
| 101330 | | - sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 101331 | | - sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101518 | + Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); |
| 101519 | + if( pAlt!=pExpr ){ |
| 101520 | + sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull); |
| 101521 | + }else if( op==TK_AND ){ |
| 101522 | + int d2 = sqlite3VdbeMakeLabel(pParse); |
| 101523 | + testcase( jumpIfNull==0 ); |
| 101524 | + sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, |
| 101525 | + jumpIfNull^SQLITE_JUMPIFNULL); |
| 101526 | + sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101527 | + sqlite3VdbeResolveLabel(v, d2); |
| 101528 | + }else{ |
| 101529 | + testcase( jumpIfNull==0 ); |
| 101530 | + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 101531 | + sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101532 | + } |
| 101332 | 101533 | break; |
| 101333 | 101534 | } |
| 101334 | 101535 | case TK_NOT: { |
| 101335 | 101536 | testcase( jumpIfNull==0 ); |
| 101336 | 101537 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| | @@ -101412,13 +101613,13 @@ |
| 101412 | 101613 | break; |
| 101413 | 101614 | } |
| 101414 | 101615 | #endif |
| 101415 | 101616 | default: { |
| 101416 | 101617 | default_expr: |
| 101417 | | - if( exprAlwaysTrue(pExpr) ){ |
| 101618 | + if( ExprAlwaysTrue(pExpr) ){ |
| 101418 | 101619 | sqlite3VdbeGoto(v, dest); |
| 101419 | | - }else if( exprAlwaysFalse(pExpr) ){ |
| 101620 | + }else if( ExprAlwaysFalse(pExpr) ){ |
| 101420 | 101621 | /* No-op */ |
| 101421 | 101622 | }else{ |
| 101422 | 101623 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 101423 | 101624 | sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
| 101424 | 101625 | VdbeCoverage(v); |
| | @@ -101482,22 +101683,27 @@ |
| 101482 | 101683 | assert( pExpr->op!=TK_LE || op==OP_Gt ); |
| 101483 | 101684 | assert( pExpr->op!=TK_GT || op==OP_Le ); |
| 101484 | 101685 | assert( pExpr->op!=TK_GE || op==OP_Lt ); |
| 101485 | 101686 | |
| 101486 | 101687 | switch( pExpr->op ){ |
| 101487 | | - case TK_AND: { |
| 101488 | | - testcase( jumpIfNull==0 ); |
| 101489 | | - sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 101490 | | - sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101491 | | - break; |
| 101492 | | - } |
| 101688 | + case TK_AND: |
| 101493 | 101689 | case TK_OR: { |
| 101494 | | - int d2 = sqlite3VdbeMakeLabel(pParse); |
| 101495 | | - testcase( jumpIfNull==0 ); |
| 101496 | | - sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); |
| 101497 | | - sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101498 | | - sqlite3VdbeResolveLabel(v, d2); |
| 101690 | + Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); |
| 101691 | + if( pAlt!=pExpr ){ |
| 101692 | + sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull); |
| 101693 | + }else if( pExpr->op==TK_AND ){ |
| 101694 | + testcase( jumpIfNull==0 ); |
| 101695 | + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 101696 | + sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101697 | + }else{ |
| 101698 | + int d2 = sqlite3VdbeMakeLabel(pParse); |
| 101699 | + testcase( jumpIfNull==0 ); |
| 101700 | + sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, |
| 101701 | + jumpIfNull^SQLITE_JUMPIFNULL); |
| 101702 | + sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 101703 | + sqlite3VdbeResolveLabel(v, d2); |
| 101704 | + } |
| 101499 | 101705 | break; |
| 101500 | 101706 | } |
| 101501 | 101707 | case TK_NOT: { |
| 101502 | 101708 | testcase( jumpIfNull==0 ); |
| 101503 | 101709 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| | @@ -101582,13 +101788,13 @@ |
| 101582 | 101788 | break; |
| 101583 | 101789 | } |
| 101584 | 101790 | #endif |
| 101585 | 101791 | default: { |
| 101586 | 101792 | default_expr: |
| 101587 | | - if( exprAlwaysFalse(pExpr) ){ |
| 101793 | + if( ExprAlwaysFalse(pExpr) ){ |
| 101588 | 101794 | sqlite3VdbeGoto(v, dest); |
| 101589 | | - }else if( exprAlwaysTrue(pExpr) ){ |
| 101795 | + }else if( ExprAlwaysTrue(pExpr) ){ |
| 101590 | 101796 | /* no-op */ |
| 101591 | 101797 | }else{ |
| 101592 | 101798 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 101593 | 101799 | sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
| 101594 | 101800 | VdbeCoverage(v); |
| | @@ -101822,11 +102028,15 @@ |
| 101822 | 102028 | && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab) |
| 101823 | 102029 | || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) ) |
| 101824 | 102030 | ){ |
| 101825 | 102031 | return 1; |
| 101826 | 102032 | } |
| 101827 | | - if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){ |
| 102033 | + if( pE2->op==TK_NOTNULL |
| 102034 | + && pE1->op!=TK_ISNULL |
| 102035 | + && pE1->op!=TK_IS |
| 102036 | + && pE1->op!=TK_OR |
| 102037 | + ){ |
| 101828 | 102038 | Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft); |
| 101829 | 102039 | testcase( pX!=pE1->pLeft ); |
| 101830 | 102040 | if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1; |
| 101831 | 102041 | } |
| 101832 | 102042 | return 0; |
| | @@ -102399,11 +102609,11 @@ |
| 102399 | 102609 | */ |
| 102400 | 102610 | static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){ |
| 102401 | 102611 | sqlite3NestedParse(pParse, |
| 102402 | 102612 | "SELECT 1 " |
| 102403 | 102613 | "FROM \"%w\".%s " |
| 102404 | | - "WHERE name NOT LIKE 'sqlite_%%'" |
| 102614 | + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 102405 | 102615 | " AND sql NOT LIKE 'create virtual%%'" |
| 102406 | 102616 | " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ", |
| 102407 | 102617 | zDb, MASTER_NAME, |
| 102408 | 102618 | zDb, bTemp |
| 102409 | 102619 | ); |
| | @@ -102410,11 +102620,11 @@ |
| 102410 | 102620 | |
| 102411 | 102621 | if( bTemp==0 ){ |
| 102412 | 102622 | sqlite3NestedParse(pParse, |
| 102413 | 102623 | "SELECT 1 " |
| 102414 | 102624 | "FROM temp.%s " |
| 102415 | | - "WHERE name NOT LIKE 'sqlite_%%'" |
| 102625 | + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 102416 | 102626 | " AND sql NOT LIKE 'create virtual%%'" |
| 102417 | 102627 | " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ", |
| 102418 | 102628 | MASTER_NAME, zDb |
| 102419 | 102629 | ); |
| 102420 | 102630 | } |
| | @@ -102531,11 +102741,11 @@ |
| 102531 | 102741 | ** the schema to use the new table name. */ |
| 102532 | 102742 | sqlite3NestedParse(pParse, |
| 102533 | 102743 | "UPDATE \"%w\".%s SET " |
| 102534 | 102744 | "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) " |
| 102535 | 102745 | "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)" |
| 102536 | | - "AND name NOT LIKE 'sqlite_%%'" |
| 102746 | + "AND name NOT LIKE 'sqliteX_%%' ESCAPE 'X'" |
| 102537 | 102747 | , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName |
| 102538 | 102748 | ); |
| 102539 | 102749 | |
| 102540 | 102750 | /* Update the tbl_name and name columns of the sqlite_master table |
| 102541 | 102751 | ** as required. */ |
| | @@ -102542,11 +102752,12 @@ |
| 102542 | 102752 | sqlite3NestedParse(pParse, |
| 102543 | 102753 | "UPDATE %Q.%s SET " |
| 102544 | 102754 | "tbl_name = %Q, " |
| 102545 | 102755 | "name = CASE " |
| 102546 | 102756 | "WHEN type='table' THEN %Q " |
| 102547 | | - "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " |
| 102757 | + "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' " |
| 102758 | + " AND type='index' THEN " |
| 102548 | 102759 | "'sqlite_autoindex_' || %Q || substr(name,%d+18) " |
| 102549 | 102760 | "ELSE name END " |
| 102550 | 102761 | "WHERE tbl_name=%Q COLLATE nocase AND " |
| 102551 | 102762 | "(type='table' OR type='index' OR type='trigger');", |
| 102552 | 102763 | zDb, MASTER_NAME, |
| | @@ -102916,11 +103127,12 @@ |
| 102916 | 103127 | assert( pNew->n>0 ); |
| 102917 | 103128 | bQuote = sqlite3Isquote(pNew->z[0]); |
| 102918 | 103129 | sqlite3NestedParse(pParse, |
| 102919 | 103130 | "UPDATE \"%w\".%s SET " |
| 102920 | 103131 | "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) " |
| 102921 | | - "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)" |
| 103132 | + "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' " |
| 103133 | + " AND (type != 'index' OR tbl_name = %Q)" |
| 102922 | 103134 | " AND sql NOT LIKE 'create virtual%%'", |
| 102923 | 103135 | zDb, MASTER_NAME, |
| 102924 | 103136 | zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1, |
| 102925 | 103137 | pTab->zName |
| 102926 | 103138 | ); |
| | @@ -108168,11 +108380,11 @@ |
| 108168 | 108380 | ** |
| 108169 | 108381 | ** This is goofy. But to preserve backwards compatibility we continue to |
| 108170 | 108382 | ** accept it. This routine does the necessary conversion. It converts |
| 108171 | 108383 | ** the expression given in its argument from a TK_STRING into a TK_ID |
| 108172 | 108384 | ** if the expression is just a TK_STRING with an optional COLLATE clause. |
| 108173 | | -** If the epxression is anything other than TK_STRING, the expression is |
| 108385 | +** If the expression is anything other than TK_STRING, the expression is |
| 108174 | 108386 | ** unchanged. |
| 108175 | 108387 | */ |
| 108176 | 108388 | static void sqlite3StringToId(Expr *p){ |
| 108177 | 108389 | if( p->op==TK_STRING ){ |
| 108178 | 108390 | p->op = TK_ID; |
| | @@ -108565,14 +108777,55 @@ |
| 108565 | 108777 | wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst; |
| 108566 | 108778 | } |
| 108567 | 108779 | pIdx->szIdxRow = sqlite3LogEst(wIndex*4); |
| 108568 | 108780 | } |
| 108569 | 108781 | |
| 108570 | | -/* Return true if value x is found any of the first nCol entries of aiCol[] |
| 108782 | +/* Return true if column number x is any of the first nCol entries of aiCol[]. |
| 108783 | +** This is used to determine if the column number x appears in any of the |
| 108784 | +** first nCol entries of an index. |
| 108571 | 108785 | */ |
| 108572 | 108786 | static int hasColumn(const i16 *aiCol, int nCol, int x){ |
| 108573 | | - while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1; |
| 108787 | + while( nCol-- > 0 ){ |
| 108788 | + assert( aiCol[0]>=0 ); |
| 108789 | + if( x==*(aiCol++) ){ |
| 108790 | + return 1; |
| 108791 | + } |
| 108792 | + } |
| 108793 | + return 0; |
| 108794 | +} |
| 108795 | + |
| 108796 | +/* |
| 108797 | +** Return true if any of the first nKey entries of index pIdx exactly |
| 108798 | +** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID |
| 108799 | +** PRIMARY KEY index. pIdx is an index on the same table. pIdx may |
| 108800 | +** or may not be the same index as pPk. |
| 108801 | +** |
| 108802 | +** The first nKey entries of pIdx are guaranteed to be ordinary columns, |
| 108803 | +** not a rowid or expression. |
| 108804 | +** |
| 108805 | +** This routine differs from hasColumn() in that both the column and the |
| 108806 | +** collating sequence must match for this routine, but for hasColumn() only |
| 108807 | +** the column name must match. |
| 108808 | +*/ |
| 108809 | +static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){ |
| 108810 | + int i, j; |
| 108811 | + assert( nKey<=pIdx->nColumn ); |
| 108812 | + assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) ); |
| 108813 | + assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY ); |
| 108814 | + assert( pPk->pTable->tabFlags & TF_WithoutRowid ); |
| 108815 | + assert( pPk->pTable==pIdx->pTable ); |
| 108816 | + testcase( pPk==pIdx ); |
| 108817 | + j = pPk->aiColumn[iCol]; |
| 108818 | + assert( j!=XN_ROWID && j!=XN_EXPR ); |
| 108819 | + for(i=0; i<nKey; i++){ |
| 108820 | + assert( pIdx->aiColumn[i]>=0 || j>=0 ); |
| 108821 | + if( pIdx->aiColumn[i]==j |
| 108822 | + && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0 |
| 108823 | + ){ |
| 108824 | + return 1; |
| 108825 | + } |
| 108826 | + } |
| 108574 | 108827 | return 0; |
| 108575 | 108828 | } |
| 108576 | 108829 | |
| 108577 | 108830 | /* Recompute the colNotIdxed field of the Index. |
| 108578 | 108831 | ** |
| | @@ -108657,17 +108910,20 @@ |
| 108657 | 108910 | Token ipkToken; |
| 108658 | 108911 | sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName); |
| 108659 | 108912 | pList = sqlite3ExprListAppend(pParse, 0, |
| 108660 | 108913 | sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0)); |
| 108661 | 108914 | if( pList==0 ) return; |
| 108915 | + if( IN_RENAME_OBJECT ){ |
| 108916 | + sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey); |
| 108917 | + } |
| 108662 | 108918 | pList->a[0].sortOrder = pParse->iPkSortOrder; |
| 108663 | 108919 | assert( pParse->pNewTable==pTab ); |
| 108920 | + pTab->iPKey = -1; |
| 108664 | 108921 | sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0, |
| 108665 | 108922 | SQLITE_IDXTYPE_PRIMARYKEY); |
| 108666 | 108923 | if( db->mallocFailed || pParse->nErr ) return; |
| 108667 | 108924 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 108668 | | - pTab->iPKey = -1; |
| 108669 | 108925 | }else{ |
| 108670 | 108926 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 108671 | 108927 | assert( pPk!=0 ); |
| 108672 | 108928 | |
| 108673 | 108929 | /* |
| | @@ -108674,13 +108930,14 @@ |
| 108674 | 108930 | ** Remove all redundant columns from the PRIMARY KEY. For example, change |
| 108675 | 108931 | ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)". Later |
| 108676 | 108932 | ** code assumes the PRIMARY KEY contains no repeated columns. |
| 108677 | 108933 | */ |
| 108678 | 108934 | for(i=j=1; i<pPk->nKeyCol; i++){ |
| 108679 | | - if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){ |
| 108935 | + if( isDupColumn(pPk, j, pPk, i) ){ |
| 108680 | 108936 | pPk->nColumn--; |
| 108681 | 108937 | }else{ |
| 108938 | + testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ); |
| 108682 | 108939 | pPk->aiColumn[j++] = pPk->aiColumn[i]; |
| 108683 | 108940 | } |
| 108684 | 108941 | } |
| 108685 | 108942 | pPk->nKeyCol = j; |
| 108686 | 108943 | } |
| | @@ -108706,20 +108963,24 @@ |
| 108706 | 108963 | */ |
| 108707 | 108964 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 108708 | 108965 | int n; |
| 108709 | 108966 | if( IsPrimaryKeyIndex(pIdx) ) continue; |
| 108710 | 108967 | for(i=n=0; i<nPk; i++){ |
| 108711 | | - if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++; |
| 108968 | + if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 108969 | + testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
| 108970 | + n++; |
| 108971 | + } |
| 108712 | 108972 | } |
| 108713 | 108973 | if( n==0 ){ |
| 108714 | 108974 | /* This index is a superset of the primary key */ |
| 108715 | 108975 | pIdx->nColumn = pIdx->nKeyCol; |
| 108716 | 108976 | continue; |
| 108717 | 108977 | } |
| 108718 | 108978 | if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return; |
| 108719 | 108979 | for(i=0, j=pIdx->nKeyCol; i<nPk; i++){ |
| 108720 | | - if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){ |
| 108980 | + if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){ |
| 108981 | + testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ); |
| 108721 | 108982 | pIdx->aiColumn[j] = pPk->aiColumn[i]; |
| 108722 | 108983 | pIdx->azColl[j] = pPk->azColl[i]; |
| 108723 | 108984 | j++; |
| 108724 | 108985 | } |
| 108725 | 108986 | } |
| | @@ -110231,13 +110492,14 @@ |
| 110231 | 110492 | */ |
| 110232 | 110493 | if( pPk ){ |
| 110233 | 110494 | for(j=0; j<pPk->nKeyCol; j++){ |
| 110234 | 110495 | int x = pPk->aiColumn[j]; |
| 110235 | 110496 | assert( x>=0 ); |
| 110236 | | - if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){ |
| 110497 | + if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){ |
| 110237 | 110498 | pIndex->nColumn--; |
| 110238 | 110499 | }else{ |
| 110500 | + testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) ); |
| 110239 | 110501 | pIndex->aiColumn[i] = x; |
| 110240 | 110502 | pIndex->azColl[i] = pPk->azColl[j]; |
| 110241 | 110503 | pIndex->aSortOrder[i] = pPk->aSortOrder[j]; |
| 110242 | 110504 | i++; |
| 110243 | 110505 | } |
| | @@ -113004,10 +113266,11 @@ |
| 113004 | 113266 | ** time functions, are implemented separately.) |
| 113005 | 113267 | */ |
| 113006 | 113268 | /* #include "sqliteInt.h" */ |
| 113007 | 113269 | /* #include <stdlib.h> */ |
| 113008 | 113270 | /* #include <assert.h> */ |
| 113271 | +/* #include <math.h> */ |
| 113009 | 113272 | /* #include "vdbeInt.h" */ |
| 113010 | 113273 | |
| 113011 | 113274 | /* |
| 113012 | 113275 | ** Return the collating function associated with a function. |
| 113013 | 113276 | */ |
| | @@ -113384,11 +113647,14 @@ |
| 113384 | 113647 | zBuf = sqlite3_mprintf("%.*f",n,r); |
| 113385 | 113648 | if( zBuf==0 ){ |
| 113386 | 113649 | sqlite3_result_error_nomem(context); |
| 113387 | 113650 | return; |
| 113388 | 113651 | } |
| 113389 | | - sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); |
| 113652 | + if( !sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8) ){ |
| 113653 | + assert( sqlite3_strglob("*Inf", zBuf)==0 ); |
| 113654 | + r = zBuf[0]=='-' ? -HUGE_VAL : +HUGE_VAL; |
| 113655 | + } |
| 113390 | 113656 | sqlite3_free(zBuf); |
| 113391 | 113657 | } |
| 113392 | 113658 | sqlite3_result_double(context, r); |
| 113393 | 113659 | } |
| 113394 | 113660 | #endif |
| | @@ -113831,12 +114097,10 @@ |
| 113831 | 114097 | #endif |
| 113832 | 114098 | sqlite3_result_int(context, 0); |
| 113833 | 114099 | return; |
| 113834 | 114100 | } |
| 113835 | 114101 | #endif |
| 113836 | | - zB = sqlite3_value_text(argv[0]); |
| 113837 | | - zA = sqlite3_value_text(argv[1]); |
| 113838 | 114102 | |
| 113839 | 114103 | /* Limit the length of the LIKE or GLOB pattern to avoid problems |
| 113840 | 114104 | ** of deep recursion and N*N behavior in patternCompare(). |
| 113841 | 114105 | */ |
| 113842 | 114106 | nPat = sqlite3_value_bytes(argv[0]); |
| | @@ -113844,12 +114108,10 @@ |
| 113844 | 114108 | testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); |
| 113845 | 114109 | if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ |
| 113846 | 114110 | sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); |
| 113847 | 114111 | return; |
| 113848 | 114112 | } |
| 113849 | | - assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ |
| 113850 | | - |
| 113851 | 114113 | if( argc==3 ){ |
| 113852 | 114114 | /* The escape character string must consist of a single UTF-8 character. |
| 113853 | 114115 | ** Otherwise, return an error. |
| 113854 | 114116 | */ |
| 113855 | 114117 | const unsigned char *zEsc = sqlite3_value_text(argv[2]); |
| | @@ -113861,10 +114123,12 @@ |
| 113861 | 114123 | } |
| 113862 | 114124 | escape = sqlite3Utf8Read(&zEsc); |
| 113863 | 114125 | }else{ |
| 113864 | 114126 | escape = pInfo->matchSet; |
| 113865 | 114127 | } |
| 114128 | + zB = sqlite3_value_text(argv[0]); |
| 114129 | + zA = sqlite3_value_text(argv[1]); |
| 113866 | 114130 | if( zA && zB ){ |
| 113867 | 114131 | #ifdef SQLITE_TEST |
| 113868 | 114132 | sqlite3_like_count++; |
| 113869 | 114133 | #endif |
| 113870 | 114134 | sqlite3_result_int(context, |
| | @@ -114786,43 +115050,28 @@ |
| 114786 | 115050 | sqlite3OomFault(db); |
| 114787 | 115051 | } |
| 114788 | 115052 | } |
| 114789 | 115053 | |
| 114790 | 115054 | /* |
| 114791 | | -** Set the LIKEOPT flag on the 2-argument function with the given name. |
| 114792 | | -*/ |
| 114793 | | -static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ |
| 114794 | | - FuncDef *pDef; |
| 114795 | | - pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0); |
| 114796 | | - if( ALWAYS(pDef) ){ |
| 114797 | | - pDef->funcFlags |= flagVal; |
| 114798 | | - } |
| 114799 | | - pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0); |
| 114800 | | - if( pDef ){ |
| 114801 | | - pDef->funcFlags |= flagVal; |
| 114802 | | - } |
| 114803 | | -} |
| 114804 | | - |
| 114805 | | -/* |
| 114806 | | -** Register the built-in LIKE and GLOB functions. The caseSensitive |
| 115055 | +** Re-register the built-in LIKE functions. The caseSensitive |
| 114807 | 115056 | ** parameter determines whether or not the LIKE operator is case |
| 114808 | | -** sensitive. GLOB is always case sensitive. |
| 115057 | +** sensitive. |
| 114809 | 115058 | */ |
| 114810 | 115059 | SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ |
| 114811 | 115060 | struct compareInfo *pInfo; |
| 115061 | + int flags; |
| 114812 | 115062 | if( caseSensitive ){ |
| 114813 | 115063 | pInfo = (struct compareInfo*)&likeInfoAlt; |
| 115064 | + flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE; |
| 114814 | 115065 | }else{ |
| 114815 | 115066 | pInfo = (struct compareInfo*)&likeInfoNorm; |
| 115067 | + flags = SQLITE_FUNC_LIKE; |
| 114816 | 115068 | } |
| 114817 | 115069 | sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); |
| 114818 | 115070 | sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0); |
| 114819 | | - sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, |
| 114820 | | - (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0, 0, 0); |
| 114821 | | - setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); |
| 114822 | | - setLikeOptFlag(db, "like", |
| 114823 | | - caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); |
| 115071 | + sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags; |
| 115072 | + sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags; |
| 114824 | 115073 | } |
| 114825 | 115074 | |
| 114826 | 115075 | /* |
| 114827 | 115076 | ** pExpr points to an expression which implements a function. If |
| 114828 | 115077 | ** it is appropriate to apply the LIKE optimization to that function |
| | @@ -115608,11 +115857,11 @@ |
| 115608 | 115857 | iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; |
| 115609 | 115858 | assert( iCol>=0 ); |
| 115610 | 115859 | zCol = pFKey->pFrom->aCol[iCol].zName; |
| 115611 | 115860 | pRight = sqlite3Expr(db, TK_ID, zCol); |
| 115612 | 115861 | pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight); |
| 115613 | | - pWhere = sqlite3ExprAnd(db, pWhere, pEq); |
| 115862 | + pWhere = sqlite3ExprAnd(pParse, pWhere, pEq); |
| 115614 | 115863 | } |
| 115615 | 115864 | |
| 115616 | 115865 | /* If the child table is the same as the parent table, then add terms |
| 115617 | 115866 | ** to the WHERE clause that prevent this entry from being scanned. |
| 115618 | 115867 | ** The added WHERE clause terms are like this: |
| | @@ -115642,15 +115891,15 @@ |
| 115642 | 115891 | i16 iCol = pIdx->aiColumn[i]; |
| 115643 | 115892 | assert( iCol>=0 ); |
| 115644 | 115893 | pLeft = exprTableRegister(pParse, pTab, regData, iCol); |
| 115645 | 115894 | pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zName); |
| 115646 | 115895 | pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight); |
| 115647 | | - pAll = sqlite3ExprAnd(db, pAll, pEq); |
| 115896 | + pAll = sqlite3ExprAnd(pParse, pAll, pEq); |
| 115648 | 115897 | } |
| 115649 | 115898 | pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0); |
| 115650 | 115899 | } |
| 115651 | | - pWhere = sqlite3ExprAnd(db, pWhere, pNe); |
| 115900 | + pWhere = sqlite3ExprAnd(pParse, pWhere, pNe); |
| 115652 | 115901 | } |
| 115653 | 115902 | |
| 115654 | 115903 | /* Resolve the references in the WHERE clause. */ |
| 115655 | 115904 | memset(&sNameContext, 0, sizeof(NameContext)); |
| 115656 | 115905 | sNameContext.pSrcList = pSrc; |
| | @@ -116252,11 +116501,11 @@ |
| 116252 | 116501 | sqlite3PExpr(pParse, TK_DOT, |
| 116253 | 116502 | sqlite3ExprAlloc(db, TK_ID, &tOld, 0), |
| 116254 | 116503 | sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)), |
| 116255 | 116504 | sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0) |
| 116256 | 116505 | ); |
| 116257 | | - pWhere = sqlite3ExprAnd(db, pWhere, pEq); |
| 116506 | + pWhere = sqlite3ExprAnd(pParse, pWhere, pEq); |
| 116258 | 116507 | |
| 116259 | 116508 | /* For ON UPDATE, construct the next term of the WHEN clause. |
| 116260 | 116509 | ** The final WHEN clause will be like this: |
| 116261 | 116510 | ** |
| 116262 | 116511 | ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN) |
| | @@ -116268,11 +116517,11 @@ |
| 116268 | 116517 | sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)), |
| 116269 | 116518 | sqlite3PExpr(pParse, TK_DOT, |
| 116270 | 116519 | sqlite3ExprAlloc(db, TK_ID, &tNew, 0), |
| 116271 | 116520 | sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)) |
| 116272 | 116521 | ); |
| 116273 | | - pWhen = sqlite3ExprAnd(db, pWhen, pEq); |
| 116522 | + pWhen = sqlite3ExprAnd(pParse, pWhen, pEq); |
| 116274 | 116523 | } |
| 116275 | 116524 | |
| 116276 | 116525 | if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){ |
| 116277 | 116526 | Expr *pNew; |
| 116278 | 116527 | if( action==OE_Cascade ){ |
| | @@ -117265,19 +117514,20 @@ |
| 117265 | 117514 | /* If this is not a view, open the table and and all indices */ |
| 117266 | 117515 | if( !isView ){ |
| 117267 | 117516 | int nIdx; |
| 117268 | 117517 | nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0, |
| 117269 | 117518 | &iDataCur, &iIdxCur); |
| 117270 | | - aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1)); |
| 117519 | + aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2)); |
| 117271 | 117520 | if( aRegIdx==0 ){ |
| 117272 | 117521 | goto insert_cleanup; |
| 117273 | 117522 | } |
| 117274 | 117523 | for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){ |
| 117275 | 117524 | assert( pIdx ); |
| 117276 | 117525 | aRegIdx[i] = ++pParse->nMem; |
| 117277 | 117526 | pParse->nMem += pIdx->nColumn; |
| 117278 | 117527 | } |
| 117528 | + aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */ |
| 117279 | 117529 | } |
| 117280 | 117530 | #ifndef SQLITE_OMIT_UPSERT |
| 117281 | 117531 | if( pUpsert ){ |
| 117282 | 117532 | if( IsVirtual(pTab) ){ |
| 117283 | 117533 | sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"", |
| | @@ -117676,10 +117926,18 @@ |
| 117676 | 117926 | ** The code generated by this routine will store new index entries into |
| 117677 | 117927 | ** registers identified by aRegIdx[]. No index entry is created for |
| 117678 | 117928 | ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is |
| 117679 | 117929 | ** the same as the order of indices on the linked list of indices |
| 117680 | 117930 | ** at pTab->pIndex. |
| 117931 | +** |
| 117932 | +** (2019-05-07) The generated code also creates a new record for the |
| 117933 | +** main table, if pTab is a rowid table, and stores that record in the |
| 117934 | +** register identified by aRegIdx[nIdx] - in other words in the first |
| 117935 | +** entry of aRegIdx[] past the last index. It is important that the |
| 117936 | +** record be generated during constraint checks to avoid affinity changes |
| 117937 | +** to the register content that occur after constraint checks but before |
| 117938 | +** the new record is inserted. |
| 117681 | 117939 | ** |
| 117682 | 117940 | ** The caller must have already opened writeable cursors on the main |
| 117683 | 117941 | ** table and all applicable indices (that is to say, all indices for which |
| 117684 | 117942 | ** aRegIdx[] is not zero). iDataCur is the cursor for the main table when |
| 117685 | 117943 | ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY |
| | @@ -118295,10 +118553,20 @@ |
| 118295 | 118553 | if( ipkTop ){ |
| 118296 | 118554 | sqlite3VdbeGoto(v, ipkTop); |
| 118297 | 118555 | VdbeComment((v, "Do IPK REPLACE")); |
| 118298 | 118556 | sqlite3VdbeJumpHere(v, ipkBottom); |
| 118299 | 118557 | } |
| 118558 | + |
| 118559 | + /* Generate the table record */ |
| 118560 | + if( HasRowid(pTab) ){ |
| 118561 | + int regRec = aRegIdx[ix]; |
| 118562 | + sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nCol, regRec); |
| 118563 | + sqlite3SetMakeRecordP5(v, pTab); |
| 118564 | + if( !bAffinityDone ){ |
| 118565 | + sqlite3TableAffinity(v, pTab, 0); |
| 118566 | + } |
| 118567 | + } |
| 118300 | 118568 | |
| 118301 | 118569 | *pbMayReplace = seenReplace; |
| 118302 | 118570 | VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace)); |
| 118303 | 118571 | } |
| 118304 | 118572 | |
| | @@ -118345,14 +118613,11 @@ |
| 118345 | 118613 | int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ |
| 118346 | 118614 | ){ |
| 118347 | 118615 | Vdbe *v; /* Prepared statements under construction */ |
| 118348 | 118616 | Index *pIdx; /* An index being inserted or updated */ |
| 118349 | 118617 | u8 pik_flags; /* flag values passed to the btree insert */ |
| 118350 | | - int regData; /* Content registers (after the rowid) */ |
| 118351 | | - int regRec; /* Register holding assembled record for the table */ |
| 118352 | 118618 | int i; /* Loop counter */ |
| 118353 | | - u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */ |
| 118354 | 118619 | |
| 118355 | 118620 | assert( update_flags==0 |
| 118356 | 118621 | || update_flags==OPFLAG_ISUPDATE |
| 118357 | 118622 | || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION) |
| 118358 | 118623 | ); |
| | @@ -118360,11 +118625,10 @@ |
| 118360 | 118625 | v = sqlite3GetVdbe(pParse); |
| 118361 | 118626 | assert( v!=0 ); |
| 118362 | 118627 | assert( pTab->pSelect==0 ); /* This table is not a VIEW */ |
| 118363 | 118628 | for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ |
| 118364 | 118629 | if( aRegIdx[i]==0 ) continue; |
| 118365 | | - bAffinityDone = 1; |
| 118366 | 118630 | if( pIdx->pPartIdxWhere ){ |
| 118367 | 118631 | sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2); |
| 118368 | 118632 | VdbeCoverage(v); |
| 118369 | 118633 | } |
| 118370 | 118634 | pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0); |
| | @@ -118388,17 +118652,10 @@ |
| 118388 | 118652 | aRegIdx[i]+1, |
| 118389 | 118653 | pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn); |
| 118390 | 118654 | sqlite3VdbeChangeP5(v, pik_flags); |
| 118391 | 118655 | } |
| 118392 | 118656 | if( !HasRowid(pTab) ) return; |
| 118393 | | - regData = regNewData + 1; |
| 118394 | | - regRec = sqlite3GetTempReg(pParse); |
| 118395 | | - sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); |
| 118396 | | - sqlite3SetMakeRecordP5(v, pTab); |
| 118397 | | - if( !bAffinityDone ){ |
| 118398 | | - sqlite3TableAffinity(v, pTab, 0); |
| 118399 | | - } |
| 118400 | 118657 | if( pParse->nested ){ |
| 118401 | 118658 | pik_flags = 0; |
| 118402 | 118659 | }else{ |
| 118403 | 118660 | pik_flags = OPFLAG_NCHANGE; |
| 118404 | 118661 | pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID); |
| | @@ -118407,11 +118664,11 @@ |
| 118407 | 118664 | pik_flags |= OPFLAG_APPEND; |
| 118408 | 118665 | } |
| 118409 | 118666 | if( useSeekResult ){ |
| 118410 | 118667 | pik_flags |= OPFLAG_USESEEKRESULT; |
| 118411 | 118668 | } |
| 118412 | | - sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData); |
| 118669 | + sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData); |
| 118413 | 118670 | if( !pParse->nested ){ |
| 118414 | 118671 | sqlite3VdbeAppendP4(v, pTab, P4_TABLE); |
| 118415 | 118672 | } |
| 118416 | 118673 | sqlite3VdbeChangeP5(v, pik_flags); |
| 118417 | 118674 | } |
| | @@ -120725,15 +120982,17 @@ |
| 120725 | 120982 | /* ePragTyp: */ PragTyp_CACHE_SPILL, |
| 120726 | 120983 | /* ePragFlg: */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1, |
| 120727 | 120984 | /* ColNames: */ 0, 0, |
| 120728 | 120985 | /* iArg: */ 0 }, |
| 120729 | 120986 | #endif |
| 120987 | +#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA) |
| 120730 | 120988 | {/* zName: */ "case_sensitive_like", |
| 120731 | 120989 | /* ePragTyp: */ PragTyp_CASE_SENSITIVE_LIKE, |
| 120732 | 120990 | /* ePragFlg: */ PragFlg_NoColumns, |
| 120733 | 120991 | /* ColNames: */ 0, 0, |
| 120734 | 120992 | /* iArg: */ 0 }, |
| 120993 | +#endif |
| 120735 | 120994 | {/* zName: */ "cell_size_check", |
| 120736 | 120995 | /* ePragTyp: */ PragTyp_FLAG, |
| 120737 | 120996 | /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, |
| 120738 | 120997 | /* ColNames: */ 0, 0, |
| 120739 | 120998 | /* iArg: */ SQLITE_CellSizeCk }, |
| | @@ -122610,19 +122869,21 @@ |
| 122610 | 122869 | } |
| 122611 | 122870 | break; |
| 122612 | 122871 | #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
| 122613 | 122872 | #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ |
| 122614 | 122873 | |
| 122874 | +#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA |
| 122615 | 122875 | /* Reinstall the LIKE and GLOB functions. The variant of LIKE |
| 122616 | 122876 | ** used will be case sensitive or not depending on the RHS. |
| 122617 | 122877 | */ |
| 122618 | 122878 | case PragTyp_CASE_SENSITIVE_LIKE: { |
| 122619 | 122879 | if( zRight ){ |
| 122620 | 122880 | sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0)); |
| 122621 | 122881 | } |
| 122622 | 122882 | } |
| 122623 | 122883 | break; |
| 122884 | +#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */ |
| 122624 | 122885 | |
| 122625 | 122886 | #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX |
| 122626 | 122887 | # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 |
| 122627 | 122888 | #endif |
| 122628 | 122889 | |
| | @@ -124974,11 +125235,11 @@ |
| 124974 | 125235 | ExprSetProperty(pEq, EP_FromJoin); |
| 124975 | 125236 | assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); |
| 124976 | 125237 | ExprSetVVAProperty(pEq, EP_NoReduce); |
| 124977 | 125238 | pEq->iRightJoinTable = (i16)pE2->iTable; |
| 124978 | 125239 | } |
| 124979 | | - *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq); |
| 125240 | + *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq); |
| 124980 | 125241 | } |
| 124981 | 125242 | |
| 124982 | 125243 | /* |
| 124983 | 125244 | ** Set the EP_FromJoin property on all terms of the given expression. |
| 124984 | 125245 | ** And set the Expr.iRightJoinTable to iTable for every term in the |
| | @@ -125108,11 +125369,11 @@ |
| 125108 | 125369 | /* Add the ON clause to the end of the WHERE clause, connected by |
| 125109 | 125370 | ** an AND operator. |
| 125110 | 125371 | */ |
| 125111 | 125372 | if( pRight->pOn ){ |
| 125112 | 125373 | if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor); |
| 125113 | | - p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn); |
| 125374 | + p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn); |
| 125114 | 125375 | pRight->pOn = 0; |
| 125115 | 125376 | } |
| 125116 | 125377 | |
| 125117 | 125378 | /* Create extra terms on the WHERE clause for each column named |
| 125118 | 125379 | ** in the USING clause. Example: If the two tables to be joined are |
| | @@ -128653,11 +128914,11 @@ |
| 128653 | 128914 | pWhere = pSub->pWhere; |
| 128654 | 128915 | pSub->pWhere = 0; |
| 128655 | 128916 | if( isLeftJoin>0 ){ |
| 128656 | 128917 | setJoinExpr(pWhere, iNewParent); |
| 128657 | 128918 | } |
| 128658 | | - pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere); |
| 128919 | + pParent->pWhere = sqlite3ExprAnd(pParse, pWhere, pParent->pWhere); |
| 128659 | 128920 | if( db->mallocFailed==0 ){ |
| 128660 | 128921 | SubstContext x; |
| 128661 | 128922 | x.pParse = pParse; |
| 128662 | 128923 | x.iTable = iParent; |
| 128663 | 128924 | x.iNewTable = iNewParent; |
| | @@ -128988,13 +129249,13 @@ |
| 128988 | 129249 | x.iNewTable = iCursor; |
| 128989 | 129250 | x.isLeftJoin = 0; |
| 128990 | 129251 | x.pEList = pSubq->pEList; |
| 128991 | 129252 | pNew = substExpr(&x, pNew); |
| 128992 | 129253 | if( pSubq->selFlags & SF_Aggregate ){ |
| 128993 | | - pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew); |
| 129254 | + pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew); |
| 128994 | 129255 | }else{ |
| 128995 | | - pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew); |
| 129256 | + pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew); |
| 128996 | 129257 | } |
| 128997 | 129258 | pSubq = pSubq->pPrior; |
| 128998 | 129259 | } |
| 128999 | 129260 | } |
| 129000 | 129261 | return nChng; |
| | @@ -129416,11 +129677,11 @@ |
| 129416 | 129677 | sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); |
| 129417 | 129678 | pTab->iPKey = -1; |
| 129418 | 129679 | pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
| 129419 | 129680 | pTab->tabFlags |= TF_Ephemeral; |
| 129420 | 129681 | |
| 129421 | | - return SQLITE_OK; |
| 129682 | + return pParse->nErr ? SQLITE_ERROR : SQLITE_OK; |
| 129422 | 129683 | } |
| 129423 | 129684 | |
| 129424 | 129685 | /* |
| 129425 | 129686 | ** This routine is a Walker callback for "expanding" a SELECT statement. |
| 129426 | 129687 | ** "Expanding" means to do the following: |
| | @@ -130037,11 +130298,11 @@ |
| 130037 | 130298 | sqlite3 *db = pWalker->pParse->db; |
| 130038 | 130299 | Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0); |
| 130039 | 130300 | if( pNew ){ |
| 130040 | 130301 | Expr *pWhere = pS->pWhere; |
| 130041 | 130302 | SWAP(Expr, *pNew, *pExpr); |
| 130042 | | - pNew = sqlite3ExprAnd(db, pWhere, pNew); |
| 130303 | + pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew); |
| 130043 | 130304 | pS->pWhere = pNew; |
| 130044 | 130305 | pWalker->eCode = 1; |
| 130045 | 130306 | } |
| 130046 | 130307 | } |
| 130047 | 130308 | return WRC_Prune; |
| | @@ -130100,11 +130361,13 @@ |
| 130100 | 130361 | if( pThis->pSelect->selId!=pS1->selId ){ |
| 130101 | 130362 | /* The query flattener left two different CTE tables with identical |
| 130102 | 130363 | ** names in the same FROM clause. */ |
| 130103 | 130364 | continue; |
| 130104 | 130365 | } |
| 130105 | | - if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1) ){ |
| 130366 | + if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1) |
| 130367 | + || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1) |
| 130368 | + ){ |
| 130106 | 130369 | /* The view was modified by some other optimization such as |
| 130107 | 130370 | ** pushDownWhereTerms() */ |
| 130108 | 130371 | continue; |
| 130109 | 130372 | } |
| 130110 | 130373 | return pItem; |
| | @@ -132786,15 +133049,16 @@ |
| 132786 | 133049 | WhereInfo *pWInfo; /* Information about the WHERE clause */ |
| 132787 | 133050 | Vdbe *v; /* The virtual database engine */ |
| 132788 | 133051 | Index *pIdx; /* For looping over indices */ |
| 132789 | 133052 | Index *pPk; /* The PRIMARY KEY index for WITHOUT ROWID tables */ |
| 132790 | 133053 | int nIdx; /* Number of indices that need updating */ |
| 133054 | + int nAllIdx; /* Total number of indexes */ |
| 132791 | 133055 | int iBaseCur; /* Base cursor number */ |
| 132792 | 133056 | int iDataCur; /* Cursor for the canonical data btree */ |
| 132793 | 133057 | int iIdxCur; /* Cursor for the first index */ |
| 132794 | 133058 | sqlite3 *db; /* The database structure */ |
| 132795 | | - int *aRegIdx = 0; /* First register in array assigned to each index */ |
| 133059 | + int *aRegIdx = 0; /* Registers for to each index and the main table */ |
| 132796 | 133060 | int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the |
| 132797 | 133061 | ** an expression for the i-th column of the table. |
| 132798 | 133062 | ** aXRef[i]==-1 if the i-th column is not changed. */ |
| 132799 | 133063 | u8 *aToOpen; /* 1 for tables and indices to be opened */ |
| 132800 | 133064 | u8 chngPk; /* PRIMARY KEY changed in a WITHOUT ROWID table */ |
| | @@ -132904,14 +133168,14 @@ |
| 132904 | 133168 | pTabList->a[0].iCursor = iDataCur; |
| 132905 | 133169 | |
| 132906 | 133170 | /* Allocate space for aXRef[], aRegIdx[], and aToOpen[]. |
| 132907 | 133171 | ** Initialize aXRef[] and aToOpen[] to their default values. |
| 132908 | 133172 | */ |
| 132909 | | - aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 ); |
| 133173 | + aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 ); |
| 132910 | 133174 | if( aXRef==0 ) goto update_cleanup; |
| 132911 | 133175 | aRegIdx = aXRef+pTab->nCol; |
| 132912 | | - aToOpen = (u8*)(aRegIdx+nIdx); |
| 133176 | + aToOpen = (u8*)(aRegIdx+nIdx+1); |
| 132913 | 133177 | memset(aToOpen, 1, nIdx+1); |
| 132914 | 133178 | aToOpen[nIdx+1] = 0; |
| 132915 | 133179 | for(i=0; i<pTab->nCol; i++) aXRef[i] = -1; |
| 132916 | 133180 | |
| 132917 | 133181 | /* Initialize the name-context */ |
| | @@ -132986,11 +133250,11 @@ |
| 132986 | 133250 | /* There is one entry in the aRegIdx[] array for each index on the table |
| 132987 | 133251 | ** being updated. Fill in aRegIdx[] with a register number that will hold |
| 132988 | 133252 | ** the key for accessing each index. |
| 132989 | 133253 | */ |
| 132990 | 133254 | if( onError==OE_Replace ) bReplace = 1; |
| 132991 | | - for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ |
| 133255 | + for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){ |
| 132992 | 133256 | int reg; |
| 132993 | 133257 | if( chngKey || hasFK>1 || pIdx==pPk |
| 132994 | 133258 | || indexWhereClauseMightChange(pIdx,aXRef,chngRowid) |
| 132995 | 133259 | ){ |
| 132996 | 133260 | reg = ++pParse->nMem; |
| | @@ -133006,13 +133270,14 @@ |
| 133006 | 133270 | } |
| 133007 | 133271 | break; |
| 133008 | 133272 | } |
| 133009 | 133273 | } |
| 133010 | 133274 | } |
| 133011 | | - if( reg==0 ) aToOpen[j+1] = 0; |
| 133012 | | - aRegIdx[j] = reg; |
| 133275 | + if( reg==0 ) aToOpen[nAllIdx+1] = 0; |
| 133276 | + aRegIdx[nAllIdx] = reg; |
| 133013 | 133277 | } |
| 133278 | + aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */ |
| 133014 | 133279 | if( bReplace ){ |
| 133015 | 133280 | /* If REPLACE conflict resolution might be invoked, open cursors on all |
| 133016 | 133281 | ** indexes in case they are needed to delete records. */ |
| 133017 | 133282 | memset(aToOpen, 1, nIdx+1); |
| 133018 | 133283 | } |
| | @@ -133023,11 +133288,17 @@ |
| 133023 | 133288 | if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); |
| 133024 | 133289 | sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb); |
| 133025 | 133290 | |
| 133026 | 133291 | /* Allocate required registers. */ |
| 133027 | 133292 | if( !IsVirtual(pTab) ){ |
| 133028 | | - regRowSet = ++pParse->nMem; |
| 133293 | + /* For now, regRowSet and aRegIdx[nAllIdx] share the same register. |
| 133294 | + ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be |
| 133295 | + ** reallocated. aRegIdx[nAllIdx] is the register in which the main |
| 133296 | + ** table record is written. regRowSet holds the RowSet for the |
| 133297 | + ** two-pass update algorithm. */ |
| 133298 | + assert( aRegIdx[nAllIdx]==pParse->nMem ); |
| 133299 | + regRowSet = aRegIdx[nAllIdx]; |
| 133029 | 133300 | regOldRowid = regNewRowid = ++pParse->nMem; |
| 133030 | 133301 | if( chngPk || pTrigger || hasFK ){ |
| 133031 | 133302 | regOld = pParse->nMem + 1; |
| 133032 | 133303 | pParse->nMem += pTab->nCol; |
| 133033 | 133304 | } |
| | @@ -133153,10 +133424,12 @@ |
| 133153 | 133424 | /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF |
| 133154 | 133425 | ** mode, write the rowid into the FIFO. In either of the one-pass modes, |
| 133155 | 133426 | ** leave it in register regOldRowid. */ |
| 133156 | 133427 | sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid); |
| 133157 | 133428 | if( eOnePass==ONEPASS_OFF ){ |
| 133429 | + /* We need to use regRowSet, so reallocate aRegIdx[nAllIdx] */ |
| 133430 | + aRegIdx[nAllIdx] = ++pParse->nMem; |
| 133158 | 133431 | sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); |
| 133159 | 133432 | } |
| 133160 | 133433 | }else{ |
| 133161 | 133434 | /* Read the PK of the current row into an array of registers. In |
| 133162 | 133435 | ** ONEPASS_OFF mode, serialize the array into a record and store it in |
| | @@ -133984,10 +134257,11 @@ |
| 133984 | 134257 | */ |
| 133985 | 134258 | SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){ |
| 133986 | 134259 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 133987 | 134260 | int iDb = 0; |
| 133988 | 134261 | if( v==0 ) goto build_vacuum_end; |
| 134262 | + if( pParse->nErr ) goto build_vacuum_end; |
| 133989 | 134263 | if( pNm ){ |
| 133990 | 134264 | #ifndef SQLITE_BUG_COMPATIBLE_20160819 |
| 133991 | 134265 | /* Default behavior: Report an error if the argument to VACUUM is |
| 133992 | 134266 | ** not recognized */ |
| 133993 | 134267 | iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm); |
| | @@ -135136,18 +135410,20 @@ |
| 135136 | 135410 | } |
| 135137 | 135411 | } |
| 135138 | 135412 | p = vtabDisconnectAll(db, pTab); |
| 135139 | 135413 | xDestroy = p->pMod->pModule->xDestroy; |
| 135140 | 135414 | assert( xDestroy!=0 ); /* Checked before the virtual table is created */ |
| 135415 | + pTab->nTabRef++; |
| 135141 | 135416 | rc = xDestroy(p->pVtab); |
| 135142 | 135417 | /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ |
| 135143 | 135418 | if( rc==SQLITE_OK ){ |
| 135144 | 135419 | assert( pTab->pVTable==p && p->pNext==0 ); |
| 135145 | 135420 | p->pVtab = 0; |
| 135146 | 135421 | pTab->pVTable = 0; |
| 135147 | 135422 | sqlite3VtabUnlock(p); |
| 135148 | 135423 | } |
| 135424 | + sqlite3DeleteTable(db, pTab); |
| 135149 | 135425 | } |
| 135150 | 135426 | |
| 135151 | 135427 | return rc; |
| 135152 | 135428 | } |
| 135153 | 135429 | |
| | @@ -135586,10 +135862,12 @@ |
| 135586 | 135862 | ** |
| 135587 | 135863 | ** This file contains structure and macro definitions for the query |
| 135588 | 135864 | ** planner logic in "where.c". These definitions are broken out into |
| 135589 | 135865 | ** a separate source file for easier editing. |
| 135590 | 135866 | */ |
| 135867 | +#ifndef SQLITE_WHEREINT_H |
| 135868 | +#define SQLITE_WHEREINT_H |
| 135591 | 135869 | |
| 135592 | 135870 | /* |
| 135593 | 135871 | ** Trace output macros |
| 135594 | 135872 | */ |
| 135595 | 135873 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| | @@ -136156,10 +136434,12 @@ |
| 136156 | 136434 | #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */ |
| 136157 | 136435 | #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */ |
| 136158 | 136436 | #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/ |
| 136159 | 136437 | #define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */ |
| 136160 | 136438 | #define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */ |
| 136439 | + |
| 136440 | +#endif /* !defined(SQLITE_WHEREINT_H) */ |
| 136161 | 136441 | |
| 136162 | 136442 | /************** End of whereInt.h ********************************************/ |
| 136163 | 136443 | /************** Continuing where we left off in wherecode.c ******************/ |
| 136164 | 136444 | |
| 136165 | 136445 | #ifndef SQLITE_OMIT_EXPLAIN |
| | @@ -137139,11 +137419,11 @@ |
| 137139 | 137419 | sqlite3WalkExpr(&sWalker, pTerm->pExpr); |
| 137140 | 137420 | if( sWalker.eCode ) continue; |
| 137141 | 137421 | } |
| 137142 | 137422 | |
| 137143 | 137423 | /* If we survive all prior tests, that means this term is worth hinting */ |
| 137144 | | - pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); |
| 137424 | + pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); |
| 137145 | 137425 | } |
| 137146 | 137426 | if( pExpr!=0 ){ |
| 137147 | 137427 | sWalker.xExprCallback = codeCursorHintFixExpr; |
| 137148 | 137428 | sqlite3WalkExpr(&sWalker, pExpr); |
| 137149 | 137429 | sqlite3VdbeAddOp4(v, OP_CursorHint, |
| | @@ -138104,11 +138384,11 @@ |
| 138104 | 138384 | testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); |
| 138105 | 138385 | if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue; |
| 138106 | 138386 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
| 138107 | 138387 | testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
| 138108 | 138388 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 138109 | | - pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
| 138389 | + pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); |
| 138110 | 138390 | } |
| 138111 | 138391 | if( pAndExpr ){ |
| 138112 | 138392 | /* The extra 0x10000 bit on the opcode is masked off and does not |
| 138113 | 138393 | ** become part of the new Expr.op. However, it does make the |
| 138114 | 138394 | ** op==TK_AND comparison inside of sqlite3PExpr() false, and this |
| | @@ -138255,11 +138535,11 @@ |
| 138255 | 138535 | } |
| 138256 | 138536 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
| 138257 | 138537 | sqlite3VdbeGoto(v, pLevel->addrBrk); |
| 138258 | 138538 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 138259 | 138539 | |
| 138260 | | - if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
| 138540 | + if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); } |
| 138261 | 138541 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 138262 | 138542 | }else |
| 138263 | 138543 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 138264 | 138544 | |
| 138265 | 138545 | { |
| | @@ -138688,26 +138968,28 @@ |
| 138688 | 138968 | for(iFrom=iTo=0; iFrom<cnt; iFrom++){ |
| 138689 | 138969 | if( zNew[iFrom]==wc[3] ) iFrom++; |
| 138690 | 138970 | zNew[iTo++] = zNew[iFrom]; |
| 138691 | 138971 | } |
| 138692 | 138972 | zNew[iTo] = 0; |
| 138973 | + assert( iTo>0 ); |
| 138693 | 138974 | |
| 138694 | | - /* If the RHS begins with a digit or a minus sign, then the LHS must be |
| 138975 | + /* If the RHS begins with a digit or a +/- sign, then the LHS must be |
| 138695 | 138976 | ** an ordinary column (not a virtual table column) with TEXT affinity. |
| 138696 | 138977 | ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false |
| 138697 | 138978 | ** even though "lhs LIKE rhs" is true. But if the RHS does not start |
| 138698 | | - ** with a digit or '-', then "lhs LIKE rhs" will always be false if |
| 138979 | + ** with a digit or +/-, then "lhs LIKE rhs" will always be false if |
| 138699 | 138980 | ** the LHS is numeric and so the optimization still works. |
| 138700 | 138981 | ** |
| 138701 | 138982 | ** 2018-09-10 ticket c94369cae9b561b1f996d0054bfab11389f9d033 |
| 138702 | 138983 | ** The RHS pattern must not be '/%' because the termination condition |
| 138703 | 138984 | ** will then become "x<'0'" and if the affinity is numeric, will then |
| 138704 | 138985 | ** be converted into "x<0", which is incorrect. |
| 138705 | 138986 | */ |
| 138706 | 138987 | if( sqlite3Isdigit(zNew[0]) |
| 138707 | 138988 | || zNew[0]=='-' |
| 138708 | | - || (zNew[0]+1=='0' && iTo==1) |
| 138989 | + || zNew[0]=='+' |
| 138990 | + || zNew[iTo-1]=='0'-1 |
| 138709 | 138991 | ){ |
| 138710 | 138992 | if( pLeft->op!=TK_COLUMN |
| 138711 | 138993 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
| 138712 | 138994 | || IsVirtual(pLeft->y.pTab) /* Value might be numeric */ |
| 138713 | 138995 | ){ |
| | @@ -140770,11 +141052,11 @@ |
| 140770 | 141052 | || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ |
| 140771 | 141053 | if( pLoop->prereq==0 |
| 140772 | 141054 | && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
| 140773 | 141055 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 140774 | 141056 | && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){ |
| 140775 | | - pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
| 141057 | + pPartial = sqlite3ExprAnd(pParse, pPartial, |
| 140776 | 141058 | sqlite3ExprDup(pParse->db, pExpr, 0)); |
| 140777 | 141059 | } |
| 140778 | 141060 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 140779 | 141061 | int iCol = pTerm->u.leftColumn; |
| 140780 | 141062 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
| | @@ -146275,17 +146557,22 @@ |
| 146275 | 146557 | ** expression list pList. Return a pointer to the result list. |
| 146276 | 146558 | */ |
| 146277 | 146559 | static ExprList *exprListAppendList( |
| 146278 | 146560 | Parse *pParse, /* Parsing context */ |
| 146279 | 146561 | ExprList *pList, /* List to which to append. Might be NULL */ |
| 146280 | | - ExprList *pAppend /* List of values to append. Might be NULL */ |
| 146562 | + ExprList *pAppend, /* List of values to append. Might be NULL */ |
| 146563 | + int bIntToNull |
| 146281 | 146564 | ){ |
| 146282 | 146565 | if( pAppend ){ |
| 146283 | 146566 | int i; |
| 146284 | 146567 | int nInit = pList ? pList->nExpr : 0; |
| 146285 | 146568 | for(i=0; i<pAppend->nExpr; i++){ |
| 146286 | 146569 | Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0); |
| 146570 | + if( bIntToNull && pDup && pDup->op==TK_INTEGER ){ |
| 146571 | + pDup->op = TK_NULL; |
| 146572 | + pDup->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse); |
| 146573 | + } |
| 146287 | 146574 | pList = sqlite3ExprListAppend(pParse, pList, pDup); |
| 146288 | 146575 | if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder; |
| 146289 | 146576 | } |
| 146290 | 146577 | } |
| 146291 | 146578 | return pList; |
| | @@ -146321,11 +146608,11 @@ |
| 146321 | 146608 | |
| 146322 | 146609 | /* Create the ORDER BY clause for the sub-select. This is the concatenation |
| 146323 | 146610 | ** of the window PARTITION and ORDER BY clauses. Then, if this makes it |
| 146324 | 146611 | ** redundant, remove the ORDER BY from the parent SELECT. */ |
| 146325 | 146612 | pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0); |
| 146326 | | - pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy); |
| 146613 | + pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1); |
| 146327 | 146614 | if( pSort && p->pOrderBy ){ |
| 146328 | 146615 | if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){ |
| 146329 | 146616 | sqlite3ExprListDelete(db, p->pOrderBy); |
| 146330 | 146617 | p->pOrderBy = 0; |
| 146331 | 146618 | } |
| | @@ -146342,20 +146629,20 @@ |
| 146342 | 146629 | pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0); |
| 146343 | 146630 | |
| 146344 | 146631 | /* Append the PARTITION BY and ORDER BY expressions to the to the |
| 146345 | 146632 | ** sub-select expression list. They are required to figure out where |
| 146346 | 146633 | ** boundaries for partitions and sets of peer rows lie. */ |
| 146347 | | - pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition); |
| 146348 | | - pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy); |
| 146634 | + pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0); |
| 146635 | + pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0); |
| 146349 | 146636 | |
| 146350 | 146637 | /* Append the arguments passed to each window function to the |
| 146351 | 146638 | ** sub-select expression list. Also allocate two registers for each |
| 146352 | 146639 | ** window function - one for the accumulator, another for interim |
| 146353 | 146640 | ** results. */ |
| 146354 | 146641 | for(pWin=pMWin; pWin; pWin=pWin->pNextWin){ |
| 146355 | 146642 | pWin->iArgCol = (pSublist ? pSublist->nExpr : 0); |
| 146356 | | - pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList); |
| 146643 | + pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList, 0); |
| 146357 | 146644 | if( pWin->pFilter ){ |
| 146358 | 146645 | Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0); |
| 146359 | 146646 | pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter); |
| 146360 | 146647 | } |
| 146361 | 146648 | pWin->regAccum = ++pParse->nMem; |
| | @@ -152081,11 +152368,13 @@ |
| 152081 | 152368 | sqlite3ExprListDelete(pParse->db, pList); |
| 152082 | 152369 | } |
| 152083 | 152370 | } |
| 152084 | 152371 | break; |
| 152085 | 152372 | case 179: /* expr ::= expr AND expr */ |
| 152086 | | - case 180: /* expr ::= expr OR expr */ yytestcase(yyruleno==180); |
| 152373 | +{yymsp[-2].minor.yy524=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy524,yymsp[0].minor.yy524);} |
| 152374 | + break; |
| 152375 | + case 180: /* expr ::= expr OR expr */ |
| 152087 | 152376 | case 181: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==181); |
| 152088 | 152377 | case 182: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==182); |
| 152089 | 152378 | case 183: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==183); |
| 152090 | 152379 | case 184: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==184); |
| 152091 | 152380 | case 185: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==185); |
| | @@ -158717,10 +159006,26 @@ |
| 158717 | 159006 | FILE *out = va_arg(ap, FILE*); |
| 158718 | 159007 | if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR; |
| 158719 | 159008 | break; |
| 158720 | 159009 | } |
| 158721 | 159010 | #endif /* defined(YYCOVERAGE) */ |
| 159011 | + |
| 159012 | + /* sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*); |
| 159013 | + ** |
| 159014 | + ** This test-control causes the most recent sqlite3_result_int64() value |
| 159015 | + ** to be interpreted as a MEM_IntReal instead of as an MEM_Int. Normally, |
| 159016 | + ** MEM_IntReal values only arise during an INSERT operation of integer |
| 159017 | + ** values into a REAL column, so they can be challenging to test. This |
| 159018 | + ** test-control enables us to write an intreal() SQL function that can |
| 159019 | + ** inject an intreal() value at arbitrary places in an SQL statement, |
| 159020 | + ** for testing purposes. |
| 159021 | + */ |
| 159022 | + case SQLITE_TESTCTRL_RESULT_INTREAL: { |
| 159023 | + sqlite3_context *pCtx = va_arg(ap, sqlite3_context*); |
| 159024 | + sqlite3ResultIntReal(pCtx); |
| 159025 | + break; |
| 159026 | + } |
| 158722 | 159027 | } |
| 158723 | 159028 | va_end(ap); |
| 158724 | 159029 | #endif /* SQLITE_UNTESTABLE */ |
| 158725 | 159030 | return rc; |
| 158726 | 159031 | } |
| | @@ -174130,11 +174435,11 @@ |
| 174130 | 174435 | if( bFirst==0 ){ |
| 174131 | 174436 | p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix); |
| 174132 | 174437 | } |
| 174133 | 174438 | p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix); |
| 174134 | 174439 | |
| 174135 | | - if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){ |
| 174440 | + if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){ |
| 174136 | 174441 | return FTS_CORRUPT_VTAB; |
| 174137 | 174442 | } |
| 174138 | 174443 | blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc); |
| 174139 | 174444 | if( rc==SQLITE_OK ){ |
| 174140 | 174445 | memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix); |
| | @@ -174149,11 +174454,11 @@ |
| 174149 | 174454 | p->iOff += p->nDoclist; |
| 174150 | 174455 | } |
| 174151 | 174456 | } |
| 174152 | 174457 | } |
| 174153 | 174458 | |
| 174154 | | - assert( p->iOff<=p->nNode ); |
| 174459 | + assert_fts3_nc( p->iOff<=p->nNode ); |
| 174155 | 174460 | return rc; |
| 174156 | 174461 | } |
| 174157 | 174462 | |
| 174158 | 174463 | /* |
| 174159 | 174464 | ** Release all dynamic resources held by node-reader object *p. |
| | @@ -189790,11 +190095,12 @@ |
| 189790 | 190095 | assert( argc==1 || argc==2 ); |
| 189791 | 190096 | |
| 189792 | 190097 | zIn = (const char*)sqlite3_value_text(argv[0]); |
| 189793 | 190098 | if( zIn ){ |
| 189794 | 190099 | if( rbuIsVacuum(p) ){ |
| 189795 | | - if( argc==1 || 0==sqlite3_value_int(argv[1]) ){ |
| 190100 | + assert( argc==2 ); |
| 190101 | + if( 0==sqlite3_value_int(argv[1]) ){ |
| 189796 | 190102 | sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC); |
| 189797 | 190103 | } |
| 189798 | 190104 | }else{ |
| 189799 | 190105 | if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){ |
| 189800 | 190106 | int i; |
| | @@ -190241,11 +190547,12 @@ |
| 190241 | 190547 | SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]); |
| 190242 | 190548 | SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]); |
| 190243 | 190549 | } |
| 190244 | 190550 | |
| 190245 | 190551 | pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc); |
| 190246 | | - pIter->abTblPk[iOrder] = (iPk!=0); |
| 190552 | + assert( iPk>=0 ); |
| 190553 | + pIter->abTblPk[iOrder] = (u8)iPk; |
| 190247 | 190554 | pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0); |
| 190248 | 190555 | iOrder++; |
| 190249 | 190556 | } |
| 190250 | 190557 | } |
| 190251 | 190558 | |
| | @@ -190275,10 +190582,217 @@ |
| 190275 | 190582 | zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z); |
| 190276 | 190583 | zSep = ", "; |
| 190277 | 190584 | } |
| 190278 | 190585 | return zList; |
| 190279 | 190586 | } |
| 190587 | + |
| 190588 | +/* |
| 190589 | +** Return a comma separated list of the quoted PRIMARY KEY column names, |
| 190590 | +** in order, for the current table. Before each column name, add the text |
| 190591 | +** zPre. After each column name, add the zPost text. Use zSeparator as |
| 190592 | +** the separator text (usually ", "). |
| 190593 | +*/ |
| 190594 | +static char *rbuObjIterGetPkList( |
| 190595 | + sqlite3rbu *p, /* RBU object */ |
| 190596 | + RbuObjIter *pIter, /* Object iterator for column names */ |
| 190597 | + const char *zPre, /* Before each quoted column name */ |
| 190598 | + const char *zSeparator, /* Separator to use between columns */ |
| 190599 | + const char *zPost /* After each quoted column name */ |
| 190600 | +){ |
| 190601 | + int iPk = 1; |
| 190602 | + char *zRet = 0; |
| 190603 | + const char *zSep = ""; |
| 190604 | + while( 1 ){ |
| 190605 | + int i; |
| 190606 | + for(i=0; i<pIter->nTblCol; i++){ |
| 190607 | + if( (int)pIter->abTblPk[i]==iPk ){ |
| 190608 | + const char *zCol = pIter->azTblCol[i]; |
| 190609 | + zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost); |
| 190610 | + zSep = zSeparator; |
| 190611 | + break; |
| 190612 | + } |
| 190613 | + } |
| 190614 | + if( i==pIter->nTblCol ) break; |
| 190615 | + iPk++; |
| 190616 | + } |
| 190617 | + return zRet; |
| 190618 | +} |
| 190619 | + |
| 190620 | +/* |
| 190621 | +** This function is called as part of restarting an RBU vacuum within |
| 190622 | +** stage 1 of the process (while the *-oal file is being built) while |
| 190623 | +** updating a table (not an index). The table may be a rowid table or |
| 190624 | +** a WITHOUT ROWID table. It queries the target database to find the |
| 190625 | +** largest key that has already been written to the target table and |
| 190626 | +** constructs a WHERE clause that can be used to extract the remaining |
| 190627 | +** rows from the source table. For a rowid table, the WHERE clause |
| 190628 | +** is of the form: |
| 190629 | +** |
| 190630 | +** "WHERE _rowid_ > ?" |
| 190631 | +** |
| 190632 | +** and for WITHOUT ROWID tables: |
| 190633 | +** |
| 190634 | +** "WHERE (key1, key2) > (?, ?)" |
| 190635 | +** |
| 190636 | +** Instead of "?" placeholders, the actual WHERE clauses created by |
| 190637 | +** this function contain literal SQL values. |
| 190638 | +*/ |
| 190639 | +static char *rbuVacuumTableStart( |
| 190640 | + sqlite3rbu *p, /* RBU handle */ |
| 190641 | + RbuObjIter *pIter, /* RBU iterator object */ |
| 190642 | + int bRowid, /* True for a rowid table */ |
| 190643 | + const char *zWrite /* Target table name prefix */ |
| 190644 | +){ |
| 190645 | + sqlite3_stmt *pMax = 0; |
| 190646 | + char *zRet = 0; |
| 190647 | + if( bRowid ){ |
| 190648 | + p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg, |
| 190649 | + sqlite3_mprintf( |
| 190650 | + "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl |
| 190651 | + ) |
| 190652 | + ); |
| 190653 | + if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){ |
| 190654 | + sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0); |
| 190655 | + zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax); |
| 190656 | + } |
| 190657 | + rbuFinalize(p, pMax); |
| 190658 | + }else{ |
| 190659 | + char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC"); |
| 190660 | + char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")"); |
| 190661 | + char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", ""); |
| 190662 | + |
| 190663 | + if( p->rc==SQLITE_OK ){ |
| 190664 | + p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg, |
| 190665 | + sqlite3_mprintf( |
| 190666 | + "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1", |
| 190667 | + zSelect, zWrite, pIter->zTbl, zOrder |
| 190668 | + ) |
| 190669 | + ); |
| 190670 | + if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){ |
| 190671 | + const char *zVal = (const char*)sqlite3_column_text(pMax, 0); |
| 190672 | + zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal); |
| 190673 | + } |
| 190674 | + rbuFinalize(p, pMax); |
| 190675 | + } |
| 190676 | + |
| 190677 | + sqlite3_free(zOrder); |
| 190678 | + sqlite3_free(zSelect); |
| 190679 | + sqlite3_free(zList); |
| 190680 | + } |
| 190681 | + return zRet; |
| 190682 | +} |
| 190683 | + |
| 190684 | +/* |
| 190685 | +** This function is called as part of restating an RBU vacuum when the |
| 190686 | +** current operation is writing content to an index. If possible, it |
| 190687 | +** queries the target index b-tree for the largest key already written to |
| 190688 | +** it, then composes and returns an expression that can be used in a WHERE |
| 190689 | +** clause to select the remaining required rows from the source table. |
| 190690 | +** It is only possible to return such an expression if: |
| 190691 | +** |
| 190692 | +** * The index contains no DESC columns, and |
| 190693 | +** * The last key written to the index before the operation was |
| 190694 | +** suspended does not contain any NULL values. |
| 190695 | +** |
| 190696 | +** The expression is of the form: |
| 190697 | +** |
| 190698 | +** (index-field1, index-field2, ...) > (?, ?, ...) |
| 190699 | +** |
| 190700 | +** except that the "?" placeholders are replaced with literal values. |
| 190701 | +** |
| 190702 | +** If the expression cannot be created, NULL is returned. In this case, |
| 190703 | +** the caller has to use an OFFSET clause to extract only the required |
| 190704 | +** rows from the sourct table, just as it does for an RBU update operation. |
| 190705 | +*/ |
| 190706 | +char *rbuVacuumIndexStart( |
| 190707 | + sqlite3rbu *p, /* RBU handle */ |
| 190708 | + RbuObjIter *pIter /* RBU iterator object */ |
| 190709 | +){ |
| 190710 | + char *zOrder = 0; |
| 190711 | + char *zLhs = 0; |
| 190712 | + char *zSelect = 0; |
| 190713 | + char *zVector = 0; |
| 190714 | + char *zRet = 0; |
| 190715 | + int bFailed = 0; |
| 190716 | + const char *zSep = ""; |
| 190717 | + int iCol = 0; |
| 190718 | + sqlite3_stmt *pXInfo = 0; |
| 190719 | + |
| 190720 | + p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg, |
| 190721 | + sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx) |
| 190722 | + ); |
| 190723 | + while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){ |
| 190724 | + int iCid = sqlite3_column_int(pXInfo, 1); |
| 190725 | + const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4); |
| 190726 | + const char *zCol; |
| 190727 | + if( sqlite3_column_int(pXInfo, 3) ){ |
| 190728 | + bFailed = 1; |
| 190729 | + break; |
| 190730 | + } |
| 190731 | + |
| 190732 | + if( iCid<0 ){ |
| 190733 | + if( pIter->eType==RBU_PK_IPK ){ |
| 190734 | + int i; |
| 190735 | + for(i=0; pIter->abTblPk[i]==0; i++); |
| 190736 | + assert( i<pIter->nTblCol ); |
| 190737 | + zCol = pIter->azTblCol[i]; |
| 190738 | + }else{ |
| 190739 | + zCol = "_rowid_"; |
| 190740 | + } |
| 190741 | + }else{ |
| 190742 | + zCol = pIter->azTblCol[iCid]; |
| 190743 | + } |
| 190744 | + |
| 190745 | + zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q", |
| 190746 | + zLhs, zSep, zCol, zCollate |
| 190747 | + ); |
| 190748 | + zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC", |
| 190749 | + zOrder, zSep, iCol, zCol, zCollate |
| 190750 | + ); |
| 190751 | + zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")", |
| 190752 | + zSelect, zSep, iCol, zCol |
| 190753 | + ); |
| 190754 | + zSep = ", "; |
| 190755 | + iCol++; |
| 190756 | + } |
| 190757 | + rbuFinalize(p, pXInfo); |
| 190758 | + if( bFailed ) goto index_start_out; |
| 190759 | + |
| 190760 | + if( p->rc==SQLITE_OK ){ |
| 190761 | + sqlite3_stmt *pSel = 0; |
| 190762 | + |
| 190763 | + p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg, |
| 190764 | + sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1", |
| 190765 | + zSelect, pIter->zTbl, zOrder |
| 190766 | + ) |
| 190767 | + ); |
| 190768 | + if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){ |
| 190769 | + zSep = ""; |
| 190770 | + for(iCol=0; iCol<pIter->nCol; iCol++){ |
| 190771 | + const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol); |
| 190772 | + if( zQuoted[0]=='N' ){ |
| 190773 | + bFailed = 1; |
| 190774 | + break; |
| 190775 | + } |
| 190776 | + zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted); |
| 190777 | + zSep = ", "; |
| 190778 | + } |
| 190779 | + |
| 190780 | + if( !bFailed ){ |
| 190781 | + zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector); |
| 190782 | + } |
| 190783 | + } |
| 190784 | + rbuFinalize(p, pSel); |
| 190785 | + } |
| 190786 | + |
| 190787 | + index_start_out: |
| 190788 | + sqlite3_free(zOrder); |
| 190789 | + sqlite3_free(zSelect); |
| 190790 | + sqlite3_free(zVector); |
| 190791 | + sqlite3_free(zLhs); |
| 190792 | + return zRet; |
| 190793 | +} |
| 190280 | 190794 | |
| 190281 | 190795 | /* |
| 190282 | 190796 | ** This function is used to create a SELECT list (the list of SQL |
| 190283 | 190797 | ** expressions that follows a SELECT keyword) for a SELECT statement |
| 190284 | 190798 | ** used to read from an data_xxx or rbu_tmp_xxx table while updating the |
| | @@ -190952,16 +191466,28 @@ |
| 190952 | 191466 | |
| 190953 | 191467 | /* Create the SELECT statement to read keys in sorted order */ |
| 190954 | 191468 | if( p->rc==SQLITE_OK ){ |
| 190955 | 191469 | char *zSql; |
| 190956 | 191470 | if( rbuIsVacuum(p) ){ |
| 191471 | + char *zStart = 0; |
| 191472 | + if( nOffset ){ |
| 191473 | + zStart = rbuVacuumIndexStart(p, pIter); |
| 191474 | + if( zStart ){ |
| 191475 | + sqlite3_free(zLimit); |
| 191476 | + zLimit = 0; |
| 191477 | + } |
| 191478 | + } |
| 191479 | + |
| 190957 | 191480 | zSql = sqlite3_mprintf( |
| 190958 | | - "SELECT %s, 0 AS rbu_control FROM '%q' %s ORDER BY %s%s", |
| 191481 | + "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s", |
| 190959 | 191482 | zCollist, |
| 190960 | 191483 | pIter->zDataTbl, |
| 190961 | | - zPart, zCollist, zLimit |
| 191484 | + zPart, |
| 191485 | + (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart, |
| 191486 | + zCollist, zLimit |
| 190962 | 191487 | ); |
| 191488 | + sqlite3_free(zStart); |
| 190963 | 191489 | }else |
| 190964 | 191490 | |
| 190965 | 191491 | if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){ |
| 190966 | 191492 | zSql = sqlite3_mprintf( |
| 190967 | 191493 | "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s", |
| | @@ -190980,11 +191506,15 @@ |
| 190980 | 191506 | zPart, |
| 190981 | 191507 | (zPart ? "AND" : "WHERE"), |
| 190982 | 191508 | zCollist, zLimit |
| 190983 | 191509 | ); |
| 190984 | 191510 | } |
| 190985 | | - p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql); |
| 191511 | + if( p->rc==SQLITE_OK ){ |
| 191512 | + p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql); |
| 191513 | + }else{ |
| 191514 | + sqlite3_free(zSql); |
| 191515 | + } |
| 190986 | 191516 | } |
| 190987 | 191517 | |
| 190988 | 191518 | sqlite3_free(zImposterCols); |
| 190989 | 191519 | sqlite3_free(zImposterPK); |
| 190990 | 191520 | sqlite3_free(zWhere); |
| | @@ -191080,22 +191610,46 @@ |
| 191080 | 191610 | } |
| 191081 | 191611 | |
| 191082 | 191612 | /* Create the SELECT statement to read keys from data_xxx */ |
| 191083 | 191613 | if( p->rc==SQLITE_OK ){ |
| 191084 | 191614 | const char *zRbuRowid = ""; |
| 191615 | + char *zStart = 0; |
| 191616 | + char *zOrder = 0; |
| 191085 | 191617 | if( bRbuRowid ){ |
| 191086 | 191618 | zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid"; |
| 191087 | 191619 | } |
| 191088 | | - p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, |
| 191089 | | - sqlite3_mprintf( |
| 191090 | | - "SELECT %s,%s rbu_control%s FROM '%q'%s", |
| 191091 | | - zCollist, |
| 191092 | | - (rbuIsVacuum(p) ? "0 AS " : ""), |
| 191093 | | - zRbuRowid, |
| 191094 | | - pIter->zDataTbl, zLimit |
| 191095 | | - ) |
| 191096 | | - ); |
| 191620 | + |
| 191621 | + if( rbuIsVacuum(p) ){ |
| 191622 | + if( nOffset ){ |
| 191623 | + zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite); |
| 191624 | + if( zStart ){ |
| 191625 | + sqlite3_free(zLimit); |
| 191626 | + zLimit = 0; |
| 191627 | + } |
| 191628 | + } |
| 191629 | + if( bRbuRowid ){ |
| 191630 | + zOrder = rbuMPrintf(p, "_rowid_"); |
| 191631 | + }else{ |
| 191632 | + zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", ""); |
| 191633 | + } |
| 191634 | + } |
| 191635 | + |
| 191636 | + if( p->rc==SQLITE_OK ){ |
| 191637 | + p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, |
| 191638 | + sqlite3_mprintf( |
| 191639 | + "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s", |
| 191640 | + zCollist, |
| 191641 | + (rbuIsVacuum(p) ? "0 AS " : ""), |
| 191642 | + zRbuRowid, |
| 191643 | + pIter->zDataTbl, (zStart ? zStart : ""), |
| 191644 | + (zOrder ? "ORDER BY" : ""), zOrder, |
| 191645 | + zLimit |
| 191646 | + ) |
| 191647 | + ); |
| 191648 | + } |
| 191649 | + sqlite3_free(zStart); |
| 191650 | + sqlite3_free(zOrder); |
| 191097 | 191651 | } |
| 191098 | 191652 | |
| 191099 | 191653 | sqlite3_free(zWhere); |
| 191100 | 191654 | sqlite3_free(zOldlist); |
| 191101 | 191655 | sqlite3_free(zNewlist); |
| | @@ -193661,11 +194215,12 @@ |
| 193661 | 194215 | ** be intercepted (see the rbuVfsOpen() function) and the *-oal |
| 193662 | 194216 | ** file opened instead. |
| 193663 | 194217 | */ |
| 193664 | 194218 | if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){ |
| 193665 | 194219 | rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1); |
| 193666 | | - if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){ |
| 194220 | + if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){ |
| 194221 | + assert( pDb->pRbu ); |
| 193667 | 194222 | if( *pResOut ){ |
| 193668 | 194223 | rc = SQLITE_CANTOPEN; |
| 193669 | 194224 | }else{ |
| 193670 | 194225 | sqlite3_int64 sz = 0; |
| 193671 | 194226 | rc = rbuVfsFileSize(&pDb->base, &sz); |
| | @@ -204285,11 +204840,15 @@ |
| 204285 | 204840 | return 1; |
| 204286 | 204841 | }else{ |
| 204287 | 204842 | i64 iOff = *piOff; |
| 204288 | 204843 | int iVal; |
| 204289 | 204844 | fts5FastGetVarint32(a, i, iVal); |
| 204290 | | - if( iVal==1 ){ |
| 204845 | + if( iVal<=1 ){ |
| 204846 | + if( iVal==0 ){ |
| 204847 | + *pi = i; |
| 204848 | + return 0; |
| 204849 | + } |
| 204291 | 204850 | fts5FastGetVarint32(a, i, iVal); |
| 204292 | 204851 | iOff = ((i64)iVal) << 32; |
| 204293 | 204852 | fts5FastGetVarint32(a, i, iVal); |
| 204294 | 204853 | } |
| 204295 | 204854 | *piOff = iOff + ((iVal-2) & 0x7FFFFFFF); |
| | @@ -218101,11 +218660,11 @@ |
| 218101 | 218660 | int nArg, /* Number of args */ |
| 218102 | 218661 | sqlite3_value **apUnused /* Function arguments */ |
| 218103 | 218662 | ){ |
| 218104 | 218663 | assert( nArg==0 ); |
| 218105 | 218664 | UNUSED_PARAM2(nArg, apUnused); |
| 218106 | | - sqlite3_result_text(pCtx, "fts5: 2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f83156b50", -1, SQLITE_TRANSIENT); |
| 218665 | + sqlite3_result_text(pCtx, "fts5: 2019-05-10 17:50:33 2846bc0429c0956473bfe99dde135f2c206720f0be4c2800118b280e446ce325", -1, SQLITE_TRANSIENT); |
| 218107 | 218666 | } |
| 218108 | 218667 | |
| 218109 | 218668 | /* |
| 218110 | 218669 | ** Return true if zName is the extension on one of the shadow tables used |
| 218111 | 218670 | ** by this module. |
| | @@ -222865,12 +223424,12 @@ |
| 222865 | 223424 | } |
| 222866 | 223425 | #endif /* SQLITE_CORE */ |
| 222867 | 223426 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */ |
| 222868 | 223427 | |
| 222869 | 223428 | /************** End of stmt.c ************************************************/ |
| 222870 | | -#if __LINE__!=222870 |
| 223429 | +#if __LINE__!=223429 |
| 222871 | 223430 | #undef SQLITE_SOURCE_ID |
| 222872 | | -#define SQLITE_SOURCE_ID "2019-04-16 19:49:53 884b4b7e502b4e991677b53971277adfaf0a04a284f8e483e2553d0f8315alt2" |
| 223431 | +#define SQLITE_SOURCE_ID "2019-05-10 17:54:58 956ca2a452aa3707bca553007a7ef221af3d4f6b0af747d17070926e000falt2" |
| 222873 | 223432 | #endif |
| 222874 | 223433 | /* Return the source-id for this library */ |
| 222875 | 223434 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } |
| 222876 | 223435 | /************************** End of sqlite3.c ******************************/ |
| 222877 | 223436 | |