Fossil SCM

Update the built-in SQLite to the latest 3.43.0 alpha for testing.

drh 2023-07-08 14:36 trunk
Commit 65c6bda8d63af43a0bc8a1254fb72186016486b13a35703d6bccc7b7c06129ae
3 files changed +444 -110 +521 -332 +3 -2
+444 -110
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -1222,10 +1222,50 @@
12221222
freeText(&s);
12231223
s.z = 0;
12241224
}
12251225
return s.z;
12261226
}
1227
+
1228
+/*
1229
+** SQL function: strtod(X)
1230
+**
1231
+** Use the C-library strtod() function to convert string X into a double.
1232
+** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1233
+** routines against the C-library.
1234
+*/
1235
+static void shellStrtod(
1236
+ sqlite3_context *pCtx,
1237
+ int nVal,
1238
+ sqlite3_value **apVal
1239
+){
1240
+ char *z = (char*)sqlite3_value_text(apVal[0]);
1241
+ UNUSED_PARAMETER(nVal);
1242
+ if( z==0 ) return;
1243
+ sqlite3_result_double(pCtx, strtod(z,0));
1244
+}
1245
+
1246
+/*
1247
+** SQL function: dtostr(X)
1248
+**
1249
+** Use the C-library printf() function to convert real value X into a string.
1250
+** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1251
+** routines against the C-library.
1252
+*/
1253
+static void shellDtostr(
1254
+ sqlite3_context *pCtx,
1255
+ int nVal,
1256
+ sqlite3_value **apVal
1257
+){
1258
+ double r = sqlite3_value_double(apVal[0]);
1259
+ int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1260
+ char z[400];
1261
+ if( n<1 ) n = 1;
1262
+ if( n>350 ) n = 350;
1263
+ sprintf(z, "%#+.*e", n, r);
1264
+ sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1265
+}
1266
+
12271267
12281268
/*
12291269
** SQL function: shell_module_schema(X)
12301270
**
12311271
** Return a fake schema for the table-valued function or eponymous virtual
@@ -2864,45 +2904,28 @@
28642904
sqlite3_free(p);
28652905
}
28662906
}
28672907
28682908
/*
2869
-** Allocate a new Decimal object. Initialize it to the number given
2870
-** by the input string.
2909
+** Allocate a new Decimal object initialized to the text in zIn[].
2910
+** Return NULL if any kind of error occurs.
28712911
*/
2872
-static Decimal *decimal_new(
2873
- sqlite3_context *pCtx,
2874
- sqlite3_value *pIn,
2875
- int nAlt,
2876
- const unsigned char *zAlt
2877
-){
2878
- Decimal *p;
2879
- int n, i;
2880
- const unsigned char *zIn;
2912
+static Decimal *decimalNewFromText(const char *zIn, int n){
2913
+ Decimal *p = 0;
2914
+ int i;
28812915
int iExp = 0;
2916
+
28822917
p = sqlite3_malloc( sizeof(*p) );
2883
- if( p==0 ) goto new_no_mem;
2918
+ if( p==0 ) goto new_from_text_failed;
28842919
p->sign = 0;
28852920
p->oom = 0;
28862921
p->isInit = 1;
28872922
p->isNull = 0;
28882923
p->nDigit = 0;
28892924
p->nFrac = 0;
2890
- if( zAlt ){
2891
- n = nAlt,
2892
- zIn = zAlt;
2893
- }else{
2894
- if( sqlite3_value_type(pIn)==SQLITE_NULL ){
2895
- p->a = 0;
2896
- p->isNull = 1;
2897
- return p;
2898
- }
2899
- n = sqlite3_value_bytes(pIn);
2900
- zIn = sqlite3_value_text(pIn);
2901
- }
29022925
p->a = sqlite3_malloc64( n+1 );
2903
- if( p->a==0 ) goto new_no_mem;
2926
+ if( p->a==0 ) goto new_from_text_failed;
29042927
for(i=0; isspace(zIn[i]); i++){}
29052928
if( zIn[i]=='-' ){
29062929
p->sign = 1;
29072930
i++;
29082931
}else if( zIn[i]=='+' ){
@@ -2949,11 +2972,11 @@
29492972
p->nFrac = 0;
29502973
}
29512974
}
29522975
if( iExp>0 ){
29532976
p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
2954
- if( p->a==0 ) goto new_no_mem;
2977
+ if( p->a==0 ) goto new_from_text_failed;
29552978
memset(p->a+p->nDigit, 0, iExp);
29562979
p->nDigit += iExp;
29572980
}
29582981
}else if( iExp<0 ){
29592982
int nExtra;
@@ -2968,20 +2991,89 @@
29682991
p->nFrac = p->nDigit - 1;
29692992
}
29702993
}
29712994
if( iExp>0 ){
29722995
p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
2973
- if( p->a==0 ) goto new_no_mem;
2996
+ if( p->a==0 ) goto new_from_text_failed;
29742997
memmove(p->a+iExp, p->a, p->nDigit);
29752998
memset(p->a, 0, iExp);
29762999
p->nDigit += iExp;
29773000
p->nFrac += iExp;
29783001
}
29793002
}
29803003
return p;
29813004
2982
-new_no_mem:
3005
+new_from_text_failed:
3006
+ if( p ){
3007
+ if( p->a ) sqlite3_free(p->a);
3008
+ sqlite3_free(p);
3009
+ }
3010
+ return 0;
3011
+}
3012
+
3013
+/* Forward reference */
3014
+static Decimal *decimalFromDouble(double);
3015
+
3016
+/*
3017
+** Allocate a new Decimal object from an sqlite3_value. Return a pointer
3018
+** to the new object, or NULL if there is an error. If the pCtx argument
3019
+** is not NULL, then errors are reported on it as well.
3020
+**
3021
+** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
3022
+** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length
3023
+** 8 bytes, the resulting double value is expanded into its decimal equivalent.
3024
+** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
3025
+** then NULL is returned.
3026
+*/
3027
+static Decimal *decimal_new(
3028
+ sqlite3_context *pCtx, /* Report error here, if not null */
3029
+ sqlite3_value *pIn, /* Construct the decimal object from this */
3030
+ int bTextOnly /* Always interpret pIn as text if true */
3031
+){
3032
+ Decimal *p = 0;
3033
+ int eType = sqlite3_value_type(pIn);
3034
+ if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){
3035
+ eType = SQLITE_TEXT;
3036
+ }
3037
+ switch( eType ){
3038
+ case SQLITE_TEXT:
3039
+ case SQLITE_INTEGER: {
3040
+ const char *zIn = (const char*)sqlite3_value_text(pIn);
3041
+ int n = sqlite3_value_bytes(pIn);
3042
+ p = decimalNewFromText(zIn, n);
3043
+ if( p==0 ) goto new_failed;
3044
+ break;
3045
+ }
3046
+
3047
+ case SQLITE_FLOAT: {
3048
+ p = decimalFromDouble(sqlite3_value_double(pIn));
3049
+ break;
3050
+ }
3051
+
3052
+ case SQLITE_BLOB: {
3053
+ const unsigned char *x;
3054
+ unsigned int i;
3055
+ sqlite3_uint64 v = 0;
3056
+ double r;
3057
+
3058
+ if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break;
3059
+ x = sqlite3_value_blob(pIn);
3060
+ for(i=0; i<sizeof(r); i++){
3061
+ v = (v<<8) | x[i];
3062
+ }
3063
+ memcpy(&r, &v, sizeof(r));
3064
+ p = decimalFromDouble(r);
3065
+ break;
3066
+ }
3067
+
3068
+ case SQLITE_NULL: {
3069
+ break;
3070
+ }
3071
+ }
3072
+ return p;
3073
+
3074
+new_failed:
29833075
if( pCtx ) sqlite3_result_error_nomem(pCtx);
29843076
sqlite3_free(p);
29853077
return 0;
29863078
}
29873079
@@ -3037,23 +3129,68 @@
30373129
z[i] = 0;
30383130
sqlite3_result_text(pCtx, z, i, sqlite3_free);
30393131
}
30403132
30413133
/*
3042
-** SQL Function: decimal(X)
3043
-**
3044
-** Convert input X into decimal and then back into text
3134
+** Make the given Decimal the result in an format similar to '%+#e'.
3135
+** In other words, show exponential notation with leading and trailing
3136
+** zeros omitted.
30453137
*/
3046
-static void decimalFunc(
3047
- sqlite3_context *context,
3048
- int argc,
3049
- sqlite3_value **argv
3050
-){
3051
- Decimal *p = decimal_new(context, argv[0], 0, 0);
3052
- UNUSED_PARAMETER(argc);
3053
- decimal_result(context, p);
3054
- decimal_free(p);
3138
+static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
3139
+ char *z; /* The output buffer */
3140
+ int i; /* Loop counter */
3141
+ int nZero; /* Number of leading zeros */
3142
+ int nDigit; /* Number of digits not counting trailing zeros */
3143
+ int nFrac; /* Digits to the right of the decimal point */
3144
+ int exp; /* Exponent value */
3145
+ signed char zero; /* Zero value */
3146
+ signed char *a; /* Array of digits */
3147
+
3148
+ if( p==0 || p->oom ){
3149
+ sqlite3_result_error_nomem(pCtx);
3150
+ return;
3151
+ }
3152
+ if( p->isNull ){
3153
+ sqlite3_result_null(pCtx);
3154
+ return;
3155
+ }
3156
+ for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
3157
+ for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
3158
+ nFrac = p->nFrac + (nDigit - p->nDigit);
3159
+ nDigit -= nZero;
3160
+ z = sqlite3_malloc( nDigit+20 );
3161
+ if( z==0 ){
3162
+ sqlite3_result_error_nomem(pCtx);
3163
+ return;
3164
+ }
3165
+ if( nDigit==0 ){
3166
+ zero = 0;
3167
+ a = &zero;
3168
+ nDigit = 1;
3169
+ nFrac = 0;
3170
+ }else{
3171
+ a = &p->a[nZero];
3172
+ }
3173
+ if( p->sign && nDigit>0 ){
3174
+ z[0] = '-';
3175
+ }else{
3176
+ z[0] = '+';
3177
+ }
3178
+ z[1] = a[0]+'0';
3179
+ z[2] = '.';
3180
+ if( nDigit==1 ){
3181
+ z[3] = '0';
3182
+ i = 4;
3183
+ }else{
3184
+ for(i=1; i<nDigit; i++){
3185
+ z[2+i] = a[i]+'0';
3186
+ }
3187
+ i = nDigit+2;
3188
+ }
3189
+ exp = nDigit - nFrac - 1;
3190
+ sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
3191
+ sqlite3_result_text(pCtx, z, -1, sqlite3_free);
30553192
}
30563193
30573194
/*
30583195
** Compare to Decimal objects. Return negative, 0, or positive if the
30593196
** first object is less than, equal to, or greater than the second.
@@ -3102,13 +3239,13 @@
31023239
){
31033240
Decimal *pA = 0, *pB = 0;
31043241
int rc;
31053242
31063243
UNUSED_PARAMETER(argc);
3107
- pA = decimal_new(context, argv[0], 0, 0);
3244
+ pA = decimal_new(context, argv[0], 1);
31083245
if( pA==0 || pA->isNull ) goto cmp_done;
3109
- pB = decimal_new(context, argv[1], 0, 0);
3246
+ pB = decimal_new(context, argv[1], 1);
31103247
if( pB==0 || pB->isNull ) goto cmp_done;
31113248
rc = decimal_cmp(pA, pB);
31123249
if( rc<0 ) rc = -1;
31133250
else if( rc>0 ) rc = +1;
31143251
sqlite3_result_int(context, rc);
@@ -3144,11 +3281,11 @@
31443281
p->nFrac += nAddFrac;
31453282
}
31463283
}
31473284
31483285
/*
3149
-** Add the value pB into pA.
3286
+** Add the value pB into pA. A := A + B.
31503287
**
31513288
** Both pA and pB might become denormalized by this routine.
31523289
*/
31533290
static void decimal_add(Decimal *pA, Decimal *pB){
31543291
int nSig, nFrac, nDigit;
@@ -3212,10 +3349,176 @@
32123349
}
32133350
}
32143351
}
32153352
}
32163353
}
3354
+
3355
+/*
3356
+** Multiply A by B. A := A * B
3357
+**
3358
+** All significant digits after the decimal point are retained.
3359
+** Trailing zeros after the decimal point are omitted as long as
3360
+** the number of digits after the decimal point is no less than
3361
+** either the number of digits in either input.
3362
+*/
3363
+static void decimalMul(Decimal *pA, Decimal *pB){
3364
+ signed char *acc = 0;
3365
+ int i, j, k;
3366
+ int minFrac;
3367
+
3368
+ if( pA==0 || pA->oom || pA->isNull
3369
+ || pB==0 || pB->oom || pB->isNull
3370
+ ){
3371
+ goto mul_end;
3372
+ }
3373
+ acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
3374
+ if( acc==0 ){
3375
+ pA->oom = 1;
3376
+ goto mul_end;
3377
+ }
3378
+ memset(acc, 0, pA->nDigit + pB->nDigit + 2);
3379
+ minFrac = pA->nFrac;
3380
+ if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
3381
+ for(i=pA->nDigit-1; i>=0; i--){
3382
+ signed char f = pA->a[i];
3383
+ int carry = 0, x;
3384
+ for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
3385
+ x = acc[k] + f*pB->a[j] + carry;
3386
+ acc[k] = x%10;
3387
+ carry = x/10;
3388
+ }
3389
+ x = acc[k] + carry;
3390
+ acc[k] = x%10;
3391
+ acc[k-1] += x/10;
3392
+ }
3393
+ sqlite3_free(pA->a);
3394
+ pA->a = acc;
3395
+ acc = 0;
3396
+ pA->nDigit += pB->nDigit + 2;
3397
+ pA->nFrac += pB->nFrac;
3398
+ pA->sign ^= pB->sign;
3399
+ while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
3400
+ pA->nFrac--;
3401
+ pA->nDigit--;
3402
+ }
3403
+
3404
+mul_end:
3405
+ sqlite3_free(acc);
3406
+}
3407
+
3408
+/*
3409
+** Create a new Decimal object that contains an integer power of 2.
3410
+*/
3411
+static Decimal *decimalPow2(int N){
3412
+ Decimal *pA = 0; /* The result to be returned */
3413
+ Decimal *pX = 0; /* Multiplier */
3414
+ if( N<-20000 || N>20000 ) goto pow2_fault;
3415
+ pA = decimalNewFromText("1.0", 3);
3416
+ if( pA==0 || pA->oom ) goto pow2_fault;
3417
+ if( N==0 ) return pA;
3418
+ if( N>0 ){
3419
+ pX = decimalNewFromText("2.0", 3);
3420
+ }else{
3421
+ N = -N;
3422
+ pX = decimalNewFromText("0.5", 3);
3423
+ }
3424
+ if( pX==0 || pX->oom ) goto pow2_fault;
3425
+ while( 1 /* Exit by break */ ){
3426
+ if( N & 1 ){
3427
+ decimalMul(pA, pX);
3428
+ if( pA->oom ) goto pow2_fault;
3429
+ }
3430
+ N >>= 1;
3431
+ if( N==0 ) break;
3432
+ decimalMul(pX, pX);
3433
+ }
3434
+ decimal_free(pX);
3435
+ return pA;
3436
+
3437
+pow2_fault:
3438
+ decimal_free(pA);
3439
+ decimal_free(pX);
3440
+ return 0;
3441
+}
3442
+
3443
+/*
3444
+** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
3445
+*/
3446
+static Decimal *decimalFromDouble(double r){
3447
+ sqlite3_int64 m, a;
3448
+ int e;
3449
+ int isNeg;
3450
+ Decimal *pA;
3451
+ Decimal *pX;
3452
+ char zNum[100];
3453
+ if( r<0.0 ){
3454
+ isNeg = 1;
3455
+ r = -r;
3456
+ }else{
3457
+ isNeg = 0;
3458
+ }
3459
+ memcpy(&a,&r,sizeof(a));
3460
+ if( a==0 ){
3461
+ e = 0;
3462
+ m = 0;
3463
+ }else{
3464
+ e = a>>52;
3465
+ m = a & ((((sqlite3_int64)1)<<52)-1);
3466
+ if( e==0 ){
3467
+ m <<= 1;
3468
+ }else{
3469
+ m |= ((sqlite3_int64)1)<<52;
3470
+ }
3471
+ while( e<1075 && m>0 && (m&1)==0 ){
3472
+ m >>= 1;
3473
+ e++;
3474
+ }
3475
+ if( isNeg ) m = -m;
3476
+ e = e - 1075;
3477
+ if( e>971 ){
3478
+ return 0; /* A NaN or an Infinity */
3479
+ }
3480
+ }
3481
+
3482
+ /* At this point m is the integer significand and e is the exponent */
3483
+ sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m);
3484
+ pA = decimalNewFromText(zNum, (int)strlen(zNum));
3485
+ pX = decimalPow2(e);
3486
+ decimalMul(pA, pX);
3487
+ decimal_free(pX);
3488
+ return pA;
3489
+}
3490
+
3491
+/*
3492
+** SQL Function: decimal(X)
3493
+** OR: decimal_sci(X)
3494
+**
3495
+** Convert input X into decimal and then back into text.
3496
+**
3497
+** If X is originally a float, then a full decimal expansion of that floating
3498
+** point value is done. Or if X is an 8-byte blob, it is interpreted
3499
+** as a float and similarly expanded.
3500
+**
3501
+** The decimal_sci(X) function returns the result in scientific notation.
3502
+** decimal(X) returns a complete decimal, without the e+NNN at the end.
3503
+*/
3504
+static void decimalFunc(
3505
+ sqlite3_context *context,
3506
+ int argc,
3507
+ sqlite3_value **argv
3508
+){
3509
+ Decimal *p = decimal_new(context, argv[0], 0);
3510
+ UNUSED_PARAMETER(argc);
3511
+ if( p ){
3512
+ if( sqlite3_user_data(context)!=0 ){
3513
+ decimal_result_sci(context, p);
3514
+ }else{
3515
+ decimal_result(context, p);
3516
+ }
3517
+ decimal_free(p);
3518
+ }
3519
+}
32173520
32183521
/*
32193522
** Compare text in decimal order.
32203523
*/
32213524
static int decimalCollFunc(
@@ -3223,12 +3526,12 @@
32233526
int nKey1, const void *pKey1,
32243527
int nKey2, const void *pKey2
32253528
){
32263529
const unsigned char *zA = (const unsigned char*)pKey1;
32273530
const unsigned char *zB = (const unsigned char*)pKey2;
3228
- Decimal *pA = decimal_new(0, 0, nKey1, zA);
3229
- Decimal *pB = decimal_new(0, 0, nKey2, zB);
3531
+ Decimal *pA = decimalNewFromText((const char*)zA, nKey1);
3532
+ Decimal *pB = decimalNewFromText((const char*)zB, nKey2);
32303533
int rc;
32313534
UNUSED_PARAMETER(notUsed);
32323535
if( pA==0 || pB==0 ){
32333536
rc = 0;
32343537
}else{
@@ -3249,12 +3552,12 @@
32493552
static void decimalAddFunc(
32503553
sqlite3_context *context,
32513554
int argc,
32523555
sqlite3_value **argv
32533556
){
3254
- Decimal *pA = decimal_new(context, argv[0], 0, 0);
3255
- Decimal *pB = decimal_new(context, argv[1], 0, 0);
3557
+ Decimal *pA = decimal_new(context, argv[0], 1);
3558
+ Decimal *pB = decimal_new(context, argv[1], 1);
32563559
UNUSED_PARAMETER(argc);
32573560
decimal_add(pA, pB);
32583561
decimal_result(context, pA);
32593562
decimal_free(pA);
32603563
decimal_free(pB);
@@ -3262,12 +3565,12 @@
32623565
static void decimalSubFunc(
32633566
sqlite3_context *context,
32643567
int argc,
32653568
sqlite3_value **argv
32663569
){
3267
- Decimal *pA = decimal_new(context, argv[0], 0, 0);
3268
- Decimal *pB = decimal_new(context, argv[1], 0, 0);
3570
+ Decimal *pA = decimal_new(context, argv[0], 1);
3571
+ Decimal *pB = decimal_new(context, argv[1], 1);
32693572
UNUSED_PARAMETER(argc);
32703573
if( pB ){
32713574
pB->sign = !pB->sign;
32723575
decimal_add(pA, pB);
32733576
decimal_result(context, pA);
@@ -3301,11 +3604,11 @@
33013604
}
33023605
p->nDigit = 1;
33033606
p->nFrac = 0;
33043607
}
33053608
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3306
- pArg = decimal_new(context, argv[0], 0, 0);
3609
+ pArg = decimal_new(context, argv[0], 1);
33073610
decimal_add(p, pArg);
33083611
decimal_free(pArg);
33093612
}
33103613
static void decimalSumInverse(
33113614
sqlite3_context *context,
@@ -3316,11 +3619,11 @@
33163619
Decimal *pArg;
33173620
UNUSED_PARAMETER(argc);
33183621
p = sqlite3_aggregate_context(context, sizeof(*p));
33193622
if( p==0 ) return;
33203623
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3321
- pArg = decimal_new(context, argv[0], 0, 0);
3624
+ pArg = decimal_new(context, argv[0], 1);
33223625
if( pArg ) pArg->sign = !pArg->sign;
33233626
decimal_add(p, pArg);
33243627
decimal_free(pArg);
33253628
}
33263629
static void decimalSumValue(sqlite3_context *context){
@@ -3337,69 +3640,52 @@
33373640
33383641
/*
33393642
** SQL Function: decimal_mul(X, Y)
33403643
**
33413644
** Return the product of X and Y.
3342
-**
3343
-** All significant digits after the decimal point are retained.
3344
-** Trailing zeros after the decimal point are omitted as long as
3345
-** the number of digits after the decimal point is no less than
3346
-** either the number of digits in either input.
33473645
*/
33483646
static void decimalMulFunc(
33493647
sqlite3_context *context,
33503648
int argc,
33513649
sqlite3_value **argv
33523650
){
3353
- Decimal *pA = decimal_new(context, argv[0], 0, 0);
3354
- Decimal *pB = decimal_new(context, argv[1], 0, 0);
3355
- signed char *acc = 0;
3356
- int i, j, k;
3357
- int minFrac;
3651
+ Decimal *pA = decimal_new(context, argv[0], 1);
3652
+ Decimal *pB = decimal_new(context, argv[1], 1);
33583653
UNUSED_PARAMETER(argc);
33593654
if( pA==0 || pA->oom || pA->isNull
33603655
|| pB==0 || pB->oom || pB->isNull
33613656
){
33623657
goto mul_end;
33633658
}
3364
- acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
3365
- if( acc==0 ){
3366
- sqlite3_result_error_nomem(context);
3659
+ decimalMul(pA, pB);
3660
+ if( pA->oom ){
33673661
goto mul_end;
33683662
}
3369
- memset(acc, 0, pA->nDigit + pB->nDigit + 2);
3370
- minFrac = pA->nFrac;
3371
- if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
3372
- for(i=pA->nDigit-1; i>=0; i--){
3373
- signed char f = pA->a[i];
3374
- int carry = 0, x;
3375
- for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
3376
- x = acc[k] + f*pB->a[j] + carry;
3377
- acc[k] = x%10;
3378
- carry = x/10;
3379
- }
3380
- x = acc[k] + carry;
3381
- acc[k] = x%10;
3382
- acc[k-1] += x/10;
3383
- }
3384
- sqlite3_free(pA->a);
3385
- pA->a = acc;
3386
- acc = 0;
3387
- pA->nDigit += pB->nDigit + 2;
3388
- pA->nFrac += pB->nFrac;
3389
- pA->sign ^= pB->sign;
3390
- while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
3391
- pA->nFrac--;
3392
- pA->nDigit--;
3393
- }
33943663
decimal_result(context, pA);
33953664
33963665
mul_end:
3397
- sqlite3_free(acc);
33983666
decimal_free(pA);
33993667
decimal_free(pB);
34003668
}
3669
+
3670
+/*
3671
+** SQL Function: decimal_pow2(N)
3672
+**
3673
+** Return the N-th power of 2. N must be an integer.
3674
+*/
3675
+static void decimalPow2Func(
3676
+ sqlite3_context *context,
3677
+ int argc,
3678
+ sqlite3_value **argv
3679
+){
3680
+ UNUSED_PARAMETER(argc);
3681
+ if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
3682
+ Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
3683
+ decimal_result_sci(context, pA);
3684
+ decimal_free(pA);
3685
+ }
3686
+}
34013687
34023688
#ifdef _WIN32
34033689
34043690
#endif
34053691
int sqlite3_decimal_init(
@@ -3409,27 +3695,30 @@
34093695
){
34103696
int rc = SQLITE_OK;
34113697
static const struct {
34123698
const char *zFuncName;
34133699
int nArg;
3700
+ int iArg;
34143701
void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
34153702
} aFunc[] = {
3416
- { "decimal", 1, decimalFunc },
3417
- { "decimal_cmp", 2, decimalCmpFunc },
3418
- { "decimal_add", 2, decimalAddFunc },
3419
- { "decimal_sub", 2, decimalSubFunc },
3420
- { "decimal_mul", 2, decimalMulFunc },
3703
+ { "decimal", 1, 0, decimalFunc },
3704
+ { "decimal_sci", 1, 1, decimalFunc },
3705
+ { "decimal_cmp", 2, 0, decimalCmpFunc },
3706
+ { "decimal_add", 2, 0, decimalAddFunc },
3707
+ { "decimal_sub", 2, 0, decimalSubFunc },
3708
+ { "decimal_mul", 2, 0, decimalMulFunc },
3709
+ { "decimal_pow2", 1, 0, decimalPow2Func },
34213710
};
34223711
unsigned int i;
34233712
(void)pzErrMsg; /* Unused parameter */
34243713
34253714
SQLITE_EXTENSION_INIT2(pApi);
34263715
34273716
for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
34283717
rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
34293718
SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
3430
- 0, aFunc[i].xFunc, 0, 0);
3719
+ aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0);
34313720
}
34323721
if( rc==SQLITE_OK ){
34333722
rc = sqlite3_create_window_function(db, "decimal_sum", 1,
34343723
SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
34353724
decimalSumStep, decimalSumFinalize,
@@ -4453,10 +4742,41 @@
44534742
}
44544743
sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
44554744
}
44564745
}
44574746
4747
+/*
4748
+** SQL Function: ieee754_inc(r,N)
4749
+**
4750
+** Move the floating point value r by N quantums and return the new
4751
+** values.
4752
+**
4753
+** Behind the scenes: this routine merely casts r into a 64-bit unsigned
4754
+** integer, adds N, then casts the value back into float.
4755
+**
4756
+** Example: To find the smallest positive number:
4757
+**
4758
+** SELECT ieee754_inc(0.0,+1);
4759
+*/
4760
+static void ieee754inc(
4761
+ sqlite3_context *context,
4762
+ int argc,
4763
+ sqlite3_value **argv
4764
+){
4765
+ double r;
4766
+ sqlite3_int64 N;
4767
+ sqlite3_uint64 m1, m2;
4768
+ double r2;
4769
+ UNUSED_PARAMETER(argc);
4770
+ r = sqlite3_value_double(argv[0]);
4771
+ N = sqlite3_value_int64(argv[1]);
4772
+ memcpy(&m1, &r, 8);
4773
+ m2 = m1 + N;
4774
+ memcpy(&r2, &m2, 8);
4775
+ sqlite3_result_double(context, r2);
4776
+}
4777
+
44584778
44594779
#ifdef _WIN32
44604780
44614781
#endif
44624782
int sqlite3_ieee_init(
@@ -4474,11 +4794,11 @@
44744794
{ "ieee754", 2, 0, ieee754func },
44754795
{ "ieee754_mantissa", 1, 1, ieee754func },
44764796
{ "ieee754_exponent", 1, 2, ieee754func },
44774797
{ "ieee754_to_blob", 1, 0, ieee754func_to_blob },
44784798
{ "ieee754_from_blob", 1, 0, ieee754func_from_blob },
4479
-
4799
+ { "ieee754_inc", 2, 0, ieee754inc },
44804800
};
44814801
unsigned int i;
44824802
int rc = SQLITE_OK;
44834803
SQLITE_EXTENSION_INIT2(pApi);
44844804
(void)pzErrMsg; /* Unused parameter */
@@ -20964,10 +21284,16 @@
2096421284
#undef SHELL_SUB_MACRO
2096521285
#undef SHELL_SUBMACRO
2096621286
}
2096721287
#endif
2096821288
21289
+ sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
21290
+ shellStrtod, 0, 0);
21291
+ sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
21292
+ shellDtostr, 0, 0);
21293
+ sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
21294
+ shellDtostr, 0, 0);
2096921295
sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
2097021296
shellAddSchemaName, 0, 0);
2097121297
sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
2097221298
shellModuleSchema, 0, 0);
2097321299
sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
@@ -26033,11 +26359,12 @@
2603326359
zRevText = sqlite3_mprintf(
2603426360
/* lower-case query is first run, producing upper-case query. */
2603526361
"with tabcols as materialized(\n"
2603626362
"select tname, cname\n"
2603726363
"from ("
26038
- " select ss.tname as tname, ti.name as cname\n"
26364
+ " select printf('\"%%w\"',ss.tname) as tname,"
26365
+ " printf('\"%%w\"',ti.name) as cname\n"
2603926366
" from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
2604026367
"select 'SELECT total(bad_text_count) AS bad_text_count\n"
2604126368
"FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
2604226369
" from (select 'SELECT COUNT(*) AS bad_text_count\n"
2604326370
"FROM '||tname||' WHERE '\n"
@@ -26305,11 +26632,11 @@
2630526632
#ifndef SQLITE_UNTESTABLE
2630626633
if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
2630726634
static const struct {
2630826635
const char *zCtrlName; /* Name of a test-control option */
2630926636
int ctrlCode; /* Integer code for that option */
26310
- int unSafe; /* Not valid for --safe mode */
26637
+ int unSafe; /* Not valid unless --unsafe-testing */
2631126638
const char *zUsage; /* Usage notes */
2631226639
} aCtrl[] = {
2631326640
{"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
2631426641
{"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
2631526642
/*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
@@ -26330,24 +26657,19 @@
2633026657
{"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
2633126658
{"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
2633226659
{"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
2633326660
{"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
2633426661
{"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
26662
+ {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
2633526663
};
2633626664
int testctrl = -1;
2633726665
int iCtrl = -1;
2633826666
int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
2633926667
int isOk = 0;
2634026668
int i, n2;
2634126669
const char *zCmd = 0;
2634226670
26343
- if( !ShellHasFlag(p,SHFLG_TestingMode) ){
26344
- utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n",
26345
- "testctrl");
26346
- rc = 1;
26347
- goto meta_command_exit;
26348
- }
2634926671
open_db(p, 0);
2635026672
zCmd = nArg>=2 ? azArg[1] : "help";
2635126673
2635226674
/* The argument can optionally begin with "-" or "--" */
2635326675
if( zCmd[0]=='-' && zCmd[1] ){
@@ -26357,10 +26679,11 @@
2635726679
2635826680
/* --help lists all test-controls */
2635926681
if( cli_strcmp(zCmd,"help")==0 ){
2636026682
utf8_printf(p->out, "Available test-controls:\n");
2636126683
for(i=0; i<ArraySize(aCtrl); i++){
26684
+ if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
2636226685
utf8_printf(p->out, " .testctrl %s %s\n",
2636326686
aCtrl[i].zCtrlName, aCtrl[i].zUsage);
2636426687
}
2636526688
rc = 1;
2636626689
goto meta_command_exit;
@@ -26368,10 +26691,11 @@
2636826691
2636926692
/* convert testctrl text option to value. allow any unique prefix
2637026693
** of the option name, or a numerical value. */
2637126694
n2 = strlen30(zCmd);
2637226695
for(i=0; i<ArraySize(aCtrl); i++){
26696
+ if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
2637326697
if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
2637426698
if( testctrl<0 ){
2637526699
testctrl = aCtrl[i].ctrlCode;
2637626700
iCtrl = i;
2637726701
}else{
@@ -26383,15 +26707,10 @@
2638326707
}
2638426708
}
2638526709
if( testctrl<0 ){
2638626710
utf8_printf(stderr,"Error: unknown test-control: %s\n"
2638726711
"Use \".testctrl --help\" for help\n", zCmd);
26388
- }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
26389
- utf8_printf(stderr,
26390
- "line %d: \".testctrl %s\" may not be used in safe mode\n",
26391
- p->lineno, aCtrl[iCtrl].zCtrlName);
26392
- exit(1);
2639326712
}else{
2639426713
switch(testctrl){
2639526714
2639626715
/* sqlite3_test_control(int, db, int) */
2639726716
case SQLITE_TESTCTRL_OPTIMIZATIONS:
@@ -26459,10 +26778,25 @@
2645926778
int opt = booleanValue(azArg[2]);
2646026779
rc2 = sqlite3_test_control(testctrl, opt);
2646126780
isOk = 3;
2646226781
}
2646326782
break;
26783
+
26784
+ /* sqlite3_test_control(int, int) */
26785
+ case SQLITE_TESTCTRL_USELONGDOUBLE: {
26786
+ int opt = -1;
26787
+ if( nArg==3 ){
26788
+ if( cli_strcmp(azArg[2],"default")==0 ){
26789
+ opt = 2;
26790
+ }else{
26791
+ opt = booleanValue(azArg[2]);
26792
+ }
26793
+ }
26794
+ rc2 = sqlite3_test_control(testctrl, opt);
26795
+ isOk = 1;
26796
+ break;
26797
+ }
2646426798
2646526799
/* sqlite3_test_control(sqlite3*) */
2646626800
case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
2646726801
rc2 = sqlite3_test_control(testctrl, p->db);
2646826802
isOk = 3;
2646926803
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -1222,10 +1222,50 @@
1222 freeText(&s);
1223 s.z = 0;
1224 }
1225 return s.z;
1226 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1227
1228 /*
1229 ** SQL function: shell_module_schema(X)
1230 **
1231 ** Return a fake schema for the table-valued function or eponymous virtual
@@ -2864,45 +2904,28 @@
2864 sqlite3_free(p);
2865 }
2866 }
2867
2868 /*
2869 ** Allocate a new Decimal object. Initialize it to the number given
2870 ** by the input string.
2871 */
2872 static Decimal *decimal_new(
2873 sqlite3_context *pCtx,
2874 sqlite3_value *pIn,
2875 int nAlt,
2876 const unsigned char *zAlt
2877 ){
2878 Decimal *p;
2879 int n, i;
2880 const unsigned char *zIn;
2881 int iExp = 0;
 
2882 p = sqlite3_malloc( sizeof(*p) );
2883 if( p==0 ) goto new_no_mem;
2884 p->sign = 0;
2885 p->oom = 0;
2886 p->isInit = 1;
2887 p->isNull = 0;
2888 p->nDigit = 0;
2889 p->nFrac = 0;
2890 if( zAlt ){
2891 n = nAlt,
2892 zIn = zAlt;
2893 }else{
2894 if( sqlite3_value_type(pIn)==SQLITE_NULL ){
2895 p->a = 0;
2896 p->isNull = 1;
2897 return p;
2898 }
2899 n = sqlite3_value_bytes(pIn);
2900 zIn = sqlite3_value_text(pIn);
2901 }
2902 p->a = sqlite3_malloc64( n+1 );
2903 if( p->a==0 ) goto new_no_mem;
2904 for(i=0; isspace(zIn[i]); i++){}
2905 if( zIn[i]=='-' ){
2906 p->sign = 1;
2907 i++;
2908 }else if( zIn[i]=='+' ){
@@ -2949,11 +2972,11 @@
2949 p->nFrac = 0;
2950 }
2951 }
2952 if( iExp>0 ){
2953 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
2954 if( p->a==0 ) goto new_no_mem;
2955 memset(p->a+p->nDigit, 0, iExp);
2956 p->nDigit += iExp;
2957 }
2958 }else if( iExp<0 ){
2959 int nExtra;
@@ -2968,20 +2991,89 @@
2968 p->nFrac = p->nDigit - 1;
2969 }
2970 }
2971 if( iExp>0 ){
2972 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
2973 if( p->a==0 ) goto new_no_mem;
2974 memmove(p->a+iExp, p->a, p->nDigit);
2975 memset(p->a, 0, iExp);
2976 p->nDigit += iExp;
2977 p->nFrac += iExp;
2978 }
2979 }
2980 return p;
2981
2982 new_no_mem:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2983 if( pCtx ) sqlite3_result_error_nomem(pCtx);
2984 sqlite3_free(p);
2985 return 0;
2986 }
2987
@@ -3037,23 +3129,68 @@
3037 z[i] = 0;
3038 sqlite3_result_text(pCtx, z, i, sqlite3_free);
3039 }
3040
3041 /*
3042 ** SQL Function: decimal(X)
3043 **
3044 ** Convert input X into decimal and then back into text
3045 */
3046 static void decimalFunc(
3047 sqlite3_context *context,
3048 int argc,
3049 sqlite3_value **argv
3050 ){
3051 Decimal *p = decimal_new(context, argv[0], 0, 0);
3052 UNUSED_PARAMETER(argc);
3053 decimal_result(context, p);
3054 decimal_free(p);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3055 }
3056
3057 /*
3058 ** Compare to Decimal objects. Return negative, 0, or positive if the
3059 ** first object is less than, equal to, or greater than the second.
@@ -3102,13 +3239,13 @@
3102 ){
3103 Decimal *pA = 0, *pB = 0;
3104 int rc;
3105
3106 UNUSED_PARAMETER(argc);
3107 pA = decimal_new(context, argv[0], 0, 0);
3108 if( pA==0 || pA->isNull ) goto cmp_done;
3109 pB = decimal_new(context, argv[1], 0, 0);
3110 if( pB==0 || pB->isNull ) goto cmp_done;
3111 rc = decimal_cmp(pA, pB);
3112 if( rc<0 ) rc = -1;
3113 else if( rc>0 ) rc = +1;
3114 sqlite3_result_int(context, rc);
@@ -3144,11 +3281,11 @@
3144 p->nFrac += nAddFrac;
3145 }
3146 }
3147
3148 /*
3149 ** Add the value pB into pA.
3150 **
3151 ** Both pA and pB might become denormalized by this routine.
3152 */
3153 static void decimal_add(Decimal *pA, Decimal *pB){
3154 int nSig, nFrac, nDigit;
@@ -3212,10 +3349,176 @@
3212 }
3213 }
3214 }
3215 }
3216 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3217
3218 /*
3219 ** Compare text in decimal order.
3220 */
3221 static int decimalCollFunc(
@@ -3223,12 +3526,12 @@
3223 int nKey1, const void *pKey1,
3224 int nKey2, const void *pKey2
3225 ){
3226 const unsigned char *zA = (const unsigned char*)pKey1;
3227 const unsigned char *zB = (const unsigned char*)pKey2;
3228 Decimal *pA = decimal_new(0, 0, nKey1, zA);
3229 Decimal *pB = decimal_new(0, 0, nKey2, zB);
3230 int rc;
3231 UNUSED_PARAMETER(notUsed);
3232 if( pA==0 || pB==0 ){
3233 rc = 0;
3234 }else{
@@ -3249,12 +3552,12 @@
3249 static void decimalAddFunc(
3250 sqlite3_context *context,
3251 int argc,
3252 sqlite3_value **argv
3253 ){
3254 Decimal *pA = decimal_new(context, argv[0], 0, 0);
3255 Decimal *pB = decimal_new(context, argv[1], 0, 0);
3256 UNUSED_PARAMETER(argc);
3257 decimal_add(pA, pB);
3258 decimal_result(context, pA);
3259 decimal_free(pA);
3260 decimal_free(pB);
@@ -3262,12 +3565,12 @@
3262 static void decimalSubFunc(
3263 sqlite3_context *context,
3264 int argc,
3265 sqlite3_value **argv
3266 ){
3267 Decimal *pA = decimal_new(context, argv[0], 0, 0);
3268 Decimal *pB = decimal_new(context, argv[1], 0, 0);
3269 UNUSED_PARAMETER(argc);
3270 if( pB ){
3271 pB->sign = !pB->sign;
3272 decimal_add(pA, pB);
3273 decimal_result(context, pA);
@@ -3301,11 +3604,11 @@
3301 }
3302 p->nDigit = 1;
3303 p->nFrac = 0;
3304 }
3305 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3306 pArg = decimal_new(context, argv[0], 0, 0);
3307 decimal_add(p, pArg);
3308 decimal_free(pArg);
3309 }
3310 static void decimalSumInverse(
3311 sqlite3_context *context,
@@ -3316,11 +3619,11 @@
3316 Decimal *pArg;
3317 UNUSED_PARAMETER(argc);
3318 p = sqlite3_aggregate_context(context, sizeof(*p));
3319 if( p==0 ) return;
3320 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3321 pArg = decimal_new(context, argv[0], 0, 0);
3322 if( pArg ) pArg->sign = !pArg->sign;
3323 decimal_add(p, pArg);
3324 decimal_free(pArg);
3325 }
3326 static void decimalSumValue(sqlite3_context *context){
@@ -3337,69 +3640,52 @@
3337
3338 /*
3339 ** SQL Function: decimal_mul(X, Y)
3340 **
3341 ** Return the product of X and Y.
3342 **
3343 ** All significant digits after the decimal point are retained.
3344 ** Trailing zeros after the decimal point are omitted as long as
3345 ** the number of digits after the decimal point is no less than
3346 ** either the number of digits in either input.
3347 */
3348 static void decimalMulFunc(
3349 sqlite3_context *context,
3350 int argc,
3351 sqlite3_value **argv
3352 ){
3353 Decimal *pA = decimal_new(context, argv[0], 0, 0);
3354 Decimal *pB = decimal_new(context, argv[1], 0, 0);
3355 signed char *acc = 0;
3356 int i, j, k;
3357 int minFrac;
3358 UNUSED_PARAMETER(argc);
3359 if( pA==0 || pA->oom || pA->isNull
3360 || pB==0 || pB->oom || pB->isNull
3361 ){
3362 goto mul_end;
3363 }
3364 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
3365 if( acc==0 ){
3366 sqlite3_result_error_nomem(context);
3367 goto mul_end;
3368 }
3369 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
3370 minFrac = pA->nFrac;
3371 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
3372 for(i=pA->nDigit-1; i>=0; i--){
3373 signed char f = pA->a[i];
3374 int carry = 0, x;
3375 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
3376 x = acc[k] + f*pB->a[j] + carry;
3377 acc[k] = x%10;
3378 carry = x/10;
3379 }
3380 x = acc[k] + carry;
3381 acc[k] = x%10;
3382 acc[k-1] += x/10;
3383 }
3384 sqlite3_free(pA->a);
3385 pA->a = acc;
3386 acc = 0;
3387 pA->nDigit += pB->nDigit + 2;
3388 pA->nFrac += pB->nFrac;
3389 pA->sign ^= pB->sign;
3390 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
3391 pA->nFrac--;
3392 pA->nDigit--;
3393 }
3394 decimal_result(context, pA);
3395
3396 mul_end:
3397 sqlite3_free(acc);
3398 decimal_free(pA);
3399 decimal_free(pB);
3400 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3401
3402 #ifdef _WIN32
3403
3404 #endif
3405 int sqlite3_decimal_init(
@@ -3409,27 +3695,30 @@
3409 ){
3410 int rc = SQLITE_OK;
3411 static const struct {
3412 const char *zFuncName;
3413 int nArg;
 
3414 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
3415 } aFunc[] = {
3416 { "decimal", 1, decimalFunc },
3417 { "decimal_cmp", 2, decimalCmpFunc },
3418 { "decimal_add", 2, decimalAddFunc },
3419 { "decimal_sub", 2, decimalSubFunc },
3420 { "decimal_mul", 2, decimalMulFunc },
 
 
3421 };
3422 unsigned int i;
3423 (void)pzErrMsg; /* Unused parameter */
3424
3425 SQLITE_EXTENSION_INIT2(pApi);
3426
3427 for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
3428 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
3429 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
3430 0, aFunc[i].xFunc, 0, 0);
3431 }
3432 if( rc==SQLITE_OK ){
3433 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
3434 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
3435 decimalSumStep, decimalSumFinalize,
@@ -4453,10 +4742,41 @@
4453 }
4454 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
4455 }
4456 }
4457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4458
4459 #ifdef _WIN32
4460
4461 #endif
4462 int sqlite3_ieee_init(
@@ -4474,11 +4794,11 @@
4474 { "ieee754", 2, 0, ieee754func },
4475 { "ieee754_mantissa", 1, 1, ieee754func },
4476 { "ieee754_exponent", 1, 2, ieee754func },
4477 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
4478 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
4479
4480 };
4481 unsigned int i;
4482 int rc = SQLITE_OK;
4483 SQLITE_EXTENSION_INIT2(pApi);
4484 (void)pzErrMsg; /* Unused parameter */
@@ -20964,10 +21284,16 @@
20964 #undef SHELL_SUB_MACRO
20965 #undef SHELL_SUBMACRO
20966 }
20967 #endif
20968
 
 
 
 
 
 
20969 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
20970 shellAddSchemaName, 0, 0);
20971 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
20972 shellModuleSchema, 0, 0);
20973 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
@@ -26033,11 +26359,12 @@
26033 zRevText = sqlite3_mprintf(
26034 /* lower-case query is first run, producing upper-case query. */
26035 "with tabcols as materialized(\n"
26036 "select tname, cname\n"
26037 "from ("
26038 " select ss.tname as tname, ti.name as cname\n"
 
26039 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
26040 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
26041 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
26042 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
26043 "FROM '||tname||' WHERE '\n"
@@ -26305,11 +26632,11 @@
26305 #ifndef SQLITE_UNTESTABLE
26306 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
26307 static const struct {
26308 const char *zCtrlName; /* Name of a test-control option */
26309 int ctrlCode; /* Integer code for that option */
26310 int unSafe; /* Not valid for --safe mode */
26311 const char *zUsage; /* Usage notes */
26312 } aCtrl[] = {
26313 {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
26314 {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
26315 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
@@ -26330,24 +26657,19 @@
26330 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
26331 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
26332 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
26333 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
26334 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
 
26335 };
26336 int testctrl = -1;
26337 int iCtrl = -1;
26338 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
26339 int isOk = 0;
26340 int i, n2;
26341 const char *zCmd = 0;
26342
26343 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
26344 utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n",
26345 "testctrl");
26346 rc = 1;
26347 goto meta_command_exit;
26348 }
26349 open_db(p, 0);
26350 zCmd = nArg>=2 ? azArg[1] : "help";
26351
26352 /* The argument can optionally begin with "-" or "--" */
26353 if( zCmd[0]=='-' && zCmd[1] ){
@@ -26357,10 +26679,11 @@
26357
26358 /* --help lists all test-controls */
26359 if( cli_strcmp(zCmd,"help")==0 ){
26360 utf8_printf(p->out, "Available test-controls:\n");
26361 for(i=0; i<ArraySize(aCtrl); i++){
 
26362 utf8_printf(p->out, " .testctrl %s %s\n",
26363 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
26364 }
26365 rc = 1;
26366 goto meta_command_exit;
@@ -26368,10 +26691,11 @@
26368
26369 /* convert testctrl text option to value. allow any unique prefix
26370 ** of the option name, or a numerical value. */
26371 n2 = strlen30(zCmd);
26372 for(i=0; i<ArraySize(aCtrl); i++){
 
26373 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
26374 if( testctrl<0 ){
26375 testctrl = aCtrl[i].ctrlCode;
26376 iCtrl = i;
26377 }else{
@@ -26383,15 +26707,10 @@
26383 }
26384 }
26385 if( testctrl<0 ){
26386 utf8_printf(stderr,"Error: unknown test-control: %s\n"
26387 "Use \".testctrl --help\" for help\n", zCmd);
26388 }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
26389 utf8_printf(stderr,
26390 "line %d: \".testctrl %s\" may not be used in safe mode\n",
26391 p->lineno, aCtrl[iCtrl].zCtrlName);
26392 exit(1);
26393 }else{
26394 switch(testctrl){
26395
26396 /* sqlite3_test_control(int, db, int) */
26397 case SQLITE_TESTCTRL_OPTIMIZATIONS:
@@ -26459,10 +26778,25 @@
26459 int opt = booleanValue(azArg[2]);
26460 rc2 = sqlite3_test_control(testctrl, opt);
26461 isOk = 3;
26462 }
26463 break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26464
26465 /* sqlite3_test_control(sqlite3*) */
26466 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
26467 rc2 = sqlite3_test_control(testctrl, p->db);
26468 isOk = 3;
26469
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -1222,10 +1222,50 @@
1222 freeText(&s);
1223 s.z = 0;
1224 }
1225 return s.z;
1226 }
1227
1228 /*
1229 ** SQL function: strtod(X)
1230 **
1231 ** Use the C-library strtod() function to convert string X into a double.
1232 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1233 ** routines against the C-library.
1234 */
1235 static void shellStrtod(
1236 sqlite3_context *pCtx,
1237 int nVal,
1238 sqlite3_value **apVal
1239 ){
1240 char *z = (char*)sqlite3_value_text(apVal[0]);
1241 UNUSED_PARAMETER(nVal);
1242 if( z==0 ) return;
1243 sqlite3_result_double(pCtx, strtod(z,0));
1244 }
1245
1246 /*
1247 ** SQL function: dtostr(X)
1248 **
1249 ** Use the C-library printf() function to convert real value X into a string.
1250 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1251 ** routines against the C-library.
1252 */
1253 static void shellDtostr(
1254 sqlite3_context *pCtx,
1255 int nVal,
1256 sqlite3_value **apVal
1257 ){
1258 double r = sqlite3_value_double(apVal[0]);
1259 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1260 char z[400];
1261 if( n<1 ) n = 1;
1262 if( n>350 ) n = 350;
1263 sprintf(z, "%#+.*e", n, r);
1264 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1265 }
1266
1267
1268 /*
1269 ** SQL function: shell_module_schema(X)
1270 **
1271 ** Return a fake schema for the table-valued function or eponymous virtual
@@ -2864,45 +2904,28 @@
2904 sqlite3_free(p);
2905 }
2906 }
2907
2908 /*
2909 ** Allocate a new Decimal object initialized to the text in zIn[].
2910 ** Return NULL if any kind of error occurs.
2911 */
2912 static Decimal *decimalNewFromText(const char *zIn, int n){
2913 Decimal *p = 0;
2914 int i;
 
 
 
 
 
 
2915 int iExp = 0;
2916
2917 p = sqlite3_malloc( sizeof(*p) );
2918 if( p==0 ) goto new_from_text_failed;
2919 p->sign = 0;
2920 p->oom = 0;
2921 p->isInit = 1;
2922 p->isNull = 0;
2923 p->nDigit = 0;
2924 p->nFrac = 0;
 
 
 
 
 
 
 
 
 
 
 
 
2925 p->a = sqlite3_malloc64( n+1 );
2926 if( p->a==0 ) goto new_from_text_failed;
2927 for(i=0; isspace(zIn[i]); i++){}
2928 if( zIn[i]=='-' ){
2929 p->sign = 1;
2930 i++;
2931 }else if( zIn[i]=='+' ){
@@ -2949,11 +2972,11 @@
2972 p->nFrac = 0;
2973 }
2974 }
2975 if( iExp>0 ){
2976 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
2977 if( p->a==0 ) goto new_from_text_failed;
2978 memset(p->a+p->nDigit, 0, iExp);
2979 p->nDigit += iExp;
2980 }
2981 }else if( iExp<0 ){
2982 int nExtra;
@@ -2968,20 +2991,89 @@
2991 p->nFrac = p->nDigit - 1;
2992 }
2993 }
2994 if( iExp>0 ){
2995 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
2996 if( p->a==0 ) goto new_from_text_failed;
2997 memmove(p->a+iExp, p->a, p->nDigit);
2998 memset(p->a, 0, iExp);
2999 p->nDigit += iExp;
3000 p->nFrac += iExp;
3001 }
3002 }
3003 return p;
3004
3005 new_from_text_failed:
3006 if( p ){
3007 if( p->a ) sqlite3_free(p->a);
3008 sqlite3_free(p);
3009 }
3010 return 0;
3011 }
3012
3013 /* Forward reference */
3014 static Decimal *decimalFromDouble(double);
3015
3016 /*
3017 ** Allocate a new Decimal object from an sqlite3_value. Return a pointer
3018 ** to the new object, or NULL if there is an error. If the pCtx argument
3019 ** is not NULL, then errors are reported on it as well.
3020 **
3021 ** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
3022 ** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length
3023 ** 8 bytes, the resulting double value is expanded into its decimal equivalent.
3024 ** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
3025 ** then NULL is returned.
3026 */
3027 static Decimal *decimal_new(
3028 sqlite3_context *pCtx, /* Report error here, if not null */
3029 sqlite3_value *pIn, /* Construct the decimal object from this */
3030 int bTextOnly /* Always interpret pIn as text if true */
3031 ){
3032 Decimal *p = 0;
3033 int eType = sqlite3_value_type(pIn);
3034 if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){
3035 eType = SQLITE_TEXT;
3036 }
3037 switch( eType ){
3038 case SQLITE_TEXT:
3039 case SQLITE_INTEGER: {
3040 const char *zIn = (const char*)sqlite3_value_text(pIn);
3041 int n = sqlite3_value_bytes(pIn);
3042 p = decimalNewFromText(zIn, n);
3043 if( p==0 ) goto new_failed;
3044 break;
3045 }
3046
3047 case SQLITE_FLOAT: {
3048 p = decimalFromDouble(sqlite3_value_double(pIn));
3049 break;
3050 }
3051
3052 case SQLITE_BLOB: {
3053 const unsigned char *x;
3054 unsigned int i;
3055 sqlite3_uint64 v = 0;
3056 double r;
3057
3058 if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break;
3059 x = sqlite3_value_blob(pIn);
3060 for(i=0; i<sizeof(r); i++){
3061 v = (v<<8) | x[i];
3062 }
3063 memcpy(&r, &v, sizeof(r));
3064 p = decimalFromDouble(r);
3065 break;
3066 }
3067
3068 case SQLITE_NULL: {
3069 break;
3070 }
3071 }
3072 return p;
3073
3074 new_failed:
3075 if( pCtx ) sqlite3_result_error_nomem(pCtx);
3076 sqlite3_free(p);
3077 return 0;
3078 }
3079
@@ -3037,23 +3129,68 @@
3129 z[i] = 0;
3130 sqlite3_result_text(pCtx, z, i, sqlite3_free);
3131 }
3132
3133 /*
3134 ** Make the given Decimal the result in an format similar to '%+#e'.
3135 ** In other words, show exponential notation with leading and trailing
3136 ** zeros omitted.
3137 */
3138 static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
3139 char *z; /* The output buffer */
3140 int i; /* Loop counter */
3141 int nZero; /* Number of leading zeros */
3142 int nDigit; /* Number of digits not counting trailing zeros */
3143 int nFrac; /* Digits to the right of the decimal point */
3144 int exp; /* Exponent value */
3145 signed char zero; /* Zero value */
3146 signed char *a; /* Array of digits */
3147
3148 if( p==0 || p->oom ){
3149 sqlite3_result_error_nomem(pCtx);
3150 return;
3151 }
3152 if( p->isNull ){
3153 sqlite3_result_null(pCtx);
3154 return;
3155 }
3156 for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
3157 for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
3158 nFrac = p->nFrac + (nDigit - p->nDigit);
3159 nDigit -= nZero;
3160 z = sqlite3_malloc( nDigit+20 );
3161 if( z==0 ){
3162 sqlite3_result_error_nomem(pCtx);
3163 return;
3164 }
3165 if( nDigit==0 ){
3166 zero = 0;
3167 a = &zero;
3168 nDigit = 1;
3169 nFrac = 0;
3170 }else{
3171 a = &p->a[nZero];
3172 }
3173 if( p->sign && nDigit>0 ){
3174 z[0] = '-';
3175 }else{
3176 z[0] = '+';
3177 }
3178 z[1] = a[0]+'0';
3179 z[2] = '.';
3180 if( nDigit==1 ){
3181 z[3] = '0';
3182 i = 4;
3183 }else{
3184 for(i=1; i<nDigit; i++){
3185 z[2+i] = a[i]+'0';
3186 }
3187 i = nDigit+2;
3188 }
3189 exp = nDigit - nFrac - 1;
3190 sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
3191 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
3192 }
3193
3194 /*
3195 ** Compare to Decimal objects. Return negative, 0, or positive if the
3196 ** first object is less than, equal to, or greater than the second.
@@ -3102,13 +3239,13 @@
3239 ){
3240 Decimal *pA = 0, *pB = 0;
3241 int rc;
3242
3243 UNUSED_PARAMETER(argc);
3244 pA = decimal_new(context, argv[0], 1);
3245 if( pA==0 || pA->isNull ) goto cmp_done;
3246 pB = decimal_new(context, argv[1], 1);
3247 if( pB==0 || pB->isNull ) goto cmp_done;
3248 rc = decimal_cmp(pA, pB);
3249 if( rc<0 ) rc = -1;
3250 else if( rc>0 ) rc = +1;
3251 sqlite3_result_int(context, rc);
@@ -3144,11 +3281,11 @@
3281 p->nFrac += nAddFrac;
3282 }
3283 }
3284
3285 /*
3286 ** Add the value pB into pA. A := A + B.
3287 **
3288 ** Both pA and pB might become denormalized by this routine.
3289 */
3290 static void decimal_add(Decimal *pA, Decimal *pB){
3291 int nSig, nFrac, nDigit;
@@ -3212,10 +3349,176 @@
3349 }
3350 }
3351 }
3352 }
3353 }
3354
3355 /*
3356 ** Multiply A by B. A := A * B
3357 **
3358 ** All significant digits after the decimal point are retained.
3359 ** Trailing zeros after the decimal point are omitted as long as
3360 ** the number of digits after the decimal point is no less than
3361 ** either the number of digits in either input.
3362 */
3363 static void decimalMul(Decimal *pA, Decimal *pB){
3364 signed char *acc = 0;
3365 int i, j, k;
3366 int minFrac;
3367
3368 if( pA==0 || pA->oom || pA->isNull
3369 || pB==0 || pB->oom || pB->isNull
3370 ){
3371 goto mul_end;
3372 }
3373 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
3374 if( acc==0 ){
3375 pA->oom = 1;
3376 goto mul_end;
3377 }
3378 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
3379 minFrac = pA->nFrac;
3380 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
3381 for(i=pA->nDigit-1; i>=0; i--){
3382 signed char f = pA->a[i];
3383 int carry = 0, x;
3384 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
3385 x = acc[k] + f*pB->a[j] + carry;
3386 acc[k] = x%10;
3387 carry = x/10;
3388 }
3389 x = acc[k] + carry;
3390 acc[k] = x%10;
3391 acc[k-1] += x/10;
3392 }
3393 sqlite3_free(pA->a);
3394 pA->a = acc;
3395 acc = 0;
3396 pA->nDigit += pB->nDigit + 2;
3397 pA->nFrac += pB->nFrac;
3398 pA->sign ^= pB->sign;
3399 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
3400 pA->nFrac--;
3401 pA->nDigit--;
3402 }
3403
3404 mul_end:
3405 sqlite3_free(acc);
3406 }
3407
3408 /*
3409 ** Create a new Decimal object that contains an integer power of 2.
3410 */
3411 static Decimal *decimalPow2(int N){
3412 Decimal *pA = 0; /* The result to be returned */
3413 Decimal *pX = 0; /* Multiplier */
3414 if( N<-20000 || N>20000 ) goto pow2_fault;
3415 pA = decimalNewFromText("1.0", 3);
3416 if( pA==0 || pA->oom ) goto pow2_fault;
3417 if( N==0 ) return pA;
3418 if( N>0 ){
3419 pX = decimalNewFromText("2.0", 3);
3420 }else{
3421 N = -N;
3422 pX = decimalNewFromText("0.5", 3);
3423 }
3424 if( pX==0 || pX->oom ) goto pow2_fault;
3425 while( 1 /* Exit by break */ ){
3426 if( N & 1 ){
3427 decimalMul(pA, pX);
3428 if( pA->oom ) goto pow2_fault;
3429 }
3430 N >>= 1;
3431 if( N==0 ) break;
3432 decimalMul(pX, pX);
3433 }
3434 decimal_free(pX);
3435 return pA;
3436
3437 pow2_fault:
3438 decimal_free(pA);
3439 decimal_free(pX);
3440 return 0;
3441 }
3442
3443 /*
3444 ** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
3445 */
3446 static Decimal *decimalFromDouble(double r){
3447 sqlite3_int64 m, a;
3448 int e;
3449 int isNeg;
3450 Decimal *pA;
3451 Decimal *pX;
3452 char zNum[100];
3453 if( r<0.0 ){
3454 isNeg = 1;
3455 r = -r;
3456 }else{
3457 isNeg = 0;
3458 }
3459 memcpy(&a,&r,sizeof(a));
3460 if( a==0 ){
3461 e = 0;
3462 m = 0;
3463 }else{
3464 e = a>>52;
3465 m = a & ((((sqlite3_int64)1)<<52)-1);
3466 if( e==0 ){
3467 m <<= 1;
3468 }else{
3469 m |= ((sqlite3_int64)1)<<52;
3470 }
3471 while( e<1075 && m>0 && (m&1)==0 ){
3472 m >>= 1;
3473 e++;
3474 }
3475 if( isNeg ) m = -m;
3476 e = e - 1075;
3477 if( e>971 ){
3478 return 0; /* A NaN or an Infinity */
3479 }
3480 }
3481
3482 /* At this point m is the integer significand and e is the exponent */
3483 sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m);
3484 pA = decimalNewFromText(zNum, (int)strlen(zNum));
3485 pX = decimalPow2(e);
3486 decimalMul(pA, pX);
3487 decimal_free(pX);
3488 return pA;
3489 }
3490
3491 /*
3492 ** SQL Function: decimal(X)
3493 ** OR: decimal_sci(X)
3494 **
3495 ** Convert input X into decimal and then back into text.
3496 **
3497 ** If X is originally a float, then a full decimal expansion of that floating
3498 ** point value is done. Or if X is an 8-byte blob, it is interpreted
3499 ** as a float and similarly expanded.
3500 **
3501 ** The decimal_sci(X) function returns the result in scientific notation.
3502 ** decimal(X) returns a complete decimal, without the e+NNN at the end.
3503 */
3504 static void decimalFunc(
3505 sqlite3_context *context,
3506 int argc,
3507 sqlite3_value **argv
3508 ){
3509 Decimal *p = decimal_new(context, argv[0], 0);
3510 UNUSED_PARAMETER(argc);
3511 if( p ){
3512 if( sqlite3_user_data(context)!=0 ){
3513 decimal_result_sci(context, p);
3514 }else{
3515 decimal_result(context, p);
3516 }
3517 decimal_free(p);
3518 }
3519 }
3520
3521 /*
3522 ** Compare text in decimal order.
3523 */
3524 static int decimalCollFunc(
@@ -3223,12 +3526,12 @@
3526 int nKey1, const void *pKey1,
3527 int nKey2, const void *pKey2
3528 ){
3529 const unsigned char *zA = (const unsigned char*)pKey1;
3530 const unsigned char *zB = (const unsigned char*)pKey2;
3531 Decimal *pA = decimalNewFromText((const char*)zA, nKey1);
3532 Decimal *pB = decimalNewFromText((const char*)zB, nKey2);
3533 int rc;
3534 UNUSED_PARAMETER(notUsed);
3535 if( pA==0 || pB==0 ){
3536 rc = 0;
3537 }else{
@@ -3249,12 +3552,12 @@
3552 static void decimalAddFunc(
3553 sqlite3_context *context,
3554 int argc,
3555 sqlite3_value **argv
3556 ){
3557 Decimal *pA = decimal_new(context, argv[0], 1);
3558 Decimal *pB = decimal_new(context, argv[1], 1);
3559 UNUSED_PARAMETER(argc);
3560 decimal_add(pA, pB);
3561 decimal_result(context, pA);
3562 decimal_free(pA);
3563 decimal_free(pB);
@@ -3262,12 +3565,12 @@
3565 static void decimalSubFunc(
3566 sqlite3_context *context,
3567 int argc,
3568 sqlite3_value **argv
3569 ){
3570 Decimal *pA = decimal_new(context, argv[0], 1);
3571 Decimal *pB = decimal_new(context, argv[1], 1);
3572 UNUSED_PARAMETER(argc);
3573 if( pB ){
3574 pB->sign = !pB->sign;
3575 decimal_add(pA, pB);
3576 decimal_result(context, pA);
@@ -3301,11 +3604,11 @@
3604 }
3605 p->nDigit = 1;
3606 p->nFrac = 0;
3607 }
3608 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3609 pArg = decimal_new(context, argv[0], 1);
3610 decimal_add(p, pArg);
3611 decimal_free(pArg);
3612 }
3613 static void decimalSumInverse(
3614 sqlite3_context *context,
@@ -3316,11 +3619,11 @@
3619 Decimal *pArg;
3620 UNUSED_PARAMETER(argc);
3621 p = sqlite3_aggregate_context(context, sizeof(*p));
3622 if( p==0 ) return;
3623 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3624 pArg = decimal_new(context, argv[0], 1);
3625 if( pArg ) pArg->sign = !pArg->sign;
3626 decimal_add(p, pArg);
3627 decimal_free(pArg);
3628 }
3629 static void decimalSumValue(sqlite3_context *context){
@@ -3337,69 +3640,52 @@
3640
3641 /*
3642 ** SQL Function: decimal_mul(X, Y)
3643 **
3644 ** Return the product of X and Y.
 
 
 
 
 
3645 */
3646 static void decimalMulFunc(
3647 sqlite3_context *context,
3648 int argc,
3649 sqlite3_value **argv
3650 ){
3651 Decimal *pA = decimal_new(context, argv[0], 1);
3652 Decimal *pB = decimal_new(context, argv[1], 1);
 
 
 
3653 UNUSED_PARAMETER(argc);
3654 if( pA==0 || pA->oom || pA->isNull
3655 || pB==0 || pB->oom || pB->isNull
3656 ){
3657 goto mul_end;
3658 }
3659 decimalMul(pA, pB);
3660 if( pA->oom ){
 
3661 goto mul_end;
3662 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3663 decimal_result(context, pA);
3664
3665 mul_end:
 
3666 decimal_free(pA);
3667 decimal_free(pB);
3668 }
3669
3670 /*
3671 ** SQL Function: decimal_pow2(N)
3672 **
3673 ** Return the N-th power of 2. N must be an integer.
3674 */
3675 static void decimalPow2Func(
3676 sqlite3_context *context,
3677 int argc,
3678 sqlite3_value **argv
3679 ){
3680 UNUSED_PARAMETER(argc);
3681 if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
3682 Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
3683 decimal_result_sci(context, pA);
3684 decimal_free(pA);
3685 }
3686 }
3687
3688 #ifdef _WIN32
3689
3690 #endif
3691 int sqlite3_decimal_init(
@@ -3409,27 +3695,30 @@
3695 ){
3696 int rc = SQLITE_OK;
3697 static const struct {
3698 const char *zFuncName;
3699 int nArg;
3700 int iArg;
3701 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
3702 } aFunc[] = {
3703 { "decimal", 1, 0, decimalFunc },
3704 { "decimal_sci", 1, 1, decimalFunc },
3705 { "decimal_cmp", 2, 0, decimalCmpFunc },
3706 { "decimal_add", 2, 0, decimalAddFunc },
3707 { "decimal_sub", 2, 0, decimalSubFunc },
3708 { "decimal_mul", 2, 0, decimalMulFunc },
3709 { "decimal_pow2", 1, 0, decimalPow2Func },
3710 };
3711 unsigned int i;
3712 (void)pzErrMsg; /* Unused parameter */
3713
3714 SQLITE_EXTENSION_INIT2(pApi);
3715
3716 for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
3717 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
3718 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
3719 aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0);
3720 }
3721 if( rc==SQLITE_OK ){
3722 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
3723 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
3724 decimalSumStep, decimalSumFinalize,
@@ -4453,10 +4742,41 @@
4742 }
4743 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
4744 }
4745 }
4746
4747 /*
4748 ** SQL Function: ieee754_inc(r,N)
4749 **
4750 ** Move the floating point value r by N quantums and return the new
4751 ** values.
4752 **
4753 ** Behind the scenes: this routine merely casts r into a 64-bit unsigned
4754 ** integer, adds N, then casts the value back into float.
4755 **
4756 ** Example: To find the smallest positive number:
4757 **
4758 ** SELECT ieee754_inc(0.0,+1);
4759 */
4760 static void ieee754inc(
4761 sqlite3_context *context,
4762 int argc,
4763 sqlite3_value **argv
4764 ){
4765 double r;
4766 sqlite3_int64 N;
4767 sqlite3_uint64 m1, m2;
4768 double r2;
4769 UNUSED_PARAMETER(argc);
4770 r = sqlite3_value_double(argv[0]);
4771 N = sqlite3_value_int64(argv[1]);
4772 memcpy(&m1, &r, 8);
4773 m2 = m1 + N;
4774 memcpy(&r2, &m2, 8);
4775 sqlite3_result_double(context, r2);
4776 }
4777
4778
4779 #ifdef _WIN32
4780
4781 #endif
4782 int sqlite3_ieee_init(
@@ -4474,11 +4794,11 @@
4794 { "ieee754", 2, 0, ieee754func },
4795 { "ieee754_mantissa", 1, 1, ieee754func },
4796 { "ieee754_exponent", 1, 2, ieee754func },
4797 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
4798 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
4799 { "ieee754_inc", 2, 0, ieee754inc },
4800 };
4801 unsigned int i;
4802 int rc = SQLITE_OK;
4803 SQLITE_EXTENSION_INIT2(pApi);
4804 (void)pzErrMsg; /* Unused parameter */
@@ -20964,10 +21284,16 @@
21284 #undef SHELL_SUB_MACRO
21285 #undef SHELL_SUBMACRO
21286 }
21287 #endif
21288
21289 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
21290 shellStrtod, 0, 0);
21291 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
21292 shellDtostr, 0, 0);
21293 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
21294 shellDtostr, 0, 0);
21295 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
21296 shellAddSchemaName, 0, 0);
21297 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
21298 shellModuleSchema, 0, 0);
21299 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
@@ -26033,11 +26359,12 @@
26359 zRevText = sqlite3_mprintf(
26360 /* lower-case query is first run, producing upper-case query. */
26361 "with tabcols as materialized(\n"
26362 "select tname, cname\n"
26363 "from ("
26364 " select printf('\"%%w\"',ss.tname) as tname,"
26365 " printf('\"%%w\"',ti.name) as cname\n"
26366 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
26367 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
26368 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
26369 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
26370 "FROM '||tname||' WHERE '\n"
@@ -26305,11 +26632,11 @@
26632 #ifndef SQLITE_UNTESTABLE
26633 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
26634 static const struct {
26635 const char *zCtrlName; /* Name of a test-control option */
26636 int ctrlCode; /* Integer code for that option */
26637 int unSafe; /* Not valid unless --unsafe-testing */
26638 const char *zUsage; /* Usage notes */
26639 } aCtrl[] = {
26640 {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
26641 {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
26642 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
@@ -26330,24 +26657,19 @@
26657 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
26658 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
26659 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
26660 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
26661 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
26662 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
26663 };
26664 int testctrl = -1;
26665 int iCtrl = -1;
26666 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
26667 int isOk = 0;
26668 int i, n2;
26669 const char *zCmd = 0;
26670
 
 
 
 
 
 
26671 open_db(p, 0);
26672 zCmd = nArg>=2 ? azArg[1] : "help";
26673
26674 /* The argument can optionally begin with "-" or "--" */
26675 if( zCmd[0]=='-' && zCmd[1] ){
@@ -26357,10 +26679,11 @@
26679
26680 /* --help lists all test-controls */
26681 if( cli_strcmp(zCmd,"help")==0 ){
26682 utf8_printf(p->out, "Available test-controls:\n");
26683 for(i=0; i<ArraySize(aCtrl); i++){
26684 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
26685 utf8_printf(p->out, " .testctrl %s %s\n",
26686 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
26687 }
26688 rc = 1;
26689 goto meta_command_exit;
@@ -26368,10 +26691,11 @@
26691
26692 /* convert testctrl text option to value. allow any unique prefix
26693 ** of the option name, or a numerical value. */
26694 n2 = strlen30(zCmd);
26695 for(i=0; i<ArraySize(aCtrl); i++){
26696 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
26697 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
26698 if( testctrl<0 ){
26699 testctrl = aCtrl[i].ctrlCode;
26700 iCtrl = i;
26701 }else{
@@ -26383,15 +26707,10 @@
26707 }
26708 }
26709 if( testctrl<0 ){
26710 utf8_printf(stderr,"Error: unknown test-control: %s\n"
26711 "Use \".testctrl --help\" for help\n", zCmd);
 
 
 
 
 
26712 }else{
26713 switch(testctrl){
26714
26715 /* sqlite3_test_control(int, db, int) */
26716 case SQLITE_TESTCTRL_OPTIMIZATIONS:
@@ -26459,10 +26778,25 @@
26778 int opt = booleanValue(azArg[2]);
26779 rc2 = sqlite3_test_control(testctrl, opt);
26780 isOk = 3;
26781 }
26782 break;
26783
26784 /* sqlite3_test_control(int, int) */
26785 case SQLITE_TESTCTRL_USELONGDOUBLE: {
26786 int opt = -1;
26787 if( nArg==3 ){
26788 if( cli_strcmp(azArg[2],"default")==0 ){
26789 opt = 2;
26790 }else{
26791 opt = booleanValue(azArg[2]);
26792 }
26793 }
26794 rc2 = sqlite3_test_control(testctrl, opt);
26795 isOk = 1;
26796 break;
26797 }
26798
26799 /* sqlite3_test_control(sqlite3*) */
26800 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
26801 rc2 = sqlite3_test_control(testctrl, p->db);
26802 isOk = 3;
26803
+521 -332
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
1616
** if you want a wrapper to interface SQLite with your choice of programming
1717
** language. The code for the "sqlite3" command-line shell is also in a
1818
** separate file. This file contains only code for the core SQLite library.
1919
**
2020
** The content in this amalgamation comes from Fossil check-in
21
-** a5f77862c0fe0189aa4246a1e55bb7c537c.
21
+** eab3c98639be531744e60440223bb9ee76b.
2222
*/
2323
#define SQLITE_CORE 1
2424
#define SQLITE_AMALGAMATION 1
2525
#ifndef SQLITE_PRIVATE
2626
# define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459459
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460460
** [sqlite_version()] and [sqlite_source_id()].
461461
*/
462462
#define SQLITE_VERSION "3.43.0"
463463
#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"
465465
466466
/*
467467
** CAPI3REF: Run-Time Library Version Numbers
468468
** KEYWORDS: sqlite3_version sqlite3_sourceid
469469
**
@@ -8487,11 +8487,12 @@
84878487
#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
84888488
#define SQLITE_TESTCTRL_SEEK_COUNT 30
84898489
#define SQLITE_TESTCTRL_TRACEFLAGS 31
84908490
#define SQLITE_TESTCTRL_TUNE 32
84918491
#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 */
84938494
84948495
/*
84958496
** CAPI3REF: SQL Keyword Checking
84968497
**
84978498
** These routines provide access to the set of SQL language keywords
@@ -14934,14 +14935,16 @@
1493414935
typedef struct Column Column;
1493514936
typedef struct Cte Cte;
1493614937
typedef struct CteUse CteUse;
1493714938
typedef struct Db Db;
1493814939
typedef struct DbFixer DbFixer;
14940
+typedef struct DblDbl DblDbl;
1493914941
typedef struct Schema Schema;
1494014942
typedef struct Expr Expr;
1494114943
typedef struct ExprList ExprList;
1494214944
typedef struct FKey FKey;
14945
+typedef struct FpDecode FpDecode;
1494314946
typedef struct FuncDestructor FuncDestructor;
1494414947
typedef struct FuncDef FuncDef;
1494514948
typedef struct FuncDefHash FuncDefHash;
1494614949
typedef struct IdList IdList;
1494714950
typedef struct Index Index;
@@ -16399,11 +16402,11 @@
1639916402
#define OPFLG_INITIALIZER {\
1640016403
/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
1640116404
/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
1640216405
/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\
1640316406
/* 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,\
1640516408
/* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
1640616409
/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
1640716410
/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\
1640816411
/* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
1640916412
/* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
@@ -16411,11 +16414,11 @@
1641116414
/* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\
1641216415
/* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x26, 0x26,\
1641316416
/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
1641416417
/* 112 */ 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40, 0x00,\
1641516418
/* 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,\
1641716420
/* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
1641816421
/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
1641916422
/* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
1642016423
/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
1642116424
/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x50, 0x40,\
@@ -19706,10 +19709,11 @@
1970619709
u8 bFullMutex; /* True to enable full mutexing */
1970719710
u8 bOpenUri; /* True to interpret filenames as URIs */
1970819711
u8 bUseCis; /* Use covering indices for full-scans */
1970919712
u8 bSmallMalloc; /* Avoid large memory allocations if true */
1971019713
u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
19714
+ u8 bUseLongDouble; /* Make use of long double */
1971119715
int mxStrlen; /* Maximum string length */
1971219716
int neverCorrupt; /* Database is always well-formed */
1971319717
int szLookaside; /* Default lookaside buffer size */
1971419718
int nLookaside; /* Default lookaside buffer count */
1971519719
int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
@@ -20214,10 +20218,24 @@
2021420218
int nArg; /* Total number of arguments */
2021520219
int nUsed; /* Number of arguments used so far */
2021620220
sqlite3_value **apArg; /* The argument values */
2021720221
};
2021820222
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);
2021920237
SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
2022020238
SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
2022120239
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
2022220240
SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
2022320241
#endif
@@ -20653,10 +20671,11 @@
2065320671
SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
2065420672
SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
2065520673
SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
2065620674
SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
2065720675
SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
20676
+
2065820677
SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
2065920678
SQLITE_PRIVATE i64 sqlite3RealToI64(double);
2066020679
SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*);
2066120680
SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
2066220681
SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
@@ -22374,10 +22393,11 @@
2237422393
SQLITE_THREADSAFE==1, /* bFullMutex */
2237522394
SQLITE_USE_URI, /* bOpenUri */
2237622395
SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
2237722396
0, /* bSmallMalloc */
2237822397
1, /* bExtraSchemaChecks */
22398
+ sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */
2237922399
0x7ffffffe, /* mxStrlen */
2238022400
0, /* neverCorrupt */
2238122401
SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
2238222402
SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
2238322403
{0,0,0,0,0,0,0,0}, /* m */
@@ -30416,61 +30436,10 @@
3041630436
**
3041730437
** %S Takes a pointer to SrcItem. Shows name or database.name
3041830438
** %!S Like %S but prefer the zName over the zAlias
3041930439
*/
3042030440
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
-
3047230441
/*
3047330442
** Set the StrAccum object to an error mode.
3047430443
*/
3047530444
SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
3047630445
assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
@@ -30558,24 +30527,19 @@
3055830527
etByte cThousand; /* Thousands separator for %d and %u */
3055930528
etByte xtype = etINVALID; /* Conversion paradigm */
3056030529
u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
3056130530
char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
3056230531
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 */
3056630533
const et_info *infop; /* Pointer to the appropriate info structure */
3056730534
char *zOut; /* Rendering buffer */
3056830535
int nOut; /* Size of the rendering buffer */
3056930536
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 */
3057430538
etByte flag_dp; /* True if decimal point should be shown */
3057530539
etByte flag_rtz; /* True if trailing zeros should be removed */
30576
-#endif
30540
+
3057730541
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
3057830542
char buf[etBUFSIZE]; /* Conversion buffer */
3057930543
3058030544
/* pAccum never starts out with an empty buffer that was obtained from
3058130545
** malloc(). This precondition is required by the mprintf("%z...")
@@ -30846,98 +30810,65 @@
3084630810
}
3084730811
length = (int)(&zOut[nOut-1]-bufpt);
3084830812
break;
3084930813
case etFLOAT:
3085030814
case etEXP:
30851
- case etGENERIC:
30815
+ case etGENERIC: {
30816
+ FpDecode s;
30817
+ int iRound;
30818
+ int j;
30819
+
3085230820
if( bArgList ){
3085330821
realvalue = getDoubleArg(pArgList);
3085430822
}else{
3085530823
realvalue = va_arg(ap,double);
3085630824
}
30857
-#ifdef SQLITE_OMIT_FLOATING_POINT
30858
- length = 0;
30859
-#else
3086030825
if( precision<0 ) precision = 6; /* Set default precision */
3086130826
#ifdef SQLITE_FP_PRECISION_LIMIT
3086230827
if( precision>SQLITE_FP_PRECISION_LIMIT ){
3086330828
precision = SQLITE_FP_PRECISION_LIMIT;
3086430829
}
3086530830
#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=='-' ){
3086830863
prefix = '-';
3086930864
}else{
3087030865
prefix = flag_prefix;
3087130866
}
30872
- exp = 0;
30867
+
30868
+ exp = s.iDP-1;
3087330869
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
- }
3093930870
3094030871
/*
3094130872
** If the field type is etGENERIC, then convert to either etEXP
3094230873
** or etFLOAT, as appropriate.
3094330874
*/
@@ -30953,13 +30884,12 @@
3095330884
flag_rtz = flag_altform2;
3095430885
}
3095530886
if( xtype==etEXP ){
3095630887
e2 = 0;
3095730888
}else{
30958
- e2 = exp;
30889
+ e2 = s.iDP - 1;
3095930890
}
30960
- nsd = 16 + flag_altform2*10;
3096130891
bufpt = buf;
3096230892
{
3096330893
i64 szBufNeeded; /* Size of a temporary buffer needed */
3096430894
szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
3096530895
if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
@@ -30973,42 +30903,31 @@
3097330903
/* The sign in front of the number */
3097430904
if( prefix ){
3097530905
*(bufpt++) = prefix;
3097630906
}
3097730907
/* Digits prior to the decimal point */
30908
+ j = 0;
3097830909
if( e2<0 ){
3097930910
*(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
- }
3098530911
}else{
3098630912
for(; e2>=0; e2--){
30987
- *(bufpt++) = et_getdigit(&realvalue,&nsd);
30913
+ *(bufpt++) = j<s.n ? s.z[j++] : '0';
3098830914
if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
3098930915
}
3099030916
}
3099130917
/* The decimal point */
3099230918
if( flag_dp ){
3099330919
*(bufpt++) = '.';
3099430920
}
3099530921
/* "0" digits after the decimal point but before the first
3099630922
** 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++){
3099930924
*(bufpt++) = '0';
3100030925
}
3100130926
/* 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';
3101030929
}
3101130930
/* Remove trailing zeros and the "." if no digits follow the "." */
3101230931
if( flag_rtz && flag_dp ){
3101330932
while( bufpt[-1]=='0' ) *(--bufpt) = 0;
3101430933
assert( bufpt>zOut );
@@ -31020,10 +30939,11 @@
3102030939
}
3102130940
}
3102230941
}
3102330942
/* Add the "eNNN" suffix */
3102430943
if( xtype==etEXP ){
30944
+ exp = s.iDP - 1;
3102530945
*(bufpt++) = aDigits[infop->charset];
3102630946
if( exp<0 ){
3102730947
*(bufpt++) = '-'; exp = -exp;
3102830948
}else{
3102930949
*(bufpt++) = '+';
@@ -31053,12 +30973,12 @@
3105330973
}
3105430974
i = prefix!=0;
3105530975
while( nPad-- ) bufpt[i++] = '0';
3105630976
length = width;
3105730977
}
31058
-#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
3105930978
break;
30979
+ }
3106030980
case etSIZE:
3106130981
if( !bArgList ){
3106230982
*(va_arg(ap,int*)) = pAccum->nChar;
3106330983
}
3106430984
length = width = 0;
@@ -34447,47 +34367,44 @@
3444734367
z++;
3444834368
}
3444934369
return h;
3445034370
}
3445134371
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)
3445534373
**
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.
3445734377
*/
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;
3448934406
}
3449034407
3449134408
/*
3449234409
** The string z[] is an text representation of a real number.
3449334410
** Convert this string to a double and write it into *pResult.
@@ -34524,16 +34441,15 @@
3452434441
#ifndef SQLITE_OMIT_FLOATING_POINT
3452534442
int incr;
3452634443
const char *zEnd;
3452734444
/* sign * significand * (10 ^ (esign * exponent)) */
3452834445
int sign = 1; /* sign of significand */
34529
- i64 s = 0; /* significand */
34446
+ u64 s = 0; /* significand */
3453034447
int d = 0; /* adjust exponent for shifting decimal point */
3453134448
int esign = 1; /* sign of exponent */
3453234449
int e = 0; /* exponent */
3453334450
int eValid = 1; /* True exponent is either not used or is well-formed */
34534
- double result;
3453534451
int nDigit = 0; /* Number of digits processed */
3453634452
int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
3453734453
3453834454
assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
3453934455
*pResult = 0.0; /* Default return value, in case of an error */
@@ -34569,11 +34485,11 @@
3456934485
3457034486
/* copy max significant digits to significand */
3457134487
while( z<zEnd && sqlite3Isdigit(*z) ){
3457234488
s = s*10 + (*z - '0');
3457334489
z+=incr; nDigit++;
34574
- if( s>=((LARGEST_INT64-9)/10) ){
34490
+ if( s>=((LARGEST_UINT64-9)/10) ){
3457534491
/* skip non-significant significand digits
3457634492
** (increase exponent by d to shift decimal left) */
3457734493
while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
3457834494
}
3457934495
}
@@ -34584,11 +34500,11 @@
3458434500
z+=incr;
3458534501
eType++;
3458634502
/* copy digits from after decimal to significand
3458734503
** (decrease exponent by d to shift decimal right) */
3458834504
while( z<zEnd && sqlite3Isdigit(*z) ){
34589
- if( s<((LARGEST_INT64-9)/10) ){
34505
+ if( s<((LARGEST_UINT64-9)/10) ){
3459034506
s = s*10 + (*z - '0');
3459134507
d--;
3459234508
nDigit++;
3459334509
}
3459434510
z+=incr;
@@ -34624,82 +34540,83 @@
3462434540
3462534541
/* skip trailing spaces */
3462634542
while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
3462734543
3462834544
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
+
3462934551
/* adjust exponent by d, and update sign */
3463034552
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:
3470134618
/* return true if number and no extra non-whitespace characters after */
3470234619
if( z==zEnd && nDigit>0 && eValid && eType>0 ){
3470334620
return eType;
3470434621
}else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
3470534622
return -1;
@@ -34988,10 +34905,157 @@
3498834905
SQLITE_PRIVATE int sqlite3Atoi(const char *z){
3498934906
int x = 0;
3499034907
sqlite3GetInt32(z, &x);
3499134908
return x;
3499234909
}
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
+}
3499335057
3499435058
/*
3499535059
** Try to convert z into an unsigned 32-bit integer. Return true on
3499635060
** success and false if there is an error.
3499735061
**
@@ -74248,10 +74312,11 @@
7424874312
pCur->aiIdx[pCur->iPage] = pCur->ix;
7424974313
pCur->apPage[pCur->iPage] = pCur->pPage;
7425074314
pCur->ix = 0;
7425174315
pCur->iPage++;
7425274316
rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
74317
+ assert( pCur->pPage!=0 || rc!=SQLITE_OK );
7425374318
if( rc==SQLITE_OK
7425474319
&& (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
7425574320
){
7425674321
releasePage(pCur->pPage);
7425774322
rc = SQLITE_CORRUPT_PGNO(newPgno);
@@ -74476,11 +74541,11 @@
7447674541
if( rc==SQLITE_OK ){
7447774542
assert( pCur->pPage->nCell>0 );
7447874543
*pRes = 0;
7447974544
rc = moveToLeftmost(pCur);
7448074545
}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) );
7448274547
*pRes = 1;
7448374548
rc = SQLITE_OK;
7448474549
}
7448574550
return rc;
7448674551
}
@@ -81636,40 +81701,10 @@
8163681701
SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem *p){
8163781702
assert( !VdbeMemDynamic(p) );
8163881703
if( p->szMalloc ) vdbeMemClear(p);
8163981704
}
8164081705
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
-
8167181706
/*
8167281707
** Return some kind of integer value which is the best we can do
8167381708
** at representing the value that *pMem describes as an integer.
8167481709
** If pMem is an integer, then the value is exact. If pMem is
8167581710
** a floating-point then the value returned is the integer part.
@@ -81692,11 +81727,11 @@
8169281727
flags = pMem->flags;
8169381728
if( flags & (MEM_Int|MEM_IntReal) ){
8169481729
testcase( flags & MEM_IntReal );
8169581730
return pMem->u.i;
8169681731
}else if( flags & MEM_Real ){
81697
- return doubleToInt64(pMem->u.r);
81732
+ return sqlite3RealToI64(pMem->u.r);
8169881733
}else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
8169981734
return memIntValue(pMem);
8170081735
}else{
8170181736
return 0;
8170281737
}
@@ -81754,11 +81789,11 @@
8175481789
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
8175581790
8175681791
if( pMem->flags & MEM_IntReal ){
8175781792
MemSetTypeFlag(pMem, MEM_Int);
8175881793
}else{
81759
- i64 ix = doubleToInt64(pMem->u.r);
81794
+ i64 ix = sqlite3RealToI64(pMem->u.r);
8176081795
8176181796
/* Only mark the value as an integer if
8176281797
**
8176381798
** (1) the round-trip conversion real->int->real is a no-op, and
8176481799
** (2) The integer is neither the largest nor the smallest
@@ -81822,12 +81857,12 @@
8182281857
/* Convert a floating point value to its closest integer. Do so in
8182381858
** a way that avoids 'outside the range of representable values' warnings
8182481859
** from UBSAN.
8182581860
*/
8182681861
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;
8182981864
return (i64)r;
8183081865
}
8183181866
8183281867
/*
8183381868
** Convert pMem so that it has type MEM_Real or MEM_Int.
@@ -83601,11 +83636,11 @@
8360183636
zMsg, P4_DYNAMIC);
8360283637
sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
8360383638
if( bPush){
8360483639
pParse->addrExplain = iThis;
8360583640
}
83606
- sqlite3VdbeScanStatus(v, iThis, 0, 0, 0, 0);
83641
+ sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
8360783642
}
8360883643
return addr;
8360983644
}
8361083645
8361183646
/*
@@ -84313,12 +84348,12 @@
8431384348
pScan = &p->aScan[ii];
8431484349
if( pScan->addrExplain==addrExplain ) break;
8431584350
pScan = 0;
8431684351
}
8431784352
if( pScan ){
84318
- pScan->addrLoop = addrLoop;
84319
- pScan->addrVisit = addrVisit;
84353
+ if( addrLoop>0 ) pScan->addrLoop = addrLoop;
84354
+ if( addrVisit>0 ) pScan->addrVisit = addrVisit;
8432084355
}
8432184356
}
8432284357
}
8432384358
#endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */
8432484359
@@ -96891,11 +96926,11 @@
9689196926
** a register that is the source for a pseudo-table cursor created using
9689296927
** OpenPseudo. That pseudo-table cursor is the one that is identified by
9689396928
** parameter P3. Clearing the P3 column cache as part of this opcode saves
9689496929
** us from having to issue a separate NullRow instruction to clear that cache.
9689596930
*/
96896
-case OP_SorterData: {
96931
+case OP_SorterData: { /* ncycle */
9689796932
VdbeCursor *pC;
9689896933
9689996934
pOut = &aMem[pOp->p2];
9690096935
pC = p->apCsr[pOp->p1];
9690196936
assert( isSorter(pC) );
@@ -97166,12 +97201,12 @@
9716697201
** end. We use the OP_Sort opcode instead of OP_Rewind to do the
9716797202
** rewinding so that the global variable will be incremented and
9716897203
** regression tests can determine whether or not the optimizer is
9716997204
** correctly optimizing out sorts.
9717097205
*/
97171
-case OP_SorterSort: /* jump */
97172
-case OP_Sort: { /* jump */
97206
+case OP_SorterSort: /* jump ncycle */
97207
+case OP_Sort: { /* jump ncycle */
9717397208
#ifdef SQLITE_TEST
9717497209
sqlite3_sort_count++;
9717597210
sqlite3_search_count--;
9717697211
#endif
9717797212
p->aCounter[SQLITE_STMTSTATUS_SORT]++;
@@ -126267,11 +126302,11 @@
126267126302
if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
126268126303
/* The value has no fractional part so there is nothing to round */
126269126304
}else if( n==0 ){
126270126305
r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
126271126306
}else{
126272
- zBuf = sqlite3_mprintf("%.*f",n,r);
126307
+ zBuf = sqlite3_mprintf("%!.*f",n,r);
126273126308
if( zBuf==0 ){
126274126309
sqlite3_result_error_nomem(context);
126275126310
return;
126276126311
}
126277126312
sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
@@ -127476,16 +127511,71 @@
127476127511
** An instance of the following structure holds the context of a
127477127512
** sum() or avg() aggregate computation.
127478127513
*/
127479127514
typedef struct SumCtx SumCtx;
127480127515
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 */
127483127519
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 */
127486127522
};
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
+}
127487127577
127488127578
/*
127489127579
** Routines used to compute the sum, average, and total.
127490127580
**
127491127581
** The SUM() function follows the (broken) SQL standard which means
@@ -127502,19 +127592,34 @@
127502127592
UNUSED_PARAMETER(argc);
127503127593
p = sqlite3_aggregate_context(context, sizeof(*p));
127504127594
type = sqlite3_value_numeric_type(argv[0]);
127505127595
if( p && type!=SQLITE_NULL ){
127506127596
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
+ }
127512127612
}
127513127613
}else{
127514
- p->rSum += sqlite3_value_double(argv[0]);
127515127614
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
+ }
127516127621
}
127517127622
}
127518127623
}
127519127624
#ifndef SQLITE_OMIT_WINDOWFUNC
127520127625
static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
@@ -127527,17 +127632,22 @@
127527127632
/* p is always non-NULL because sumStep() will have been called first
127528127633
** to initialize it */
127529127634
if( ALWAYS(p) && type!=SQLITE_NULL ){
127530127635
assert( p->cnt>0 );
127531127636
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
+ }
127537127647
}else{
127538
- p->rSum -= sqlite3_value_double(argv[0]);
127648
+ kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
127539127649
}
127540127650
}
127541127651
}
127542127652
#else
127543127653
# define sumInverse 0
@@ -127544,31 +127654,46 @@
127544127654
#endif /* SQLITE_OMIT_WINDOWFUNC */
127545127655
static void sumFinalize(sqlite3_context *context){
127546127656
SumCtx *p;
127547127657
p = sqlite3_aggregate_context(context, 0);
127548127658
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
+ }
127553127665
}else{
127554127666
sqlite3_result_int64(context, p->iSum);
127555127667
}
127556127668
}
127557127669
}
127558127670
static void avgFinalize(sqlite3_context *context){
127559127671
SumCtx *p;
127560127672
p = sqlite3_aggregate_context(context, 0);
127561127673
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);
127563127681
}
127564127682
}
127565127683
static void totalFinalize(sqlite3_context *context){
127566127684
SumCtx *p;
127685
+ double r = 0.0;
127567127686
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);
127570127695
}
127571127696
127572127697
/*
127573127698
** The following structure keeps track of state information for the
127574127699
** count() aggregate function.
@@ -128154,10 +128279,41 @@
128154128279
if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
128155128280
x = sqlite3_value_double(argv[0]);
128156128281
sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
128157128282
}
128158128283
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
+
128159128315
/*
128160128316
** All of the FuncDef structures in the aBuiltinFunc[] array above
128161128317
** to the global function hash table. This occurs at start-time (as
128162128318
** a consequence of calling sqlite3_initialize()).
128163128319
**
@@ -128225,10 +128381,13 @@
128225128381
FUNCTION(printf, -1, 0, 0, printfFunc ),
128226128382
FUNCTION(format, -1, 0, 0, printfFunc ),
128227128383
FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
128228128384
FUNCTION(char, -1, 0, 0, charFunc ),
128229128385
FUNCTION(abs, 1, 0, 0, absFunc ),
128386
+#ifdef SQLITE_DEBUG
128387
+ FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ),
128388
+#endif
128230128389
#ifndef SQLITE_OMIT_FLOATING_POINT
128231128390
FUNCTION(round, 1, 0, 0, roundFunc ),
128232128391
FUNCTION(round, 2, 0, 0, roundFunc ),
128233128392
#endif
128234128393
FUNCTION(upper, 1, 0, 0, upperFunc ),
@@ -147420,13 +147579,17 @@
147420147579
int regBase;
147421147580
int regRecord;
147422147581
int nCol;
147423147582
int nGroupBy;
147424147583
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",
147426147588
(sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
147427
- "DISTINCT" : "GROUP BY");
147589
+ "DISTINCT" : "GROUP BY"
147590
+ ));
147428147591
147429147592
groupBySort = 1;
147430147593
nGroupBy = pGroupBy->nExpr;
147431147594
nCol = nGroupBy;
147432147595
j = nGroupBy;
@@ -147447,22 +147610,27 @@
147447147610
j++;
147448147611
}
147449147612
}
147450147613
pAggInfo->directMode = 0;
147451147614
regRecord = sqlite3GetTempReg(pParse);
147615
+ sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v));
147452147616
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
147453147617
sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
147618
+ sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
147454147619
sqlite3ReleaseTempReg(pParse, regRecord);
147455147620
sqlite3ReleaseTempRange(pParse, regBase, nCol);
147456147621
TREETRACE(0x2,pParse,p,("WhereEnd\n"));
147457147622
sqlite3WhereEnd(pWInfo);
147458147623
pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
147459147624
sortOut = sqlite3GetTempReg(pParse);
147625
+ sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0);
147460147626
sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
147461147627
sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
147462147628
VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
147463147629
pAggInfo->useSortingIdx = 1;
147630
+ sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
147631
+ sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
147464147632
}
147465147633
147466147634
/* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
147467147635
** that are indexed (and that were previously identified and tagged
147468147636
** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
@@ -153901,10 +154069,16 @@
153901154069
sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
153902154070
}
153903154071
if( wsFlags & WHERE_INDEXED ){
153904154072
sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
153905154073
}
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);
153906154080
}
153907154081
}
153908154082
}
153909154083
#endif
153910154084
@@ -179822,10 +179996,25 @@
179822179996
*pI1 = rLogEst;
179823179997
*pU64 = sqlite3LogEstToInt(rLogEst);
179824179998
*pI2 = sqlite3LogEst(*pU64);
179825179999
break;
179826180000
}
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
+ }
179827180016
179828180017
179829180018
#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
179830180019
/* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
179831180020
**
@@ -243110,11 +243299,11 @@
243110243299
int nArg, /* Number of args */
243111243300
sqlite3_value **apUnused /* Function arguments */
243112243301
){
243113243302
assert( nArg==0 );
243114243303
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);
243116243305
}
243117243306
243118243307
/*
243119243308
** Return true if zName is the extension on one of the shadow tables used
243120243309
** by this module.
243121243310
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** a5f77862c0fe0189aa4246a1e55bb7c537c.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.43.0"
463 #define SQLITE_VERSION_NUMBER 3043000
464 #define SQLITE_SOURCE_ID "2023-06-23 11:10:13 fa5f77862c0fe0189aa4246a1e55bb7c537c28c436ec10b75f5fa141e5e4aff0"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -8487,11 +8487,12 @@
8487 #define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8488 #define SQLITE_TESTCTRL_SEEK_COUNT 30
8489 #define SQLITE_TESTCTRL_TRACEFLAGS 31
8490 #define SQLITE_TESTCTRL_TUNE 32
8491 #define SQLITE_TESTCTRL_LOGEST 33
8492 #define SQLITE_TESTCTRL_LAST 33 /* Largest TESTCTRL */
 
