| | @@ -16,11 +16,11 @@ |
| 16 | 16 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 17 | 17 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 18 | 18 | ** separate file. This file contains only code for the core SQLite library. |
| 19 | 19 | ** |
| 20 | 20 | ** The content in this amalgamation comes from Fossil check-in |
| 21 | | -** a5f77862c0fe0189aa4246a1e55bb7c537c. |
| 21 | +** eab3c98639be531744e60440223bb9ee76b. |
| 22 | 22 | */ |
| 23 | 23 | #define SQLITE_CORE 1 |
| 24 | 24 | #define SQLITE_AMALGAMATION 1 |
| 25 | 25 | #ifndef SQLITE_PRIVATE |
| 26 | 26 | # define SQLITE_PRIVATE static |
| | @@ -459,11 +459,11 @@ |
| 459 | 459 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 460 | 460 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 461 | 461 | */ |
| 462 | 462 | #define SQLITE_VERSION "3.43.0" |
| 463 | 463 | #define SQLITE_VERSION_NUMBER 3043000 |
| 464 | | -#define SQLITE_SOURCE_ID "2023-06-23 11:10:13 fa5f77862c0fe0189aa4246a1e55bb7c537c28c436ec10b75f5fa141e5e4aff0" |
| 464 | +#define SQLITE_SOURCE_ID "2023-07-08 14:27:55 beab3c98639be531744e60440223bb9ee76bc15234aff05e5efb273c8241dfd8" |
| 465 | 465 | |
| 466 | 466 | /* |
| 467 | 467 | ** CAPI3REF: Run-Time Library Version Numbers |
| 468 | 468 | ** KEYWORDS: sqlite3_version sqlite3_sourceid |
| 469 | 469 | ** |
| | @@ -8487,11 +8487,12 @@ |
| 8487 | 8487 | #define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29 |
| 8488 | 8488 | #define SQLITE_TESTCTRL_SEEK_COUNT 30 |
| 8489 | 8489 | #define SQLITE_TESTCTRL_TRACEFLAGS 31 |
| 8490 | 8490 | #define SQLITE_TESTCTRL_TUNE 32 |
| 8491 | 8491 | #define SQLITE_TESTCTRL_LOGEST 33 |
| 8492 | | -#define SQLITE_TESTCTRL_LAST 33 /* Largest TESTCTRL */ |
| 8492 | +#define SQLITE_TESTCTRL_USELONGDOUBLE 34 |
| 8493 | +#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */ |
| 8493 | 8494 | |
| 8494 | 8495 | /* |
| 8495 | 8496 | ** CAPI3REF: SQL Keyword Checking |
| 8496 | 8497 | ** |
| 8497 | 8498 | ** These routines provide access to the set of SQL language keywords |
| | @@ -14934,14 +14935,16 @@ |
| 14934 | 14935 | typedef struct Column Column; |
| 14935 | 14936 | typedef struct Cte Cte; |
| 14936 | 14937 | typedef struct CteUse CteUse; |
| 14937 | 14938 | typedef struct Db Db; |
| 14938 | 14939 | typedef struct DbFixer DbFixer; |
| 14940 | +typedef struct DblDbl DblDbl; |
| 14939 | 14941 | typedef struct Schema Schema; |
| 14940 | 14942 | typedef struct Expr Expr; |
| 14941 | 14943 | typedef struct ExprList ExprList; |
| 14942 | 14944 | typedef struct FKey FKey; |
| 14945 | +typedef struct FpDecode FpDecode; |
| 14943 | 14946 | typedef struct FuncDestructor FuncDestructor; |
| 14944 | 14947 | typedef struct FuncDef FuncDef; |
| 14945 | 14948 | typedef struct FuncDefHash FuncDefHash; |
| 14946 | 14949 | typedef struct IdList IdList; |
| 14947 | 14950 | typedef struct Index Index; |
| | @@ -16399,11 +16402,11 @@ |
| 16399 | 16402 | #define OPFLG_INITIALIZER {\ |
| 16400 | 16403 | /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\ |
| 16401 | 16404 | /* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\ |
| 16402 | 16405 | /* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\ |
| 16403 | 16406 | /* 24 */ 0x49, 0x01, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,\ |
| 16404 | | -/* 32 */ 0x41, 0x01, 0x01, 0x01, 0x41, 0x01, 0x41, 0x41,\ |
| 16407 | +/* 32 */ 0x41, 0x01, 0x41, 0x41, 0x41, 0x01, 0x41, 0x41,\ |
| 16405 | 16408 | /* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\ |
| 16406 | 16409 | /* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\ |
| 16407 | 16410 | /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\ |
| 16408 | 16411 | /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\ |
| 16409 | 16412 | /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\ |
| | @@ -16411,11 +16414,11 @@ |
| 16411 | 16414 | /* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\ |
| 16412 | 16415 | /* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x26, 0x26,\ |
| 16413 | 16416 | /* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\ |
| 16414 | 16417 | /* 112 */ 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40, 0x00,\ |
| 16415 | 16418 | /* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\ |
| 16416 | | -/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,\ |
| 16419 | +/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,\ |
| 16417 | 16420 | /* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\ |
| 16418 | 16421 | /* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\ |
| 16419 | 16422 | /* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\ |
| 16420 | 16423 | /* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 16421 | 16424 | /* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x50, 0x40,\ |
| | @@ -19706,10 +19709,11 @@ |
| 19706 | 19709 | u8 bFullMutex; /* True to enable full mutexing */ |
| 19707 | 19710 | u8 bOpenUri; /* True to interpret filenames as URIs */ |
| 19708 | 19711 | u8 bUseCis; /* Use covering indices for full-scans */ |
| 19709 | 19712 | u8 bSmallMalloc; /* Avoid large memory allocations if true */ |
| 19710 | 19713 | u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */ |
| 19714 | + u8 bUseLongDouble; /* Make use of long double */ |
| 19711 | 19715 | int mxStrlen; /* Maximum string length */ |
| 19712 | 19716 | int neverCorrupt; /* Database is always well-formed */ |
| 19713 | 19717 | int szLookaside; /* Default lookaside buffer size */ |
| 19714 | 19718 | int nLookaside; /* Default lookaside buffer count */ |
| 19715 | 19719 | int nStmtSpill; /* Stmt-journal spill-to-disk threshold */ |
| | @@ -20214,10 +20218,24 @@ |
| 20214 | 20218 | int nArg; /* Total number of arguments */ |
| 20215 | 20219 | int nUsed; /* Number of arguments used so far */ |
| 20216 | 20220 | sqlite3_value **apArg; /* The argument values */ |
| 20217 | 20221 | }; |
| 20218 | 20222 | |
| 20223 | +/* |
| 20224 | +** An instance of this object receives the decoding of a floating point |
| 20225 | +** value into an approximate decimal representation. |
| 20226 | +*/ |
| 20227 | +struct FpDecode { |
| 20228 | + char sign; /* '+' or '-' */ |
| 20229 | + char isSpecial; /* 1: Infinity 2: NaN */ |
| 20230 | + int n; /* Significant digits in the decode */ |
| 20231 | + int iDP; /* Location of the decimal point */ |
| 20232 | + char *z; /* Start of significant digits */ |
| 20233 | + char zBuf[24]; /* Storage for significant digits */ |
| 20234 | +}; |
| 20235 | + |
| 20236 | +SQLITE_PRIVATE void sqlite3FpDecode(FpDecode*,double,int,int); |
| 20219 | 20237 | SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...); |
| 20220 | 20238 | SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list); |
| 20221 | 20239 | #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 20222 | 20240 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...); |
| 20223 | 20241 | #endif |
| | @@ -20653,10 +20671,11 @@ |
| 20653 | 20671 | SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*); |
| 20654 | 20672 | SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*); |
| 20655 | 20673 | SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*); |
| 20656 | 20674 | SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*); |
| 20657 | 20675 | SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*); |
| 20676 | + |
| 20658 | 20677 | SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64); |
| 20659 | 20678 | SQLITE_PRIVATE i64 sqlite3RealToI64(double); |
| 20660 | 20679 | SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*); |
| 20661 | 20680 | SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8); |
| 20662 | 20681 | SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*); |
| | @@ -22374,10 +22393,11 @@ |
| 22374 | 22393 | SQLITE_THREADSAFE==1, /* bFullMutex */ |
| 22375 | 22394 | SQLITE_USE_URI, /* bOpenUri */ |
| 22376 | 22395 | SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */ |
| 22377 | 22396 | 0, /* bSmallMalloc */ |
| 22378 | 22397 | 1, /* bExtraSchemaChecks */ |
| 22398 | + sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */ |
| 22379 | 22399 | 0x7ffffffe, /* mxStrlen */ |
| 22380 | 22400 | 0, /* neverCorrupt */ |
| 22381 | 22401 | SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */ |
| 22382 | 22402 | SQLITE_STMTJRNL_SPILL, /* nStmtSpill */ |
| 22383 | 22403 | {0,0,0,0,0,0,0,0}, /* m */ |
| | @@ -30416,61 +30436,10 @@ |
| 30416 | 30436 | ** |
| 30417 | 30437 | ** %S Takes a pointer to SrcItem. Shows name or database.name |
| 30418 | 30438 | ** %!S Like %S but prefer the zName over the zAlias |
| 30419 | 30439 | */ |
| 30420 | 30440 | |
| 30421 | | -/* Floating point constants used for rounding */ |
| 30422 | | -static const double arRound[] = { |
| 30423 | | - 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05, |
| 30424 | | - 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10, |
| 30425 | | -}; |
| 30426 | | - |
| 30427 | | -/* |
| 30428 | | -** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
| 30429 | | -** conversions will work. |
| 30430 | | -*/ |
| 30431 | | -#ifndef SQLITE_OMIT_FLOATING_POINT |
| 30432 | | -/* |
| 30433 | | -** "*val" is a double such that 0.1 <= *val < 10.0 |
| 30434 | | -** Return the ascii code for the leading digit of *val, then |
| 30435 | | -** multiply "*val" by 10.0 to renormalize. |
| 30436 | | -** |
| 30437 | | -** Example: |
| 30438 | | -** input: *val = 3.14159 |
| 30439 | | -** output: *val = 1.4159 function return = '3' |
| 30440 | | -** |
| 30441 | | -** The counter *cnt is incremented each time. After counter exceeds |
| 30442 | | -** 16 (the number of significant digits in a 64-bit float) '0' is |
| 30443 | | -** always returned. |
| 30444 | | -*/ |
| 30445 | | -static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ |
| 30446 | | - int digit; |
| 30447 | | - LONGDOUBLE_TYPE d; |
| 30448 | | - if( (*cnt)<=0 ) return '0'; |
| 30449 | | - (*cnt)--; |
| 30450 | | - digit = (int)*val; |
| 30451 | | - d = digit; |
| 30452 | | - digit += '0'; |
| 30453 | | - *val = (*val - d)*10.0; |
| 30454 | | - return (char)digit; |
| 30455 | | -} |
| 30456 | | -#endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 30457 | | - |
| 30458 | | -#ifndef SQLITE_OMIT_FLOATING_POINT |
| 30459 | | -/* |
| 30460 | | -** "*val" is a u64. *msd is a divisor used to extract the |
| 30461 | | -** most significant digit of *val. Extract that most significant |
| 30462 | | -** digit and return it. |
| 30463 | | -*/ |
| 30464 | | -static char et_getdigit_int(u64 *val, u64 *msd){ |
| 30465 | | - u64 x = (*val)/(*msd); |
| 30466 | | - *val -= x*(*msd); |
| 30467 | | - if( *msd>=10 ) *msd /= 10; |
| 30468 | | - return '0' + (char)(x & 15); |
| 30469 | | -} |
| 30470 | | -#endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 30471 | | - |
| 30472 | 30441 | /* |
| 30473 | 30442 | ** Set the StrAccum object to an error mode. |
| 30474 | 30443 | */ |
| 30475 | 30444 | SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){ |
| 30476 | 30445 | assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG ); |
| | @@ -30558,24 +30527,19 @@ |
| 30558 | 30527 | etByte cThousand; /* Thousands separator for %d and %u */ |
| 30559 | 30528 | etByte xtype = etINVALID; /* Conversion paradigm */ |
| 30560 | 30529 | u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
| 30561 | 30530 | char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 30562 | 30531 | sqlite_uint64 longvalue; /* Value for integer types */ |
| 30563 | | - LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 30564 | | - sqlite_uint64 msd; /* Divisor to get most-significant-digit |
| 30565 | | - ** of longvalue */ |
| 30532 | + double realvalue; /* Value for real types */ |
| 30566 | 30533 | const et_info *infop; /* Pointer to the appropriate info structure */ |
| 30567 | 30534 | char *zOut; /* Rendering buffer */ |
| 30568 | 30535 | int nOut; /* Size of the rendering buffer */ |
| 30569 | 30536 | char *zExtra = 0; /* Malloced memory used by some conversion */ |
| 30570 | | -#ifndef SQLITE_OMIT_FLOATING_POINT |
| 30571 | | - int exp, e2; /* exponent of real numbers */ |
| 30572 | | - int nsd; /* Number of significant digits returned */ |
| 30573 | | - double rounder; /* Used for rounding floating point values */ |
| 30537 | + int exp, e2; /* exponent of real numbers */ |
| 30574 | 30538 | etByte flag_dp; /* True if decimal point should be shown */ |
| 30575 | 30539 | etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 30576 | | -#endif |
| 30540 | + |
| 30577 | 30541 | PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 30578 | 30542 | char buf[etBUFSIZE]; /* Conversion buffer */ |
| 30579 | 30543 | |
| 30580 | 30544 | /* pAccum never starts out with an empty buffer that was obtained from |
| 30581 | 30545 | ** malloc(). This precondition is required by the mprintf("%z...") |
| | @@ -30846,98 +30810,65 @@ |
| 30846 | 30810 | } |
| 30847 | 30811 | length = (int)(&zOut[nOut-1]-bufpt); |
| 30848 | 30812 | break; |
| 30849 | 30813 | case etFLOAT: |
| 30850 | 30814 | case etEXP: |
| 30851 | | - case etGENERIC: |
| 30815 | + case etGENERIC: { |
| 30816 | + FpDecode s; |
| 30817 | + int iRound; |
| 30818 | + int j; |
| 30819 | + |
| 30852 | 30820 | if( bArgList ){ |
| 30853 | 30821 | realvalue = getDoubleArg(pArgList); |
| 30854 | 30822 | }else{ |
| 30855 | 30823 | realvalue = va_arg(ap,double); |
| 30856 | 30824 | } |
| 30857 | | -#ifdef SQLITE_OMIT_FLOATING_POINT |
| 30858 | | - length = 0; |
| 30859 | | -#else |
| 30860 | 30825 | if( precision<0 ) precision = 6; /* Set default precision */ |
| 30861 | 30826 | #ifdef SQLITE_FP_PRECISION_LIMIT |
| 30862 | 30827 | if( precision>SQLITE_FP_PRECISION_LIMIT ){ |
| 30863 | 30828 | precision = SQLITE_FP_PRECISION_LIMIT; |
| 30864 | 30829 | } |
| 30865 | 30830 | #endif |
| 30866 | | - if( realvalue<0.0 ){ |
| 30867 | | - realvalue = -realvalue; |
| 30831 | + if( xtype==etFLOAT ){ |
| 30832 | + iRound = -precision; |
| 30833 | + }else if( xtype==etGENERIC ){ |
| 30834 | + iRound = precision; |
| 30835 | + }else{ |
| 30836 | + iRound = precision+1; |
| 30837 | + } |
| 30838 | + sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16); |
| 30839 | + if( s.isSpecial ){ |
| 30840 | + if( s.isSpecial==2 ){ |
| 30841 | + bufpt = flag_zeropad ? "null" : "NaN"; |
| 30842 | + length = sqlite3Strlen30(bufpt); |
| 30843 | + break; |
| 30844 | + }else if( flag_zeropad ){ |
| 30845 | + s.z[0] = '9'; |
| 30846 | + s.iDP = 1000; |
| 30847 | + s.n = 1; |
| 30848 | + }else{ |
| 30849 | + memcpy(buf, "-Inf", 5); |
| 30850 | + bufpt = buf; |
| 30851 | + if( s.sign=='-' ){ |
| 30852 | + /* no-op */ |
| 30853 | + }else if( flag_prefix ){ |
| 30854 | + buf[0] = flag_prefix; |
| 30855 | + }else{ |
| 30856 | + bufpt++; |
| 30857 | + } |
| 30858 | + length = sqlite3Strlen30(bufpt); |
| 30859 | + break; |
| 30860 | + } |
| 30861 | + } |
| 30862 | + if( s.sign=='-' ){ |
| 30868 | 30863 | prefix = '-'; |
| 30869 | 30864 | }else{ |
| 30870 | 30865 | prefix = flag_prefix; |
| 30871 | 30866 | } |
| 30872 | | - exp = 0; |
| 30867 | + |
| 30868 | + exp = s.iDP-1; |
| 30873 | 30869 | if( xtype==etGENERIC && precision>0 ) precision--; |
| 30874 | | - testcase( precision>0xfff ); |
| 30875 | | - if( realvalue<1.0e+16 |
| 30876 | | - && realvalue==(LONGDOUBLE_TYPE)(longvalue = (u64)realvalue) |
| 30877 | | - ){ |
| 30878 | | - /* Number is a pure integer that can be represented as u64 */ |
| 30879 | | - for(msd=1; msd*10<=longvalue; msd *= 10, exp++){} |
| 30880 | | - if( exp>precision && xtype!=etFLOAT ){ |
| 30881 | | - u64 rnd = msd/2; |
| 30882 | | - int kk = precision; |
| 30883 | | - while( kk-- > 0 ){ rnd /= 10; } |
| 30884 | | - longvalue += rnd; |
| 30885 | | - } |
| 30886 | | - }else{ |
| 30887 | | - msd = 0; |
| 30888 | | - longvalue = 0; /* To prevent a compiler warning */ |
| 30889 | | - idx = precision & 0xfff; |
| 30890 | | - rounder = arRound[idx%10]; |
| 30891 | | - while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; } |
| 30892 | | - if( xtype==etFLOAT ){ |
| 30893 | | - double rx = (double)realvalue; |
| 30894 | | - sqlite3_uint64 u; |
| 30895 | | - int ex; |
| 30896 | | - memcpy(&u, &rx, sizeof(u)); |
| 30897 | | - ex = -1023 + (int)((u>>52)&0x7ff); |
| 30898 | | - if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16; |
| 30899 | | - realvalue += rounder; |
| 30900 | | - } |
| 30901 | | - if( sqlite3IsNaN((double)realvalue) ){ |
| 30902 | | - if( flag_zeropad ){ |
| 30903 | | - bufpt = "null"; |
| 30904 | | - length = 4; |
| 30905 | | - }else{ |
| 30906 | | - bufpt = "NaN"; |
| 30907 | | - length = 3; |
| 30908 | | - } |
| 30909 | | - break; |
| 30910 | | - } |
| 30911 | | - |
| 30912 | | - /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 30913 | | - if( ALWAYS(realvalue>0.0) ){ |
| 30914 | | - LONGDOUBLE_TYPE scale = 1.0; |
| 30915 | | - while( realvalue>=1e100*scale && exp<=350){ scale*=1e100;exp+=100;} |
| 30916 | | - while( realvalue>=1e10*scale && exp<=350 ){ scale*=1e10; exp+=10; } |
| 30917 | | - while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } |
| 30918 | | - realvalue /= scale; |
| 30919 | | - while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } |
| 30920 | | - while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } |
| 30921 | | - if( exp>350 ){ |
| 30922 | | - if( flag_zeropad ){ |
| 30923 | | - realvalue = 9.0; |
| 30924 | | - exp = 999; |
| 30925 | | - }else{ |
| 30926 | | - bufpt = buf; |
| 30927 | | - buf[0] = prefix; |
| 30928 | | - memcpy(buf+(prefix!=0),"Inf",4); |
| 30929 | | - length = 3+(prefix!=0); |
| 30930 | | - break; |
| 30931 | | - } |
| 30932 | | - } |
| 30933 | | - if( xtype!=etFLOAT ){ |
| 30934 | | - realvalue += rounder; |
| 30935 | | - if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 30936 | | - } |
| 30937 | | - } |
| 30938 | | - } |
| 30939 | 30870 | |
| 30940 | 30871 | /* |
| 30941 | 30872 | ** If the field type is etGENERIC, then convert to either etEXP |
| 30942 | 30873 | ** or etFLOAT, as appropriate. |
| 30943 | 30874 | */ |
| | @@ -30953,13 +30884,12 @@ |
| 30953 | 30884 | flag_rtz = flag_altform2; |
| 30954 | 30885 | } |
| 30955 | 30886 | if( xtype==etEXP ){ |
| 30956 | 30887 | e2 = 0; |
| 30957 | 30888 | }else{ |
| 30958 | | - e2 = exp; |
| 30889 | + e2 = s.iDP - 1; |
| 30959 | 30890 | } |
| 30960 | | - nsd = 16 + flag_altform2*10; |
| 30961 | 30891 | bufpt = buf; |
| 30962 | 30892 | { |
| 30963 | 30893 | i64 szBufNeeded; /* Size of a temporary buffer needed */ |
| 30964 | 30894 | szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15; |
| 30965 | 30895 | if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3; |
| | @@ -30973,42 +30903,31 @@ |
| 30973 | 30903 | /* The sign in front of the number */ |
| 30974 | 30904 | if( prefix ){ |
| 30975 | 30905 | *(bufpt++) = prefix; |
| 30976 | 30906 | } |
| 30977 | 30907 | /* Digits prior to the decimal point */ |
| 30908 | + j = 0; |
| 30978 | 30909 | if( e2<0 ){ |
| 30979 | 30910 | *(bufpt++) = '0'; |
| 30980 | | - }else if( msd>0 ){ |
| 30981 | | - for(; e2>=0; e2--){ |
| 30982 | | - *(bufpt++) = et_getdigit_int(&longvalue,&msd); |
| 30983 | | - if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ','; |
| 30984 | | - } |
| 30985 | 30911 | }else{ |
| 30986 | 30912 | for(; e2>=0; e2--){ |
| 30987 | | - *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 30913 | + *(bufpt++) = j<s.n ? s.z[j++] : '0'; |
| 30988 | 30914 | if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ','; |
| 30989 | 30915 | } |
| 30990 | 30916 | } |
| 30991 | 30917 | /* The decimal point */ |
| 30992 | 30918 | if( flag_dp ){ |
| 30993 | 30919 | *(bufpt++) = '.'; |
| 30994 | 30920 | } |
| 30995 | 30921 | /* "0" digits after the decimal point but before the first |
| 30996 | 30922 | ** significant digit of the number */ |
| 30997 | | - for(e2++; e2<0; precision--, e2++){ |
| 30998 | | - assert( precision>0 ); |
| 30923 | + for(e2++; e2<0 && precision>0; precision--, e2++){ |
| 30999 | 30924 | *(bufpt++) = '0'; |
| 31000 | 30925 | } |
| 31001 | 30926 | /* Significant digits after the decimal point */ |
| 31002 | | - if( msd>0 ){ |
| 31003 | | - while( (precision--)>0 ){ |
| 31004 | | - *(bufpt++) = et_getdigit_int(&longvalue,&msd); |
| 31005 | | - } |
| 31006 | | - }else{ |
| 31007 | | - while( (precision--)>0 ){ |
| 31008 | | - *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 31009 | | - } |
| 30927 | + while( (precision--)>0 ){ |
| 30928 | + *(bufpt++) = j<s.n ? s.z[j++] : '0'; |
| 31010 | 30929 | } |
| 31011 | 30930 | /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 31012 | 30931 | if( flag_rtz && flag_dp ){ |
| 31013 | 30932 | while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
| 31014 | 30933 | assert( bufpt>zOut ); |
| | @@ -31020,10 +30939,11 @@ |
| 31020 | 30939 | } |
| 31021 | 30940 | } |
| 31022 | 30941 | } |
| 31023 | 30942 | /* Add the "eNNN" suffix */ |
| 31024 | 30943 | if( xtype==etEXP ){ |
| 30944 | + exp = s.iDP - 1; |
| 31025 | 30945 | *(bufpt++) = aDigits[infop->charset]; |
| 31026 | 30946 | if( exp<0 ){ |
| 31027 | 30947 | *(bufpt++) = '-'; exp = -exp; |
| 31028 | 30948 | }else{ |
| 31029 | 30949 | *(bufpt++) = '+'; |
| | @@ -31053,12 +30973,12 @@ |
| 31053 | 30973 | } |
| 31054 | 30974 | i = prefix!=0; |
| 31055 | 30975 | while( nPad-- ) bufpt[i++] = '0'; |
| 31056 | 30976 | length = width; |
| 31057 | 30977 | } |
| 31058 | | -#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ |
| 31059 | 30978 | break; |
| 30979 | + } |
| 31060 | 30980 | case etSIZE: |
| 31061 | 30981 | if( !bArgList ){ |
| 31062 | 30982 | *(va_arg(ap,int*)) = pAccum->nChar; |
| 31063 | 30983 | } |
| 31064 | 30984 | length = width = 0; |
| | @@ -34447,47 +34367,44 @@ |
| 34447 | 34367 | z++; |
| 34448 | 34368 | } |
| 34449 | 34369 | return h; |
| 34450 | 34370 | } |
| 34451 | 34371 | |
| 34452 | | -/* |
| 34453 | | -** Compute 10 to the E-th power. Examples: E==1 results in 10. |
| 34454 | | -** E==2 results in 100. E==50 results in 1.0e50. |
| 34372 | +/* Double-Double multiplication. (x[0],x[1]) *= (y,yy) |
| 34455 | 34373 | ** |
| 34456 | | -** This routine only works for values of E between 1 and 341. |
| 34374 | +** Reference: |
| 34375 | +** T. J. Dekker, "A Floating-Point Technique for Extending the |
| 34376 | +** Available Precision". 1971-07-26. |
| 34457 | 34377 | */ |
| 34458 | | -static LONGDOUBLE_TYPE sqlite3Pow10(int E){ |
| 34459 | | -#if defined(_MSC_VER) |
| 34460 | | - static const LONGDOUBLE_TYPE x[] = { |
| 34461 | | - 1.0e+001L, |
| 34462 | | - 1.0e+002L, |
| 34463 | | - 1.0e+004L, |
| 34464 | | - 1.0e+008L, |
| 34465 | | - 1.0e+016L, |
| 34466 | | - 1.0e+032L, |
| 34467 | | - 1.0e+064L, |
| 34468 | | - 1.0e+128L, |
| 34469 | | - 1.0e+256L |
| 34470 | | - }; |
| 34471 | | - LONGDOUBLE_TYPE r = 1.0; |
| 34472 | | - int i; |
| 34473 | | - assert( E>=0 && E<=307 ); |
| 34474 | | - for(i=0; E!=0; i++, E >>=1){ |
| 34475 | | - if( E & 1 ) r *= x[i]; |
| 34476 | | - } |
| 34477 | | - return r; |
| 34478 | | -#else |
| 34479 | | - LONGDOUBLE_TYPE x = 10.0; |
| 34480 | | - LONGDOUBLE_TYPE r = 1.0; |
| 34481 | | - while(1){ |
| 34482 | | - if( E & 1 ) r *= x; |
| 34483 | | - E >>= 1; |
| 34484 | | - if( E==0 ) break; |
| 34485 | | - x *= x; |
| 34486 | | - } |
| 34487 | | - return r; |
| 34488 | | -#endif |
| 34378 | +static void dekkerMul2(volatile double *x, double y, double yy){ |
| 34379 | + /* |
| 34380 | + ** The "volatile" keywords on parameter x[] and on local variables |
| 34381 | + ** below are needed force intermediate results to be truncated to |
| 34382 | + ** binary64 rather than be carried around in an extended-precision |
| 34383 | + ** format. The truncation is necessary for the Dekker algorithm to |
| 34384 | + ** work. Intel x86 floating point might omit the truncation without |
| 34385 | + ** the use of volatile. |
| 34386 | + */ |
| 34387 | + volatile double tx, ty, p, q, c, cc; |
| 34388 | + double hx, hy; |
| 34389 | + u64 m; |
| 34390 | + memcpy(&m, (void*)&x[0], 8); |
| 34391 | + m &= 0xfffffffffc000000L; |
| 34392 | + memcpy(&hx, &m, 8); |
| 34393 | + tx = x[0] - hx; |
| 34394 | + memcpy(&m, &y, 8); |
| 34395 | + m &= 0xfffffffffc000000L; |
| 34396 | + memcpy(&hy, &m, 8); |
| 34397 | + ty = y - hy; |
| 34398 | + p = hx*hy; |
| 34399 | + q = hx*ty + tx*hy; |
| 34400 | + c = p+q; |
| 34401 | + cc = p - c + q + tx*ty; |
| 34402 | + cc = x[0]*yy + x[1]*y + cc; |
| 34403 | + x[0] = c + cc; |
| 34404 | + x[1] = c - x[0]; |
| 34405 | + x[1] += cc; |
| 34489 | 34406 | } |
| 34490 | 34407 | |
| 34491 | 34408 | /* |
| 34492 | 34409 | ** The string z[] is an text representation of a real number. |
| 34493 | 34410 | ** Convert this string to a double and write it into *pResult. |
| | @@ -34524,16 +34441,15 @@ |
| 34524 | 34441 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 34525 | 34442 | int incr; |
| 34526 | 34443 | const char *zEnd; |
| 34527 | 34444 | /* sign * significand * (10 ^ (esign * exponent)) */ |
| 34528 | 34445 | int sign = 1; /* sign of significand */ |
| 34529 | | - i64 s = 0; /* significand */ |
| 34446 | + u64 s = 0; /* significand */ |
| 34530 | 34447 | int d = 0; /* adjust exponent for shifting decimal point */ |
| 34531 | 34448 | int esign = 1; /* sign of exponent */ |
| 34532 | 34449 | int e = 0; /* exponent */ |
| 34533 | 34450 | int eValid = 1; /* True exponent is either not used or is well-formed */ |
| 34534 | | - double result; |
| 34535 | 34451 | int nDigit = 0; /* Number of digits processed */ |
| 34536 | 34452 | int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ |
| 34537 | 34453 | |
| 34538 | 34454 | assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 34539 | 34455 | *pResult = 0.0; /* Default return value, in case of an error */ |
| | @@ -34569,11 +34485,11 @@ |
| 34569 | 34485 | |
| 34570 | 34486 | /* copy max significant digits to significand */ |
| 34571 | 34487 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 34572 | 34488 | s = s*10 + (*z - '0'); |
| 34573 | 34489 | z+=incr; nDigit++; |
| 34574 | | - if( s>=((LARGEST_INT64-9)/10) ){ |
| 34490 | + if( s>=((LARGEST_UINT64-9)/10) ){ |
| 34575 | 34491 | /* skip non-significant significand digits |
| 34576 | 34492 | ** (increase exponent by d to shift decimal left) */ |
| 34577 | 34493 | while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; } |
| 34578 | 34494 | } |
| 34579 | 34495 | } |
| | @@ -34584,11 +34500,11 @@ |
| 34584 | 34500 | z+=incr; |
| 34585 | 34501 | eType++; |
| 34586 | 34502 | /* copy digits from after decimal to significand |
| 34587 | 34503 | ** (decrease exponent by d to shift decimal right) */ |
| 34588 | 34504 | while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 34589 | | - if( s<((LARGEST_INT64-9)/10) ){ |
| 34505 | + if( s<((LARGEST_UINT64-9)/10) ){ |
| 34590 | 34506 | s = s*10 + (*z - '0'); |
| 34591 | 34507 | d--; |
| 34592 | 34508 | nDigit++; |
| 34593 | 34509 | } |
| 34594 | 34510 | z+=incr; |
| | @@ -34624,82 +34540,83 @@ |
| 34624 | 34540 | |
| 34625 | 34541 | /* skip trailing spaces */ |
| 34626 | 34542 | while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
| 34627 | 34543 | |
| 34628 | 34544 | do_atof_calc: |
| 34545 | + /* Zero is a special case */ |
| 34546 | + if( s==0 ){ |
| 34547 | + *pResult = sign<0 ? -0.0 : +0.0; |
| 34548 | + goto atof_return; |
| 34549 | + } |
| 34550 | + |
| 34629 | 34551 | /* adjust exponent by d, and update sign */ |
| 34630 | 34552 | e = (e*esign) + d; |
| 34631 | | - if( e<0 ) { |
| 34632 | | - esign = -1; |
| 34633 | | - e *= -1; |
| 34634 | | - } else { |
| 34635 | | - esign = 1; |
| 34636 | | - } |
| 34637 | | - |
| 34638 | | - if( s==0 ) { |
| 34639 | | - /* In the IEEE 754 standard, zero is signed. */ |
| 34640 | | - result = sign<0 ? -(double)0 : (double)0; |
| 34641 | | - } else { |
| 34642 | | - /* Attempt to reduce exponent. |
| 34643 | | - ** |
| 34644 | | - ** Branches that are not required for the correct answer but which only |
| 34645 | | - ** help to obtain the correct answer faster are marked with special |
| 34646 | | - ** comments, as a hint to the mutation tester. |
| 34647 | | - */ |
| 34648 | | - while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 34649 | | - if( esign>0 ){ |
| 34650 | | - if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/ |
| 34651 | | - s *= 10; |
| 34652 | | - }else{ |
| 34653 | | - if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/ |
| 34654 | | - s /= 10; |
| 34655 | | - } |
| 34656 | | - e--; |
| 34657 | | - } |
| 34658 | | - |
| 34659 | | - /* adjust the sign of significand */ |
| 34660 | | - s = sign<0 ? -s : s; |
| 34661 | | - |
| 34662 | | - if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 34663 | | - result = (double)s; |
| 34664 | | - }else{ |
| 34665 | | - /* attempt to handle extremely small/large numbers better */ |
| 34666 | | - if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 34667 | | - if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 34668 | | - LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308); |
| 34669 | | - if( esign<0 ){ |
| 34670 | | - result = s / scale; |
| 34671 | | - result /= 1.0e+308; |
| 34672 | | - }else{ |
| 34673 | | - result = s * scale; |
| 34674 | | - result *= 1.0e+308; |
| 34675 | | - } |
| 34676 | | - }else{ assert( e>=342 ); |
| 34677 | | - if( esign<0 ){ |
| 34678 | | - result = 0.0*s; |
| 34679 | | - }else{ |
| 34680 | | -#ifdef INFINITY |
| 34681 | | - result = INFINITY*s; |
| 34682 | | -#else |
| 34683 | | - result = 1e308*1e308*s; /* Infinity */ |
| 34684 | | -#endif |
| 34685 | | - } |
| 34686 | | - } |
| 34687 | | - }else{ |
| 34688 | | - LONGDOUBLE_TYPE scale = sqlite3Pow10(e); |
| 34689 | | - if( esign<0 ){ |
| 34690 | | - result = s / scale; |
| 34691 | | - }else{ |
| 34692 | | - result = s * scale; |
| 34693 | | - } |
| 34694 | | - } |
| 34695 | | - } |
| 34696 | | - } |
| 34697 | | - |
| 34698 | | - /* store the result */ |
| 34699 | | - *pResult = result; |
| 34700 | | - |
| 34553 | + |
| 34554 | + /* Try to adjust the exponent to make it smaller */ |
| 34555 | + while( e>0 && s<(LARGEST_UINT64/10) ){ |
| 34556 | + s *= 10; |
| 34557 | + e--; |
| 34558 | + } |
| 34559 | + while( e<0 && (s%10)==0 ){ |
| 34560 | + s /= 10; |
| 34561 | + e++; |
| 34562 | + } |
| 34563 | + |
| 34564 | + if( e==0 ){ |
| 34565 | + *pResult = s; |
| 34566 | + }else if( sqlite3Config.bUseLongDouble ){ |
| 34567 | + LONGDOUBLE_TYPE r = (LONGDOUBLE_TYPE)s; |
| 34568 | + if( e>0 ){ |
| 34569 | + while( e>=100 ){ e-=100; r *= 1.0e+100L; } |
| 34570 | + while( e>=10 ){ e-=10; r *= 1.0e+10L; } |
| 34571 | + while( e>=1 ){ e-=1; r *= 1.0e+01L; } |
| 34572 | + }else{ |
| 34573 | + while( e<=-100 ){ e+=100; r *= 1.0e-100L; } |
| 34574 | + while( e<=-10 ){ e+=10; r *= 1.0e-10L; } |
| 34575 | + while( e<=-1 ){ e+=1; r *= 1.0e-01L; } |
| 34576 | + } |
| 34577 | + *pResult = r; |
| 34578 | + }else{ |
| 34579 | + double rr[2]; |
| 34580 | + u64 s2; |
| 34581 | + rr[0] = (double)s; |
| 34582 | + s2 = (u64)rr[0]; |
| 34583 | + rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); |
| 34584 | + if( e>0 ){ |
| 34585 | + while( e>=100 ){ |
| 34586 | + e -= 100; |
| 34587 | + dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
| 34588 | + } |
| 34589 | + while( e>=10 ){ |
| 34590 | + e -= 10; |
| 34591 | + dekkerMul2(rr, 1.0e+10, 0.0); |
| 34592 | + } |
| 34593 | + while( e>=1 ){ |
| 34594 | + e -= 1; |
| 34595 | + dekkerMul2(rr, 1.0e+01, 0.0); |
| 34596 | + } |
| 34597 | + }else{ |
| 34598 | + while( e<=-100 ){ |
| 34599 | + e += 100; |
| 34600 | + dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); |
| 34601 | + } |
| 34602 | + while( e<=-10 ){ |
| 34603 | + e += 10; |
| 34604 | + dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); |
| 34605 | + } |
| 34606 | + while( e<=-1 ){ |
| 34607 | + e += 1; |
| 34608 | + dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); |
| 34609 | + } |
| 34610 | + } |
| 34611 | + *pResult = rr[0]+rr[1]; |
| 34612 | + if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300; |
| 34613 | + } |
| 34614 | + if( sign<0 ) *pResult = -*pResult; |
| 34615 | + assert( !sqlite3IsNaN(*pResult) ); |
| 34616 | + |
| 34617 | +atof_return: |
| 34701 | 34618 | /* return true if number and no extra non-whitespace characters after */ |
| 34702 | 34619 | if( z==zEnd && nDigit>0 && eValid && eType>0 ){ |
| 34703 | 34620 | return eType; |
| 34704 | 34621 | }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){ |
| 34705 | 34622 | return -1; |
| | @@ -34988,10 +34905,157 @@ |
| 34988 | 34905 | SQLITE_PRIVATE int sqlite3Atoi(const char *z){ |
| 34989 | 34906 | int x = 0; |
| 34990 | 34907 | sqlite3GetInt32(z, &x); |
| 34991 | 34908 | return x; |
| 34992 | 34909 | } |
| 34910 | + |
| 34911 | +/* |
| 34912 | +** Decode a floating-point value into an approximate decimal |
| 34913 | +** representation. |
| 34914 | +** |
| 34915 | +** Round the decimal representation to n significant digits if |
| 34916 | +** n is positive. Or round to -n signficant digits after the |
| 34917 | +** decimal point if n is negative. No rounding is performed if |
| 34918 | +** n is zero. |
| 34919 | +** |
| 34920 | +** The significant digits of the decimal representation are |
| 34921 | +** stored in p->z[] which is a often (but not always) a pointer |
| 34922 | +** into the middle of p->zBuf[]. There are p->n significant digits. |
| 34923 | +** The p->z[] array is *not* zero-terminated. |
| 34924 | +*/ |
| 34925 | +SQLITE_PRIVATE void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){ |
| 34926 | + int i; |
| 34927 | + u64 v; |
| 34928 | + int e, exp = 0; |
| 34929 | + p->isSpecial = 0; |
| 34930 | + p->z = p->zBuf; |
| 34931 | + |
| 34932 | + /* Convert negative numbers to positive. Deal with Infinity, 0.0, and |
| 34933 | + ** NaN. */ |
| 34934 | + if( r<0.0 ){ |
| 34935 | + p->sign = '-'; |
| 34936 | + r = -r; |
| 34937 | + }else if( r==0.0 ){ |
| 34938 | + p->sign = '+'; |
| 34939 | + p->n = 1; |
| 34940 | + p->iDP = 1; |
| 34941 | + p->z = "0"; |
| 34942 | + return; |
| 34943 | + }else{ |
| 34944 | + p->sign = '+'; |
| 34945 | + } |
| 34946 | + memcpy(&v,&r,8); |
| 34947 | + e = v>>52; |
| 34948 | + if( (e&0x7ff)==0x7ff ){ |
| 34949 | + p->isSpecial = 1 + (v!=0x7ff0000000000000L); |
| 34950 | + p->n = 0; |
| 34951 | + p->iDP = 0; |
| 34952 | + return; |
| 34953 | + } |
| 34954 | + |
| 34955 | + /* Multiply r by powers of ten until it lands somewhere in between |
| 34956 | + ** 1.0e+19 and 1.0e+17. |
| 34957 | + */ |
| 34958 | + if( sqlite3Config.bUseLongDouble ){ |
| 34959 | + LONGDOUBLE_TYPE rr = r; |
| 34960 | + if( rr>=1.0e+19 ){ |
| 34961 | + while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; } |
| 34962 | + while( rr>=1.0e+29L ){ exp+=10; rr *= 1.0e-10L; } |
| 34963 | + while( rr>=1.0e+19L ){ exp++; rr *= 1.0e-1L; } |
| 34964 | + }else{ |
| 34965 | + while( rr<1.0e-97L ){ exp-=100; rr *= 1.0e+100L; } |
| 34966 | + while( rr<1.0e+07L ){ exp-=10; rr *= 1.0e+10L; } |
| 34967 | + while( rr<1.0e+17L ){ exp--; rr *= 1.0e+1L; } |
| 34968 | + } |
| 34969 | + v = (u64)rr; |
| 34970 | + }else{ |
| 34971 | + /* If high-precision floating point is not available using "long double", |
| 34972 | + ** then use Dekker-style double-double computation to increase the |
| 34973 | + ** precision. |
| 34974 | + ** |
| 34975 | + ** The error terms on constants like 1.0e+100 computed using the |
| 34976 | + ** decimal extension, for example as follows: |
| 34977 | + ** |
| 34978 | + ** SELECT decimal_sci(decimal_sub('1.0e+100',decimal(1.0e+100))); |
| 34979 | + */ |
| 34980 | + double rr[2]; |
| 34981 | + rr[0] = r; |
| 34982 | + rr[1] = 0.0; |
| 34983 | + if( rr[0]>1.84e+19 ){ |
| 34984 | + while( rr[0]>1.84e+119 ){ |
| 34985 | + exp += 100; |
| 34986 | + dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); |
| 34987 | + } |
| 34988 | + while( rr[0]>1.84e+29 ){ |
| 34989 | + exp += 10; |
| 34990 | + dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); |
| 34991 | + } |
| 34992 | + while( rr[0]>1.84e+19 ){ |
| 34993 | + exp += 1; |
| 34994 | + dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); |
| 34995 | + } |
| 34996 | + }else{ |
| 34997 | + while( rr[0]<1.84e-82 ){ |
| 34998 | + exp -= 100; |
| 34999 | + dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); |
| 35000 | + } |
| 35001 | + while( rr[0]<1.84e+08 ){ |
| 35002 | + exp -= 10; |
| 35003 | + dekkerMul2(rr, 1.0e+10, 0.0); |
| 35004 | + } |
| 35005 | + while( rr[0]<1.84e+18 ){ |
| 35006 | + exp -= 1; |
| 35007 | + dekkerMul2(rr, 1.0e+01, 0.0); |
| 35008 | + } |
| 35009 | + } |
| 35010 | + v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; |
| 35011 | + } |
| 35012 | + |
| 35013 | + |
| 35014 | + /* Extract significant digits. */ |
| 35015 | + i = sizeof(p->zBuf)-1; |
| 35016 | + assert( v>0 ); |
| 35017 | + while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } |
| 35018 | + assert( i>=0 && i<sizeof(p->zBuf)-1 ); |
| 35019 | + p->n = sizeof(p->zBuf) - 1 - i; |
| 35020 | + assert( p->n>0 ); |
| 35021 | + assert( p->n<sizeof(p->zBuf) ); |
| 35022 | + p->iDP = p->n + exp; |
| 35023 | + if( iRound<0 ){ |
| 35024 | + iRound = p->iDP - iRound; |
| 35025 | + if( iRound==0 && p->zBuf[i+1]>='5' ){ |
| 35026 | + iRound = 1; |
| 35027 | + p->zBuf[i--] = '0'; |
| 35028 | + p->n++; |
| 35029 | + p->iDP++; |
| 35030 | + } |
| 35031 | + } |
| 35032 | + if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ |
| 35033 | + char *z = &p->zBuf[i+1]; |
| 35034 | + if( iRound>mxRound ) iRound = mxRound; |
| 35035 | + p->n = iRound; |
| 35036 | + if( z[iRound]>='5' ){ |
| 35037 | + int j = iRound-1; |
| 35038 | + while( 1 /*exit-by-break*/ ){ |
| 35039 | + z[j]++; |
| 35040 | + if( z[j]<='9' ) break; |
| 35041 | + z[j] = '0'; |
| 35042 | + if( j==0 ){ |
| 35043 | + p->z[i--] = '1'; |
| 35044 | + p->n++; |
| 35045 | + p->iDP++; |
| 35046 | + break; |
| 35047 | + }else{ |
| 35048 | + j--; |
| 35049 | + } |
| 35050 | + } |
| 35051 | + } |
| 35052 | + } |
| 35053 | + p->z = &p->zBuf[i+1]; |
| 35054 | + assert( i+p->n < sizeof(p->zBuf) ); |
| 35055 | + while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; } |
| 35056 | +} |
| 34993 | 35057 | |
| 34994 | 35058 | /* |
| 34995 | 35059 | ** Try to convert z into an unsigned 32-bit integer. Return true on |
| 34996 | 35060 | ** success and false if there is an error. |
| 34997 | 35061 | ** |
| | @@ -74248,10 +74312,11 @@ |
| 74248 | 74312 | pCur->aiIdx[pCur->iPage] = pCur->ix; |
| 74249 | 74313 | pCur->apPage[pCur->iPage] = pCur->pPage; |
| 74250 | 74314 | pCur->ix = 0; |
| 74251 | 74315 | pCur->iPage++; |
| 74252 | 74316 | rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags); |
| 74317 | + assert( pCur->pPage!=0 || rc!=SQLITE_OK ); |
| 74253 | 74318 | if( rc==SQLITE_OK |
| 74254 | 74319 | && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey) |
| 74255 | 74320 | ){ |
| 74256 | 74321 | releasePage(pCur->pPage); |
| 74257 | 74322 | rc = SQLITE_CORRUPT_PGNO(newPgno); |
| | @@ -74476,11 +74541,11 @@ |
| 74476 | 74541 | if( rc==SQLITE_OK ){ |
| 74477 | 74542 | assert( pCur->pPage->nCell>0 ); |
| 74478 | 74543 | *pRes = 0; |
| 74479 | 74544 | rc = moveToLeftmost(pCur); |
| 74480 | 74545 | }else if( rc==SQLITE_EMPTY ){ |
| 74481 | | - assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); |
| 74546 | + assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) ); |
| 74482 | 74547 | *pRes = 1; |
| 74483 | 74548 | rc = SQLITE_OK; |
| 74484 | 74549 | } |
| 74485 | 74550 | return rc; |
| 74486 | 74551 | } |
| | @@ -81636,40 +81701,10 @@ |
| 81636 | 81701 | SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem *p){ |
| 81637 | 81702 | assert( !VdbeMemDynamic(p) ); |
| 81638 | 81703 | if( p->szMalloc ) vdbeMemClear(p); |
| 81639 | 81704 | } |
| 81640 | 81705 | |
| 81641 | | -/* |
| 81642 | | -** Convert a 64-bit IEEE double into a 64-bit signed integer. |
| 81643 | | -** If the double is out of range of a 64-bit signed integer then |
| 81644 | | -** return the closest available 64-bit signed integer. |
| 81645 | | -*/ |
| 81646 | | -static SQLITE_NOINLINE i64 doubleToInt64(double r){ |
| 81647 | | -#ifdef SQLITE_OMIT_FLOATING_POINT |
| 81648 | | - /* When floating-point is omitted, double and int64 are the same thing */ |
| 81649 | | - return r; |
| 81650 | | -#else |
| 81651 | | - /* |
| 81652 | | - ** Many compilers we encounter do not define constants for the |
| 81653 | | - ** minimum and maximum 64-bit integers, or they define them |
| 81654 | | - ** inconsistently. And many do not understand the "LL" notation. |
| 81655 | | - ** So we define our own static constants here using nothing |
| 81656 | | - ** larger than a 32-bit integer constant. |
| 81657 | | - */ |
| 81658 | | - static const i64 maxInt = LARGEST_INT64; |
| 81659 | | - static const i64 minInt = SMALLEST_INT64; |
| 81660 | | - |
| 81661 | | - if( r<=(double)minInt ){ |
| 81662 | | - return minInt; |
| 81663 | | - }else if( r>=(double)maxInt ){ |
| 81664 | | - return maxInt; |
| 81665 | | - }else{ |
| 81666 | | - return (i64)r; |
| 81667 | | - } |
| 81668 | | -#endif |
| 81669 | | -} |
| 81670 | | - |
| 81671 | 81706 | /* |
| 81672 | 81707 | ** Return some kind of integer value which is the best we can do |
| 81673 | 81708 | ** at representing the value that *pMem describes as an integer. |
| 81674 | 81709 | ** If pMem is an integer, then the value is exact. If pMem is |
| 81675 | 81710 | ** a floating-point then the value returned is the integer part. |
| | @@ -81692,11 +81727,11 @@ |
| 81692 | 81727 | flags = pMem->flags; |
| 81693 | 81728 | if( flags & (MEM_Int|MEM_IntReal) ){ |
| 81694 | 81729 | testcase( flags & MEM_IntReal ); |
| 81695 | 81730 | return pMem->u.i; |
| 81696 | 81731 | }else if( flags & MEM_Real ){ |
| 81697 | | - return doubleToInt64(pMem->u.r); |
| 81732 | + return sqlite3RealToI64(pMem->u.r); |
| 81698 | 81733 | }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){ |
| 81699 | 81734 | return memIntValue(pMem); |
| 81700 | 81735 | }else{ |
| 81701 | 81736 | return 0; |
| 81702 | 81737 | } |
| | @@ -81754,11 +81789,11 @@ |
| 81754 | 81789 | assert( EIGHT_BYTE_ALIGNMENT(pMem) ); |
| 81755 | 81790 | |
| 81756 | 81791 | if( pMem->flags & MEM_IntReal ){ |
| 81757 | 81792 | MemSetTypeFlag(pMem, MEM_Int); |
| 81758 | 81793 | }else{ |
| 81759 | | - i64 ix = doubleToInt64(pMem->u.r); |
| 81794 | + i64 ix = sqlite3RealToI64(pMem->u.r); |
| 81760 | 81795 | |
| 81761 | 81796 | /* Only mark the value as an integer if |
| 81762 | 81797 | ** |
| 81763 | 81798 | ** (1) the round-trip conversion real->int->real is a no-op, and |
| 81764 | 81799 | ** (2) The integer is neither the largest nor the smallest |
| | @@ -81822,12 +81857,12 @@ |
| 81822 | 81857 | /* Convert a floating point value to its closest integer. Do so in |
| 81823 | 81858 | ** a way that avoids 'outside the range of representable values' warnings |
| 81824 | 81859 | ** from UBSAN. |
| 81825 | 81860 | */ |
| 81826 | 81861 | SQLITE_PRIVATE i64 sqlite3RealToI64(double r){ |
| 81827 | | - if( r<=(double)SMALLEST_INT64 ) return SMALLEST_INT64; |
| 81828 | | - if( r>=(double)LARGEST_INT64) return LARGEST_INT64; |
| 81862 | + if( r<-9223372036854774784.0 ) return SMALLEST_INT64; |
| 81863 | + if( r>+9223372036854774784.0 ) return LARGEST_INT64; |
| 81829 | 81864 | return (i64)r; |
| 81830 | 81865 | } |
| 81831 | 81866 | |
| 81832 | 81867 | /* |
| 81833 | 81868 | ** Convert pMem so that it has type MEM_Real or MEM_Int. |
| | @@ -83601,11 +83636,11 @@ |
| 83601 | 83636 | zMsg, P4_DYNAMIC); |
| 83602 | 83637 | sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z); |
| 83603 | 83638 | if( bPush){ |
| 83604 | 83639 | pParse->addrExplain = iThis; |
| 83605 | 83640 | } |
| 83606 | | - sqlite3VdbeScanStatus(v, iThis, 0, 0, 0, 0); |
| 83641 | + sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0); |
| 83607 | 83642 | } |
| 83608 | 83643 | return addr; |
| 83609 | 83644 | } |
| 83610 | 83645 | |
| 83611 | 83646 | /* |
| | @@ -84313,12 +84348,12 @@ |
| 84313 | 84348 | pScan = &p->aScan[ii]; |
| 84314 | 84349 | if( pScan->addrExplain==addrExplain ) break; |
| 84315 | 84350 | pScan = 0; |
| 84316 | 84351 | } |
| 84317 | 84352 | if( pScan ){ |
| 84318 | | - pScan->addrLoop = addrLoop; |
| 84319 | | - pScan->addrVisit = addrVisit; |
| 84353 | + if( addrLoop>0 ) pScan->addrLoop = addrLoop; |
| 84354 | + if( addrVisit>0 ) pScan->addrVisit = addrVisit; |
| 84320 | 84355 | } |
| 84321 | 84356 | } |
| 84322 | 84357 | } |
| 84323 | 84358 | #endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */ |
| 84324 | 84359 | |
| | @@ -96891,11 +96926,11 @@ |
| 96891 | 96926 | ** a register that is the source for a pseudo-table cursor created using |
| 96892 | 96927 | ** OpenPseudo. That pseudo-table cursor is the one that is identified by |
| 96893 | 96928 | ** parameter P3. Clearing the P3 column cache as part of this opcode saves |
| 96894 | 96929 | ** us from having to issue a separate NullRow instruction to clear that cache. |
| 96895 | 96930 | */ |
| 96896 | | -case OP_SorterData: { |
| 96931 | +case OP_SorterData: { /* ncycle */ |
| 96897 | 96932 | VdbeCursor *pC; |
| 96898 | 96933 | |
| 96899 | 96934 | pOut = &aMem[pOp->p2]; |
| 96900 | 96935 | pC = p->apCsr[pOp->p1]; |
| 96901 | 96936 | assert( isSorter(pC) ); |
| | @@ -97166,12 +97201,12 @@ |
| 97166 | 97201 | ** end. We use the OP_Sort opcode instead of OP_Rewind to do the |
| 97167 | 97202 | ** rewinding so that the global variable will be incremented and |
| 97168 | 97203 | ** regression tests can determine whether or not the optimizer is |
| 97169 | 97204 | ** correctly optimizing out sorts. |
| 97170 | 97205 | */ |
| 97171 | | -case OP_SorterSort: /* jump */ |
| 97172 | | -case OP_Sort: { /* jump */ |
| 97206 | +case OP_SorterSort: /* jump ncycle */ |
| 97207 | +case OP_Sort: { /* jump ncycle */ |
| 97173 | 97208 | #ifdef SQLITE_TEST |
| 97174 | 97209 | sqlite3_sort_count++; |
| 97175 | 97210 | sqlite3_search_count--; |
| 97176 | 97211 | #endif |
| 97177 | 97212 | p->aCounter[SQLITE_STMTSTATUS_SORT]++; |
| | @@ -126267,11 +126302,11 @@ |
| 126267 | 126302 | if( r<-4503599627370496.0 || r>+4503599627370496.0 ){ |
| 126268 | 126303 | /* The value has no fractional part so there is nothing to round */ |
| 126269 | 126304 | }else if( n==0 ){ |
| 126270 | 126305 | r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5))); |
| 126271 | 126306 | }else{ |
| 126272 | | - zBuf = sqlite3_mprintf("%.*f",n,r); |
| 126307 | + zBuf = sqlite3_mprintf("%!.*f",n,r); |
| 126273 | 126308 | if( zBuf==0 ){ |
| 126274 | 126309 | sqlite3_result_error_nomem(context); |
| 126275 | 126310 | return; |
| 126276 | 126311 | } |
| 126277 | 126312 | sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8); |
| | @@ -127476,16 +127511,71 @@ |
| 127476 | 127511 | ** An instance of the following structure holds the context of a |
| 127477 | 127512 | ** sum() or avg() aggregate computation. |
| 127478 | 127513 | */ |
| 127479 | 127514 | typedef struct SumCtx SumCtx; |
| 127480 | 127515 | struct SumCtx { |
| 127481 | | - double rSum; /* Floating point sum */ |
| 127482 | | - i64 iSum; /* Integer sum */ |
| 127516 | + double rSum; /* Running sum as as a double */ |
| 127517 | + double rErr; /* Error term for Kahan-Babushka-Neumaier summation */ |
| 127518 | + i64 iSum; /* Running sum as a signed integer */ |
| 127483 | 127519 | i64 cnt; /* Number of elements summed */ |
| 127484 | | - u8 overflow; /* True if integer overflow seen */ |
| 127485 | | - u8 approx; /* True if non-integer value was input to the sum */ |
| 127520 | + u8 approx; /* True if any non-integer value was input to the sum */ |
| 127521 | + u8 ovrfl; /* Integer overflow seen */ |
| 127486 | 127522 | }; |
| 127523 | + |
| 127524 | +/* |
| 127525 | +** Do one step of the Kahan-Babushka-Neumaier summation. |
| 127526 | +** |
| 127527 | +** https://en.wikipedia.org/wiki/Kahan_summation_algorithm |
| 127528 | +** |
| 127529 | +** Variables are marked "volatile" to defeat c89 x86 floating point |
| 127530 | +** optimizations can mess up this algorithm. |
| 127531 | +*/ |
| 127532 | +static void kahanBabuskaNeumaierStep( |
| 127533 | + volatile SumCtx *pSum, |
| 127534 | + volatile double r |
| 127535 | +){ |
| 127536 | + volatile double s = pSum->rSum; |
| 127537 | + volatile double t = s + r; |
| 127538 | + if( fabs(s) > fabs(r) ){ |
| 127539 | + pSum->rErr += (s - t) + r; |
| 127540 | + }else{ |
| 127541 | + pSum->rErr += (r - t) + s; |
| 127542 | + } |
| 127543 | + pSum->rSum = t; |
| 127544 | +} |
| 127545 | + |
| 127546 | +/* |
| 127547 | +** Add a (possibly large) integer to the running sum. |
| 127548 | +*/ |
| 127549 | +static void kahanBabuskaNeumaierStepInt64(volatile SumCtx *pSum, i64 iVal){ |
| 127550 | + if( iVal<=-4503599627370496 || iVal>=+4503599627370496 ){ |
| 127551 | + i64 iBig, iSm; |
| 127552 | + iSm = iVal % 16384; |
| 127553 | + iBig = iVal - iSm; |
| 127554 | + kahanBabuskaNeumaierStep(pSum, iBig); |
| 127555 | + kahanBabuskaNeumaierStep(pSum, iSm); |
| 127556 | + }else{ |
| 127557 | + kahanBabuskaNeumaierStep(pSum, (double)iVal); |
| 127558 | + } |
| 127559 | +} |
| 127560 | + |
| 127561 | +/* |
| 127562 | +** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer |
| 127563 | +*/ |
| 127564 | +static void kahanBabuskaNeumaierInit( |
| 127565 | + volatile SumCtx *p, |
| 127566 | + i64 iVal |
| 127567 | +){ |
| 127568 | + if( iVal<=-4503599627370496 || iVal>=+4503599627370496 ){ |
| 127569 | + i64 iSm = iVal % 16384; |
| 127570 | + p->rSum = (double)(iVal - iSm); |
| 127571 | + p->rErr = (double)iSm; |
| 127572 | + }else{ |
| 127573 | + p->rSum = (double)iVal; |
| 127574 | + p->rErr = 0.0; |
| 127575 | + } |
| 127576 | +} |
| 127487 | 127577 | |
| 127488 | 127578 | /* |
| 127489 | 127579 | ** Routines used to compute the sum, average, and total. |
| 127490 | 127580 | ** |
| 127491 | 127581 | ** The SUM() function follows the (broken) SQL standard which means |
| | @@ -127502,19 +127592,34 @@ |
| 127502 | 127592 | UNUSED_PARAMETER(argc); |
| 127503 | 127593 | p = sqlite3_aggregate_context(context, sizeof(*p)); |
| 127504 | 127594 | type = sqlite3_value_numeric_type(argv[0]); |
| 127505 | 127595 | if( p && type!=SQLITE_NULL ){ |
| 127506 | 127596 | p->cnt++; |
| 127507 | | - if( type==SQLITE_INTEGER ){ |
| 127508 | | - i64 v = sqlite3_value_int64(argv[0]); |
| 127509 | | - p->rSum += v; |
| 127510 | | - if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ |
| 127511 | | - p->approx = p->overflow = 1; |
| 127597 | + if( p->approx==0 ){ |
| 127598 | + if( type!=SQLITE_INTEGER ){ |
| 127599 | + kahanBabuskaNeumaierInit(p, p->iSum); |
| 127600 | + p->approx = 1; |
| 127601 | + kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); |
| 127602 | + }else{ |
| 127603 | + i64 x = p->iSum; |
| 127604 | + if( sqlite3AddInt64(&x, sqlite3_value_int64(argv[0]))==0 ){ |
| 127605 | + p->iSum = x; |
| 127606 | + }else{ |
| 127607 | + p->ovrfl = 1; |
| 127608 | + kahanBabuskaNeumaierInit(p, p->iSum); |
| 127609 | + p->approx = 1; |
| 127610 | + kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); |
| 127611 | + } |
| 127512 | 127612 | } |
| 127513 | 127613 | }else{ |
| 127514 | | - p->rSum += sqlite3_value_double(argv[0]); |
| 127515 | 127614 | p->approx = 1; |
| 127615 | + if( type==SQLITE_INTEGER ){ |
| 127616 | + kahanBabuskaNeumaierStepInt64(p, sqlite3_value_int64(argv[0])); |
| 127617 | + }else{ |
| 127618 | + p->ovrfl = 0; |
| 127619 | + kahanBabuskaNeumaierStep(p, sqlite3_value_double(argv[0])); |
| 127620 | + } |
| 127516 | 127621 | } |
| 127517 | 127622 | } |
| 127518 | 127623 | } |
| 127519 | 127624 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 127520 | 127625 | static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){ |
| | @@ -127527,17 +127632,22 @@ |
| 127527 | 127632 | /* p is always non-NULL because sumStep() will have been called first |
| 127528 | 127633 | ** to initialize it */ |
| 127529 | 127634 | if( ALWAYS(p) && type!=SQLITE_NULL ){ |
| 127530 | 127635 | assert( p->cnt>0 ); |
| 127531 | 127636 | p->cnt--; |
| 127532 | | - assert( type==SQLITE_INTEGER || p->approx ); |
| 127533 | | - if( type==SQLITE_INTEGER && p->approx==0 ){ |
| 127534 | | - i64 v = sqlite3_value_int64(argv[0]); |
| 127535 | | - p->rSum -= v; |
| 127536 | | - p->iSum -= v; |
| 127637 | + if( !p->approx ){ |
| 127638 | + p->iSum -= sqlite3_value_int64(argv[0]); |
| 127639 | + }else if( type==SQLITE_INTEGER ){ |
| 127640 | + i64 iVal = sqlite3_value_int64(argv[0]); |
| 127641 | + if( iVal!=SMALLEST_INT64 ){ |
| 127642 | + kahanBabuskaNeumaierStepInt64(p, -iVal); |
| 127643 | + }else{ |
| 127644 | + kahanBabuskaNeumaierStepInt64(p, LARGEST_INT64); |
| 127645 | + kahanBabuskaNeumaierStepInt64(p, 1); |
| 127646 | + } |
| 127537 | 127647 | }else{ |
| 127538 | | - p->rSum -= sqlite3_value_double(argv[0]); |
| 127648 | + kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0])); |
| 127539 | 127649 | } |
| 127540 | 127650 | } |
| 127541 | 127651 | } |
| 127542 | 127652 | #else |
| 127543 | 127653 | # define sumInverse 0 |
| | @@ -127544,31 +127654,46 @@ |
| 127544 | 127654 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 127545 | 127655 | static void sumFinalize(sqlite3_context *context){ |
| 127546 | 127656 | SumCtx *p; |
| 127547 | 127657 | p = sqlite3_aggregate_context(context, 0); |
| 127548 | 127658 | if( p && p->cnt>0 ){ |
| 127549 | | - if( p->overflow ){ |
| 127550 | | - sqlite3_result_error(context,"integer overflow",-1); |
| 127551 | | - }else if( p->approx ){ |
| 127552 | | - sqlite3_result_double(context, p->rSum); |
| 127659 | + if( p->approx ){ |
| 127660 | + if( p->ovrfl ){ |
| 127661 | + sqlite3_result_error(context,"integer overflow",-1); |
| 127662 | + }else{ |
| 127663 | + sqlite3_result_double(context, p->rSum+p->rErr); |
| 127664 | + } |
| 127553 | 127665 | }else{ |
| 127554 | 127666 | sqlite3_result_int64(context, p->iSum); |
| 127555 | 127667 | } |
| 127556 | 127668 | } |
| 127557 | 127669 | } |
| 127558 | 127670 | static void avgFinalize(sqlite3_context *context){ |
| 127559 | 127671 | SumCtx *p; |
| 127560 | 127672 | p = sqlite3_aggregate_context(context, 0); |
| 127561 | 127673 | if( p && p->cnt>0 ){ |
| 127562 | | - sqlite3_result_double(context, p->rSum/(double)p->cnt); |
| 127674 | + double r; |
| 127675 | + if( p->approx ){ |
| 127676 | + r = p->rSum+p->rErr; |
| 127677 | + }else{ |
| 127678 | + r = (double)(p->iSum); |
| 127679 | + } |
| 127680 | + sqlite3_result_double(context, r/(double)p->cnt); |
| 127563 | 127681 | } |
| 127564 | 127682 | } |
| 127565 | 127683 | static void totalFinalize(sqlite3_context *context){ |
| 127566 | 127684 | SumCtx *p; |
| 127685 | + double r = 0.0; |
| 127567 | 127686 | p = sqlite3_aggregate_context(context, 0); |
| 127568 | | - /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ |
| 127569 | | - sqlite3_result_double(context, p ? p->rSum : (double)0); |
| 127687 | + if( p ){ |
| 127688 | + if( p->approx ){ |
| 127689 | + r = p->rSum+p->rErr; |
| 127690 | + }else{ |
| 127691 | + r = (double)(p->iSum); |
| 127692 | + } |
| 127693 | + } |
| 127694 | + sqlite3_result_double(context, r); |
| 127570 | 127695 | } |
| 127571 | 127696 | |
| 127572 | 127697 | /* |
| 127573 | 127698 | ** The following structure keeps track of state information for the |
| 127574 | 127699 | ** count() aggregate function. |
| | @@ -128154,10 +128279,41 @@ |
| 128154 | 128279 | if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return; |
| 128155 | 128280 | x = sqlite3_value_double(argv[0]); |
| 128156 | 128281 | sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0); |
| 128157 | 128282 | } |
| 128158 | 128283 | |
| 128284 | +#ifdef SQLITE_DEBUG |
| 128285 | +/* |
| 128286 | +** Implementation of fpdecode(x,y,z) function. |
| 128287 | +** |
| 128288 | +** x is a real number that is to be decoded. y is the precision. |
| 128289 | +** z is the maximum real precision. |
| 128290 | +*/ |
| 128291 | +static void fpdecodeFunc( |
| 128292 | + sqlite3_context *context, |
| 128293 | + int argc, |
| 128294 | + sqlite3_value **argv |
| 128295 | +){ |
| 128296 | + FpDecode s; |
| 128297 | + double x; |
| 128298 | + int y, z; |
| 128299 | + char zBuf[100]; |
| 128300 | + UNUSED_PARAMETER(argc); |
| 128301 | + assert( argc==3 ); |
| 128302 | + x = sqlite3_value_double(argv[0]); |
| 128303 | + y = sqlite3_value_int(argv[1]); |
| 128304 | + z = sqlite3_value_int(argv[2]); |
| 128305 | + sqlite3FpDecode(&s, x, y, z); |
| 128306 | + if( s.isSpecial==2 ){ |
| 128307 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "NaN"); |
| 128308 | + }else{ |
| 128309 | + sqlite3_snprintf(sizeof(zBuf), zBuf, "%c%.*s/%d", s.sign, s.n, s.z, s.iDP); |
| 128310 | + } |
| 128311 | + sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); |
| 128312 | +} |
| 128313 | +#endif /* SQLITE_DEBUG */ |
| 128314 | + |
| 128159 | 128315 | /* |
| 128160 | 128316 | ** All of the FuncDef structures in the aBuiltinFunc[] array above |
| 128161 | 128317 | ** to the global function hash table. This occurs at start-time (as |
| 128162 | 128318 | ** a consequence of calling sqlite3_initialize()). |
| 128163 | 128319 | ** |
| | @@ -128225,10 +128381,13 @@ |
| 128225 | 128381 | FUNCTION(printf, -1, 0, 0, printfFunc ), |
| 128226 | 128382 | FUNCTION(format, -1, 0, 0, printfFunc ), |
| 128227 | 128383 | FUNCTION(unicode, 1, 0, 0, unicodeFunc ), |
| 128228 | 128384 | FUNCTION(char, -1, 0, 0, charFunc ), |
| 128229 | 128385 | FUNCTION(abs, 1, 0, 0, absFunc ), |
| 128386 | +#ifdef SQLITE_DEBUG |
| 128387 | + FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ), |
| 128388 | +#endif |
| 128230 | 128389 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 128231 | 128390 | FUNCTION(round, 1, 0, 0, roundFunc ), |
| 128232 | 128391 | FUNCTION(round, 2, 0, 0, roundFunc ), |
| 128233 | 128392 | #endif |
| 128234 | 128393 | FUNCTION(upper, 1, 0, 0, upperFunc ), |
| | @@ -147420,13 +147579,17 @@ |
| 147420 | 147579 | int regBase; |
| 147421 | 147580 | int regRecord; |
| 147422 | 147581 | int nCol; |
| 147423 | 147582 | int nGroupBy; |
| 147424 | 147583 | |
| 147425 | | - explainTempTable(pParse, |
| 147584 | +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 147585 | + int addrExp; /* Address of OP_Explain instruction */ |
| 147586 | +#endif |
| 147587 | + ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s", |
| 147426 | 147588 | (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ? |
| 147427 | | - "DISTINCT" : "GROUP BY"); |
| 147589 | + "DISTINCT" : "GROUP BY" |
| 147590 | + )); |
| 147428 | 147591 | |
| 147429 | 147592 | groupBySort = 1; |
| 147430 | 147593 | nGroupBy = pGroupBy->nExpr; |
| 147431 | 147594 | nCol = nGroupBy; |
| 147432 | 147595 | j = nGroupBy; |
| | @@ -147447,22 +147610,27 @@ |
| 147447 | 147610 | j++; |
| 147448 | 147611 | } |
| 147449 | 147612 | } |
| 147450 | 147613 | pAggInfo->directMode = 0; |
| 147451 | 147614 | regRecord = sqlite3GetTempReg(pParse); |
| 147615 | + sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v)); |
| 147452 | 147616 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); |
| 147453 | 147617 | sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord); |
| 147618 | + sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1); |
| 147454 | 147619 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 147455 | 147620 | sqlite3ReleaseTempRange(pParse, regBase, nCol); |
| 147456 | 147621 | TREETRACE(0x2,pParse,p,("WhereEnd\n")); |
| 147457 | 147622 | sqlite3WhereEnd(pWInfo); |
| 147458 | 147623 | pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++; |
| 147459 | 147624 | sortOut = sqlite3GetTempReg(pParse); |
| 147625 | + sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0); |
| 147460 | 147626 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol); |
| 147461 | 147627 | sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd); |
| 147462 | 147628 | VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v); |
| 147463 | 147629 | pAggInfo->useSortingIdx = 1; |
| 147630 | + sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab); |
| 147631 | + sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx); |
| 147464 | 147632 | } |
| 147465 | 147633 | |
| 147466 | 147634 | /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions |
| 147467 | 147635 | ** that are indexed (and that were previously identified and tagged |
| 147468 | 147636 | ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions |
| | @@ -153901,10 +154069,16 @@ |
| 153901 | 154069 | sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur); |
| 153902 | 154070 | } |
| 153903 | 154071 | if( wsFlags & WHERE_INDEXED ){ |
| 153904 | 154072 | sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur); |
| 153905 | 154073 | } |
| 154074 | + }else{ |
| 154075 | + int addr = pSrclist->a[pLvl->iFrom].addrFillSub; |
| 154076 | + VdbeOp *pOp = sqlite3VdbeGetOp(v, addr-1); |
| 154077 | + assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine ); |
| 154078 | + assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr ); |
| 154079 | + sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1); |
| 153906 | 154080 | } |
| 153907 | 154081 | } |
| 153908 | 154082 | } |
| 153909 | 154083 | #endif |
| 153910 | 154084 | |
| | @@ -179822,10 +179996,25 @@ |
| 179822 | 179996 | *pI1 = rLogEst; |
| 179823 | 179997 | *pU64 = sqlite3LogEstToInt(rLogEst); |
| 179824 | 179998 | *pI2 = sqlite3LogEst(*pU64); |
| 179825 | 179999 | break; |
| 179826 | 180000 | } |
| 180001 | + |
| 180002 | + /* sqlite3_test_control(SQLITE_TESTCTRL_USELONGDOUBLE, int X); |
| 180003 | + ** |
| 180004 | + ** X<0 Make no changes to the bUseLongDouble. Just report value. |
| 180005 | + ** X==0 Disable bUseLongDouble |
| 180006 | + ** X==1 Enable bUseLongDouble |
| 180007 | + ** X==2 Set bUseLongDouble to its default value for this platform |
| 180008 | + */ |
| 180009 | + case SQLITE_TESTCTRL_USELONGDOUBLE: { |
| 180010 | + int b = va_arg(ap, int); |
| 180011 | + if( b==2 ) b = sizeof(LONGDOUBLE_TYPE)>8; |
| 180012 | + if( b>=0 ) sqlite3Config.bUseLongDouble = b>0; |
| 180013 | + rc = sqlite3Config.bUseLongDouble!=0; |
| 180014 | + break; |
| 180015 | + } |
| 179827 | 180016 | |
| 179828 | 180017 | |
| 179829 | 180018 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD) |
| 179830 | 180019 | /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue) |
| 179831 | 180020 | ** |
| | @@ -243110,11 +243299,11 @@ |
| 243110 | 243299 | int nArg, /* Number of args */ |
| 243111 | 243300 | sqlite3_value **apUnused /* Function arguments */ |
| 243112 | 243301 | ){ |
| 243113 | 243302 | assert( nArg==0 ); |
| 243114 | 243303 | UNUSED_PARAM2(nArg, apUnused); |
| 243115 | | - sqlite3_result_text(pCtx, "fts5: 2023-06-22 13:01:02 d35c214811aac7dec0000ca2aa77231f74a7963dd0c53cf25a65ade5ef0f8dc0", -1, SQLITE_TRANSIENT); |
| 243304 | + sqlite3_result_text(pCtx, "fts5: 2023-07-08 14:27:55 beab3c98639be531744e60440223bb9ee76bc15234aff05e5efb273c8241dfd8", -1, SQLITE_TRANSIENT); |
| 243116 | 243305 | } |
| 243117 | 243306 | |
| 243118 | 243307 | /* |
| 243119 | 243308 | ** Return true if zName is the extension on one of the shadow tables used |
| 243120 | 243309 | ** by this module. |
| 243121 | 243310 | |