8493
8494 /*
8495 ** CAPI3REF: SQL Keyword Checking
8496 **
8497 ** These routines provide access to the set of SQL language keywords
@@ -14934,14 +14935,16 @@
14934 typedef struct Column Column;
14935 typedef struct Cte Cte;
14936 typedef struct CteUse CteUse;
14937 typedef struct Db Db;
14938 typedef struct DbFixer DbFixer;
 
14939 typedef struct Schema Schema;
14940 typedef struct Expr Expr;
14941 typedef struct ExprList ExprList;
14942 typedef struct FKey FKey;
 
14943 typedef struct FuncDestructor FuncDestructor;
14944 typedef struct FuncDef FuncDef;
14945 typedef struct FuncDefHash FuncDefHash;
14946 typedef struct IdList IdList;
14947 typedef struct Index Index;
@@ -16399,11 +16402,11 @@
16399 #define OPFLG_INITIALIZER {\
16400 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
16401 /* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
16402 /* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\
16403 /* 24 */ 0x49, 0x01, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,\
16404 /* 32 */ 0x41, 0x01, 0x01, 0x01, 0x41, 0x01, 0x41, 0x41,\
16405 /* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
16406 /* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16407 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\
16408 /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
16409 /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
@@ -16411,11 +16414,11 @@
16411 /* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\
16412 /* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x26, 0x26,\
16413 /* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
16414 /* 112 */ 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40, 0x00,\
16415 /* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\
16416 /* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,\
16417 /* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
16418 /* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
16419 /* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
16420 /* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
16421 /* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x50, 0x40,\
@@ -19706,10 +19709,11 @@
19706 u8 bFullMutex; /* True to enable full mutexing */
19707 u8 bOpenUri; /* True to interpret filenames as URIs */
19708 u8 bUseCis; /* Use covering indices for full-scans */
19709 u8 bSmallMalloc; /* Avoid large memory allocations if true */
19710 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
 
19711 int mxStrlen; /* Maximum string length */
19712 int neverCorrupt; /* Database is always well-formed */
19713 int szLookaside; /* Default lookaside buffer size */
19714 int nLookaside; /* Default lookaside buffer count */
19715 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
@@ -20214,10 +20218,24 @@
20214 int nArg; /* Total number of arguments */
20215 int nUsed; /* Number of arguments used so far */
20216 sqlite3_value **apArg; /* The argument values */
20217 };
20218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20219 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
20220 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
20221 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
20222 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
20223 #endif
@@ -20653,10 +20671,11 @@
20653 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
20654 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
20655 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
20656 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
20657 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
 
20658 SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
20659 SQLITE_PRIVATE i64 sqlite3RealToI64(double);
20660 SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*);
20661 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
20662 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
@@ -22374,10 +22393,11 @@
22374 SQLITE_THREADSAFE==1, /* bFullMutex */
22375 SQLITE_USE_URI, /* bOpenUri */
22376 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
22377 0, /* bSmallMalloc */
22378 1, /* bExtraSchemaChecks */
 
22379 0x7ffffffe, /* mxStrlen */
22380 0, /* neverCorrupt */
22381 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
22382 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
22383 {0,0,0,0,0,0,0,0}, /* m */
@@ -30416,61 +30436,10 @@
30416 **
30417 ** %S Takes a pointer to SrcItem. Shows name or database.name
30418 ** %!S Like %S but prefer the zName over the zAlias
30419 */
30420
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 /*
30473 ** Set the StrAccum object to an error mode.
30474 */
30475 SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
30476 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
@@ -30558,24 +30527,19 @@
30558 etByte cThousand; /* Thousands separator for %d and %u */
30559 etByte xtype = etINVALID; /* Conversion paradigm */
30560 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
30561 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
30562 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 */
30566 const et_info *infop; /* Pointer to the appropriate info structure */
30567 char *zOut; /* Rendering buffer */
30568 int nOut; /* Size of the rendering buffer */
30569 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 */
30574 etByte flag_dp; /* True if decimal point should be shown */
30575 etByte flag_rtz; /* True if trailing zeros should be removed */
30576 #endif
30577 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
30578 char buf[etBUFSIZE]; /* Conversion buffer */
30579
30580 /* pAccum never starts out with an empty buffer that was obtained from
30581 ** malloc(). This precondition is required by the mprintf("%z...")
@@ -30846,98 +30810,65 @@
30846 }
30847 length = (int)(&zOut[nOut-1]-bufpt);
30848 break;
30849 case etFLOAT:
30850 case etEXP:
30851 case etGENERIC:
 
 
 
 
30852 if( bArgList ){
30853 realvalue = getDoubleArg(pArgList);
30854 }else{
30855 realvalue = va_arg(ap,double);
30856 }
30857 #ifdef SQLITE_OMIT_FLOATING_POINT
30858 length = 0;
30859 #else
30860 if( precision<0 ) precision = 6; /* Set default precision */
30861 #ifdef SQLITE_FP_PRECISION_LIMIT
30862 if( precision>SQLITE_FP_PRECISION_LIMIT ){
30863 precision = SQLITE_FP_PRECISION_LIMIT;
30864 }
30865 #endif
30866 if( realvalue<0.0 ){
30867 realvalue = -realvalue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30868 prefix = '-';
30869 }else{
30870 prefix = flag_prefix;
30871 }
30872 exp = 0;
 
30873 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
30940 /*
30941 ** If the field type is etGENERIC, then convert to either etEXP
30942 ** or etFLOAT, as appropriate.
30943 */
@@ -30953,13 +30884,12 @@
30953 flag_rtz = flag_altform2;
30954 }
30955 if( xtype==etEXP ){
30956 e2 = 0;
30957 }else{
30958 e2 = exp;
30959 }
30960 nsd = 16 + flag_altform2*10;
30961 bufpt = buf;
30962 {
30963 i64 szBufNeeded; /* Size of a temporary buffer needed */
30964 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
30965 if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
@@ -30973,42 +30903,31 @@
30973 /* The sign in front of the number */
30974 if( prefix ){
30975 *(bufpt++) = prefix;
30976 }
30977 /* Digits prior to the decimal point */
 
30978 if( e2<0 ){
30979 *(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 }else{
30986 for(; e2>=0; e2--){
30987 *(bufpt++) = et_getdigit(&realvalue,&nsd);
30988 if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
30989 }
30990 }
30991 /* The decimal point */
30992 if( flag_dp ){
30993 *(bufpt++) = '.';
30994 }
30995 /* "0" digits after the decimal point but before the first
30996 ** significant digit of the number */
30997 for(e2++; e2<0; precision--, e2++){
30998 assert( precision>0 );
30999 *(bufpt++) = '0';
31000 }
31001 /* 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 }
31010 }
31011 /* Remove trailing zeros and the "." if no digits follow the "." */
31012 if( flag_rtz && flag_dp ){
31013 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
31014 assert( bufpt>zOut );
@@ -31020,10 +30939,11 @@
31020 }
31021 }
31022 }
31023 /* Add the "eNNN" suffix */
31024 if( xtype==etEXP ){
 
31025 *(bufpt++) = aDigits[infop->charset];
31026 if( exp<0 ){
31027 *(bufpt++) = '-'; exp = -exp;
31028 }else{
31029 *(bufpt++) = '+';
@@ -31053,12 +30973,12 @@
31053 }
31054 i = prefix!=0;
31055 while( nPad-- ) bufpt[i++] = '0';
31056 length = width;
31057 }
31058 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
31059 break;
 
31060 case etSIZE:
31061 if( !bArgList ){
31062 *(va_arg(ap,int*)) = pAccum->nChar;
31063 }
31064 length = width = 0;
@@ -34447,47 +34367,44 @@
34447 z++;
34448 }
34449 return h;
34450 }
34451
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.
34455 **
34456 ** This routine only works for values of E between 1 and 341.
 
 
34457 */
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
34489 }
34490
34491 /*
34492 ** The string z[] is an text representation of a real number.
34493 ** Convert this string to a double and write it into *pResult.
@@ -34524,16 +34441,15 @@
34524 #ifndef SQLITE_OMIT_FLOATING_POINT
34525 int incr;
34526 const char *zEnd;
34527 /* sign * significand * (10 ^ (esign * exponent)) */
34528 int sign = 1; /* sign of significand */
34529 i64 s = 0; /* significand */
34530 int d = 0; /* adjust exponent for shifting decimal point */
34531 int esign = 1; /* sign of exponent */
34532 int e = 0; /* exponent */
34533 int eValid = 1; /* True exponent is either not used or is well-formed */
34534 double result;
34535 int nDigit = 0; /* Number of digits processed */
34536 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
34537
34538 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
34539 *pResult = 0.0; /* Default return value, in case of an error */
@@ -34569,11 +34485,11 @@
34569
34570 /* copy max significant digits to significand */
34571 while( z<zEnd && sqlite3Isdigit(*z) ){
34572 s = s*10 + (*z - '0');
34573 z+=incr; nDigit++;
34574 if( s>=((LARGEST_INT64-9)/10) ){
34575 /* skip non-significant significand digits
34576 ** (increase exponent by d to shift decimal left) */
34577 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
34578 }
34579 }
@@ -34584,11 +34500,11 @@
34584 z+=incr;
34585 eType++;
34586 /* copy digits from after decimal to significand
34587 ** (decrease exponent by d to shift decimal right) */
34588 while( z<zEnd && sqlite3Isdigit(*z) ){
34589 if( s<((LARGEST_INT64-9)/10) ){
34590 s = s*10 + (*z - '0');
34591 d--;
34592 nDigit++;
34593 }
34594 z+=incr;
@@ -34624,82 +34540,83 @@
34624
34625 /* skip trailing spaces */
34626 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
34627
34628 do_atof_calc:
 
 
 
 
 
 
34629 /* adjust exponent by d, and update sign */
34630 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
34701 /* return true if number and no extra non-whitespace characters after */
34702 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
34703 return eType;
34704 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
34705 return -1;
@@ -34988,10 +34905,157 @@
34988 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
34989 int x = 0;
34990 sqlite3GetInt32(z, &x);
34991 return x;
34992 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34993
34994 /*
34995 ** Try to convert z into an unsigned 32-bit integer. Return true on
34996 ** success and false if there is an error.
34997 **
@@ -74248,10 +74312,11 @@
74248 pCur->aiIdx[pCur->iPage] = pCur->ix;
74249 pCur->apPage[pCur->iPage] = pCur->pPage;
74250 pCur->ix = 0;
74251 pCur->iPage++;
74252 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
 
74253 if( rc==SQLITE_OK
74254 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
74255 ){
74256 releasePage(pCur->pPage);
74257 rc = SQLITE_CORRUPT_PGNO(newPgno);
@@ -74476,11 +74541,11 @@
74476 if( rc==SQLITE_OK ){
74477 assert( pCur->pPage->nCell>0 );
74478 *pRes = 0;
74479 rc = moveToLeftmost(pCur);
74480 }else if( rc==SQLITE_EMPTY ){
74481 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74482 *pRes = 1;
74483 rc = SQLITE_OK;
74484 }
74485 return rc;
74486 }
@@ -81636,40 +81701,10 @@
81636 SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem *p){
81637 assert( !VdbeMemDynamic(p) );
81638 if( p->szMalloc ) vdbeMemClear(p);
81639 }
81640
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 /*
81672 ** Return some kind of integer value which is the best we can do
81673 ** at representing the value that *pMem describes as an integer.
81674 ** If pMem is an integer, then the value is exact. If pMem is
81675 ** a floating-point then the value returned is the integer part.
@@ -81692,11 +81727,11 @@
81692 flags = pMem->flags;
81693 if( flags & (MEM_Int|MEM_IntReal) ){
81694 testcase( flags & MEM_IntReal );
81695 return pMem->u.i;
81696 }else if( flags & MEM_Real ){
81697 return doubleToInt64(pMem->u.r);
81698 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
81699 return memIntValue(pMem);
81700 }else{
81701 return 0;
81702 }
@@ -81754,11 +81789,11 @@
81754 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
81755
81756 if( pMem->flags & MEM_IntReal ){
81757 MemSetTypeFlag(pMem, MEM_Int);
81758 }else{
81759 i64 ix = doubleToInt64(pMem->u.r);
81760
81761 /* Only mark the value as an integer if
81762 **
81763 ** (1) the round-trip conversion real->int->real is a no-op, and
81764 ** (2) The integer is neither the largest nor the smallest
@@ -81822,12 +81857,12 @@
81822 /* Convert a floating point value to its closest integer. Do so in
81823 ** a way that avoids 'outside the range of representable values' warnings
81824 ** from UBSAN.
81825 */
81826 SQLITE_PRIVATE i64 sqlite3RealToI64(double r){
81827 if( r<=(double)SMALLEST_INT64 ) return SMALLEST_INT64;
81828 if( r>=(double)LARGEST_INT64) return LARGEST_INT64;
81829 return (i64)r;
81830 }
81831
81832 /*
81833 ** Convert pMem so that it has type MEM_Real or MEM_Int.
@@ -83601,11 +83636,11 @@
83601 zMsg, P4_DYNAMIC);
83602 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
83603 if( bPush){
83604 pParse->addrExplain = iThis;
83605 }
83606 sqlite3VdbeScanStatus(v, iThis, 0, 0, 0, 0);
83607 }
83608 return addr;
83609 }
83610
83611 /*
@@ -84313,12 +84348,12 @@
84313 pScan = &p->aScan[ii];
84314 if( pScan->addrExplain==addrExplain ) break;
84315 pScan = 0;
84316 }
84317 if( pScan ){
84318 pScan->addrLoop = addrLoop;
84319 pScan->addrVisit = addrVisit;
84320 }
84321 }
84322 }
84323 #endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */
84324
@@ -96891,11 +96926,11 @@
96891 ** a register that is the source for a pseudo-table cursor created using
96892 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
96893 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
96894 ** us from having to issue a separate NullRow instruction to clear that cache.
96895 */
96896 case OP_SorterData: {
96897 VdbeCursor *pC;
96898
96899 pOut = &aMem[pOp->p2];
96900 pC = p->apCsr[pOp->p1];
96901 assert( isSorter(pC) );
@@ -97166,12 +97201,12 @@
97166 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
97167 ** rewinding so that the global variable will be incremented and
97168 ** regression tests can determine whether or not the optimizer is
97169 ** correctly optimizing out sorts.
97170 */
97171 case OP_SorterSort: /* jump */
97172 case OP_Sort: { /* jump */
97173 #ifdef SQLITE_TEST
97174 sqlite3_sort_count++;
97175 sqlite3_search_count--;
97176 #endif
97177 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
@@ -126267,11 +126302,11 @@
126267 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
126268 /* The value has no fractional part so there is nothing to round */
126269 }else if( n==0 ){
126270 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
126271 }else{
126272 zBuf = sqlite3_mprintf("%.*f",n,r);
126273 if( zBuf==0 ){
126274 sqlite3_result_error_nomem(context);
126275 return;
126276 }
126277 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
@@ -127476,16 +127511,71 @@
127476 ** An instance of the following structure holds the context of a
127477 ** sum() or avg() aggregate computation.
127478 */
127479 typedef struct SumCtx SumCtx;
127480 struct SumCtx {
127481 double rSum; /* Floating point sum */
127482 i64 iSum; /* Integer sum */
 
127483 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 */
127486 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127487
127488 /*
127489 ** Routines used to compute the sum, average, and total.
127490 **
127491 ** The SUM() function follows the (broken) SQL standard which means
@@ -127502,19 +127592,34 @@
127502 UNUSED_PARAMETER(argc);
127503 p = sqlite3_aggregate_context(context, sizeof(*p));
127504 type = sqlite3_value_numeric_type(argv[0]);
127505 if( p && type!=SQLITE_NULL ){
127506 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;
 
 
 
 
 
 
 
 
 
 
127512 }
127513 }else{
127514 p->rSum += sqlite3_value_double(argv[0]);
127515 p->approx = 1;
 
 
 
 
 
 
127516 }
127517 }
127518 }
127519 #ifndef SQLITE_OMIT_WINDOWFUNC
127520 static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
@@ -127527,17 +127632,22 @@
127527 /* p is always non-NULL because sumStep() will have been called first
127528 ** to initialize it */
127529 if( ALWAYS(p) && type!=SQLITE_NULL ){
127530 assert( p->cnt>0 );
127531 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;
 
 
 
 
 
127537 }else{
127538 p->rSum -= sqlite3_value_double(argv[0]);
127539 }
127540 }
127541 }
127542 #else
127543 # define sumInverse 0
@@ -127544,31 +127654,46 @@
127544 #endif /* SQLITE_OMIT_WINDOWFUNC */
127545 static void sumFinalize(sqlite3_context *context){
127546 SumCtx *p;
127547 p = sqlite3_aggregate_context(context, 0);
127548 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);
 
 
127553 }else{
127554 sqlite3_result_int64(context, p->iSum);
127555 }
127556 }
127557 }
127558 static void avgFinalize(sqlite3_context *context){
127559 SumCtx *p;
127560 p = sqlite3_aggregate_context(context, 0);
127561 if( p && p->cnt>0 ){
127562 sqlite3_result_double(context, p->rSum/(double)p->cnt);
 
 
 
 
 
 
127563 }
127564 }
127565 static void totalFinalize(sqlite3_context *context){
127566 SumCtx *p;
 
127567 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);
 
 
 
 
 
 
127570 }
127571
127572 /*
127573 ** The following structure keeps track of state information for the
127574 ** count() aggregate function.
@@ -128154,10 +128279,41 @@
128154 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
128155 x = sqlite3_value_double(argv[0]);
128156 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
128157 }
128158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128159 /*
128160 ** All of the FuncDef structures in the aBuiltinFunc[] array above
128161 ** to the global function hash table. This occurs at start-time (as
128162 ** a consequence of calling sqlite3_initialize()).
128163 **
@@ -128225,10 +128381,13 @@
128225 FUNCTION(printf, -1, 0, 0, printfFunc ),
128226 FUNCTION(format, -1, 0, 0, printfFunc ),
128227 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
128228 FUNCTION(char, -1, 0, 0, charFunc ),
128229 FUNCTION(abs, 1, 0, 0, absFunc ),
 
 
 
128230 #ifndef SQLITE_OMIT_FLOATING_POINT
128231 FUNCTION(round, 1, 0, 0, roundFunc ),
128232 FUNCTION(round, 2, 0, 0, roundFunc ),
128233 #endif
128234 FUNCTION(upper, 1, 0, 0, upperFunc ),
@@ -147420,13 +147579,17 @@
147420 int regBase;
147421 int regRecord;
147422 int nCol;
147423 int nGroupBy;
147424
147425 explainTempTable(pParse,
 
 
 
147426 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
147427 "DISTINCT" : "GROUP BY");
 
147428
147429 groupBySort = 1;
147430 nGroupBy = pGroupBy->nExpr;
147431 nCol = nGroupBy;
147432 j = nGroupBy;
@@ -147447,22 +147610,27 @@
147447 j++;
147448 }
147449 }
147450 pAggInfo->directMode = 0;
147451 regRecord = sqlite3GetTempReg(pParse);
 
147452 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
147453 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
 
147454 sqlite3ReleaseTempReg(pParse, regRecord);
147455 sqlite3ReleaseTempRange(pParse, regBase, nCol);
147456 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
147457 sqlite3WhereEnd(pWInfo);
147458 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
147459 sortOut = sqlite3GetTempReg(pParse);
 
147460 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
147461 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
147462 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
147463 pAggInfo->useSortingIdx = 1;
 
 
147464 }
147465
147466 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
147467 ** that are indexed (and that were previously identified and tagged
147468 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
@@ -153901,10 +154069,16 @@
153901 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
153902 }
153903 if( wsFlags & WHERE_INDEXED ){
153904 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
153905 }
 
 
 
 
 
 
153906 }
153907 }
153908 }
153909 #endif
153910
@@ -179822,10 +179996,25 @@
179822 *pI1 = rLogEst;
179823 *pU64 = sqlite3LogEstToInt(rLogEst);
179824 *pI2 = sqlite3LogEst(*pU64);
179825 break;
179826 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179827
179828
179829 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
179830 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
179831 **
@@ -243110,11 +243299,11 @@
243110 int nArg, /* Number of args */
243111 sqlite3_value **apUnused /* Function arguments */
243112 ){
243113 assert( nArg==0 );
243114 UNUSED_PARAM2(nArg, apUnused);
243115 sqlite3_result_text(pCtx, "fts5: 2023-06-22 13:01:02 d35c214811aac7dec0000ca2aa77231f74a7963dd0c53cf25a65ade5ef0f8dc0", -1, SQLITE_TRANSIENT);
243116 }
243117
243118 /*
243119 ** Return true if zName is the extension on one of the shadow tables used
243120 ** by this module.
243121
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** eab3c98639be531744e60440223bb9ee76b.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -459,11 +459,11 @@
459 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
460 ** [sqlite_version()] and [sqlite_source_id()].
461 */
462 #define SQLITE_VERSION "3.43.0"
463 #define SQLITE_VERSION_NUMBER 3043000
464 #define SQLITE_SOURCE_ID "2023-07-08 14:27:55 beab3c98639be531744e60440223bb9ee76bc15234aff05e5efb273c8241dfd8"
465
466 /*
467 ** CAPI3REF: Run-Time Library Version Numbers
468 ** KEYWORDS: sqlite3_version sqlite3_sourceid
469 **
@@ -8487,11 +8487,12 @@
8487 #define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8488 #define SQLITE_TESTCTRL_SEEK_COUNT 30
8489 #define SQLITE_TESTCTRL_TRACEFLAGS 31
8490 #define SQLITE_TESTCTRL_TUNE 32
8491 #define SQLITE_TESTCTRL_LOGEST 33
8492 #define SQLITE_TESTCTRL_USELONGDOUBLE 34
8493 #define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */
8494
8495 /*
8496 ** CAPI3REF: SQL Keyword Checking
8497 **
8498 ** These routines provide access to the set of SQL language keywords
@@ -14934,14 +14935,16 @@
14935 typedef struct Column Column;
14936 typedef struct Cte Cte;
14937 typedef struct CteUse CteUse;
14938 typedef struct Db Db;
14939 typedef struct DbFixer DbFixer;
14940 typedef struct DblDbl DblDbl;
14941 typedef struct Schema Schema;
14942 typedef struct Expr Expr;
14943 typedef struct ExprList ExprList;
14944 typedef struct FKey FKey;
14945 typedef struct FpDecode FpDecode;
14946 typedef struct FuncDestructor FuncDestructor;
14947 typedef struct FuncDef FuncDef;
14948 typedef struct FuncDefHash FuncDefHash;
14949 typedef struct IdList IdList;
14950 typedef struct Index Index;
@@ -16399,11 +16402,11 @@
16402 #define OPFLG_INITIALIZER {\
16403 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x41, 0x00,\
16404 /* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
16405 /* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\
16406 /* 24 */ 0x49, 0x01, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,\
16407 /* 32 */ 0x41, 0x01, 0x41, 0x41, 0x41, 0x01, 0x41, 0x41,\
16408 /* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
16409 /* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16410 /* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\
16411 /* 64 */ 0x01, 0x00, 0x00, 0x02, 0x02, 0x08, 0x00, 0x10,\
16412 /* 72 */ 0x10, 0x10, 0x00, 0x10, 0x00, 0x10, 0x10, 0x00,\
@@ -16411,11 +16414,11 @@
16414 /* 88 */ 0x02, 0x00, 0x00, 0x12, 0x1e, 0x20, 0x40, 0x00,\
16415 /* 96 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x40, 0x26, 0x26,\
16416 /* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
16417 /* 112 */ 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40, 0x00,\
16418 /* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\
16419 /* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,\
16420 /* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
16421 /* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
16422 /* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
16423 /* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
16424 /* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x50, 0x40,\
@@ -19706,10 +19709,11 @@
19709 u8 bFullMutex; /* True to enable full mutexing */
19710 u8 bOpenUri; /* True to interpret filenames as URIs */
19711 u8 bUseCis; /* Use covering indices for full-scans */
19712 u8 bSmallMalloc; /* Avoid large memory allocations if true */
19713 u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
19714 u8 bUseLongDouble; /* Make use of long double */
19715 int mxStrlen; /* Maximum string length */
19716 int neverCorrupt; /* Database is always well-formed */
19717 int szLookaside; /* Default lookaside buffer size */
19718 int nLookaside; /* Default lookaside buffer count */
19719 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
@@ -20214,10 +20218,24 @@
20218 int nArg; /* Total number of arguments */
20219 int nUsed; /* Number of arguments used so far */
20220 sqlite3_value **apArg; /* The argument values */
20221 };
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);
20237 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
20238 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
20239 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
20240 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
20241 #endif
@@ -20653,10 +20671,11 @@
20671 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
20672 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
20673 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
20674 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
20675 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
20676
20677 SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
20678 SQLITE_PRIVATE i64 sqlite3RealToI64(double);
20679 SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*);
20680 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
20681 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
@@ -22374,10 +22393,11 @@
22393 SQLITE_THREADSAFE==1, /* bFullMutex */
22394 SQLITE_USE_URI, /* bOpenUri */
22395 SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
22396 0, /* bSmallMalloc */
22397 1, /* bExtraSchemaChecks */
22398 sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */
22399 0x7ffffffe, /* mxStrlen */
22400 0, /* neverCorrupt */
22401 SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
22402 SQLITE_STMTJRNL_SPILL, /* nStmtSpill */
22403 {0,0,0,0,0,0,0,0}, /* m */
@@ -30416,61 +30436,10 @@
30436 **
30437 ** %S Takes a pointer to SrcItem. Shows name or database.name
30438 ** %!S Like %S but prefer the zName over the zAlias
30439 */
30440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30441 /*
30442 ** Set the StrAccum object to an error mode.
30443 */
30444 SQLITE_PRIVATE void sqlite3StrAccumSetError(StrAccum *p, u8 eError){
30445 assert( eError==SQLITE_NOMEM || eError==SQLITE_TOOBIG );
@@ -30558,24 +30527,19 @@
30527 etByte cThousand; /* Thousands separator for %d and %u */
30528 etByte xtype = etINVALID; /* Conversion paradigm */
30529 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
30530 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
30531 sqlite_uint64 longvalue; /* Value for integer types */
30532 double realvalue; /* Value for real types */
 
 
30533 const et_info *infop; /* Pointer to the appropriate info structure */
30534 char *zOut; /* Rendering buffer */
30535 int nOut; /* Size of the rendering buffer */
30536 char *zExtra = 0; /* Malloced memory used by some conversion */
30537 int exp, e2; /* exponent of real numbers */
 
 
 
30538 etByte flag_dp; /* True if decimal point should be shown */
30539 etByte flag_rtz; /* True if trailing zeros should be removed */
30540
30541 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
30542 char buf[etBUFSIZE]; /* Conversion buffer */
30543
30544 /* pAccum never starts out with an empty buffer that was obtained from
30545 ** malloc(). This precondition is required by the mprintf("%z...")
@@ -30846,98 +30810,65 @@
30810 }
30811 length = (int)(&zOut[nOut-1]-bufpt);
30812 break;
30813 case etFLOAT:
30814 case etEXP:
30815 case etGENERIC: {
30816 FpDecode s;
30817 int iRound;
30818 int j;
30819
30820 if( bArgList ){
30821 realvalue = getDoubleArg(pArgList);
30822 }else{
30823 realvalue = va_arg(ap,double);
30824 }
 
 
 
30825 if( precision<0 ) precision = 6; /* Set default precision */
30826 #ifdef SQLITE_FP_PRECISION_LIMIT
30827 if( precision>SQLITE_FP_PRECISION_LIMIT ){
30828 precision = SQLITE_FP_PRECISION_LIMIT;
30829 }
30830 #endif
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=='-' ){
30863 prefix = '-';
30864 }else{
30865 prefix = flag_prefix;
30866 }
30867
30868 exp = s.iDP-1;
30869 if( xtype==etGENERIC && precision>0 ) precision--;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30870
30871 /*
30872 ** If the field type is etGENERIC, then convert to either etEXP
30873 ** or etFLOAT, as appropriate.
30874 */
@@ -30953,13 +30884,12 @@
30884 flag_rtz = flag_altform2;
30885 }
30886 if( xtype==etEXP ){
30887 e2 = 0;
30888 }else{
30889 e2 = s.iDP - 1;
30890 }
 
30891 bufpt = buf;
30892 {
30893 i64 szBufNeeded; /* Size of a temporary buffer needed */
30894 szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
30895 if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
@@ -30973,42 +30903,31 @@
30903 /* The sign in front of the number */
30904 if( prefix ){
30905 *(bufpt++) = prefix;
30906 }
30907 /* Digits prior to the decimal point */
30908 j = 0;
30909 if( e2<0 ){
30910 *(bufpt++) = '0';
 
 
 
 
 
30911 }else{
30912 for(; e2>=0; e2--){
30913 *(bufpt++) = j<s.n ? s.z[j++] : '0';
30914 if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
30915 }
30916 }
30917 /* The decimal point */
30918 if( flag_dp ){
30919 *(bufpt++) = '.';
30920 }
30921 /* "0" digits after the decimal point but before the first
30922 ** significant digit of the number */
30923 for(e2++; e2<0 && precision>0; precision--, e2++){
 
30924 *(bufpt++) = '0';
30925 }
30926 /* Significant digits after the decimal point */
30927 while( (precision--)>0 ){
30928 *(bufpt++) = j<s.n ? s.z[j++] : '0';
 
 
 
 
 
 
30929 }
30930 /* Remove trailing zeros and the "." if no digits follow the "." */
30931 if( flag_rtz && flag_dp ){
30932 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
30933 assert( bufpt>zOut );
@@ -31020,10 +30939,11 @@
30939 }
30940 }
30941 }
30942 /* Add the "eNNN" suffix */
30943 if( xtype==etEXP ){
30944 exp = s.iDP - 1;
30945 *(bufpt++) = aDigits[infop->charset];
30946 if( exp<0 ){
30947 *(bufpt++) = '-'; exp = -exp;
30948 }else{
30949 *(bufpt++) = '+';
@@ -31053,12 +30973,12 @@
30973 }
30974 i = prefix!=0;
30975 while( nPad-- ) bufpt[i++] = '0';
30976 length = width;
30977 }
 
30978 break;
30979 }
30980 case etSIZE:
30981 if( !bArgList ){
30982 *(va_arg(ap,int*)) = pAccum->nChar;
30983 }
30984 length = width = 0;
@@ -34447,47 +34367,44 @@
34367 z++;
34368 }
34369 return h;
34370 }
34371
34372 /* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
 
 
34373 **
34374 ** Reference:
34375 ** T. J. Dekker, "A Floating-Point Technique for Extending the
34376 ** Available Precision". 1971-07-26.
34377 */
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;
 
 
 
34406 }
34407
34408 /*
34409 ** The string z[] is an text representation of a real number.
34410 ** Convert this string to a double and write it into *pResult.
@@ -34524,16 +34441,15 @@
34441 #ifndef SQLITE_OMIT_FLOATING_POINT
34442 int incr;
34443 const char *zEnd;
34444 /* sign * significand * (10 ^ (esign * exponent)) */
34445 int sign = 1; /* sign of significand */
34446 u64 s = 0; /* significand */
34447 int d = 0; /* adjust exponent for shifting decimal point */
34448 int esign = 1; /* sign of exponent */
34449 int e = 0; /* exponent */
34450 int eValid = 1; /* True exponent is either not used or is well-formed */
 
34451 int nDigit = 0; /* Number of digits processed */
34452 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
34453
34454 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
34455 *pResult = 0.0; /* Default return value, in case of an error */
@@ -34569,11 +34485,11 @@
34485
34486 /* copy max significant digits to significand */
34487 while( z<zEnd && sqlite3Isdigit(*z) ){
34488 s = s*10 + (*z - '0');
34489 z+=incr; nDigit++;
34490 if( s>=((LARGEST_UINT64-9)/10) ){
34491 /* skip non-significant significand digits
34492 ** (increase exponent by d to shift decimal left) */
34493 while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
34494 }
34495 }
@@ -34584,11 +34500,11 @@
34500 z+=incr;
34501 eType++;
34502 /* copy digits from after decimal to significand
34503 ** (decrease exponent by d to shift decimal right) */
34504 while( z<zEnd && sqlite3Isdigit(*z) ){
34505 if( s<((LARGEST_UINT64-9)/10) ){
34506 s = s*10 + (*z - '0');
34507 d--;
34508 nDigit++;
34509 }
34510 z+=incr;
@@ -34624,82 +34540,83 @@
34540
34541 /* skip trailing spaces */
34542 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
34543
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
34551 /* adjust exponent by d, and update sign */
34552 e = (e*esign) + d;
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:
 
 
 
 
 
34618 /* return true if number and no extra non-whitespace characters after */
34619 if( z==zEnd && nDigit>0 && eValid && eType>0 ){
34620 return eType;
34621 }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
34622 return -1;
@@ -34988,10 +34905,157 @@
34905 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
34906 int x = 0;
34907 sqlite3GetInt32(z, &x);
34908 return x;
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 }
35057
35058 /*
35059 ** Try to convert z into an unsigned 32-bit integer. Return true on
35060 ** success and false if there is an error.
35061 **
@@ -74248,10 +74312,11 @@
74312 pCur->aiIdx[pCur->iPage] = pCur->ix;
74313 pCur->apPage[pCur->iPage] = pCur->pPage;
74314 pCur->ix = 0;
74315 pCur->iPage++;
74316 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
74317 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
74318 if( rc==SQLITE_OK
74319 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
74320 ){
74321 releasePage(pCur->pPage);
74322 rc = SQLITE_CORRUPT_PGNO(newPgno);
@@ -74476,11 +74541,11 @@
74541 if( rc==SQLITE_OK ){
74542 assert( pCur->pPage->nCell>0 );
74543 *pRes = 0;
74544 rc = moveToLeftmost(pCur);
74545 }else if( rc==SQLITE_EMPTY ){
74546 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
74547 *pRes = 1;
74548 rc = SQLITE_OK;
74549 }
74550 return rc;
74551 }
@@ -81636,40 +81701,10 @@
81701 SQLITE_PRIVATE void sqlite3VdbeMemReleaseMalloc(Mem *p){
81702 assert( !VdbeMemDynamic(p) );
81703 if( p->szMalloc ) vdbeMemClear(p);
81704 }
81705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81706 /*
81707 ** Return some kind of integer value which is the best we can do
81708 ** at representing the value that *pMem describes as an integer.
81709 ** If pMem is an integer, then the value is exact. If pMem is
81710 ** a floating-point then the value returned is the integer part.
@@ -81692,11 +81727,11 @@
81727 flags = pMem->flags;
81728 if( flags & (MEM_Int|MEM_IntReal) ){
81729 testcase( flags & MEM_IntReal );
81730 return pMem->u.i;
81731 }else if( flags & MEM_Real ){
81732 return sqlite3RealToI64(pMem->u.r);
81733 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
81734 return memIntValue(pMem);
81735 }else{
81736 return 0;
81737 }
@@ -81754,11 +81789,11 @@
81789 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
81790
81791 if( pMem->flags & MEM_IntReal ){
81792 MemSetTypeFlag(pMem, MEM_Int);
81793 }else{
81794 i64 ix = sqlite3RealToI64(pMem->u.r);
81795
81796 /* Only mark the value as an integer if
81797 **
81798 ** (1) the round-trip conversion real->int->real is a no-op, and
81799 ** (2) The integer is neither the largest nor the smallest
@@ -81822,12 +81857,12 @@
81857 /* Convert a floating point value to its closest integer. Do so in
81858 ** a way that avoids 'outside the range of representable values' warnings
81859 ** from UBSAN.
81860 */
81861 SQLITE_PRIVATE i64 sqlite3RealToI64(double r){
81862 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;
81863 if( r>+9223372036854774784.0 ) return LARGEST_INT64;
81864 return (i64)r;
81865 }
81866
81867 /*
81868 ** Convert pMem so that it has type MEM_Real or MEM_Int.
@@ -83601,11 +83636,11 @@
83636 zMsg, P4_DYNAMIC);
83637 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
83638 if( bPush){
83639 pParse->addrExplain = iThis;
83640 }
83641 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
83642 }
83643 return addr;
83644 }
83645
83646 /*
@@ -84313,12 +84348,12 @@
84348 pScan = &p->aScan[ii];
84349 if( pScan->addrExplain==addrExplain ) break;
84350 pScan = 0;
84351 }
84352 if( pScan ){
84353 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
84354 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
84355 }
84356 }
84357 }
84358 #endif /* defined(SQLITE_ENABLE_STMT_SCANSTATUS) */
84359
@@ -96891,11 +96926,11 @@
96926 ** a register that is the source for a pseudo-table cursor created using
96927 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
96928 ** parameter P3. Clearing the P3 column cache as part of this opcode saves
96929 ** us from having to issue a separate NullRow instruction to clear that cache.
96930 */
96931 case OP_SorterData: { /* ncycle */
96932 VdbeCursor *pC;
96933
96934 pOut = &aMem[pOp->p2];
96935 pC = p->apCsr[pOp->p1];
96936 assert( isSorter(pC) );
@@ -97166,12 +97201,12 @@
97201 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
97202 ** rewinding so that the global variable will be incremented and
97203 ** regression tests can determine whether or not the optimizer is
97204 ** correctly optimizing out sorts.
97205 */
97206 case OP_SorterSort: /* jump ncycle */
97207 case OP_Sort: { /* jump ncycle */
97208 #ifdef SQLITE_TEST
97209 sqlite3_sort_count++;
97210 sqlite3_search_count--;
97211 #endif
97212 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
@@ -126267,11 +126302,11 @@
126302 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
126303 /* The value has no fractional part so there is nothing to round */
126304 }else if( n==0 ){
126305 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
126306 }else{
126307 zBuf = sqlite3_mprintf("%!.*f",n,r);
126308 if( zBuf==0 ){
126309 sqlite3_result_error_nomem(context);
126310 return;
126311 }
126312 sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
@@ -127476,16 +127511,71 @@
127511 ** An instance of the following structure holds the context of a
127512 ** sum() or avg() aggregate computation.
127513 */
127514 typedef struct SumCtx SumCtx;
127515 struct SumCtx {
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 */
127519 i64 cnt; /* Number of elements summed */
127520 u8 approx; /* True if any non-integer value was input to the sum */
127521 u8 ovrfl; /* Integer overflow seen */
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 }
127577
127578 /*
127579 ** Routines used to compute the sum, average, and total.
127580 **
127581 ** The SUM() function follows the (broken) SQL standard which means
@@ -127502,19 +127592,34 @@
127592 UNUSED_PARAMETER(argc);
127593 p = sqlite3_aggregate_context(context, sizeof(*p));
127594 type = sqlite3_value_numeric_type(argv[0]);
127595 if( p && type!=SQLITE_NULL ){
127596 p->cnt++;
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 }
127612 }
127613 }else{
 
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 }
127621 }
127622 }
127623 }
127624 #ifndef SQLITE_OMIT_WINDOWFUNC
127625 static void sumInverse(sqlite3_context *context, int argc, sqlite3_value**argv){
@@ -127527,17 +127632,22 @@
127632 /* p is always non-NULL because sumStep() will have been called first
127633 ** to initialize it */
127634 if( ALWAYS(p) && type!=SQLITE_NULL ){
127635 assert( p->cnt>0 );
127636 p->cnt--;
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 }
127647 }else{
127648 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
127649 }
127650 }
127651 }
127652 #else
127653 # define sumInverse 0
@@ -127544,31 +127654,46 @@
127654 #endif /* SQLITE_OMIT_WINDOWFUNC */
127655 static void sumFinalize(sqlite3_context *context){
127656 SumCtx *p;
127657 p = sqlite3_aggregate_context(context, 0);
127658 if( p && p->cnt>0 ){
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 }
127665 }else{
127666 sqlite3_result_int64(context, p->iSum);
127667 }
127668 }
127669 }
127670 static void avgFinalize(sqlite3_context *context){
127671 SumCtx *p;
127672 p = sqlite3_aggregate_context(context, 0);
127673 if( p && p->cnt>0 ){
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);
127681 }
127682 }
127683 static void totalFinalize(sqlite3_context *context){
127684 SumCtx *p;
127685 double r = 0.0;
127686 p = sqlite3_aggregate_context(context, 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);
127695 }
127696
127697 /*
127698 ** The following structure keeps track of state information for the
127699 ** count() aggregate function.
@@ -128154,10 +128279,41 @@
128279 if( type0!=SQLITE_INTEGER && type0!=SQLITE_FLOAT ) return;
128280 x = sqlite3_value_double(argv[0]);
128281 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
128282 }
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
128315 /*
128316 ** All of the FuncDef structures in the aBuiltinFunc[] array above
128317 ** to the global function hash table. This occurs at start-time (as
128318 ** a consequence of calling sqlite3_initialize()).
128319 **
@@ -128225,10 +128381,13 @@
128381 FUNCTION(printf, -1, 0, 0, printfFunc ),
128382 FUNCTION(format, -1, 0, 0, printfFunc ),
128383 FUNCTION(unicode, 1, 0, 0, unicodeFunc ),
128384 FUNCTION(char, -1, 0, 0, charFunc ),
128385 FUNCTION(abs, 1, 0, 0, absFunc ),
128386 #ifdef SQLITE_DEBUG
128387 FUNCTION(fpdecode, 3, 0, 0, fpdecodeFunc ),
128388 #endif
128389 #ifndef SQLITE_OMIT_FLOATING_POINT
128390 FUNCTION(round, 1, 0, 0, roundFunc ),
128391 FUNCTION(round, 2, 0, 0, roundFunc ),
128392 #endif
128393 FUNCTION(upper, 1, 0, 0, upperFunc ),
@@ -147420,13 +147579,17 @@
147579 int regBase;
147580 int regRecord;
147581 int nCol;
147582 int nGroupBy;
147583
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",
147588 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
147589 "DISTINCT" : "GROUP BY"
147590 ));
147591
147592 groupBySort = 1;
147593 nGroupBy = pGroupBy->nExpr;
147594 nCol = nGroupBy;
147595 j = nGroupBy;
@@ -147447,22 +147610,27 @@
147610 j++;
147611 }
147612 }
147613 pAggInfo->directMode = 0;
147614 regRecord = sqlite3GetTempReg(pParse);
147615 sqlite3VdbeScanStatusCounters(v, addrExp, 0, sqlite3VdbeCurrentAddr(v));
147616 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
147617 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
147618 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
147619 sqlite3ReleaseTempReg(pParse, regRecord);
147620 sqlite3ReleaseTempRange(pParse, regBase, nCol);
147621 TREETRACE(0x2,pParse,p,("WhereEnd\n"));
147622 sqlite3WhereEnd(pWInfo);
147623 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
147624 sortOut = sqlite3GetTempReg(pParse);
147625 sqlite3VdbeScanStatusCounters(v, addrExp, sqlite3VdbeCurrentAddr(v), 0);
147626 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
147627 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
147628 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
147629 pAggInfo->useSortingIdx = 1;
147630 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
147631 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
147632 }
147633
147634 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
147635 ** that are indexed (and that were previously identified and tagged
147636 ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions
@@ -153901,10 +154069,16 @@
154069 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
154070 }
154071 if( wsFlags & WHERE_INDEXED ){
154072 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
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);
154080 }
154081 }
154082 }
154083 #endif
154084
@@ -179822,10 +179996,25 @@
179996 *pI1 = rLogEst;
179997 *pU64 = sqlite3LogEstToInt(rLogEst);
179998 *pI2 = sqlite3LogEst(*pU64);
179999 break;
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 }
180016
180017
180018 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_WSD)
180019 /* sqlite3_test_control(SQLITE_TESTCTRL_TUNE, id, *piValue)
180020 **
@@ -243110,11 +243299,11 @@
243299 int nArg, /* Number of args */
243300 sqlite3_value **apUnused /* Function arguments */
243301 ){
243302 assert( nArg==0 );
243303 UNUSED_PARAM2(nArg, apUnused);
243304 sqlite3_result_text(pCtx, "fts5: 2023-07-08 14:27:55 beab3c98639be531744e60440223bb9ee76bc15234aff05e5efb273c8241dfd8", -1, SQLITE_TRANSIENT);
243305 }
243306
243307 /*
243308 ** Return true if zName is the extension on one of the shadow tables used
243309 ** by this module.
243310
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.43.0"
150150
#define SQLITE_VERSION_NUMBER 3043000
151
-#define SQLITE_SOURCE_ID "2023-06-23 11:10:13 fa5f77862c0fe0189aa4246a1e55bb7c537c28c436ec10b75f5fa141e5e4aff0"
151
+#define SQLITE_SOURCE_ID "2023-07-08 14:27:55 beab3c98639be531744e60440223bb9ee76bc15234aff05e5efb273c8241dfd8"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -8174,11 +8174,12 @@
81748174
#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
81758175
#define SQLITE_TESTCTRL_SEEK_COUNT 30
81768176
#define SQLITE_TESTCTRL_TRACEFLAGS 31
81778177
#define SQLITE_TESTCTRL_TUNE 32
81788178
#define SQLITE_TESTCTRL_LOGEST 33
8179
-#define SQLITE_TESTCTRL_LAST 33 /* Largest TESTCTRL */
8179
+#define SQLITE_TESTCTRL_USELONGDOUBLE 34
8180
+#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */
81808181
81818182
/*
81828183
** CAPI3REF: SQL Keyword Checking
81838184
**
81848185
** These routines provide access to the set of SQL language keywords
81858186
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.43.0"
150 #define SQLITE_VERSION_NUMBER 3043000
151 #define SQLITE_SOURCE_ID "2023-06-23 11:10:13 fa5f77862c0fe0189aa4246a1e55bb7c537c28c436ec10b75f5fa141e5e4aff0"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -8174,11 +8174,12 @@
8174 #define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8175 #define SQLITE_TESTCTRL_SEEK_COUNT 30
8176 #define SQLITE_TESTCTRL_TRACEFLAGS 31
8177 #define SQLITE_TESTCTRL_TUNE 32
8178 #define SQLITE_TESTCTRL_LOGEST 33
8179 #define SQLITE_TESTCTRL_LAST 33 /* Largest TESTCTRL */
 
8180
8181 /*
8182 ** CAPI3REF: SQL Keyword Checking
8183 **
8184 ** These routines provide access to the set of SQL language keywords
8185
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.43.0"
150 #define SQLITE_VERSION_NUMBER 3043000
151 #define SQLITE_SOURCE_ID "2023-07-08 14:27:55 beab3c98639be531744e60440223bb9ee76bc15234aff05e5efb273c8241dfd8"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -8174,11 +8174,12 @@
8174 #define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS 29
8175 #define SQLITE_TESTCTRL_SEEK_COUNT 30
8176 #define SQLITE_TESTCTRL_TRACEFLAGS 31
8177 #define SQLITE_TESTCTRL_TUNE 32
8178 #define SQLITE_TESTCTRL_LOGEST 33
8179 #define SQLITE_TESTCTRL_USELONGDOUBLE 34
8180 #define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */
8181
8182 /*
8183 ** CAPI3REF: SQL Keyword Checking
8184 **
8185 ** These routines provide access to the set of SQL language keywords
8186

Keyboard Shortcuts

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