Fossil SCM

Update the built-in SQLite to the latest trunk version, for beta testing.

drh 2026-02-05 12:04 trunk
Commit 7b0960d8db6a23904c54527ef21ad5d0b898c48508ead3724e8f7ecd7e04a163
3 files changed +338 -179 +314 -99 +36 -14
+338 -179
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -876,10 +876,11 @@
876876
#ifndef SQLITE_QRF_H
877877
#include "qrf.h"
878878
#endif
879879
#include <string.h>
880880
#include <assert.h>
881
+#include <stdint.h>
881882
882883
/* typedef sqlite3_int64 i64; */
883884
884885
/* A single line in the EQP output */
885886
typedef struct qrfEQPGraphRow qrfEQPGraphRow;
@@ -893,11 +894,12 @@
893894
/* All EQP output is collected into an instance of the following */
894895
typedef struct qrfEQPGraph qrfEQPGraph;
895896
struct qrfEQPGraph {
896897
qrfEQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
897898
qrfEQPGraphRow *pLast; /* Last element of the pRow list */
898
- char zPrefix[100]; /* Graph prefix */
899
+ int nWidth; /* Width of the graph */
900
+ char zPrefix[400]; /* Graph prefix */
899901
};
900902
901903
/*
902904
** Private state information. Subject to change from one release to the
903905
** next.
@@ -1087,10 +1089,49 @@
10871089
qrfEqpRenderLevel(p, pRow->iEqpId);
10881090
p->u.pGraph->zPrefix[n] = 0;
10891091
}
10901092
}
10911093
}
1094
+
1095
+/*
1096
+** Render the 64-bit value N in a more human-readable format into
1097
+** pOut.
1098
+**
1099
+** + Only show the first three significant digits.
1100
+** + Append suffixes K, M, G, T, P, and E for 1e3, 1e6, ... 1e18
1101
+*/
1102
+static void qrfApproxInt64(sqlite3_str *pOut, i64 N){
1103
+ static const char aSuffix[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
1104
+ int i;
1105
+ if( N<0 ){
1106
+ N = N==INT64_MIN ? INT64_MAX : -N;
1107
+ sqlite3_str_append(pOut, "-", 1);
1108
+ }
1109
+ if( N<10000 ){
1110
+ sqlite3_str_appendf(pOut, "%4lld ", N);
1111
+ return;
1112
+ }
1113
+ for(i=1; i<=18; i++){
1114
+ N = (N+5)/10;
1115
+ if( N<10000 ){
1116
+ int n = (int)N;
1117
+ switch( i%3 ){
1118
+ case 0:
1119
+ sqlite3_str_appendf(pOut, "%d.%02d", n/1000, (n%1000)/10);
1120
+ break;
1121
+ case 1:
1122
+ sqlite3_str_appendf(pOut, "%2d.%d", n/100, (n%100)/10);
1123
+ break;
1124
+ case 2:
1125
+ sqlite3_str_appendf(pOut, "%4d", n/10);
1126
+ break;
1127
+ }
1128
+ sqlite3_str_append(pOut, &aSuffix[i/3], 1);
1129
+ break;
1130
+ }
1131
+ }
1132
+}
10921133
10931134
/*
10941135
** Display and reset the EXPLAIN QUERY PLAN data
10951136
*/
10961137
static void qrfEqpRender(Qrf *p, i64 nCycle){
@@ -1103,11 +1144,30 @@
11031144
}
11041145
sqlite3_str_appendf(p->pOut, "%s\n", pRow->zText+3);
11051146
p->u.pGraph->pRow = pRow->pNext;
11061147
sqlite3_free(pRow);
11071148
}else if( nCycle>0 ){
1108
- sqlite3_str_appendf(p->pOut, "QUERY PLAN (cycles=%lld [100%%])\n",nCycle);
1149
+ int nSp = p->u.pGraph->nWidth - 2;
1150
+ if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1151
+ sqlite3_str_appendchar(p->pOut, nSp, ' ');
1152
+ sqlite3_str_appendall(p->pOut,
1153
+ "Cycles Loops (est) Rows (est)\n");
1154
+ sqlite3_str_appendchar(p->pOut, nSp, ' ');
1155
+ sqlite3_str_appendall(p->pOut,
1156
+ "---------- ------------ ------------\n");
1157
+ }else{
1158
+ sqlite3_str_appendchar(p->pOut, nSp, ' ');
1159
+ sqlite3_str_appendall(p->pOut,
1160
+ "Cycles Loops Rows \n");
1161
+ sqlite3_str_appendchar(p->pOut, nSp, ' ');
1162
+ sqlite3_str_appendall(p->pOut,
1163
+ "---------- ----- -----\n");
1164
+ }
1165
+ sqlite3_str_appendall(p->pOut, "QUERY PLAN");
1166
+ sqlite3_str_appendchar(p->pOut, nSp - 10, ' ');
1167
+ qrfApproxInt64(p->pOut, nCycle);
1168
+ sqlite3_str_appendall(p->pOut, " 100%\n");
11091169
}else{
11101170
sqlite3_str_appendall(p->pOut, "QUERY PLAN\n");
11111171
}
11121172
p->u.pGraph->zPrefix[0] = 0;
11131173
qrfEqpRenderLevel(p, 0);
@@ -1158,10 +1218,12 @@
11581218
static const int f = SQLITE_SCANSTAT_COMPLEX;
11591219
sqlite3_stmt *pS = p->pStmt;
11601220
int i = 0;
11611221
i64 nTotal = 0;
11621222
int nWidth = 0;
1223
+ int prevPid = -1; /* Previous iPid */
1224
+ double rEstCum = 1.0; /* Cumulative row estimate */
11631225
sqlite3_str *pLine = sqlite3_str_new(p->db);
11641226
sqlite3_str *pStats = sqlite3_str_new(p->db);
11651227
qrfEqpReset(p);
11661228
11671229
for(i=0; 1; i++){
@@ -1171,11 +1233,11 @@
11711233
break;
11721234
}
11731235
n = (int)strlen(z) + qrfStatsHeight(pS,i)*3;
11741236
if( n>nWidth ) nWidth = n;
11751237
}
1176
- nWidth += 4;
1238
+ nWidth += 2;
11771239
11781240
sqlite3_stmt_scanstatus_v2(pS,-1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
11791241
for(i=0; 1; i++){
11801242
i64 nLoop = 0;
11811243
i64 nRow = 0;
@@ -1186,54 +1248,64 @@
11861248
const char *zName = 0;
11871249
double rEst = 0.0;
11881250
11891251
if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
11901252
break;
1253
+ }
1254
+ sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
1255
+ if( iPid!=prevPid ){
1256
+ prevPid = iPid;
1257
+ rEstCum = 1.0;
11911258
}
11921259
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
1260
+ rEstCum *= rEst;
11931261
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
11941262
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
11951263
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
11961264
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
1197
- sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
11981265
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
11991266
12001267
if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
1201
- const char *zSp = "";
1202
- double rpl;
1268
+ int nSp = 0;
12031269
sqlite3_str_reset(pStats);
12041270
if( nCycle>=0 && nTotal>0 ){
1205
- sqlite3_str_appendf(pStats, "cycles=%lld [%d%%]",
1206
- nCycle, ((nCycle*100)+nTotal/2) / nTotal
1271
+ qrfApproxInt64(pStats, nCycle);
1272
+ sqlite3_str_appendf(pStats, " %3d%%",
1273
+ ((nCycle*100)+nTotal/2) / nTotal
12071274
);
1208
- zSp = " ";
1275
+ nSp = 2;
12091276
}
12101277
if( nLoop>=0 ){
1211
- sqlite3_str_appendf(pStats, "%sloops=%lld", zSp, nLoop);
1212
- zSp = " ";
1278
+ if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
1279
+ qrfApproxInt64(pStats, nLoop);
1280
+ nSp = 2;
1281
+ if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1282
+ sqlite3_str_appendf(pStats, " ");
1283
+ qrfApproxInt64(pStats, (i64)(rEstCum/rEst));
1284
+ }
12131285
}
12141286
if( nRow>=0 ){
1215
- sqlite3_str_appendf(pStats, "%srows=%lld", zSp, nRow);
1216
- zSp = " ";
1217
- }
1218
-
1219
- if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1220
- rpl = (double)nRow / (double)nLoop;
1221
- sqlite3_str_appendf(pStats, "%srpl=%.1f est=%.1f", zSp, rpl, rEst);
1222
- }
1223
-
1287
+ if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
1288
+ qrfApproxInt64(pStats, nRow);
1289
+ nSp = 2;
1290
+ if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1291
+ sqlite3_str_appendf(pStats, " ");
1292
+ qrfApproxInt64(pStats, (i64)rEstCum);
1293
+ }
1294
+ }
12241295
sqlite3_str_appendf(pLine,
1225
- "% *s (%s)", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo,
1296
+ "% *s %s", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo,
12261297
sqlite3_str_value(pStats)
12271298
);
12281299
sqlite3_str_reset(pStats);
12291300
qrfEqpAppend(p, iId, iPid, sqlite3_str_value(pLine));
12301301
sqlite3_str_reset(pLine);
12311302
}else{
12321303
qrfEqpAppend(p, iId, iPid, zo);
12331304
}
12341305
}
1306
+ if( p->u.pGraph ) p->u.pGraph->nWidth = nWidth;
12351307
qrfStrErr(p, pLine);
12361308
sqlite3_free(sqlite3_str_finish(pLine));
12371309
qrfStrErr(p, pStats);
12381310
sqlite3_free(sqlite3_str_finish(pStats));
12391311
#endif
@@ -3664,11 +3736,20 @@
36643736
sqlite3_free(p->u.sLine.azCol);
36653737
}
36663738
break;
36673739
}
36683740
case QRF_STYLE_Stats:
3669
- case QRF_STYLE_StatsEst:
3741
+ case QRF_STYLE_StatsEst: {
3742
+ i64 nCycle = 0;
3743
+#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3744
+ sqlite3_stmt_scanstatus_v2(p->pStmt, -1, SQLITE_SCANSTAT_NCYCLE,
3745
+ SQLITE_SCANSTAT_COMPLEX, (void*)&nCycle);
3746
+#endif
3747
+ qrfEqpRender(p, nCycle);
3748
+ qrfWrite(p);
3749
+ break;
3750
+ }
36703751
case QRF_STYLE_Eqp: {
36713752
qrfEqpRender(p, 0);
36723753
qrfWrite(p);
36733754
break;
36743755
}
@@ -3836,13 +3917,10 @@
38363917
38373918
38383919
#define eputz(z) cli_puts(z,stderr)
38393920
#define sputz(fp,z) cli_puts(z,fp)
38403921
3841
-/* True if the timer is enabled */
3842
-static int enableTimer = 0;
3843
-
38443922
/* A version of strcmp() that works with NULL values */
38453923
static int cli_strcmp(const char *a, const char *b){
38463924
if( a==0 ) a = "";
38473925
if( b==0 ) b = "";
38483926
return strcmp(a,b);
@@ -3883,154 +3961,10 @@
38833961
(void)gettimeofday(&sNow,0);
38843962
return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
38853963
#endif
38863964
}
38873965
3888
-#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
3889
-#include <sys/time.h>
3890
-#include <sys/resource.h>
3891
-
3892
-/* VxWorks does not support getrusage() as far as we can determine */
3893
-#if defined(_WRS_KERNEL) || defined(__RTP__)
3894
-struct rusage {
3895
- struct timeval ru_utime; /* user CPU time used */
3896
- struct timeval ru_stime; /* system CPU time used */
3897
-};
3898
-#define getrusage(A,B) memset(B,0,sizeof(*B))
3899
-#endif
3900
-
3901
-
3902
-/* Saved resource information for the beginning of an operation */
3903
-static struct rusage sBegin; /* CPU time at start */
3904
-static sqlite3_int64 iBegin; /* Wall-clock time at start */
3905
-
3906
-/*
3907
-** Begin timing an operation
3908
-*/
3909
-static void beginTimer(void){
3910
- if( enableTimer ){
3911
- getrusage(RUSAGE_SELF, &sBegin);
3912
- iBegin = timeOfDay();
3913
- }
3914
-}
3915
-
3916
-/* Return the difference of two time_structs in seconds */
3917
-static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
3918
- return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
3919
- (double)(pEnd->tv_sec - pStart->tv_sec);
3920
-}
3921
-
3922
-/*
3923
-** Print the timing results.
3924
-*/
3925
-static void endTimer(FILE *out){
3926
- if( enableTimer ){
3927
- sqlite3_int64 iEnd = timeOfDay();
3928
- struct rusage sEnd;
3929
- getrusage(RUSAGE_SELF, &sEnd);
3930
- cli_printf(out, "Run Time: real %.6f user %.6f sys %.6f\n",
3931
- (iEnd - iBegin)*0.000001,
3932
- timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
3933
- timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
3934
- }
3935
-}
3936
-
3937
-#define BEGIN_TIMER beginTimer()
3938
-#define END_TIMER(X) endTimer(X)
3939
-#define HAS_TIMER 1
3940
-
3941
-#elif (defined(_WIN32) || defined(WIN32))
3942
-
3943
-/* Saved resource information for the beginning of an operation */
3944
-static HANDLE hProcess;
3945
-static FILETIME ftKernelBegin;
3946
-static FILETIME ftUserBegin;
3947
-static sqlite3_int64 ftWallBegin;
3948
-typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
3949
- LPFILETIME, LPFILETIME);
3950
-static GETPROCTIMES getProcessTimesAddr = NULL;
3951
-
3952
-/*
3953
-** Check to see if we have timer support. Return 1 if necessary
3954
-** support found (or found previously).
3955
-*/
3956
-static int hasTimer(void){
3957
- if( getProcessTimesAddr ){
3958
- return 1;
3959
- } else {
3960
- /* GetProcessTimes() isn't supported in WIN95 and some other Windows
3961
- ** versions. See if the version we are running on has it, and if it
3962
- ** does, save off a pointer to it and the current process handle.
3963
- */
3964
- hProcess = GetCurrentProcess();
3965
- if( hProcess ){
3966
- HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
3967
- if( NULL != hinstLib ){
3968
- getProcessTimesAddr =
3969
- (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
3970
- if( NULL != getProcessTimesAddr ){
3971
- return 1;
3972
- }
3973
- FreeLibrary(hinstLib);
3974
- }
3975
- }
3976
- }
3977
- return 0;
3978
-}
3979
-
3980
-/*
3981
-** Begin timing an operation
3982
-*/
3983
-static void beginTimer(void){
3984
- if( enableTimer && getProcessTimesAddr ){
3985
- FILETIME ftCreation, ftExit;
3986
- getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
3987
- &ftKernelBegin,&ftUserBegin);
3988
- ftWallBegin = timeOfDay();
3989
- }
3990
-}
3991
-
3992
-/* Return the difference of two FILETIME structs in seconds */
3993
-static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
3994
- sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
3995
- sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
3996
- return (double) ((i64End - i64Start) / 10000000.0);
3997
-}
3998
-
3999
-/*
4000
-** Print the timing results.
4001
-*/
4002
-static void endTimer(FILE *out){
4003
- if( enableTimer && getProcessTimesAddr){
4004
- FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
4005
- sqlite3_int64 ftWallEnd = timeOfDay();
4006
- getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
4007
-#ifdef _WIN64
4008
- /* microsecond precision on 64-bit windows */
4009
- cli_printf(out, "Run Time: real %.6f user %f sys %f\n",
4010
- (ftWallEnd - ftWallBegin)*0.000001,
4011
- timeDiff(&ftUserBegin, &ftUserEnd),
4012
- timeDiff(&ftKernelBegin, &ftKernelEnd));
4013
-#else
4014
- /* millisecond precisino on 32-bit windows */
4015
- cli_printf(out, "Run Time: real %.3f user %.3f sys %.3f\n",
4016
- (ftWallEnd - ftWallBegin)*0.000001,
4017
- timeDiff(&ftUserBegin, &ftUserEnd),
4018
- timeDiff(&ftKernelBegin, &ftKernelEnd));
4019
-#endif
4020
- }
4021
-}
4022
-
4023
-#define BEGIN_TIMER beginTimer()
4024
-#define END_TIMER(X) endTimer(X)
4025
-#define HAS_TIMER hasTimer()
4026
-
4027
-#else
4028
-#define BEGIN_TIMER
4029
-#define END_TIMER(X) /*no-op*/
4030
-#define HAS_TIMER 0
4031
-#endif
40323966
40333967
/*
40343968
** Used to prevent warnings about unused parameters
40353969
*/
40363970
#define UNUSED_PARAMETER(x) (void)(x)
@@ -24226,11 +24160,14 @@
2422624160
u8 eRestoreState; /* See comments above doAutoDetectRestore() */
2422724161
unsigned statsOn; /* True to display memory stats before each finalize */
2422824162
unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
2422924163
u8 nPopOutput; /* Revert .output settings when reaching zero */
2423024164
u8 nPopMode; /* Revert .mode settings when reaching zero */
24165
+ u8 enableTimer; /* Enable the timer. 2: permanently 1: only once */
2423124166
int inputNesting; /* Track nesting level of .read and other redirects */
24167
+ double prevTimer; /* Last reported timer value */
24168
+ double tmProgress; /* --timeout option for .progress */
2423224169
i64 lineno; /* Line number of last line read from in */
2423324170
const char *zInFile; /* Name of the input file */
2423424171
int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
2423524172
FILE *in; /* Read commands from this stream */
2423624173
FILE *out; /* Write results here */
@@ -24322,10 +24259,11 @@
2432224259
#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
2432324260
#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
2432424261
** callback limit is reached, and for each
2432524262
** top-level SQL statement */
2432624263
#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
24264
+#define SHELL_PROGRESS_TMOUT 0x08 /* Stop after tmProgress seconds */
2432724265
2432824266
/* Names of values for Mode.spec.eEsc and Mode.spec.eText
2432924267
*/
2433024268
static const char *qrfEscNames[] = { "auto", "off", "ascii", "symbol" };
2433124269
static const char *qrfQuoteNames[] =
@@ -24483,10 +24421,181 @@
2448324421
** Limit input nesting via .read or any other input redirect.
2448424422
** It's not too expensive, so a generous allowance can be made.
2448524423
*/
2448624424
#define MAX_INPUT_NESTING 25
2448724425
24426
+/************************* BEGIN PERFORMANCE TIMER *****************************/
24427
+#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
24428
+#include <sys/time.h>
24429
+#include <sys/resource.h>
24430
+/* VxWorks does not support getrusage() as far as we can determine */
24431
+#if defined(_WRS_KERNEL) || defined(__RTP__)
24432
+struct rusage {
24433
+ struct timeval ru_utime; /* user CPU time used */
24434
+ struct timeval ru_stime; /* system CPU time used */
24435
+};
24436
+#define getrusage(A,B) memset(B,0,sizeof(*B))
24437
+#endif
24438
+
24439
+/* Saved resource information for the beginning of an operation */
24440
+static struct rusage sBegin; /* CPU time at start */
24441
+static sqlite3_int64 iBegin; /* Wall-clock time at start */
24442
+
24443
+/*
24444
+** Begin timing an operation
24445
+*/
24446
+static void beginTimer(ShellState *p){
24447
+ if( p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0 ){
24448
+ getrusage(RUSAGE_SELF, &sBegin);
24449
+ iBegin = timeOfDay();
24450
+ }
24451
+}
24452
+
24453
+/* Return the difference of two time_structs in seconds */
24454
+static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
24455
+ return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
24456
+ (double)(pEnd->tv_sec - pStart->tv_sec);
24457
+}
24458
+
24459
+/* Return the time since the start of the timer in
24460
+** seconds. */
24461
+static double elapseTime(ShellState *NotUsed){
24462
+ (void)NotUsed;
24463
+ if( iBegin==0 ) return 0.0;
24464
+ return (timeOfDay() - iBegin)*0.000001;
24465
+}
24466
+
24467
+/*
24468
+** Print the timing results.
24469
+*/
24470
+static void endTimer(ShellState *p){
24471
+ if( p->enableTimer ){
24472
+ sqlite3_int64 iEnd = timeOfDay();
24473
+ struct rusage sEnd;
24474
+ getrusage(RUSAGE_SELF, &sEnd);
24475
+ p->prevTimer = (iEnd - iBegin)*0.000001;
24476
+ cli_printf(p->out, "Run Time: real %.6f user %.6f sys %.6f\n",
24477
+ p->prevTimer,
24478
+ timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
24479
+ timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
24480
+ if( p->enableTimer==1 ) p->enableTimer = 0;
24481
+ iBegin = 0;
24482
+ }
24483
+}
24484
+
24485
+#define BEGIN_TIMER(X) beginTimer(X)
24486
+#define END_TIMER(X) endTimer(X)
24487
+#define ELAPSE_TIME(X) elapseTime(X)
24488
+#define HAS_TIMER 1
24489
+
24490
+#elif (defined(_WIN32) || defined(WIN32))
24491
+
24492
+/* Saved resource information for the beginning of an operation */
24493
+static HANDLE hProcess;
24494
+static FILETIME ftKernelBegin;
24495
+static FILETIME ftUserBegin;
24496
+static sqlite3_int64 ftWallBegin;
24497
+typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
24498
+ LPFILETIME, LPFILETIME);
24499
+static GETPROCTIMES getProcessTimesAddr = NULL;
24500
+
24501
+/*
24502
+** Check to see if we have timer support. Return 1 if necessary
24503
+** support found (or found previously).
24504
+*/
24505
+static int hasTimer(void){
24506
+ if( getProcessTimesAddr ){
24507
+ return 1;
24508
+ } else {
24509
+ /* GetProcessTimes() isn't supported in WIN95 and some other Windows
24510
+ ** versions. See if the version we are running on has it, and if it
24511
+ ** does, save off a pointer to it and the current process handle.
24512
+ */
24513
+ hProcess = GetCurrentProcess();
24514
+ if( hProcess ){
24515
+ HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
24516
+ if( NULL != hinstLib ){
24517
+ getProcessTimesAddr =
24518
+ (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
24519
+ if( NULL != getProcessTimesAddr ){
24520
+ return 1;
24521
+ }
24522
+ FreeLibrary(hinstLib);
24523
+ }
24524
+ }
24525
+ }
24526
+ return 0;
24527
+}
24528
+
24529
+/*
24530
+** Begin timing an operation
24531
+*/
24532
+static void beginTimer(ShellState *p){
24533
+ if( (p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0)
24534
+ && getProcessTimesAddr
24535
+ ){
24536
+ FILETIME ftCreation, ftExit;
24537
+ getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
24538
+ &ftKernelBegin,&ftUserBegin);
24539
+ ftWallBegin = timeOfDay();
24540
+ }
24541
+}
24542
+
24543
+/* Return the difference of two FILETIME structs in seconds */
24544
+static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
24545
+ sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
24546
+ sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
24547
+ return (double) ((i64End - i64Start) / 10000000.0);
24548
+}
24549
+
24550
+/* Return the time since the start of the timer in
24551
+** seconds. */
24552
+static double elapseTime(ShellState *NotUsed){
24553
+ (void)NotUsed;
24554
+ if( ftWallBegin==0 ) return 0.0;
24555
+ return (timeOfDay() - ftWallBegin)*0.000001;
24556
+}
24557
+
24558
+/*
24559
+** Print the timing results.
24560
+*/
24561
+static void endTimer(ShellState *p){
24562
+ if( p->enableTimer && getProcessTimesAddr){
24563
+ FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
24564
+ sqlite3_int64 ftWallEnd = timeOfDay();
24565
+ getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
24566
+ p->prevTimer = (ftWallEnd - ftWallBegin)*0.000001;
24567
+#ifdef _WIN64
24568
+ /* microsecond precision on 64-bit windows */
24569
+ cli_printf(p->out, "Run Time: real %.6f user %f sys %f\n",
24570
+ p->prevTimer,
24571
+ timeDiff(&ftUserBegin, &ftUserEnd),
24572
+ timeDiff(&ftKernelBegin, &ftKernelEnd));
24573
+#else
24574
+ /* millisecond precisino on 32-bit windows */
24575
+ cli_printf(p->out, "Run Time: real %.3f user %.3f sys %.3f\n",
24576
+ p->prevTimer,
24577
+ timeDiff(&ftUserBegin, &ftUserEnd),
24578
+ timeDiff(&ftKernelBegin, &ftKernelEnd));
24579
+#endif
24580
+ if( p->enableTimer==1 ) p->enableTimer = 0;
24581
+ ftWallBegin = 0;
24582
+ }
24583
+}
24584
+
24585
+#define BEGIN_TIMER(X) beginTimer(X)
24586
+#define ELAPSE_TIME(X) elapseTime(X)
24587
+#define END_TIMER(X) endTimer(X)
24588
+#define HAS_TIMER hasTimer()
24589
+
24590
+#else
24591
+#define BEGIN_TIMER(X) /* no-op */
24592
+#define ELAPSE_TIME(X) 0.0
24593
+#define END_TIMER(X) /*no-op*/
24594
+#define HAS_TIMER 0
24595
+#endif
24596
+/************************* END PERFORMANCE TIMER ******************************/
2448824597
2448924598
/*
2449024599
** Clear a display mode, freeing any allocated memory that it
2449124600
** contains.
2449224601
*/
@@ -25408,10 +25517,17 @@
2540825517
** Progress handler callback.
2540925518
*/
2541025519
static int progress_handler(void *pClientData) {
2541125520
ShellState *p = (ShellState*)pClientData;
2541225521
p->nProgress++;
25522
+ if( (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0
25523
+ && ELAPSE_TIME(p)>=p->tmProgress
25524
+ ){
25525
+ cli_printf(p->out, "Progress timeout after %.6f seconds\n",
25526
+ ELAPSE_TIME(p));
25527
+ return 1;
25528
+ }
2541325529
if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2541425530
cli_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2541525531
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2541625532
if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2541725533
return 1;
@@ -25945,10 +26061,12 @@
2594526061
char *zBuf = sqlite3_malloc64( szVar-5 );
2594626062
if( zBuf ){
2594726063
memcpy(zBuf, &zVar[6], szVar-5);
2594826064
sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
2594926065
}
26066
+ }else if( strcmp(zVar, "$TIMER")==0 ){
26067
+ sqlite3_bind_double(pStmt, i, pArg->prevTimer);
2595026068
#ifdef SQLITE_ENABLE_CARRAY
2595126069
}else if( strncmp(zVar, "$carray_", 8)==0 ){
2595226070
static char *azColorNames[] = {
2595326071
"azure", "black", "blue", "brown", "cyan", "fuchsia", "gold",
2595426072
"gray", "green", "indigo", "khaki", "lime", "magenta", "maroon",
@@ -26204,12 +26322,14 @@
2620426322
pArg->pStmt = pStmt;
2620526323
}
2620626324
2620726325
/* Show the EXPLAIN QUERY PLAN if .eqp is on */
2620826326
isExplain = sqlite3_stmt_isexplain(pStmt);
26209
- if( pArg && pArg->mode.autoEQP && isExplain==0 ){
26327
+ if( pArg && pArg->mode.autoEQP && isExplain==0 && pArg->dot.nArg==0 ){
2621026328
int triggerEQP = 0;
26329
+ u8 savedEnableTimer = pArg->enableTimer;
26330
+ pArg->enableTimer = 0;
2621126331
disable_debug_trace_modes();
2621226332
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
2621326333
if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
2621426334
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
2621526335
}
@@ -26227,10 +26347,11 @@
2622726347
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
2622826348
}
2622926349
sqlite3_reset(pStmt);
2623026350
sqlite3_stmt_explain(pStmt, 0);
2623126351
restore_debug_trace_modes();
26352
+ pArg->enableTimer = savedEnableTimer;
2623226353
}
2623326354
2623426355
bind_prepared_stmt(pArg, pStmt);
2623526356
if( isExplain && pArg->mode.autoExplain ){
2623626357
spec.eStyle = isExplain==1 ? QRF_STYLE_Explain : QRF_STYLE_Eqp;
@@ -26764,10 +26885,11 @@
2676426885
".progress N Invoke progress handler after every N opcodes",
2676526886
" --limit N Interrupt after N progress callbacks",
2676626887
" --once Do no more than one progress interrupt",
2676726888
" --quiet|-q No output except at interrupts",
2676826889
" --reset Reset the count for each input and interrupt",
26890
+ " --timeout S Halt after running for S seconds",
2676926891
#endif
2677026892
".prompt MAIN CONTINUE Replace the standard prompts",
2677126893
#ifndef SQLITE_SHELL_FIDDLE
2677226894
".quit Stop interpreting input stream, exit if primary.",
2677326895
".read FILE Read input from FILE or command output",
@@ -26832,11 +26954,11 @@
2683226954
".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
2683326955
".testcase NAME Begin a test case.",
2683426956
",testctrl CMD ... Run various sqlite3_test_control() operations",
2683526957
" Run \".testctrl\" with no arguments for details",
2683626958
".timeout MS Try opening locked tables for MS milliseconds",
26837
- ".timer on|off Turn SQL timer on or off",
26959
+ ".timer on|off|once Turn SQL timer on or off.",
2683826960
#ifndef SQLITE_OMIT_TRACE
2683926961
".trace ?OPTIONS? Output each SQL statement as it is run",
2684026962
" FILE Send output to FILE",
2684126963
" stdout Send output to stdout",
2684226964
" stderr Send output to stderr",
@@ -26897,10 +27019,12 @@
2689727019
" --ascii Do not use RFC-4180 quoting. Use \\037 and \\036\n"
2689827020
" as column and row separators on input, unless other\n"
2689927021
" delimiters are specified using --colsep and/or --rowsep\n"
2690027022
" --colsep CHAR Use CHAR as the column separator.\n"
2690127023
" --csv Input is standard RFC-4180 CSV.\n"
27024
+" --esc CHAR Use CHAR as an escape character in unquoted CSV inputs.\n"
27025
+" --qesc CHAR Use CHAR as an escape character in quoted CSV inputs.\n"
2690227026
" --rowsep CHAR Use CHAR as the row separator.\n"
2690327027
" --schema S When creating TABLE, put it in schema S\n"
2690427028
" --skip N Ignore the first N rows of input\n"
2690527029
" -v Verbose mode\n"
2690627030
},
@@ -28054,10 +28178,12 @@
2805428178
int nErr; /* Number of errors encountered */
2805528179
int bNotFirst; /* True if one or more bytes already read */
2805628180
int cTerm; /* Character that terminated the most recent field */
2805728181
int cColSep; /* The column separator character. (Usually ",") */
2805828182
int cRowSep; /* The row separator character. (Usually "\n") */
28183
+ int cQEscape; /* Escape character with "...". 0 for none */
28184
+ int cUQEscape; /* Escape character not with "...". 0 for none */
2805928185
};
2806028186
2806128187
/* Clean up resourced used by an ImportCtx */
2806228188
static void import_cleanup(ImportCtx *p){
2806328189
if( p->in!=0 && p->xCloser!=0 ){
@@ -28122,14 +28248,21 @@
2812228248
}
2812328249
if( c=='"' ){
2812428250
int pc, ppc;
2812528251
int startLine = p->nLine;
2812628252
int cQuote = c;
28253
+ int cEsc = (u8)p->cQEscape;
2812728254
pc = ppc = 0;
2812828255
while( 1 ){
2812928256
c = import_getc(p);
2813028257
if( c==rSep ) p->nLine++;
28258
+ if( c==cEsc && cEsc!=0 ){
28259
+ c = import_getc(p);
28260
+ import_append_char(p, c);
28261
+ ppc = pc = 0;
28262
+ continue;
28263
+ }
2813128264
if( c==cQuote ){
2813228265
if( pc==cQuote ){
2813328266
pc = 0;
2813428267
continue;
2813528268
}
@@ -28143,11 +28276,11 @@
2814328276
p->cTerm = c;
2814428277
break;
2814528278
}
2814628279
if( pc==cQuote && c!='\r' ){
2814728280
cli_printf(stderr,"%s:%d: unescaped %c character\n",
28148
- p->zFile, p->nLine, cQuote);
28281
+ p->zFile, p->nLine, cQuote);
2814928282
}
2815028283
if( c==EOF ){
2815128284
cli_printf(stderr,"%s:%d: unterminated %c-quoted field\n",
2815228285
p->zFile, startLine, cQuote);
2815328286
p->cTerm = c;
@@ -28158,10 +28291,11 @@
2815828291
pc = c;
2815928292
}
2816028293
}else{
2816128294
/* If this is the first field being parsed and it begins with the
2816228295
** UTF-8 BOM (0xEF BB BF) then skip the BOM */
28296
+ int cEsc = p->cUQEscape;
2816328297
if( (c&0xff)==0xef && p->bNotFirst==0 ){
2816428298
import_append_char(p, c);
2816528299
c = import_getc(p);
2816628300
if( (c&0xff)==0xbb ){
2816728301
import_append_char(p, c);
@@ -28172,10 +28306,11 @@
2817228306
return csv_read_one_field(p);
2817328307
}
2817428308
}
2817528309
}
2817628310
while( c!=EOF && c!=cSep && c!=rSep ){
28311
+ if( c==cEsc && cEsc!=0 ) c = import_getc(p);
2817728312
import_append_char(p, c);
2817828313
c = import_getc(p);
2817928314
}
2818028315
if( c==rSep ){
2818128316
p->nLine++;
@@ -30315,11 +30450,11 @@
3031530450
;
3031630451
static const char * const zCollectVar = "\
3031730452
SELECT\
3031830453
'('||x'0a'\
3031930454
|| group_concat(\
30320
- cname||' TEXT',\
30455
+ cname||' ANY',\
3032130456
','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
3032230457
||')' AS ColsSpec \
3032330458
FROM (\
3032430459
SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
3032530460
FROM ColNames ORDER BY cpos\
@@ -30535,10 +30670,12 @@
3053530670
** --ascii Do not use RFC-4180 quoting. Use \037 and \036
3053630671
** as column and row separators on input, unless other
3053730672
** delimiters are specified using --colsep and/or --rowsep
3053830673
** --colsep CHAR Use CHAR as the column separator.
3053930674
** --csv Input is standard RFC-4180 CSV.
30675
+** --esc CHAR Use CHAR as an escape character in unquoted CSV inputs.
30676
+** --qesc CHAR Use CHAR as an escape character in quoted CSV inputs.
3054030677
** --rowsep CHAR Use CHAR as the row separator.
3054130678
** --schema S When creating TABLE, put it in schema S
3054230679
** --skip N Ignore the first N rows of input
3054330680
** -v Verbose mode
3054430681
*/
@@ -30593,10 +30730,14 @@
3059330730
xRead = ascii_read_one_field;
3059430731
}else if( cli_strcmp(z,"-csv")==0 ){
3059530732
if( sCtx.cColSep==0 ) sCtx.cColSep = ',';
3059630733
if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\n';
3059730734
xRead = csv_read_one_field;
30735
+ }else if( cli_strcmp(z,"-esc")==0 ){
30736
+ sCtx.cUQEscape = azArg[++i][0];
30737
+ }else if( cli_strcmp(z,"-qesc")==0 ){
30738
+ sCtx.cQEscape = azArg[++i][0];
3059830739
}else if( cli_strcmp(z,"-colsep")==0 ){
3059930740
if( i==nArg-1 ){
3060030741
dotCmdError(p, i, "missing argument", 0);
3060130742
return 1;
3060230743
}
@@ -33351,10 +33492,23 @@
3335133492
continue;
3335233493
}
3335333494
if( cli_strcmp(z,"once")==0 ){
3335433495
p->flgProgress |= SHELL_PROGRESS_ONCE;
3335533496
continue;
33497
+ }
33498
+ if( cli_strcmp(z,"timeout")==0 ){
33499
+ if( i==nArg-1 ){
33500
+ dotCmdError(p, i, "missing argument", 0);
33501
+ return 1;
33502
+ }
33503
+ i++;
33504
+ p->tmProgress = atof(azArg[i]);
33505
+ if( p->tmProgress>0.0 ){
33506
+ p->flgProgress = SHELL_PROGRESS_QUIET|SHELL_PROGRESS_TMOUT;
33507
+ if( nn==0 ) nn = 100;
33508
+ }
33509
+ continue;
3335633510
}
3335733511
if( cli_strcmp(z,"limit")==0 ){
3335833512
if( i+1>=nArg ){
3335933513
eputz("Error: missing argument on --limit\n");
3336033514
rc = 1;
@@ -34843,17 +34997,21 @@
3484334997
sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
3484434998
}else
3484534999
3484635000
if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
3484735001
if( nArg==2 ){
34848
- enableTimer = booleanValue(azArg[1]);
34849
- if( enableTimer && !HAS_TIMER ){
35002
+ if( cli_strcmp(azArg[1],"once")==0 ){
35003
+ p->enableTimer = 1;
35004
+ }else{
35005
+ p->enableTimer = 2*booleanValue(azArg[1]);
35006
+ }
35007
+ if( p->enableTimer && !HAS_TIMER ){
3485035008
eputz("Error: timer not available on this system.\n");
34851
- enableTimer = 0;
35009
+ p->enableTimer = 0;
3485235010
}
3485335011
}else{
34854
- eputz("Usage: .timer on|off\n");
35012
+ eputz("Usage: .timer on|off|once\n");
3485535013
rc = 1;
3485635014
}
3485735015
}else
3485835016
3485935017
#ifndef SQLITE_OMIT_TRACE
@@ -35024,10 +35182,11 @@
3502435182
if( p->nPopOutput ){
3502535183
p->nPopOutput--;
3502635184
if( p->nPopOutput==0 ) output_reset(p);
3502735185
}
3502835186
p->bSafeMode = p->bSafeModePersist;
35187
+ p->dot.nArg = 0;
3502935188
return rc;
3503035189
}
3503135190
3503235191
/* Line scan result and intermediate states (supporting scan resumption)
3503335192
*/
@@ -35260,13 +35419,13 @@
3526035419
char *zErrMsg = 0;
3526135420
3526235421
open_db(p, 0);
3526335422
if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
3526435423
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
35265
- BEGIN_TIMER;
35424
+ BEGIN_TIMER(p);
3526635425
rc = shell_exec(p, zSql, &zErrMsg);
35267
- END_TIMER(p->out);
35426
+ END_TIMER(p);
3526835427
if( rc || zErrMsg ){
3526935428
char zPrefix[100];
3527035429
const char *zErrorTail;
3527135430
const char *zErrorType;
3527235431
if( zErrMsg==0 ){
3527335432
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -876,10 +876,11 @@
876 #ifndef SQLITE_QRF_H
877 #include "qrf.h"
878 #endif
879 #include <string.h>
880 #include <assert.h>
 
881
882 /* typedef sqlite3_int64 i64; */
883
884 /* A single line in the EQP output */
885 typedef struct qrfEQPGraphRow qrfEQPGraphRow;
@@ -893,11 +894,12 @@
893 /* All EQP output is collected into an instance of the following */
894 typedef struct qrfEQPGraph qrfEQPGraph;
895 struct qrfEQPGraph {
896 qrfEQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
897 qrfEQPGraphRow *pLast; /* Last element of the pRow list */
898 char zPrefix[100]; /* Graph prefix */
 
899 };
900
901 /*
902 ** Private state information. Subject to change from one release to the
903 ** next.
@@ -1087,10 +1089,49 @@
1087 qrfEqpRenderLevel(p, pRow->iEqpId);
1088 p->u.pGraph->zPrefix[n] = 0;
1089 }
1090 }
1091 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1092
1093 /*
1094 ** Display and reset the EXPLAIN QUERY PLAN data
1095 */
1096 static void qrfEqpRender(Qrf *p, i64 nCycle){
@@ -1103,11 +1144,30 @@
1103 }
1104 sqlite3_str_appendf(p->pOut, "%s\n", pRow->zText+3);
1105 p->u.pGraph->pRow = pRow->pNext;
1106 sqlite3_free(pRow);
1107 }else if( nCycle>0 ){
1108 sqlite3_str_appendf(p->pOut, "QUERY PLAN (cycles=%lld [100%%])\n",nCycle);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1109 }else{
1110 sqlite3_str_appendall(p->pOut, "QUERY PLAN\n");
1111 }
1112 p->u.pGraph->zPrefix[0] = 0;
1113 qrfEqpRenderLevel(p, 0);
@@ -1158,10 +1218,12 @@
1158 static const int f = SQLITE_SCANSTAT_COMPLEX;
1159 sqlite3_stmt *pS = p->pStmt;
1160 int i = 0;
1161 i64 nTotal = 0;
1162 int nWidth = 0;
 
 
1163 sqlite3_str *pLine = sqlite3_str_new(p->db);
1164 sqlite3_str *pStats = sqlite3_str_new(p->db);
1165 qrfEqpReset(p);
1166
1167 for(i=0; 1; i++){
@@ -1171,11 +1233,11 @@
1171 break;
1172 }
1173 n = (int)strlen(z) + qrfStatsHeight(pS,i)*3;
1174 if( n>nWidth ) nWidth = n;
1175 }
1176 nWidth += 4;
1177
1178 sqlite3_stmt_scanstatus_v2(pS,-1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
1179 for(i=0; 1; i++){
1180 i64 nLoop = 0;
1181 i64 nRow = 0;
@@ -1186,54 +1248,64 @@
1186 const char *zName = 0;
1187 double rEst = 0.0;
1188
1189 if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
1190 break;
 
 
 
 
 
1191 }
1192 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
 
1193 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
1194 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
1195 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
1196 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
1197 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
1198 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
1199
1200 if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
1201 const char *zSp = "";
1202 double rpl;
1203 sqlite3_str_reset(pStats);
1204 if( nCycle>=0 && nTotal>0 ){
1205 sqlite3_str_appendf(pStats, "cycles=%lld [%d%%]",
1206 nCycle, ((nCycle*100)+nTotal/2) / nTotal
 
1207 );
1208 zSp = " ";
1209 }
1210 if( nLoop>=0 ){
1211 sqlite3_str_appendf(pStats, "%sloops=%lld", zSp, nLoop);
1212 zSp = " ";
 
 
 
 
 
1213 }
1214 if( nRow>=0 ){
1215 sqlite3_str_appendf(pStats, "%srows=%lld", zSp, nRow);
1216 zSp = " ";
1217 }
1218
1219 if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1220 rpl = (double)nRow / (double)nLoop;
1221 sqlite3_str_appendf(pStats, "%srpl=%.1f est=%.1f", zSp, rpl, rEst);
1222 }
1223
1224 sqlite3_str_appendf(pLine,
1225 "% *s (%s)", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo,
1226 sqlite3_str_value(pStats)
1227 );
1228 sqlite3_str_reset(pStats);
1229 qrfEqpAppend(p, iId, iPid, sqlite3_str_value(pLine));
1230 sqlite3_str_reset(pLine);
1231 }else{
1232 qrfEqpAppend(p, iId, iPid, zo);
1233 }
1234 }
 
1235 qrfStrErr(p, pLine);
1236 sqlite3_free(sqlite3_str_finish(pLine));
1237 qrfStrErr(p, pStats);
1238 sqlite3_free(sqlite3_str_finish(pStats));
1239 #endif
@@ -3664,11 +3736,20 @@
3664 sqlite3_free(p->u.sLine.azCol);
3665 }
3666 break;
3667 }
3668 case QRF_STYLE_Stats:
3669 case QRF_STYLE_StatsEst:
 
 
 
 
 
 
 
 
 
3670 case QRF_STYLE_Eqp: {
3671 qrfEqpRender(p, 0);
3672 qrfWrite(p);
3673 break;
3674 }
@@ -3836,13 +3917,10 @@
3836
3837
3838 #define eputz(z) cli_puts(z,stderr)
3839 #define sputz(fp,z) cli_puts(z,fp)
3840
3841 /* True if the timer is enabled */
3842 static int enableTimer = 0;
3843
3844 /* A version of strcmp() that works with NULL values */
3845 static int cli_strcmp(const char *a, const char *b){
3846 if( a==0 ) a = "";
3847 if( b==0 ) b = "";
3848 return strcmp(a,b);
@@ -3883,154 +3961,10 @@
3883 (void)gettimeofday(&sNow,0);
3884 return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
3885 #endif
3886 }
3887
3888 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
3889 #include <sys/time.h>
3890 #include <sys/resource.h>
3891
3892 /* VxWorks does not support getrusage() as far as we can determine */
3893 #if defined(_WRS_KERNEL) || defined(__RTP__)
3894 struct rusage {
3895 struct timeval ru_utime; /* user CPU time used */
3896 struct timeval ru_stime; /* system CPU time used */
3897 };
3898 #define getrusage(A,B) memset(B,0,sizeof(*B))
3899 #endif
3900
3901
3902 /* Saved resource information for the beginning of an operation */
3903 static struct rusage sBegin; /* CPU time at start */
3904 static sqlite3_int64 iBegin; /* Wall-clock time at start */
3905
3906 /*
3907 ** Begin timing an operation
3908 */
3909 static void beginTimer(void){
3910 if( enableTimer ){
3911 getrusage(RUSAGE_SELF, &sBegin);
3912 iBegin = timeOfDay();
3913 }
3914 }
3915
3916 /* Return the difference of two time_structs in seconds */
3917 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
3918 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
3919 (double)(pEnd->tv_sec - pStart->tv_sec);
3920 }
3921
3922 /*
3923 ** Print the timing results.
3924 */
3925 static void endTimer(FILE *out){
3926 if( enableTimer ){
3927 sqlite3_int64 iEnd = timeOfDay();
3928 struct rusage sEnd;
3929 getrusage(RUSAGE_SELF, &sEnd);
3930 cli_printf(out, "Run Time: real %.6f user %.6f sys %.6f\n",
3931 (iEnd - iBegin)*0.000001,
3932 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
3933 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
3934 }
3935 }
3936
3937 #define BEGIN_TIMER beginTimer()
3938 #define END_TIMER(X) endTimer(X)
3939 #define HAS_TIMER 1
3940
3941 #elif (defined(_WIN32) || defined(WIN32))
3942
3943 /* Saved resource information for the beginning of an operation */
3944 static HANDLE hProcess;
3945 static FILETIME ftKernelBegin;
3946 static FILETIME ftUserBegin;
3947 static sqlite3_int64 ftWallBegin;
3948 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
3949 LPFILETIME, LPFILETIME);
3950 static GETPROCTIMES getProcessTimesAddr = NULL;
3951
3952 /*
3953 ** Check to see if we have timer support. Return 1 if necessary
3954 ** support found (or found previously).
3955 */
3956 static int hasTimer(void){
3957 if( getProcessTimesAddr ){
3958 return 1;
3959 } else {
3960 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
3961 ** versions. See if the version we are running on has it, and if it
3962 ** does, save off a pointer to it and the current process handle.
3963 */
3964 hProcess = GetCurrentProcess();
3965 if( hProcess ){
3966 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
3967 if( NULL != hinstLib ){
3968 getProcessTimesAddr =
3969 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
3970 if( NULL != getProcessTimesAddr ){
3971 return 1;
3972 }
3973 FreeLibrary(hinstLib);
3974 }
3975 }
3976 }
3977 return 0;
3978 }
3979
3980 /*
3981 ** Begin timing an operation
3982 */
3983 static void beginTimer(void){
3984 if( enableTimer && getProcessTimesAddr ){
3985 FILETIME ftCreation, ftExit;
3986 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
3987 &ftKernelBegin,&ftUserBegin);
3988 ftWallBegin = timeOfDay();
3989 }
3990 }
3991
3992 /* Return the difference of two FILETIME structs in seconds */
3993 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
3994 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
3995 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
3996 return (double) ((i64End - i64Start) / 10000000.0);
3997 }
3998
3999 /*
4000 ** Print the timing results.
4001 */
4002 static void endTimer(FILE *out){
4003 if( enableTimer && getProcessTimesAddr){
4004 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
4005 sqlite3_int64 ftWallEnd = timeOfDay();
4006 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
4007 #ifdef _WIN64
4008 /* microsecond precision on 64-bit windows */
4009 cli_printf(out, "Run Time: real %.6f user %f sys %f\n",
4010 (ftWallEnd - ftWallBegin)*0.000001,
4011 timeDiff(&ftUserBegin, &ftUserEnd),
4012 timeDiff(&ftKernelBegin, &ftKernelEnd));
4013 #else
4014 /* millisecond precisino on 32-bit windows */
4015 cli_printf(out, "Run Time: real %.3f user %.3f sys %.3f\n",
4016 (ftWallEnd - ftWallBegin)*0.000001,
4017 timeDiff(&ftUserBegin, &ftUserEnd),
4018 timeDiff(&ftKernelBegin, &ftKernelEnd));
4019 #endif
4020 }
4021 }
4022
4023 #define BEGIN_TIMER beginTimer()
4024 #define END_TIMER(X) endTimer(X)
4025 #define HAS_TIMER hasTimer()
4026
4027 #else
4028 #define BEGIN_TIMER
4029 #define END_TIMER(X) /*no-op*/
4030 #define HAS_TIMER 0
4031 #endif
4032
4033 /*
4034 ** Used to prevent warnings about unused parameters
4035 */
4036 #define UNUSED_PARAMETER(x) (void)(x)
@@ -24226,11 +24160,14 @@
24226 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
24227 unsigned statsOn; /* True to display memory stats before each finalize */
24228 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
24229 u8 nPopOutput; /* Revert .output settings when reaching zero */
24230 u8 nPopMode; /* Revert .mode settings when reaching zero */
 
24231 int inputNesting; /* Track nesting level of .read and other redirects */
 
 
24232 i64 lineno; /* Line number of last line read from in */
24233 const char *zInFile; /* Name of the input file */
24234 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
24235 FILE *in; /* Read commands from this stream */
24236 FILE *out; /* Write results here */
@@ -24322,10 +24259,11 @@
24322 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
24323 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
24324 ** callback limit is reached, and for each
24325 ** top-level SQL statement */
24326 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
 
24327
24328 /* Names of values for Mode.spec.eEsc and Mode.spec.eText
24329 */
24330 static const char *qrfEscNames[] = { "auto", "off", "ascii", "symbol" };
24331 static const char *qrfQuoteNames[] =
@@ -24483,10 +24421,181 @@
24483 ** Limit input nesting via .read or any other input redirect.
24484 ** It's not too expensive, so a generous allowance can be made.
24485 */
24486 #define MAX_INPUT_NESTING 25
24487
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24488
24489 /*
24490 ** Clear a display mode, freeing any allocated memory that it
24491 ** contains.
24492 */
@@ -25408,10 +25517,17 @@
25408 ** Progress handler callback.
25409 */
25410 static int progress_handler(void *pClientData) {
25411 ShellState *p = (ShellState*)pClientData;
25412 p->nProgress++;
 
 
 
 
 
 
 
25413 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
25414 cli_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
25415 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
25416 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
25417 return 1;
@@ -25945,10 +26061,12 @@
25945 char *zBuf = sqlite3_malloc64( szVar-5 );
25946 if( zBuf ){
25947 memcpy(zBuf, &zVar[6], szVar-5);
25948 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
25949 }
 
 
25950 #ifdef SQLITE_ENABLE_CARRAY
25951 }else if( strncmp(zVar, "$carray_", 8)==0 ){
25952 static char *azColorNames[] = {
25953 "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold",
25954 "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon",
@@ -26204,12 +26322,14 @@
26204 pArg->pStmt = pStmt;
26205 }
26206
26207 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
26208 isExplain = sqlite3_stmt_isexplain(pStmt);
26209 if( pArg && pArg->mode.autoEQP && isExplain==0 ){
26210 int triggerEQP = 0;
 
 
26211 disable_debug_trace_modes();
26212 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
26213 if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
26214 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
26215 }
@@ -26227,10 +26347,11 @@
26227 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
26228 }
26229 sqlite3_reset(pStmt);
26230 sqlite3_stmt_explain(pStmt, 0);
26231 restore_debug_trace_modes();
 
26232 }
26233
26234 bind_prepared_stmt(pArg, pStmt);
26235 if( isExplain && pArg->mode.autoExplain ){
26236 spec.eStyle = isExplain==1 ? QRF_STYLE_Explain : QRF_STYLE_Eqp;
@@ -26764,10 +26885,11 @@
26764 ".progress N Invoke progress handler after every N opcodes",
26765 " --limit N Interrupt after N progress callbacks",
26766 " --once Do no more than one progress interrupt",
26767 " --quiet|-q No output except at interrupts",
26768 " --reset Reset the count for each input and interrupt",
 
26769 #endif
26770 ".prompt MAIN CONTINUE Replace the standard prompts",
26771 #ifndef SQLITE_SHELL_FIDDLE
26772 ".quit Stop interpreting input stream, exit if primary.",
26773 ".read FILE Read input from FILE or command output",
@@ -26832,11 +26954,11 @@
26832 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
26833 ".testcase NAME Begin a test case.",
26834 ",testctrl CMD ... Run various sqlite3_test_control() operations",
26835 " Run \".testctrl\" with no arguments for details",
26836 ".timeout MS Try opening locked tables for MS milliseconds",
26837 ".timer on|off Turn SQL timer on or off",
26838 #ifndef SQLITE_OMIT_TRACE
26839 ".trace ?OPTIONS? Output each SQL statement as it is run",
26840 " FILE Send output to FILE",
26841 " stdout Send output to stdout",
26842 " stderr Send output to stderr",
@@ -26897,10 +27019,12 @@
26897 " --ascii Do not use RFC-4180 quoting. Use \\037 and \\036\n"
26898 " as column and row separators on input, unless other\n"
26899 " delimiters are specified using --colsep and/or --rowsep\n"
26900 " --colsep CHAR Use CHAR as the column separator.\n"
26901 " --csv Input is standard RFC-4180 CSV.\n"
 
 
26902 " --rowsep CHAR Use CHAR as the row separator.\n"
26903 " --schema S When creating TABLE, put it in schema S\n"
26904 " --skip N Ignore the first N rows of input\n"
26905 " -v Verbose mode\n"
26906 },
@@ -28054,10 +28178,12 @@
28054 int nErr; /* Number of errors encountered */
28055 int bNotFirst; /* True if one or more bytes already read */
28056 int cTerm; /* Character that terminated the most recent field */
28057 int cColSep; /* The column separator character. (Usually ",") */
28058 int cRowSep; /* The row separator character. (Usually "\n") */
 
 
28059 };
28060
28061 /* Clean up resourced used by an ImportCtx */
28062 static void import_cleanup(ImportCtx *p){
28063 if( p->in!=0 && p->xCloser!=0 ){
@@ -28122,14 +28248,21 @@
28122 }
28123 if( c=='"' ){
28124 int pc, ppc;
28125 int startLine = p->nLine;
28126 int cQuote = c;
 
28127 pc = ppc = 0;
28128 while( 1 ){
28129 c = import_getc(p);
28130 if( c==rSep ) p->nLine++;
 
 
 
 
 
 
28131 if( c==cQuote ){
28132 if( pc==cQuote ){
28133 pc = 0;
28134 continue;
28135 }
@@ -28143,11 +28276,11 @@
28143 p->cTerm = c;
28144 break;
28145 }
28146 if( pc==cQuote && c!='\r' ){
28147 cli_printf(stderr,"%s:%d: unescaped %c character\n",
28148 p->zFile, p->nLine, cQuote);
28149 }
28150 if( c==EOF ){
28151 cli_printf(stderr,"%s:%d: unterminated %c-quoted field\n",
28152 p->zFile, startLine, cQuote);
28153 p->cTerm = c;
@@ -28158,10 +28291,11 @@
28158 pc = c;
28159 }
28160 }else{
28161 /* If this is the first field being parsed and it begins with the
28162 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
 
28163 if( (c&0xff)==0xef && p->bNotFirst==0 ){
28164 import_append_char(p, c);
28165 c = import_getc(p);
28166 if( (c&0xff)==0xbb ){
28167 import_append_char(p, c);
@@ -28172,10 +28306,11 @@
28172 return csv_read_one_field(p);
28173 }
28174 }
28175 }
28176 while( c!=EOF && c!=cSep && c!=rSep ){
 
28177 import_append_char(p, c);
28178 c = import_getc(p);
28179 }
28180 if( c==rSep ){
28181 p->nLine++;
@@ -30315,11 +30450,11 @@
30315 ;
30316 static const char * const zCollectVar = "\
30317 SELECT\
30318 '('||x'0a'\
30319 || group_concat(\
30320 cname||' TEXT',\
30321 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
30322 ||')' AS ColsSpec \
30323 FROM (\
30324 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
30325 FROM ColNames ORDER BY cpos\
@@ -30535,10 +30670,12 @@
30535 ** --ascii Do not use RFC-4180 quoting. Use \037 and \036
30536 ** as column and row separators on input, unless other
30537 ** delimiters are specified using --colsep and/or --rowsep
30538 ** --colsep CHAR Use CHAR as the column separator.
30539 ** --csv Input is standard RFC-4180 CSV.
 
 
30540 ** --rowsep CHAR Use CHAR as the row separator.
30541 ** --schema S When creating TABLE, put it in schema S
30542 ** --skip N Ignore the first N rows of input
30543 ** -v Verbose mode
30544 */
@@ -30593,10 +30730,14 @@
30593 xRead = ascii_read_one_field;
30594 }else if( cli_strcmp(z,"-csv")==0 ){
30595 if( sCtx.cColSep==0 ) sCtx.cColSep = ',';
30596 if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\n';
30597 xRead = csv_read_one_field;
 
 
 
 
30598 }else if( cli_strcmp(z,"-colsep")==0 ){
30599 if( i==nArg-1 ){
30600 dotCmdError(p, i, "missing argument", 0);
30601 return 1;
30602 }
@@ -33351,10 +33492,23 @@
33351 continue;
33352 }
33353 if( cli_strcmp(z,"once")==0 ){
33354 p->flgProgress |= SHELL_PROGRESS_ONCE;
33355 continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
33356 }
33357 if( cli_strcmp(z,"limit")==0 ){
33358 if( i+1>=nArg ){
33359 eputz("Error: missing argument on --limit\n");
33360 rc = 1;
@@ -34843,17 +34997,21 @@
34843 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
34844 }else
34845
34846 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
34847 if( nArg==2 ){
34848 enableTimer = booleanValue(azArg[1]);
34849 if( enableTimer && !HAS_TIMER ){
 
 
 
 
34850 eputz("Error: timer not available on this system.\n");
34851 enableTimer = 0;
34852 }
34853 }else{
34854 eputz("Usage: .timer on|off\n");
34855 rc = 1;
34856 }
34857 }else
34858
34859 #ifndef SQLITE_OMIT_TRACE
@@ -35024,10 +35182,11 @@
35024 if( p->nPopOutput ){
35025 p->nPopOutput--;
35026 if( p->nPopOutput==0 ) output_reset(p);
35027 }
35028 p->bSafeMode = p->bSafeModePersist;
 
35029 return rc;
35030 }
35031
35032 /* Line scan result and intermediate states (supporting scan resumption)
35033 */
@@ -35260,13 +35419,13 @@
35260 char *zErrMsg = 0;
35261
35262 open_db(p, 0);
35263 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
35264 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
35265 BEGIN_TIMER;
35266 rc = shell_exec(p, zSql, &zErrMsg);
35267 END_TIMER(p->out);
35268 if( rc || zErrMsg ){
35269 char zPrefix[100];
35270 const char *zErrorTail;
35271 const char *zErrorType;
35272 if( zErrMsg==0 ){
35273
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -876,10 +876,11 @@
876 #ifndef SQLITE_QRF_H
877 #include "qrf.h"
878 #endif
879 #include <string.h>
880 #include <assert.h>
881 #include <stdint.h>
882
883 /* typedef sqlite3_int64 i64; */
884
885 /* A single line in the EQP output */
886 typedef struct qrfEQPGraphRow qrfEQPGraphRow;
@@ -893,11 +894,12 @@
894 /* All EQP output is collected into an instance of the following */
895 typedef struct qrfEQPGraph qrfEQPGraph;
896 struct qrfEQPGraph {
897 qrfEQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
898 qrfEQPGraphRow *pLast; /* Last element of the pRow list */
899 int nWidth; /* Width of the graph */
900 char zPrefix[400]; /* Graph prefix */
901 };
902
903 /*
904 ** Private state information. Subject to change from one release to the
905 ** next.
@@ -1087,10 +1089,49 @@
1089 qrfEqpRenderLevel(p, pRow->iEqpId);
1090 p->u.pGraph->zPrefix[n] = 0;
1091 }
1092 }
1093 }
1094
1095 /*
1096 ** Render the 64-bit value N in a more human-readable format into
1097 ** pOut.
1098 **
1099 ** + Only show the first three significant digits.
1100 ** + Append suffixes K, M, G, T, P, and E for 1e3, 1e6, ... 1e18
1101 */
1102 static void qrfApproxInt64(sqlite3_str *pOut, i64 N){
1103 static const char aSuffix[] = { 'K', 'M', 'G', 'T', 'P', 'E' };
1104 int i;
1105 if( N<0 ){
1106 N = N==INT64_MIN ? INT64_MAX : -N;
1107 sqlite3_str_append(pOut, "-", 1);
1108 }
1109 if( N<10000 ){
1110 sqlite3_str_appendf(pOut, "%4lld ", N);
1111 return;
1112 }
1113 for(i=1; i<=18; i++){
1114 N = (N+5)/10;
1115 if( N<10000 ){
1116 int n = (int)N;
1117 switch( i%3 ){
1118 case 0:
1119 sqlite3_str_appendf(pOut, "%d.%02d", n/1000, (n%1000)/10);
1120 break;
1121 case 1:
1122 sqlite3_str_appendf(pOut, "%2d.%d", n/100, (n%100)/10);
1123 break;
1124 case 2:
1125 sqlite3_str_appendf(pOut, "%4d", n/10);
1126 break;
1127 }
1128 sqlite3_str_append(pOut, &aSuffix[i/3], 1);
1129 break;
1130 }
1131 }
1132 }
1133
1134 /*
1135 ** Display and reset the EXPLAIN QUERY PLAN data
1136 */
1137 static void qrfEqpRender(Qrf *p, i64 nCycle){
@@ -1103,11 +1144,30 @@
1144 }
1145 sqlite3_str_appendf(p->pOut, "%s\n", pRow->zText+3);
1146 p->u.pGraph->pRow = pRow->pNext;
1147 sqlite3_free(pRow);
1148 }else if( nCycle>0 ){
1149 int nSp = p->u.pGraph->nWidth - 2;
1150 if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1151 sqlite3_str_appendchar(p->pOut, nSp, ' ');
1152 sqlite3_str_appendall(p->pOut,
1153 "Cycles Loops (est) Rows (est)\n");
1154 sqlite3_str_appendchar(p->pOut, nSp, ' ');
1155 sqlite3_str_appendall(p->pOut,
1156 "---------- ------------ ------------\n");
1157 }else{
1158 sqlite3_str_appendchar(p->pOut, nSp, ' ');
1159 sqlite3_str_appendall(p->pOut,
1160 "Cycles Loops Rows \n");
1161 sqlite3_str_appendchar(p->pOut, nSp, ' ');
1162 sqlite3_str_appendall(p->pOut,
1163 "---------- ----- -----\n");
1164 }
1165 sqlite3_str_appendall(p->pOut, "QUERY PLAN");
1166 sqlite3_str_appendchar(p->pOut, nSp - 10, ' ');
1167 qrfApproxInt64(p->pOut, nCycle);
1168 sqlite3_str_appendall(p->pOut, " 100%\n");
1169 }else{
1170 sqlite3_str_appendall(p->pOut, "QUERY PLAN\n");
1171 }
1172 p->u.pGraph->zPrefix[0] = 0;
1173 qrfEqpRenderLevel(p, 0);
@@ -1158,10 +1218,12 @@
1218 static const int f = SQLITE_SCANSTAT_COMPLEX;
1219 sqlite3_stmt *pS = p->pStmt;
1220 int i = 0;
1221 i64 nTotal = 0;
1222 int nWidth = 0;
1223 int prevPid = -1; /* Previous iPid */
1224 double rEstCum = 1.0; /* Cumulative row estimate */
1225 sqlite3_str *pLine = sqlite3_str_new(p->db);
1226 sqlite3_str *pStats = sqlite3_str_new(p->db);
1227 qrfEqpReset(p);
1228
1229 for(i=0; 1; i++){
@@ -1171,11 +1233,11 @@
1233 break;
1234 }
1235 n = (int)strlen(z) + qrfStatsHeight(pS,i)*3;
1236 if( n>nWidth ) nWidth = n;
1237 }
1238 nWidth += 2;
1239
1240 sqlite3_stmt_scanstatus_v2(pS,-1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
1241 for(i=0; 1; i++){
1242 i64 nLoop = 0;
1243 i64 nRow = 0;
@@ -1186,54 +1248,64 @@
1248 const char *zName = 0;
1249 double rEst = 0.0;
1250
1251 if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
1252 break;
1253 }
1254 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
1255 if( iPid!=prevPid ){
1256 prevPid = iPid;
1257 rEstCum = 1.0;
1258 }
1259 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
1260 rEstCum *= rEst;
1261 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
1262 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
1263 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
1264 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
 
1265 sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
1266
1267 if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
1268 int nSp = 0;
 
1269 sqlite3_str_reset(pStats);
1270 if( nCycle>=0 && nTotal>0 ){
1271 qrfApproxInt64(pStats, nCycle);
1272 sqlite3_str_appendf(pStats, " %3d%%",
1273 ((nCycle*100)+nTotal/2) / nTotal
1274 );
1275 nSp = 2;
1276 }
1277 if( nLoop>=0 ){
1278 if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
1279 qrfApproxInt64(pStats, nLoop);
1280 nSp = 2;
1281 if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1282 sqlite3_str_appendf(pStats, " ");
1283 qrfApproxInt64(pStats, (i64)(rEstCum/rEst));
1284 }
1285 }
1286 if( nRow>=0 ){
1287 if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' ');
1288 qrfApproxInt64(pStats, nRow);
1289 nSp = 2;
1290 if( p->spec.eStyle==QRF_STYLE_StatsEst ){
1291 sqlite3_str_appendf(pStats, " ");
1292 qrfApproxInt64(pStats, (i64)rEstCum);
1293 }
1294 }
 
1295 sqlite3_str_appendf(pLine,
1296 "% *s %s", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo,
1297 sqlite3_str_value(pStats)
1298 );
1299 sqlite3_str_reset(pStats);
1300 qrfEqpAppend(p, iId, iPid, sqlite3_str_value(pLine));
1301 sqlite3_str_reset(pLine);
1302 }else{
1303 qrfEqpAppend(p, iId, iPid, zo);
1304 }
1305 }
1306 if( p->u.pGraph ) p->u.pGraph->nWidth = nWidth;
1307 qrfStrErr(p, pLine);
1308 sqlite3_free(sqlite3_str_finish(pLine));
1309 qrfStrErr(p, pStats);
1310 sqlite3_free(sqlite3_str_finish(pStats));
1311 #endif
@@ -3664,11 +3736,20 @@
3736 sqlite3_free(p->u.sLine.azCol);
3737 }
3738 break;
3739 }
3740 case QRF_STYLE_Stats:
3741 case QRF_STYLE_StatsEst: {
3742 i64 nCycle = 0;
3743 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3744 sqlite3_stmt_scanstatus_v2(p->pStmt, -1, SQLITE_SCANSTAT_NCYCLE,
3745 SQLITE_SCANSTAT_COMPLEX, (void*)&nCycle);
3746 #endif
3747 qrfEqpRender(p, nCycle);
3748 qrfWrite(p);
3749 break;
3750 }
3751 case QRF_STYLE_Eqp: {
3752 qrfEqpRender(p, 0);
3753 qrfWrite(p);
3754 break;
3755 }
@@ -3836,13 +3917,10 @@
3917
3918
3919 #define eputz(z) cli_puts(z,stderr)
3920 #define sputz(fp,z) cli_puts(z,fp)
3921
 
 
 
3922 /* A version of strcmp() that works with NULL values */
3923 static int cli_strcmp(const char *a, const char *b){
3924 if( a==0 ) a = "";
3925 if( b==0 ) b = "";
3926 return strcmp(a,b);
@@ -3883,154 +3961,10 @@
3961 (void)gettimeofday(&sNow,0);
3962 return ((i64)sNow.tv_sec)*1000000 + sNow.tv_usec;
3963 #endif
3964 }
3965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3966
3967 /*
3968 ** Used to prevent warnings about unused parameters
3969 */
3970 #define UNUSED_PARAMETER(x) (void)(x)
@@ -24226,11 +24160,14 @@
24160 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
24161 unsigned statsOn; /* True to display memory stats before each finalize */
24162 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
24163 u8 nPopOutput; /* Revert .output settings when reaching zero */
24164 u8 nPopMode; /* Revert .mode settings when reaching zero */
24165 u8 enableTimer; /* Enable the timer. 2: permanently 1: only once */
24166 int inputNesting; /* Track nesting level of .read and other redirects */
24167 double prevTimer; /* Last reported timer value */
24168 double tmProgress; /* --timeout option for .progress */
24169 i64 lineno; /* Line number of last line read from in */
24170 const char *zInFile; /* Name of the input file */
24171 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
24172 FILE *in; /* Read commands from this stream */
24173 FILE *out; /* Write results here */
@@ -24322,10 +24259,11 @@
24259 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
24260 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
24261 ** callback limit is reached, and for each
24262 ** top-level SQL statement */
24263 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
24264 #define SHELL_PROGRESS_TMOUT 0x08 /* Stop after tmProgress seconds */
24265
24266 /* Names of values for Mode.spec.eEsc and Mode.spec.eText
24267 */
24268 static const char *qrfEscNames[] = { "auto", "off", "ascii", "symbol" };
24269 static const char *qrfQuoteNames[] =
@@ -24483,10 +24421,181 @@
24421 ** Limit input nesting via .read or any other input redirect.
24422 ** It's not too expensive, so a generous allowance can be made.
24423 */
24424 #define MAX_INPUT_NESTING 25
24425
24426 /************************* BEGIN PERFORMANCE TIMER *****************************/
24427 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
24428 #include <sys/time.h>
24429 #include <sys/resource.h>
24430 /* VxWorks does not support getrusage() as far as we can determine */
24431 #if defined(_WRS_KERNEL) || defined(__RTP__)
24432 struct rusage {
24433 struct timeval ru_utime; /* user CPU time used */
24434 struct timeval ru_stime; /* system CPU time used */
24435 };
24436 #define getrusage(A,B) memset(B,0,sizeof(*B))
24437 #endif
24438
24439 /* Saved resource information for the beginning of an operation */
24440 static struct rusage sBegin; /* CPU time at start */
24441 static sqlite3_int64 iBegin; /* Wall-clock time at start */
24442
24443 /*
24444 ** Begin timing an operation
24445 */
24446 static void beginTimer(ShellState *p){
24447 if( p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0 ){
24448 getrusage(RUSAGE_SELF, &sBegin);
24449 iBegin = timeOfDay();
24450 }
24451 }
24452
24453 /* Return the difference of two time_structs in seconds */
24454 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
24455 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
24456 (double)(pEnd->tv_sec - pStart->tv_sec);
24457 }
24458
24459 /* Return the time since the start of the timer in
24460 ** seconds. */
24461 static double elapseTime(ShellState *NotUsed){
24462 (void)NotUsed;
24463 if( iBegin==0 ) return 0.0;
24464 return (timeOfDay() - iBegin)*0.000001;
24465 }
24466
24467 /*
24468 ** Print the timing results.
24469 */
24470 static void endTimer(ShellState *p){
24471 if( p->enableTimer ){
24472 sqlite3_int64 iEnd = timeOfDay();
24473 struct rusage sEnd;
24474 getrusage(RUSAGE_SELF, &sEnd);
24475 p->prevTimer = (iEnd - iBegin)*0.000001;
24476 cli_printf(p->out, "Run Time: real %.6f user %.6f sys %.6f\n",
24477 p->prevTimer,
24478 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
24479 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
24480 if( p->enableTimer==1 ) p->enableTimer = 0;
24481 iBegin = 0;
24482 }
24483 }
24484
24485 #define BEGIN_TIMER(X) beginTimer(X)
24486 #define END_TIMER(X) endTimer(X)
24487 #define ELAPSE_TIME(X) elapseTime(X)
24488 #define HAS_TIMER 1
24489
24490 #elif (defined(_WIN32) || defined(WIN32))
24491
24492 /* Saved resource information for the beginning of an operation */
24493 static HANDLE hProcess;
24494 static FILETIME ftKernelBegin;
24495 static FILETIME ftUserBegin;
24496 static sqlite3_int64 ftWallBegin;
24497 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
24498 LPFILETIME, LPFILETIME);
24499 static GETPROCTIMES getProcessTimesAddr = NULL;
24500
24501 /*
24502 ** Check to see if we have timer support. Return 1 if necessary
24503 ** support found (or found previously).
24504 */
24505 static int hasTimer(void){
24506 if( getProcessTimesAddr ){
24507 return 1;
24508 } else {
24509 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
24510 ** versions. See if the version we are running on has it, and if it
24511 ** does, save off a pointer to it and the current process handle.
24512 */
24513 hProcess = GetCurrentProcess();
24514 if( hProcess ){
24515 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
24516 if( NULL != hinstLib ){
24517 getProcessTimesAddr =
24518 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
24519 if( NULL != getProcessTimesAddr ){
24520 return 1;
24521 }
24522 FreeLibrary(hinstLib);
24523 }
24524 }
24525 }
24526 return 0;
24527 }
24528
24529 /*
24530 ** Begin timing an operation
24531 */
24532 static void beginTimer(ShellState *p){
24533 if( (p->enableTimer || (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0)
24534 && getProcessTimesAddr
24535 ){
24536 FILETIME ftCreation, ftExit;
24537 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
24538 &ftKernelBegin,&ftUserBegin);
24539 ftWallBegin = timeOfDay();
24540 }
24541 }
24542
24543 /* Return the difference of two FILETIME structs in seconds */
24544 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
24545 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
24546 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
24547 return (double) ((i64End - i64Start) / 10000000.0);
24548 }
24549
24550 /* Return the time since the start of the timer in
24551 ** seconds. */
24552 static double elapseTime(ShellState *NotUsed){
24553 (void)NotUsed;
24554 if( ftWallBegin==0 ) return 0.0;
24555 return (timeOfDay() - ftWallBegin)*0.000001;
24556 }
24557
24558 /*
24559 ** Print the timing results.
24560 */
24561 static void endTimer(ShellState *p){
24562 if( p->enableTimer && getProcessTimesAddr){
24563 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
24564 sqlite3_int64 ftWallEnd = timeOfDay();
24565 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
24566 p->prevTimer = (ftWallEnd - ftWallBegin)*0.000001;
24567 #ifdef _WIN64
24568 /* microsecond precision on 64-bit windows */
24569 cli_printf(p->out, "Run Time: real %.6f user %f sys %f\n",
24570 p->prevTimer,
24571 timeDiff(&ftUserBegin, &ftUserEnd),
24572 timeDiff(&ftKernelBegin, &ftKernelEnd));
24573 #else
24574 /* millisecond precisino on 32-bit windows */
24575 cli_printf(p->out, "Run Time: real %.3f user %.3f sys %.3f\n",
24576 p->prevTimer,
24577 timeDiff(&ftUserBegin, &ftUserEnd),
24578 timeDiff(&ftKernelBegin, &ftKernelEnd));
24579 #endif
24580 if( p->enableTimer==1 ) p->enableTimer = 0;
24581 ftWallBegin = 0;
24582 }
24583 }
24584
24585 #define BEGIN_TIMER(X) beginTimer(X)
24586 #define ELAPSE_TIME(X) elapseTime(X)
24587 #define END_TIMER(X) endTimer(X)
24588 #define HAS_TIMER hasTimer()
24589
24590 #else
24591 #define BEGIN_TIMER(X) /* no-op */
24592 #define ELAPSE_TIME(X) 0.0
24593 #define END_TIMER(X) /*no-op*/
24594 #define HAS_TIMER 0
24595 #endif
24596 /************************* END PERFORMANCE TIMER ******************************/
24597
24598 /*
24599 ** Clear a display mode, freeing any allocated memory that it
24600 ** contains.
24601 */
@@ -25408,10 +25517,17 @@
25517 ** Progress handler callback.
25518 */
25519 static int progress_handler(void *pClientData) {
25520 ShellState *p = (ShellState*)pClientData;
25521 p->nProgress++;
25522 if( (p->flgProgress & SHELL_PROGRESS_TMOUT)!=0
25523 && ELAPSE_TIME(p)>=p->tmProgress
25524 ){
25525 cli_printf(p->out, "Progress timeout after %.6f seconds\n",
25526 ELAPSE_TIME(p));
25527 return 1;
25528 }
25529 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
25530 cli_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
25531 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
25532 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
25533 return 1;
@@ -25945,10 +26061,12 @@
26061 char *zBuf = sqlite3_malloc64( szVar-5 );
26062 if( zBuf ){
26063 memcpy(zBuf, &zVar[6], szVar-5);
26064 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
26065 }
26066 }else if( strcmp(zVar, "$TIMER")==0 ){
26067 sqlite3_bind_double(pStmt, i, pArg->prevTimer);
26068 #ifdef SQLITE_ENABLE_CARRAY
26069 }else if( strncmp(zVar, "$carray_", 8)==0 ){
26070 static char *azColorNames[] = {
26071 "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold",
26072 "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon",
@@ -26204,12 +26322,14 @@
26322 pArg->pStmt = pStmt;
26323 }
26324
26325 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
26326 isExplain = sqlite3_stmt_isexplain(pStmt);
26327 if( pArg && pArg->mode.autoEQP && isExplain==0 && pArg->dot.nArg==0 ){
26328 int triggerEQP = 0;
26329 u8 savedEnableTimer = pArg->enableTimer;
26330 pArg->enableTimer = 0;
26331 disable_debug_trace_modes();
26332 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
26333 if( pArg->mode.autoEQP>=AUTOEQP_trigger ){
26334 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
26335 }
@@ -26227,10 +26347,11 @@
26347 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
26348 }
26349 sqlite3_reset(pStmt);
26350 sqlite3_stmt_explain(pStmt, 0);
26351 restore_debug_trace_modes();
26352 pArg->enableTimer = savedEnableTimer;
26353 }
26354
26355 bind_prepared_stmt(pArg, pStmt);
26356 if( isExplain && pArg->mode.autoExplain ){
26357 spec.eStyle = isExplain==1 ? QRF_STYLE_Explain : QRF_STYLE_Eqp;
@@ -26764,10 +26885,11 @@
26885 ".progress N Invoke progress handler after every N opcodes",
26886 " --limit N Interrupt after N progress callbacks",
26887 " --once Do no more than one progress interrupt",
26888 " --quiet|-q No output except at interrupts",
26889 " --reset Reset the count for each input and interrupt",
26890 " --timeout S Halt after running for S seconds",
26891 #endif
26892 ".prompt MAIN CONTINUE Replace the standard prompts",
26893 #ifndef SQLITE_SHELL_FIDDLE
26894 ".quit Stop interpreting input stream, exit if primary.",
26895 ".read FILE Read input from FILE or command output",
@@ -26832,11 +26954,11 @@
26954 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
26955 ".testcase NAME Begin a test case.",
26956 ",testctrl CMD ... Run various sqlite3_test_control() operations",
26957 " Run \".testctrl\" with no arguments for details",
26958 ".timeout MS Try opening locked tables for MS milliseconds",
26959 ".timer on|off|once Turn SQL timer on or off.",
26960 #ifndef SQLITE_OMIT_TRACE
26961 ".trace ?OPTIONS? Output each SQL statement as it is run",
26962 " FILE Send output to FILE",
26963 " stdout Send output to stdout",
26964 " stderr Send output to stderr",
@@ -26897,10 +27019,12 @@
27019 " --ascii Do not use RFC-4180 quoting. Use \\037 and \\036\n"
27020 " as column and row separators on input, unless other\n"
27021 " delimiters are specified using --colsep and/or --rowsep\n"
27022 " --colsep CHAR Use CHAR as the column separator.\n"
27023 " --csv Input is standard RFC-4180 CSV.\n"
27024 " --esc CHAR Use CHAR as an escape character in unquoted CSV inputs.\n"
27025 " --qesc CHAR Use CHAR as an escape character in quoted CSV inputs.\n"
27026 " --rowsep CHAR Use CHAR as the row separator.\n"
27027 " --schema S When creating TABLE, put it in schema S\n"
27028 " --skip N Ignore the first N rows of input\n"
27029 " -v Verbose mode\n"
27030 },
@@ -28054,10 +28178,12 @@
28178 int nErr; /* Number of errors encountered */
28179 int bNotFirst; /* True if one or more bytes already read */
28180 int cTerm; /* Character that terminated the most recent field */
28181 int cColSep; /* The column separator character. (Usually ",") */
28182 int cRowSep; /* The row separator character. (Usually "\n") */
28183 int cQEscape; /* Escape character with "...". 0 for none */
28184 int cUQEscape; /* Escape character not with "...". 0 for none */
28185 };
28186
28187 /* Clean up resourced used by an ImportCtx */
28188 static void import_cleanup(ImportCtx *p){
28189 if( p->in!=0 && p->xCloser!=0 ){
@@ -28122,14 +28248,21 @@
28248 }
28249 if( c=='"' ){
28250 int pc, ppc;
28251 int startLine = p->nLine;
28252 int cQuote = c;
28253 int cEsc = (u8)p->cQEscape;
28254 pc = ppc = 0;
28255 while( 1 ){
28256 c = import_getc(p);
28257 if( c==rSep ) p->nLine++;
28258 if( c==cEsc && cEsc!=0 ){
28259 c = import_getc(p);
28260 import_append_char(p, c);
28261 ppc = pc = 0;
28262 continue;
28263 }
28264 if( c==cQuote ){
28265 if( pc==cQuote ){
28266 pc = 0;
28267 continue;
28268 }
@@ -28143,11 +28276,11 @@
28276 p->cTerm = c;
28277 break;
28278 }
28279 if( pc==cQuote && c!='\r' ){
28280 cli_printf(stderr,"%s:%d: unescaped %c character\n",
28281 p->zFile, p->nLine, cQuote);
28282 }
28283 if( c==EOF ){
28284 cli_printf(stderr,"%s:%d: unterminated %c-quoted field\n",
28285 p->zFile, startLine, cQuote);
28286 p->cTerm = c;
@@ -28158,10 +28291,11 @@
28291 pc = c;
28292 }
28293 }else{
28294 /* If this is the first field being parsed and it begins with the
28295 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
28296 int cEsc = p->cUQEscape;
28297 if( (c&0xff)==0xef && p->bNotFirst==0 ){
28298 import_append_char(p, c);
28299 c = import_getc(p);
28300 if( (c&0xff)==0xbb ){
28301 import_append_char(p, c);
@@ -28172,10 +28306,11 @@
28306 return csv_read_one_field(p);
28307 }
28308 }
28309 }
28310 while( c!=EOF && c!=cSep && c!=rSep ){
28311 if( c==cEsc && cEsc!=0 ) c = import_getc(p);
28312 import_append_char(p, c);
28313 c = import_getc(p);
28314 }
28315 if( c==rSep ){
28316 p->nLine++;
@@ -30315,11 +30450,11 @@
30450 ;
30451 static const char * const zCollectVar = "\
30452 SELECT\
30453 '('||x'0a'\
30454 || group_concat(\
30455 cname||' ANY',\
30456 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
30457 ||')' AS ColsSpec \
30458 FROM (\
30459 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
30460 FROM ColNames ORDER BY cpos\
@@ -30535,10 +30670,12 @@
30670 ** --ascii Do not use RFC-4180 quoting. Use \037 and \036
30671 ** as column and row separators on input, unless other
30672 ** delimiters are specified using --colsep and/or --rowsep
30673 ** --colsep CHAR Use CHAR as the column separator.
30674 ** --csv Input is standard RFC-4180 CSV.
30675 ** --esc CHAR Use CHAR as an escape character in unquoted CSV inputs.
30676 ** --qesc CHAR Use CHAR as an escape character in quoted CSV inputs.
30677 ** --rowsep CHAR Use CHAR as the row separator.
30678 ** --schema S When creating TABLE, put it in schema S
30679 ** --skip N Ignore the first N rows of input
30680 ** -v Verbose mode
30681 */
@@ -30593,10 +30730,14 @@
30730 xRead = ascii_read_one_field;
30731 }else if( cli_strcmp(z,"-csv")==0 ){
30732 if( sCtx.cColSep==0 ) sCtx.cColSep = ',';
30733 if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\n';
30734 xRead = csv_read_one_field;
30735 }else if( cli_strcmp(z,"-esc")==0 ){
30736 sCtx.cUQEscape = azArg[++i][0];
30737 }else if( cli_strcmp(z,"-qesc")==0 ){
30738 sCtx.cQEscape = azArg[++i][0];
30739 }else if( cli_strcmp(z,"-colsep")==0 ){
30740 if( i==nArg-1 ){
30741 dotCmdError(p, i, "missing argument", 0);
30742 return 1;
30743 }
@@ -33351,10 +33492,23 @@
33492 continue;
33493 }
33494 if( cli_strcmp(z,"once")==0 ){
33495 p->flgProgress |= SHELL_PROGRESS_ONCE;
33496 continue;
33497 }
33498 if( cli_strcmp(z,"timeout")==0 ){
33499 if( i==nArg-1 ){
33500 dotCmdError(p, i, "missing argument", 0);
33501 return 1;
33502 }
33503 i++;
33504 p->tmProgress = atof(azArg[i]);
33505 if( p->tmProgress>0.0 ){
33506 p->flgProgress = SHELL_PROGRESS_QUIET|SHELL_PROGRESS_TMOUT;
33507 if( nn==0 ) nn = 100;
33508 }
33509 continue;
33510 }
33511 if( cli_strcmp(z,"limit")==0 ){
33512 if( i+1>=nArg ){
33513 eputz("Error: missing argument on --limit\n");
33514 rc = 1;
@@ -34843,17 +34997,21 @@
34997 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
34998 }else
34999
35000 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
35001 if( nArg==2 ){
35002 if( cli_strcmp(azArg[1],"once")==0 ){
35003 p->enableTimer = 1;
35004 }else{
35005 p->enableTimer = 2*booleanValue(azArg[1]);
35006 }
35007 if( p->enableTimer && !HAS_TIMER ){
35008 eputz("Error: timer not available on this system.\n");
35009 p->enableTimer = 0;
35010 }
35011 }else{
35012 eputz("Usage: .timer on|off|once\n");
35013 rc = 1;
35014 }
35015 }else
35016
35017 #ifndef SQLITE_OMIT_TRACE
@@ -35024,10 +35182,11 @@
35182 if( p->nPopOutput ){
35183 p->nPopOutput--;
35184 if( p->nPopOutput==0 ) output_reset(p);
35185 }
35186 p->bSafeMode = p->bSafeModePersist;
35187 p->dot.nArg = 0;
35188 return rc;
35189 }
35190
35191 /* Line scan result and intermediate states (supporting scan resumption)
35192 */
@@ -35260,13 +35419,13 @@
35419 char *zErrMsg = 0;
35420
35421 open_db(p, 0);
35422 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
35423 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
35424 BEGIN_TIMER(p);
35425 rc = shell_exec(p, zSql, &zErrMsg);
35426 END_TIMER(p);
35427 if( rc || zErrMsg ){
35428 char zPrefix[100];
35429 const char *zErrorTail;
35430 const char *zErrorType;
35431 if( zErrMsg==0 ){
35432
+314 -99
--- 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
-** 4733d351ec2376291f093ba8d2ba71d82c6f with changes in files:
21
+** c476d956d0bd3065cf894de6f9d393b999ff with changes in files:
2222
**
2323
**
2424
*/
2525
#ifndef SQLITE_AMALGAMATION
2626
#define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467467
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468468
** [sqlite_version()] and [sqlite_source_id()].
469469
*/
470470
#define SQLITE_VERSION "3.52.0"
471471
#define SQLITE_VERSION_NUMBER 3052000
472
-#define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
472
+#define SQLITE_SOURCE_ID "2026-02-04 20:51:27 c476d956d0bd3065cf894de6f9d393b999ff7d2268a35f01a6d88804789ab58f"
473473
#define SQLITE_SCM_BRANCH "trunk"
474474
#define SQLITE_SCM_TAGS ""
475
-#define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
475
+#define SQLITE_SCM_DATETIME "2026-02-04T20:51:27.822Z"
476476
477477
/*
478478
** CAPI3REF: Run-Time Library Version Numbers
479479
** KEYWORDS: sqlite3_version sqlite3_sourceid
480480
**
@@ -11571,23 +11571,45 @@
1157111571
#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
1157211572
1157311573
/*
1157411574
** CAPI3REF: Bind array values to the CARRAY table-valued function
1157511575
**
11576
-** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to
11577
-** one of the first argument of the [carray() table-valued function]. The
11578
-** S parameter is a pointer to the [prepared statement] that uses the carray()
11579
-** functions. I is the parameter index to be bound. P is a pointer to the
11580
-** array to be bound, and N is the number of eements in the array. The
11581
-** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64],
11582
-** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to
11583
-** indicate the datatype of the array being bound. The X argument is not a
11584
-** NULL pointer, then SQLite will invoke the function X on the P parameter
11585
-** after it has finished using P, even if the call to
11586
-** sqlite3_carray_bind() fails. The special-case finalizer
11587
-** SQLITE_TRANSIENT has no effect here.
11576
+** The sqlite3_carray_bind_v2(S,I,P,N,F,X,D) interface binds an array value to
11577
+** parameter that is the first argument of the [carray() table-valued function].
11578
+** The S parameter is a pointer to the [prepared statement] that uses the carray()
11579
+** functions. I is the parameter index to be bound. I must be the index of the
11580
+** parameter that is the first argument to the carray() table-valued function.
11581
+** P is a pointer to the array to be bound, and N is the number of elements in
11582
+** the array. The F argument is one of constants [SQLITE_CARRAY_INT32],
11583
+** [SQLITE_CARRAY_INT64], [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT],
11584
+** or [SQLITE_CARRAY_BLOB] to indicate the datatype of the array P.
11585
+**
11586
+** If the X argument is not a NULL pointer or one of the special
11587
+** values [SQLITE_STATIC] or [SQLITE_TRANSIENT], then SQLite will invoke
11588
+** the function X with argument D when it is finished using the data in P.
11589
+** The call to X(D) is a destructor for the array P. The destructor X(D)
11590
+** is invoked even if the call to sqlite3_carray_bind() fails. If the X
11591
+** parameter is the special-case value [SQLITE_STATIC], then SQLite assumes
11592
+** that the data static and the destructor is never invoked. If the X
11593
+** parameter is the special-case value [SQLITE_TRANSIENT], then
11594
+** sqlite3_carray_bind_v2() makes its own private copy of the data prior
11595
+** to returning and never invokes the destructor X.
11596
+**
11597
+** The sqlite3_carray_bind() function works the same as sqlite_carray_bind_v2()
11598
+** with a D parameter set to P. In other words,
11599
+** sqlite3_carray_bind(S,I,P,N,F,X) is same as
11600
+** sqlite3_carray_bind(S,I,P,N,F,X,P).
1158811601
*/
11602
+SQLITE_API int sqlite3_carray_bind_v2(
11603
+ sqlite3_stmt *pStmt, /* Statement to be bound */
11604
+ int i, /* Parameter index */
11605
+ void *aData, /* Pointer to array data */
11606
+ int nData, /* Number of data elements */
11607
+ int mFlags, /* CARRAY flags */
11608
+ void (*xDel)(void*), /* Destructor for aData */
11609
+ void *pDel /* Optional argument to xDel() */
11610
+);
1158911611
SQLITE_API int sqlite3_carray_bind(
1159011612
sqlite3_stmt *pStmt, /* Statement to be bound */
1159111613
int i, /* Parameter index */
1159211614
void *aData, /* Pointer to array data */
1159311615
int nData, /* Number of data elements */
@@ -21094,10 +21116,11 @@
2109421116
u16 mWFlags; /* Use-dependent flags */
2109521117
union { /* Extra data for callback */
2109621118
NameContext *pNC; /* Naming context */
2109721119
int n; /* A counter */
2109821120
int iCur; /* A cursor number */
21121
+ int sz; /* String literal length */
2109921122
SrcList *pSrcList; /* FROM clause */
2110021123
struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
2110121124
struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
2110221125
int *aiCol; /* array of column indexes */
2110321126
struct IdxCover *pIdxCover; /* Check for index coverage */
@@ -21877,10 +21900,11 @@
2187721900
SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
2187821901
#endif
2187921902
SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*, Parse*);
2188021903
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
2188121904
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
21905
+SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr*);
2188221906
SQLITE_PRIVATE int sqlite3IsRowid(const char*);
2188321907
SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab);
2188421908
SQLITE_PRIVATE void sqlite3GenerateRowDelete(
2188521909
Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
2188621910
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
@@ -86688,10 +86712,15 @@
8668886712
** the column value into *ppVal. If *ppVal is initially NULL then a new
8668986713
** sqlite3_value object is allocated.
8669086714
**
8669186715
** If *ppVal is initially NULL then the caller is responsible for
8669286716
** ensuring that the value written into *ppVal is eventually freed.
86717
+**
86718
+** If the buffer does not contain a well-formed record, this routine may
86719
+** read several bytes past the end of the buffer. Callers must therefore
86720
+** ensure that any buffer which may contain a corrupt record is padded
86721
+** with at least 8 bytes of addressable memory.
8669386722
*/
8669486723
SQLITE_PRIVATE int sqlite3Stat4Column(
8669586724
sqlite3 *db, /* Database handle */
8669686725
const void *pRec, /* Pointer to buffer containing record */
8669786726
int nRec, /* Size of buffer pRec in bytes */
@@ -118827,11 +118856,14 @@
118827118856
if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
118828118857
}
118829118858
if( pIEpr==0 ) break;
118830118859
if( NEVER(!ExprUseYTab(pExpr)) ) break;
118831118860
for(i=0; i<pSrcList->nSrc; i++){
118832
- if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
118861
+ if( pSrcList->a[i].iCursor==pIEpr->iDataCur ){
118862
+ testcase( i>0 );
118863
+ break;
118864
+ }
118833118865
}
118834118866
if( i>=pSrcList->nSrc ) break;
118835118867
if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
118836118868
if( pParse->nErr ){ return WRC_Abort; }
118837118869
@@ -141374,10 +141406,11 @@
141374141406
int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
141375141407
/* Version 3.52.0 and later */
141376141408
void (*str_truncate)(sqlite3_str*,int);
141377141409
void (*str_free)(sqlite3_str*);
141378141410
int (*carray_bind)(sqlite3_stmt*,int,void*,int,int,void(*)(void*));
141411
+ int (*carray_bind_v2)(sqlite3_stmt*,int,void*,int,int,void(*)(void*),void*);
141379141412
};
141380141413
141381141414
/*
141382141415
** This is the function signature used for all extension entry points. It
141383141416
** is also defined in the file "loadext.c".
@@ -141716,10 +141749,11 @@
141716141749
#define sqlite3_db_status64 sqlite3_api->db_status64
141717141750
/* Version 3.52.0 and later */
141718141751
#define sqlite3_str_truncate sqlite3_api->str_truncate
141719141752
#define sqlite3_str_free sqlite3_api->str_free
141720141753
#define sqlite3_carray_bind sqlite3_api->carray_bind
141754
+#define sqlite3_carray_bind_v2 sqlite3_api->carray_bind_v2
141721141755
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
141722141756
141723141757
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
141724141758
/* This case when the file really is being compiled as a loadable
141725141759
** extension */
@@ -142247,12 +142281,14 @@
142247142281
sqlite3_db_status64,
142248142282
/* Version 3.52.0 and later */
142249142283
sqlite3_str_truncate,
142250142284
sqlite3_str_free,
142251142285
#ifdef SQLITE_ENABLE_CARRAY
142252
- sqlite3_carray_bind
142286
+ sqlite3_carray_bind,
142287
+ sqlite3_carray_bind_v2
142253142288
#else
142289
+ 0,
142254142290
0
142255142291
#endif
142256142292
};
142257142293
142258142294
/* True if x is the directory separator character
@@ -150108,11 +150144,11 @@
150108150144
** function is responsible for ensuring that this structure is eventually
150109150145
** freed.
150110150146
*/
150111150147
static KeyInfo *multiSelectByMergeKeyInfo(Parse *pParse, Select *p, int nExtra){
150112150148
ExprList *pOrderBy = p->pOrderBy;
150113
- int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
150149
+ int nOrderBy = (pOrderBy!=0) ? pOrderBy->nExpr : 0;
150114150150
sqlite3 *db = pParse->db;
150115150151
KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
150116150152
if( pRet ){
150117150153
int i;
150118150154
for(i=0; i<nOrderBy; i++){
@@ -150911,14 +150947,12 @@
150911150947
** EofB: ...
150912150948
** AltB: ...
150913150949
** AeqB: ...
150914150950
** AgtB: ...
150915150951
** Init: initialize coroutine registers
150916
-** yield coA
150917
-** if eof(A) goto EofA
150918
-** yield coB
150919
-** if eof(B) goto EofB
150952
+** yield coA, on eof goto EofA
150953
+** yield coB, on eof goto EofB
150920150954
** Cmpr: Compare A, B
150921150955
** Jump AltB, AeqB, AgtB
150922150956
** End: ...
150923150957
**
150924150958
** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
@@ -151006,30 +151040,33 @@
151006151040
}
151007151041
}
151008151042
}
151009151043
151010151044
/* Compute the comparison permutation and keyinfo that is used with
151011
- ** the permutation used to determine if the next
151012
- ** row of results comes from selectA or selectB. Also add explicit
151013
- ** collations to the ORDER BY clause terms so that when the subqueries
151014
- ** to the right and the left are evaluated, they use the correct
151015
- ** collation.
151045
+ ** the permutation to determine if the next row of results comes
151046
+ ** from selectA or selectB. Also add literal collations to the
151047
+ ** ORDER BY clause terms so that when selectA and selectB are
151048
+ ** evaluated, they use the correct collation.
151016151049
*/
151017151050
aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
151018151051
if( aPermute ){
151019151052
struct ExprList_item *pItem;
151053
+ int bKeep = 0;
151020151054
aPermute[0] = nOrderBy;
151021151055
for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
151022151056
assert( pItem!=0 );
151023151057
assert( pItem->u.x.iOrderByCol>0 );
151024151058
assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
151025151059
aPermute[i] = pItem->u.x.iOrderByCol - 1;
151060
+ if( aPermute[i]!=(u32)i-1 ) bKeep = 1;
151026151061
}
151027
- pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1);
151028
- }else{
151029
- pKeyMerge = 0;
151062
+ if( bKeep==0 ){
151063
+ sqlite3DbFreeNN(db, aPermute);
151064
+ aPermute = 0;
151065
+ }
151030151066
}
151067
+ pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1);
151031151068
151032151069
/* Allocate a range of temporary registers and the KeyInfo needed
151033151070
** for the logic that removes duplicate result rows when the
151034151071
** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
151035151072
*/
@@ -151104,11 +151141,11 @@
151104151141
/* Generate a coroutine to evaluate the SELECT statement to the
151105151142
** left of the compound operator - the "A" select.
151106151143
*/
151107151144
addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
151108151145
addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
151109
- VdbeComment((v, "left SELECT"));
151146
+ VdbeComment((v, "SUBR: next-A"));
151110151147
pPrior->iLimit = regLimitA;
151111151148
ExplainQueryPlan((pParse, 1, "LEFT"));
151112151149
sqlite3Select(pParse, pPrior, &destA);
151113151150
sqlite3VdbeEndCoroutine(v, regAddrA);
151114151151
sqlite3VdbeJumpHere(v, addr1);
@@ -151116,11 +151153,11 @@
151116151153
/* Generate a coroutine to evaluate the SELECT statement on
151117151154
** the right - the "B" select
151118151155
*/
151119151156
addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
151120151157
addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
151121
- VdbeComment((v, "right SELECT"));
151158
+ VdbeComment((v, "SUBR: next-B"));
151122151159
savedLimit = p->iLimit;
151123151160
savedOffset = p->iOffset;
151124151161
p->iLimit = regLimitB;
151125151162
p->iOffset = 0;
151126151163
ExplainQueryPlan((pParse, 1, "RIGHT"));
@@ -151130,20 +151167,20 @@
151130151167
sqlite3VdbeEndCoroutine(v, regAddrB);
151131151168
151132151169
/* Generate a subroutine that outputs the current row of the A
151133151170
** select as the next output row of the compound select.
151134151171
*/
151135
- VdbeNoopComment((v, "Output routine for A"));
151172
+ VdbeNoopComment((v, "SUBR: out-A"));
151136151173
addrOutA = generateOutputSubroutine(pParse,
151137151174
p, &destA, pDest, regOutA,
151138151175
regPrev, pKeyDup, labelEnd);
151139151176
151140151177
/* Generate a subroutine that outputs the current row of the B
151141151178
** select as the next output row of the compound select.
151142151179
*/
151143151180
if( op==TK_ALL || op==TK_UNION ){
151144
- VdbeNoopComment((v, "Output routine for B"));
151181
+ VdbeNoopComment((v, "SUBR: out-B"));
151145151182
addrOutB = generateOutputSubroutine(pParse,
151146151183
p, &destB, pDest, regOutB,
151147151184
regPrev, pKeyDup, labelEnd);
151148151185
}
151149151186
sqlite3KeyInfoUnref(pKeyDup);
@@ -151152,14 +151189,16 @@
151152151189
** are exhausted and only data in select B remains.
151153151190
*/
151154151191
if( op==TK_EXCEPT || op==TK_INTERSECT ){
151155151192
addrEofA_noB = addrEofA = labelEnd;
151156151193
}else{
151157
- VdbeNoopComment((v, "eof-A subroutine"));
151194
+ VdbeNoopComment((v, "SUBR: eof-A"));
151158151195
addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
151196
+ VdbeComment((v, "out-B"));
151159151197
addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
151160151198
VdbeCoverage(v);
151199
+ VdbeComment((v, "next-B"));
151161151200
sqlite3VdbeGoto(v, addrEofA);
151162151201
p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
151163151202
}
151164151203
151165151204
/* Generate a subroutine to run when the results from select B
@@ -151167,21 +151206,24 @@
151167151206
*/
151168151207
if( op==TK_INTERSECT ){
151169151208
addrEofB = addrEofA;
151170151209
if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
151171151210
}else{
151172
- VdbeNoopComment((v, "eof-B subroutine"));
151211
+ VdbeNoopComment((v, "SUBR: eof-B"));
151173151212
addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
151213
+ VdbeComment((v, "out-A"));
151174151214
sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
151215
+ VdbeComment((v, "next-A"));
151175151216
sqlite3VdbeGoto(v, addrEofB);
151176151217
}
151177151218
151178151219
/* Generate code to handle the case of A<B
151179151220
*/
151180
- VdbeNoopComment((v, "A-lt-B subroutine"));
151181151221
addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
151222
+ VdbeComment((v, "out-A"));
151182151223
sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
151224
+ VdbeComment((v, "next-A"));
151183151225
sqlite3VdbeGoto(v, labelCmpr);
151184151226
151185151227
/* Generate code to handle the case of A==B
151186151228
*/
151187151229
if( op==TK_ALL ){
@@ -151188,40 +151230,52 @@
151188151230
addrAeqB = addrAltB;
151189151231
}else if( op==TK_INTERSECT ){
151190151232
addrAeqB = addrAltB;
151191151233
addrAltB++;
151192151234
}else{
151193
- VdbeNoopComment((v, "A-eq-B subroutine"));
151194
- addrAeqB =
151195
- sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
151196
- sqlite3VdbeGoto(v, labelCmpr);
151235
+ addrAeqB = addrAltB + 1;
151197151236
}
151198151237
151199151238
/* Generate code to handle the case of A>B
151200151239
*/
151201
- VdbeNoopComment((v, "A-gt-B subroutine"));
151202151240
addrAgtB = sqlite3VdbeCurrentAddr(v);
151203151241
if( op==TK_ALL || op==TK_UNION ){
151204151242
sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
151243
+ VdbeComment((v, "out-B"));
151244
+ sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
151245
+ VdbeComment((v, "next-B"));
151246
+ sqlite3VdbeGoto(v, labelCmpr);
151247
+ }else{
151248
+ addrAgtB++; /* Just do next-B. Might as well use the next-B call
151249
+ ** in the next code block */
151205151250
}
151206
- sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
151207
- sqlite3VdbeGoto(v, labelCmpr);
151208151251
151209151252
/* This code runs once to initialize everything.
151210151253
*/
151211151254
sqlite3VdbeJumpHere(v, addr1);
151212151255
sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
151256
+ VdbeComment((v, "next-A"));
151257
+ /* v--- Also the A>B case for EXCEPT and INTERSECT */
151213151258
sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
151259
+ VdbeComment((v, "next-B"));
151214151260
151215151261
/* Implement the main merge loop
151216151262
*/
151263
+ if( aPermute!=0 ){
151264
+ sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
151265
+ }
151217151266
sqlite3VdbeResolveLabel(v, labelCmpr);
151218
- sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
151219151267
sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
151220151268
(char*)pKeyMerge, P4_KEYINFO);
151221
- sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
151222
- sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
151269
+ if( aPermute!=0 ){
151270
+ sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
151271
+ }
151272
+ sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
151273
+ VdbeCoverageIf(v, op==TK_ALL);
151274
+ VdbeCoverageIf(v, op==TK_UNION);
151275
+ VdbeCoverageIf(v, op==TK_EXCEPT);
151276
+ VdbeCoverageIf(v, op==TK_INTERSECT);
151223151277
151224151278
/* Jump to the this point in order to terminate the query.
151225151279
*/
151226151280
sqlite3VdbeResolveLabel(v, labelEnd);
151227151281
@@ -165697,10 +165751,38 @@
165697165751
sqlite3ValueFree(pVal);
165698165752
return rc;
165699165753
}
165700165754
#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
165701165755
165756
+/*
165757
+** If pExpr is one of "like", "glob", "match", or "regexp", then
165758
+** return the corresponding SQLITE_INDEX_CONSTRAINT_xxxx value.
165759
+** If not, return 0.
165760
+**
165761
+** pExpr is guaranteed to be a TK_FUNCTION.
165762
+*/
165763
+SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr *pExpr){
165764
+ static const struct {
165765
+ const char *zOp;
165766
+ unsigned char eOp;
165767
+ } aOp[] = {
165768
+ { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
165769
+ { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
165770
+ { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
165771
+ { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
165772
+ };
165773
+ int i;
165774
+ assert( pExpr->op==TK_FUNCTION );
165775
+ assert( !ExprHasProperty(pExpr, EP_IntValue) );
165776
+ for(i=0; i<ArraySize(aOp); i++){
165777
+ if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
165778
+ return aOp[i].eOp;
165779
+ }
165780
+ }
165781
+ return 0;
165782
+}
165783
+
165702165784
165703165785
#ifndef SQLITE_OMIT_VIRTUALTABLE
165704165786
/*
165705165787
** Check to see if the pExpr expression is a form that needs to be passed
165706165788
** to the xBestIndex method of virtual tables. Forms of interest include:
@@ -165733,19 +165815,10 @@
165733165815
unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
165734165816
Expr **ppLeft, /* Column expression to left of MATCH/op2 */
165735165817
Expr **ppRight /* Expression to left of MATCH/op2 */
165736165818
){
165737165819
if( pExpr->op==TK_FUNCTION ){
165738
- static const struct Op2 {
165739
- const char *zOp;
165740
- unsigned char eOp2;
165741
- } aOp[] = {
165742
- { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
165743
- { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
165744
- { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
165745
- { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
165746
- };
165747165820
ExprList *pList;
165748165821
Expr *pCol; /* Column reference */
165749165822
int i;
165750165823
165751165824
assert( ExprUseXList(pExpr) );
@@ -165761,20 +165834,15 @@
165761165834
** vtab_column MATCH expression
165762165835
** MATCH(expression,vtab_column)
165763165836
*/
165764165837
pCol = pList->a[1].pExpr;
165765165838
assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
165766
- if( ExprIsVtab(pCol) ){
165767
- for(i=0; i<ArraySize(aOp); i++){
165768
- assert( !ExprHasProperty(pExpr, EP_IntValue) );
165769
- if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
165770
- *peOp2 = aOp[i].eOp2;
165771
- *ppRight = pList->a[0].pExpr;
165772
- *ppLeft = pCol;
165773
- return 1;
165774
- }
165775
- }
165839
+ if( ExprIsVtab(pCol) && (i = sqlite3ExprIsLikeOperator(pExpr))!=0 ){
165840
+ *peOp2 = i;
165841
+ *ppRight = pList->a[0].pExpr;
165842
+ *ppLeft = pCol;
165843
+ return 1;
165776165844
}
165777165845
165778165846
/* We can also match against the first column of overloaded
165779165847
** functions where xFindFunction returns a value of at least
165780165848
** SQLITE_INDEX_CONSTRAINT_FUNCTION.
@@ -170218,10 +170286,71 @@
170218170286
p->u.btree.pIndex = 0;
170219170287
}
170220170288
}
170221170289
return rc;
170222170290
}
170291
+
170292
+/*
170293
+** Callback for estLikePatternLength().
170294
+**
170295
+** If this node is a string literal that is longer pWalker->sz, then set
170296
+** pWalker->sz to the byte length of that string literal.
170297
+**
170298
+** pWalker->eCode indicates how to count characters:
170299
+**
170300
+** eCode==0 Count as a GLOB pattern
170301
+** eCode==1 Count as a LIKE pattern
170302
+*/
170303
+static int exprNodePatternLengthEst(Walker *pWalker, Expr *pExpr){
170304
+ if( pExpr->op==TK_STRING ){
170305
+ int sz = 0; /* Pattern size in bytes */
170306
+ u8 *z = (u8*)pExpr->u.zToken; /* The pattern */
170307
+ u8 c; /* Next character of the pattern */
170308
+ u8 c1, c2, c3; /* Wildcards */
170309
+ if( pWalker->eCode ){
170310
+ c1 = '%';
170311
+ c2 = '_';
170312
+ c3 = 0;
170313
+ }else{
170314
+ c1 = '*';
170315
+ c2 = '?';
170316
+ c3 = '[';
170317
+ }
170318
+ while( (c = *(z++))!=0 ){
170319
+ if( c==c3 ){
170320
+ if( *z ) z++;
170321
+ while( *z && *z!=']' ) z++;
170322
+ }else if( c!=c1 && c!=c2 ){
170323
+ sz++;
170324
+ }
170325
+ }
170326
+ if( sz>pWalker->u.sz ) pWalker->u.sz = sz;
170327
+ }
170328
+ return WRC_Continue;
170329
+}
170330
+
170331
+/*
170332
+** Return the length of the longest string literal in the given
170333
+** expression.
170334
+**
170335
+** eCode indicates how to count characters:
170336
+**
170337
+** eCode==0 Count as a GLOB pattern
170338
+** eCode==1 Count as a LIKE pattern
170339
+*/
170340
+static int estLikePatternLength(Expr *p, u16 eCode){
170341
+ Walker w;
170342
+ w.u.sz = 0;
170343
+ w.eCode = eCode;
170344
+ w.xExprCallback = exprNodePatternLengthEst;
170345
+ w.xSelectCallback = sqlite3SelectWalkFail;
170346
+#ifdef SQLITE_DEBUG
170347
+ w.xSelectCallback2 = sqlite3SelectWalkAssert2;
170348
+#endif
170349
+ sqlite3WalkExpr(&w, p);
170350
+ return w.u.sz;
170351
+}
170223170352
170224170353
/*
170225170354
** Adjust the WhereLoop.nOut value downward to account for terms of the
170226170355
** WHERE clause that reference the loop but which are not used by an
170227170356
** index.
@@ -170247,10 +170376,17 @@
170247170376
** of rows in the table. In other words, assume that x==EXPR will filter
170248170377
** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
170249170378
** "x" column is boolean or else -1 or 0 or 1 is a common default value
170250170379
** on the "x" column and so in that case only cap the output row estimate
170251170380
** at 1/2 instead of 1/4.
170381
+**
170382
+** Heuristic 3: If there is a LIKE or GLOB (or REGEXP or MATCH) operator
170383
+** with a large constant pattern, then reduce the size of the search
170384
+** space according to the length of the pattern, under the theory that
170385
+** longer patterns are less likely to match. This heuristic was added
170386
+** to give better output-row count estimates when preparing queries for
170387
+** the Join-Order Benchmarks. See forum thread 2026-01-30T09:57:54z
170252170388
*/
170253170389
static void whereLoopOutputAdjust(
170254170390
WhereClause *pWC, /* The WHERE clause */
170255170391
WhereLoop *pLoop, /* The loop to adjust downward */
170256170392
LogEst nRow /* Number of rows in the entire table */
@@ -170296,25 +170432,43 @@
170296170432
** then use the probability provided by the application. */
170297170433
pLoop->nOut += pTerm->truthProb;
170298170434
}else{
170299170435
/* In the absence of explicit truth probabilities, use heuristics to
170300170436
** guess a reasonable truth probability. */
170437
+ Expr *pOpExpr = pTerm->pExpr;
170301170438
pLoop->nOut--;
170302170439
if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
170303170440
&& (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
170304170441
){
170305
- Expr *pRight = pTerm->pExpr->pRight;
170442
+ Expr *pRight = pOpExpr->pRight;
170306170443
int k = 0;
170307
- testcase( pTerm->pExpr->op==TK_IS );
170444
+ testcase( pOpExpr->op==TK_IS );
170308170445
if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){
170309170446
k = 10;
170310170447
}else{
170311170448
k = 20;
170312170449
}
170313170450
if( iReduce<k ){
170314170451
pTerm->wtFlags |= TERM_HEURTRUTH;
170315170452
iReduce = k;
170453
+ }
170454
+ }else
170455
+ if( ExprHasProperty(pOpExpr, EP_InfixFunc)
170456
+ && pOpExpr->op==TK_FUNCTION
170457
+ ){
170458
+ int eOp;
170459
+ assert( ExprUseXList(pOpExpr) );
170460
+ assert( pOpExpr->x.pList->nExpr>=2 );
170461
+ eOp = sqlite3ExprIsLikeOperator(pOpExpr);
170462
+ if( ALWAYS(eOp>0) ){
170463
+ int szPattern;
170464
+ Expr *pRHS = pOpExpr->x.pList->a[0].pExpr;
170465
+ eOp = eOp==SQLITE_INDEX_CONSTRAINT_LIKE;
170466
+ szPattern = estLikePatternLength(pRHS, eOp);
170467
+ if( szPattern>0 ){
170468
+ pLoop->nOut -= szPattern*2;
170469
+ }
170316170470
}
170317170471
}
170318170472
}
170319170473
}
170320170474
}
@@ -172136,11 +172290,11 @@
172136172290
SrcItem *pItem;
172137172291
SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
172138172292
sqlite3 *db = pWInfo->pParse->db;
172139172293
int rc = SQLITE_OK;
172140172294
int bFirstPastRJ = 0;
172141
- int hasRightJoin = 0;
172295
+ int hasRightCrossJoin = 0;
172142172296
WhereLoop *pNew;
172143172297
172144172298
172145172299
/* Loop over the tables in the join, from left to right */
172146172300
pNew = pBuilder->pNew;
@@ -172163,16 +172317,24 @@
172163172317
/* Add prerequisites to prevent reordering of FROM clause terms
172164172318
** across CROSS joins and outer joins. The bFirstPastRJ boolean
172165172319
** prevents the right operand of a RIGHT JOIN from being swapped with
172166172320
** other elements even further to the right.
172167172321
**
172168
- ** The JT_LTORJ case and the hasRightJoin flag work together to
172169
- ** prevent FROM-clause terms from moving from the right side of
172170
- ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
172171
- ** is itself on the left side of a RIGHT JOIN.
172322
+ ** The hasRightCrossJoin flag prevent FROM-clause terms from moving
172323
+ ** from the right side of a LEFT JOIN or CROSS JOIN over to the
172324
+ ** left side of that same join. This is a required restriction in
172325
+ ** the case of LEFT JOIN - an incorrect answer may results if it is
172326
+ ** not enforced. This restriction is not required for CROSS JOIN.
172327
+ ** It is provided merely as a means of controlling join order, under
172328
+ ** the theory that no real-world queries that care about performance
172329
+ ** actually use the CROSS JOIN syntax.
172172172330
*/
172173
- if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
172331
+ if( pItem->fg.jointype & (JT_LTORJ|JT_CROSS) ){
172332
+ testcase( pItem->fg.jointype & JT_LTORJ );
172333
+ testcase( pItem->fg.jointype & JT_CROSS );
172334
+ hasRightCrossJoin = 1;
172335
+ }
172174172336
mPrereq |= mPrior;
172175172337
bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
172176172338
}else if( pItem->fg.fromExists ){
172177172339
/* joins that result from the EXISTS-to-JOIN optimization should not
172178172340
** be moved to the left of any of their dependencies */
@@ -172182,11 +172344,11 @@
172182172344
for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
172183172345
if( (pNew->maskSelf & pTerm->prereqAll)!=0 ){
172184172346
mPrereq |= (pTerm->prereqAll & (pNew->maskSelf-1));
172185172347
}
172186172348
}
172187
- }else if( !hasRightJoin ){
172349
+ }else if( !hasRightCrossJoin ){
172188172350
mPrereq = 0;
172189172351
}
172190172352
#ifndef SQLITE_OMIT_VIRTUALTABLE
172191172353
if( IsVirtual(pItem->pSTab) ){
172192172354
SrcItem *p;
@@ -214034,32 +214196,36 @@
214034214196
jsonParseReset(&v);
214035214197
jsonParseReset(&ix);
214036214198
return rc;
214037214199
}
214038214200
}else if( zPath[0]=='[' ){
214201
+ u64 kk = 0;
214039214202
x = pParse->aBlob[iRoot] & 0x0f;
214040214203
if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND;
214041214204
n = jsonbPayloadSize(pParse, iRoot, &sz);
214042
- k = 0;
214043214205
i = 1;
214044214206
while( sqlite3Isdigit(zPath[i]) ){
214045
- k = k*10 + zPath[i] - '0';
214207
+ if( kk<0xffffffff ) kk = kk*10 + zPath[i] - '0';
214208
+ /* ^^^^^^^^^^--- Allow kk to be bigger than any JSON array so that
214209
+ ** we get NOTFOUND instead of PATHERROR, without overflowing kk. */
214046214210
i++;
214047214211
}
214048214212
if( i<2 || zPath[i]!=']' ){
214049214213
if( zPath[1]=='#' ){
214050
- k = jsonbArrayCount(pParse, iRoot);
214214
+ kk = jsonbArrayCount(pParse, iRoot);
214051214215
i = 2;
214052214216
if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
214053
- unsigned int nn = 0;
214217
+ u64 nn = 0;
214054214218
i = 3;
214055214219
do{
214056
- nn = nn*10 + zPath[i] - '0';
214220
+ if( nn<0xffffffff ) nn = nn*10 + zPath[i] - '0';
214221
+ /* ^^^^^^^^^^--- Allow nn to be bigger than any JSON array to
214222
+ ** get NOTFOUND instead of PATHERROR, without overflowing nn. */
214057214223
i++;
214058214224
}while( sqlite3Isdigit(zPath[i]) );
214059
- if( nn>k ) return JSON_LOOKUP_NOTFOUND;
214060
- k -= nn;
214225
+ if( nn>kk ) return JSON_LOOKUP_NOTFOUND;
214226
+ kk -= nn;
214061214227
}
214062214228
if( zPath[i]!=']' ){
214063214229
return JSON_LOOKUP_PATHERROR;
214064214230
}
214065214231
}else{
@@ -214067,22 +214233,22 @@
214067214233
}
214068214234
}
214069214235
j = iRoot+n;
214070214236
iEnd = j+sz;
214071214237
while( j<iEnd ){
214072
- if( k==0 ){
214238
+ if( kk==0 ){
214073214239
rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
214074214240
if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
214075214241
return rc;
214076214242
}
214077
- k--;
214243
+ kk--;
214078214244
n = jsonbPayloadSize(pParse, j, &sz);
214079214245
if( n==0 ) return JSON_LOOKUP_ERROR;
214080214246
j += n+sz;
214081214247
}
214082214248
if( j>iEnd ) return JSON_LOOKUP_ERROR;
214083
- if( k>0 ) return JSON_LOOKUP_NOTFOUND;
214249
+ if( kk>0 ) return JSON_LOOKUP_NOTFOUND;
214084214250
if( pParse->eEdit>=JEDIT_INS ){
214085214251
JsonParse v;
214086214252
testcase( pParse->eEdit==JEDIT_INS );
214087214253
testcase( pParse->eEdit==JEDIT_AINS );
214088214254
testcase( pParse->eEdit==JEDIT_SET );
@@ -231358,10 +231524,11 @@
231358231524
struct carray_bind {
231359231525
void *aData; /* The data */
231360231526
int nData; /* Number of elements */
231361231527
int mFlags; /* Control flags */
231362231528
void (*xDel)(void*); /* Destructor for aData */
231529
+ void *pDel; /* Alternative argument to xDel() */
231363231530
};
231364231531
231365231532
231366231533
/* carray_cursor is a subclass of sqlite3_vtab_cursor which will
231367231534
** serve as the underlying representation of a cursor that scans
@@ -231690,26 +231857,38 @@
231690231857
** Destructor for the carray_bind object
231691231858
*/
231692231859
static void carrayBindDel(void *pPtr){
231693231860
carray_bind *p = (carray_bind*)pPtr;
231694231861
if( p->xDel!=SQLITE_STATIC ){
231695
- p->xDel(p->aData);
231862
+ p->xDel(p->pDel);
231696231863
}
231697231864
sqlite3_free(p);
231698231865
}
231699231866
231700231867
/*
231701231868
** Invoke this interface in order to bind to the single-argument
231702231869
** version of CARRAY().
231870
+**
231871
+** pStmt The prepared statement to which to bind
231872
+** idx The index of the parameter of pStmt to which to bind
231873
+** aData The data to be bound
231874
+** nData The number of elements in aData
231875
+** mFlags One of SQLITE_CARRAY_xxxx indicating datatype of aData
231876
+** xDestroy Destructor for pDestroy or aData if pDestroy==NULL.
231877
+** pDestroy Invoke xDestroy on this pointer if not NULL
231878
+**
231879
+** The destructor is called pDestroy if pDestroy!=NULL, or against
231880
+** aData if pDestroy==NULL.
231703231881
*/
231704
-SQLITE_API int sqlite3_carray_bind(
231882
+SQLITE_API int sqlite3_carray_bind_v2(
231705231883
sqlite3_stmt *pStmt,
231706231884
int idx,
231707231885
void *aData,
231708231886
int nData,
231709231887
int mFlags,
231710
- void (*xDestroy)(void*)
231888
+ void (*xDestroy)(void*),
231889
+ void *pDestroy
231711231890
){
231712231891
carray_bind *pNew = 0;
231713231892
int i;
231714231893
int rc = SQLITE_OK;
231715231894
@@ -231782,23 +231961,41 @@
231782231961
}
231783231962
}else{
231784231963
memcpy(pNew->aData, aData, sz);
231785231964
}
231786231965
pNew->xDel = sqlite3_free;
231966
+ pNew->pDel = pNew->aData;
231787231967
}else{
231788231968
pNew->aData = aData;
231789231969
pNew->xDel = xDestroy;
231970
+ pNew->pDel = pDestroy;
231790231971
}
231791231972
return sqlite3_bind_pointer(pStmt, idx, pNew, "carray-bind", carrayBindDel);
231792231973
231793231974
carray_bind_error:
231794231975
if( xDestroy!=SQLITE_STATIC && xDestroy!=SQLITE_TRANSIENT ){
231795
- xDestroy(aData);
231976
+ xDestroy(pDestroy);
231796231977
}
231797231978
sqlite3_free(pNew);
231798231979
return rc;
231799231980
}
231981
+
231982
+/*
231983
+** Invoke this interface in order to bind to the single-argument
231984
+** version of CARRAY(). Same as sqlite3_carray_bind_v2() with the
231985
+** pDestroy parameter set to NULL.
231986
+*/
231987
+SQLITE_API int sqlite3_carray_bind(
231988
+ sqlite3_stmt *pStmt,
231989
+ int idx,
231990
+ void *aData,
231991
+ int nData,
231992
+ int mFlags,
231993
+ void (*xDestroy)(void*)
231994
+){
231995
+ return sqlite3_carray_bind_v2(pStmt,idx,aData,nData,mFlags,xDestroy,aData);
231996
+}
231800231997
231801231998
/*
231802231999
** Invoke this routine to register the carray() function.
231803232000
*/
231804232001
SQLITE_PRIVATE Module *sqlite3CarrayRegister(sqlite3 *db){
@@ -232157,10 +232354,24 @@
232157232354
** bytes read.
232158232355
*/
232159232356
static int sessionVarintGet(const u8 *aBuf, int *piVal){
232160232357
return getVarint32(aBuf, *piVal);
232161232358
}
232359
+
232360
+/*
232361
+** Read a varint value from buffer aBuf[], size nBuf bytes, into *piVal.
232362
+** Return the number of bytes read.
232363
+*/
232364
+static int sessionVarintGetSafe(const u8 *aBuf, int nBuf, int *piVal){
232365
+ u8 aCopy[5];
232366
+ const u8 *aRead = aBuf;
232367
+ if( nBuf<5 ){
232368
+ memcpy(aCopy, aBuf, nBuf);
232369
+ aRead = aCopy;
232370
+ }
232371
+ return getVarint32(aRead, *piVal);
232372
+}
232162232373
232163232374
/* Load an unaligned and unsigned 32-bit integer */
232164232375
#define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
232165232376
232166232377
/*
@@ -235370,11 +235581,12 @@
235370235581
235371235582
if( rc==SQLITE_OK ){
235372235583
u8 *aVal = &pIn->aData[pIn->iNext];
235373235584
if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
235374235585
int nByte;
235375
- pIn->iNext += sessionVarintGet(aVal, &nByte);
235586
+ int nRem = pIn->nData - pIn->iNext;
235587
+ pIn->iNext += sessionVarintGetSafe(aVal, nRem, &nByte);
235376235588
rc = sessionInputBuffer(pIn, nByte);
235377235589
if( rc==SQLITE_OK ){
235378235590
if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
235379235591
rc = SQLITE_CORRUPT_BKPT;
235380235592
}else{
@@ -235423,11 +235635,12 @@
235423235635
int nCol = 0;
235424235636
int nRead = 0;
235425235637
235426235638
rc = sessionInputBuffer(pIn, 9);
235427235639
if( rc==SQLITE_OK ){
235428
- nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
235640
+ int nBuf = pIn->nData - pIn->iNext;
235641
+ nRead += sessionVarintGetSafe(&pIn->aData[pIn->iNext], nBuf, &nCol);
235429235642
/* The hard upper limit for the number of columns in an SQLite
235430235643
** database table is, according to sqliteLimit.h, 32676. So
235431235644
** consider any table-header that purports to have more than 65536
235432235645
** columns to be corrupt. This is convenient because otherwise,
235433235646
** if the (nCol>65536) condition below were omitted, a sufficiently
@@ -235583,14 +235796,14 @@
235583235796
sqlite3ValueFree(p->apValue[i]);
235584235797
}
235585235798
memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
235586235799
}
235587235800
235588
- /* Make sure the buffer contains at least 10 bytes of input data, or all
235589
- ** remaining data if there are less than 10 bytes available. This is
235590
- ** sufficient either for the 'T' or 'P' byte and the varint that follows
235591
- ** it, or for the two single byte values otherwise. */
235801
+ /* Make sure the buffer contains at least 2 bytes of input data, or all
235802
+ ** remaining data if there are less than 2 bytes available. This is
235803
+ ** sufficient either for the 'T' or 'P' byte that begins a new table,
235804
+ ** or for the "op" and "bIndirect" single bytes otherwise. */
235592235805
p->rc = sessionInputBuffer(&p->in, 2);
235593235806
if( p->rc!=SQLITE_OK ) return p->rc;
235594235807
235595235808
p->in.iCurrent = p->in.iNext;
235596235809
sessionDiscardData(&p->in);
@@ -235616,15 +235829,17 @@
235616235829
** corrupt changeset. */
235617235830
assert( p->in.iNext==1 || p->zTab );
235618235831
return (p->rc = SQLITE_CORRUPT_BKPT);
235619235832
}
235620235833
235621
- p->op = op;
235622
- p->bIndirect = p->in.aData[p->in.iNext++];
235623
- if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
235834
+ if( (op!=SQLITE_UPDATE && op!=SQLITE_DELETE && op!=SQLITE_INSERT)
235835
+ || (p->in.iNext>=p->in.nData)
235836
+ ){
235624235837
return (p->rc = SQLITE_CORRUPT_BKPT);
235625235838
}
235839
+ p->op = op;
235840
+ p->bIndirect = p->in.aData[p->in.iNext++];
235626235841
235627235842
if( paRec ){
235628235843
int nVal; /* Number of values to buffer */
235629235844
if( p->bPatchset==0 && op==SQLITE_UPDATE ){
235630235845
nVal = p->nCol * 2;
@@ -261258,11 +261473,11 @@
261258261473
int nArg, /* Number of args */
261259261474
sqlite3_value **apUnused /* Function arguments */
261260261475
){
261261261476
assert( nArg==0 );
261262261477
UNUSED_PARAM2(nArg, apUnused);
261263
- sqlite3_result_text(pCtx, "fts5: 2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71", -1, SQLITE_TRANSIENT);
261478
+ sqlite3_result_text(pCtx, "fts5: 2026-02-04 18:10:49 e6902937ecdbeb449986469859b46631272fb0a9e7e1c31adea14cff072b6d67", -1, SQLITE_TRANSIENT);
261264261479
}
261265261480
261266261481
/*
261267261482
** Implementation of fts5_locale(LOCALE, TEXT) function.
261268261483
**
261269261484
--- 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 ** 4733d351ec2376291f093ba8d2ba71d82c6f with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 #define SQLITE_VERSION "3.52.0"
471 #define SQLITE_VERSION_NUMBER 3052000
472 #define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
473 #define SQLITE_SCM_BRANCH "trunk"
474 #define SQLITE_SCM_TAGS ""
475 #define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
476
477 /*
478 ** CAPI3REF: Run-Time Library Version Numbers
479 ** KEYWORDS: sqlite3_version sqlite3_sourceid
480 **
@@ -11571,23 +11571,45 @@
11571 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11572
11573 /*
11574 ** CAPI3REF: Bind array values to the CARRAY table-valued function
11575 **
11576 ** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to
11577 ** one of the first argument of the [carray() table-valued function]. The
11578 ** S parameter is a pointer to the [prepared statement] that uses the carray()
11579 ** functions. I is the parameter index to be bound. P is a pointer to the
11580 ** array to be bound, and N is the number of eements in the array. The
11581 ** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64],
11582 ** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to
11583 ** indicate the datatype of the array being bound. The X argument is not a
11584 ** NULL pointer, then SQLite will invoke the function X on the P parameter
11585 ** after it has finished using P, even if the call to
11586 ** sqlite3_carray_bind() fails. The special-case finalizer
11587 ** SQLITE_TRANSIENT has no effect here.
 
 
 
 
 
 
 
 
 
 
 
 
 
11588 */
 
 
 
 
 
 
 
 
 
11589 SQLITE_API int sqlite3_carray_bind(
11590 sqlite3_stmt *pStmt, /* Statement to be bound */
11591 int i, /* Parameter index */
11592 void *aData, /* Pointer to array data */
11593 int nData, /* Number of data elements */
@@ -21094,10 +21116,11 @@
21094 u16 mWFlags; /* Use-dependent flags */
21095 union { /* Extra data for callback */
21096 NameContext *pNC; /* Naming context */
21097 int n; /* A counter */
21098 int iCur; /* A cursor number */
 
21099 SrcList *pSrcList; /* FROM clause */
21100 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
21101 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
21102 int *aiCol; /* array of column indexes */
21103 struct IdxCover *pIdxCover; /* Check for index coverage */
@@ -21877,10 +21900,11 @@
21877 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
21878 #endif
21879 SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*, Parse*);
21880 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
21881 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
 
21882 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
21883 SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab);
21884 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
21885 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
21886 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
@@ -86688,10 +86712,15 @@
86688 ** the column value into *ppVal. If *ppVal is initially NULL then a new
86689 ** sqlite3_value object is allocated.
86690 **
86691 ** If *ppVal is initially NULL then the caller is responsible for
86692 ** ensuring that the value written into *ppVal is eventually freed.
 
 
 
 
 
86693 */
86694 SQLITE_PRIVATE int sqlite3Stat4Column(
86695 sqlite3 *db, /* Database handle */
86696 const void *pRec, /* Pointer to buffer containing record */
86697 int nRec, /* Size of buffer pRec in bytes */
@@ -118827,11 +118856,14 @@
118827 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
118828 }
118829 if( pIEpr==0 ) break;
118830 if( NEVER(!ExprUseYTab(pExpr)) ) break;
118831 for(i=0; i<pSrcList->nSrc; i++){
118832 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
 
 
 
118833 }
118834 if( i>=pSrcList->nSrc ) break;
118835 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
118836 if( pParse->nErr ){ return WRC_Abort; }
118837
@@ -141374,10 +141406,11 @@
141374 int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
141375 /* Version 3.52.0 and later */
141376 void (*str_truncate)(sqlite3_str*,int);
141377 void (*str_free)(sqlite3_str*);
141378 int (*carray_bind)(sqlite3_stmt*,int,void*,int,int,void(*)(void*));
 
141379 };
141380
141381 /*
141382 ** This is the function signature used for all extension entry points. It
141383 ** is also defined in the file "loadext.c".
@@ -141716,10 +141749,11 @@
141716 #define sqlite3_db_status64 sqlite3_api->db_status64
141717 /* Version 3.52.0 and later */
141718 #define sqlite3_str_truncate sqlite3_api->str_truncate
141719 #define sqlite3_str_free sqlite3_api->str_free
141720 #define sqlite3_carray_bind sqlite3_api->carray_bind
 
141721 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
141722
141723 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
141724 /* This case when the file really is being compiled as a loadable
141725 ** extension */
@@ -142247,12 +142281,14 @@
142247 sqlite3_db_status64,
142248 /* Version 3.52.0 and later */
142249 sqlite3_str_truncate,
142250 sqlite3_str_free,
142251 #ifdef SQLITE_ENABLE_CARRAY
142252 sqlite3_carray_bind
 
142253 #else
 
142254 0
142255 #endif
142256 };
142257
142258 /* True if x is the directory separator character
@@ -150108,11 +150144,11 @@
150108 ** function is responsible for ensuring that this structure is eventually
150109 ** freed.
150110 */
150111 static KeyInfo *multiSelectByMergeKeyInfo(Parse *pParse, Select *p, int nExtra){
150112 ExprList *pOrderBy = p->pOrderBy;
150113 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
150114 sqlite3 *db = pParse->db;
150115 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
150116 if( pRet ){
150117 int i;
150118 for(i=0; i<nOrderBy; i++){
@@ -150911,14 +150947,12 @@
150911 ** EofB: ...
150912 ** AltB: ...
150913 ** AeqB: ...
150914 ** AgtB: ...
150915 ** Init: initialize coroutine registers
150916 ** yield coA
150917 ** if eof(A) goto EofA
150918 ** yield coB
150919 ** if eof(B) goto EofB
150920 ** Cmpr: Compare A, B
150921 ** Jump AltB, AeqB, AgtB
150922 ** End: ...
150923 **
150924 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
@@ -151006,30 +151040,33 @@
151006 }
151007 }
151008 }
151009
151010 /* Compute the comparison permutation and keyinfo that is used with
151011 ** the permutation used to determine if the next
151012 ** row of results comes from selectA or selectB. Also add explicit
151013 ** collations to the ORDER BY clause terms so that when the subqueries
151014 ** to the right and the left are evaluated, they use the correct
151015 ** collation.
151016 */
151017 aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
151018 if( aPermute ){
151019 struct ExprList_item *pItem;
 
151020 aPermute[0] = nOrderBy;
151021 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
151022 assert( pItem!=0 );
151023 assert( pItem->u.x.iOrderByCol>0 );
151024 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
151025 aPermute[i] = pItem->u.x.iOrderByCol - 1;
 
151026 }
151027 pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1);
151028 }else{
151029 pKeyMerge = 0;
 
151030 }
 
151031
151032 /* Allocate a range of temporary registers and the KeyInfo needed
151033 ** for the logic that removes duplicate result rows when the
151034 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
151035 */
@@ -151104,11 +151141,11 @@
151104 /* Generate a coroutine to evaluate the SELECT statement to the
151105 ** left of the compound operator - the "A" select.
151106 */
151107 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
151108 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
151109 VdbeComment((v, "left SELECT"));
151110 pPrior->iLimit = regLimitA;
151111 ExplainQueryPlan((pParse, 1, "LEFT"));
151112 sqlite3Select(pParse, pPrior, &destA);
151113 sqlite3VdbeEndCoroutine(v, regAddrA);
151114 sqlite3VdbeJumpHere(v, addr1);
@@ -151116,11 +151153,11 @@
151116 /* Generate a coroutine to evaluate the SELECT statement on
151117 ** the right - the "B" select
151118 */
151119 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
151120 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
151121 VdbeComment((v, "right SELECT"));
151122 savedLimit = p->iLimit;
151123 savedOffset = p->iOffset;
151124 p->iLimit = regLimitB;
151125 p->iOffset = 0;
151126 ExplainQueryPlan((pParse, 1, "RIGHT"));
@@ -151130,20 +151167,20 @@
151130 sqlite3VdbeEndCoroutine(v, regAddrB);
151131
151132 /* Generate a subroutine that outputs the current row of the A
151133 ** select as the next output row of the compound select.
151134 */
151135 VdbeNoopComment((v, "Output routine for A"));
151136 addrOutA = generateOutputSubroutine(pParse,
151137 p, &destA, pDest, regOutA,
151138 regPrev, pKeyDup, labelEnd);
151139
151140 /* Generate a subroutine that outputs the current row of the B
151141 ** select as the next output row of the compound select.
151142 */
151143 if( op==TK_ALL || op==TK_UNION ){
151144 VdbeNoopComment((v, "Output routine for B"));
151145 addrOutB = generateOutputSubroutine(pParse,
151146 p, &destB, pDest, regOutB,
151147 regPrev, pKeyDup, labelEnd);
151148 }
151149 sqlite3KeyInfoUnref(pKeyDup);
@@ -151152,14 +151189,16 @@
151152 ** are exhausted and only data in select B remains.
151153 */
151154 if( op==TK_EXCEPT || op==TK_INTERSECT ){
151155 addrEofA_noB = addrEofA = labelEnd;
151156 }else{
151157 VdbeNoopComment((v, "eof-A subroutine"));
151158 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 
151159 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
151160 VdbeCoverage(v);
 
151161 sqlite3VdbeGoto(v, addrEofA);
151162 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
151163 }
151164
151165 /* Generate a subroutine to run when the results from select B
@@ -151167,21 +151206,24 @@
151167 */
151168 if( op==TK_INTERSECT ){
151169 addrEofB = addrEofA;
151170 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
151171 }else{
151172 VdbeNoopComment((v, "eof-B subroutine"));
151173 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 
151174 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
 
151175 sqlite3VdbeGoto(v, addrEofB);
151176 }
151177
151178 /* Generate code to handle the case of A<B
151179 */
151180 VdbeNoopComment((v, "A-lt-B subroutine"));
151181 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
 
151182 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
 
151183 sqlite3VdbeGoto(v, labelCmpr);
151184
151185 /* Generate code to handle the case of A==B
151186 */
151187 if( op==TK_ALL ){
@@ -151188,40 +151230,52 @@
151188 addrAeqB = addrAltB;
151189 }else if( op==TK_INTERSECT ){
151190 addrAeqB = addrAltB;
151191 addrAltB++;
151192 }else{
151193 VdbeNoopComment((v, "A-eq-B subroutine"));
151194 addrAeqB =
151195 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
151196 sqlite3VdbeGoto(v, labelCmpr);
151197 }
151198
151199 /* Generate code to handle the case of A>B
151200 */
151201 VdbeNoopComment((v, "A-gt-B subroutine"));
151202 addrAgtB = sqlite3VdbeCurrentAddr(v);
151203 if( op==TK_ALL || op==TK_UNION ){
151204 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
 
 
 
 
 
 
 
151205 }
151206 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
151207 sqlite3VdbeGoto(v, labelCmpr);
151208
151209 /* This code runs once to initialize everything.
151210 */
151211 sqlite3VdbeJumpHere(v, addr1);
151212 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
 
 
151213 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
 
151214
151215 /* Implement the main merge loop
151216 */
 
 
 
151217 sqlite3VdbeResolveLabel(v, labelCmpr);
151218 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
151219 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
151220 (char*)pKeyMerge, P4_KEYINFO);
151221 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
151222 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
 
 
 
 
 
 
151223
151224 /* Jump to the this point in order to terminate the query.
151225 */
151226 sqlite3VdbeResolveLabel(v, labelEnd);
151227
@@ -165697,10 +165751,38 @@
165697 sqlite3ValueFree(pVal);
165698 return rc;
165699 }
165700 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
165701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165702
165703 #ifndef SQLITE_OMIT_VIRTUALTABLE
165704 /*
165705 ** Check to see if the pExpr expression is a form that needs to be passed
165706 ** to the xBestIndex method of virtual tables. Forms of interest include:
@@ -165733,19 +165815,10 @@
165733 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
165734 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
165735 Expr **ppRight /* Expression to left of MATCH/op2 */
165736 ){
165737 if( pExpr->op==TK_FUNCTION ){
165738 static const struct Op2 {
165739 const char *zOp;
165740 unsigned char eOp2;
165741 } aOp[] = {
165742 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
165743 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
165744 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
165745 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
165746 };
165747 ExprList *pList;
165748 Expr *pCol; /* Column reference */
165749 int i;
165750
165751 assert( ExprUseXList(pExpr) );
@@ -165761,20 +165834,15 @@
165761 ** vtab_column MATCH expression
165762 ** MATCH(expression,vtab_column)
165763 */
165764 pCol = pList->a[1].pExpr;
165765 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
165766 if( ExprIsVtab(pCol) ){
165767 for(i=0; i<ArraySize(aOp); i++){
165768 assert( !ExprHasProperty(pExpr, EP_IntValue) );
165769 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
165770 *peOp2 = aOp[i].eOp2;
165771 *ppRight = pList->a[0].pExpr;
165772 *ppLeft = pCol;
165773 return 1;
165774 }
165775 }
165776 }
165777
165778 /* We can also match against the first column of overloaded
165779 ** functions where xFindFunction returns a value of at least
165780 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
@@ -170218,10 +170286,71 @@
170218 p->u.btree.pIndex = 0;
170219 }
170220 }
170221 return rc;
170222 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170223
170224 /*
170225 ** Adjust the WhereLoop.nOut value downward to account for terms of the
170226 ** WHERE clause that reference the loop but which are not used by an
170227 ** index.
@@ -170247,10 +170376,17 @@
170247 ** of rows in the table. In other words, assume that x==EXPR will filter
170248 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
170249 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
170250 ** on the "x" column and so in that case only cap the output row estimate
170251 ** at 1/2 instead of 1/4.
 
 
 
 
 
 
 
170252 */
170253 static void whereLoopOutputAdjust(
170254 WhereClause *pWC, /* The WHERE clause */
170255 WhereLoop *pLoop, /* The loop to adjust downward */
170256 LogEst nRow /* Number of rows in the entire table */
@@ -170296,25 +170432,43 @@
170296 ** then use the probability provided by the application. */
170297 pLoop->nOut += pTerm->truthProb;
170298 }else{
170299 /* In the absence of explicit truth probabilities, use heuristics to
170300 ** guess a reasonable truth probability. */
 
170301 pLoop->nOut--;
170302 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
170303 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
170304 ){
170305 Expr *pRight = pTerm->pExpr->pRight;
170306 int k = 0;
170307 testcase( pTerm->pExpr->op==TK_IS );
170308 if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){
170309 k = 10;
170310 }else{
170311 k = 20;
170312 }
170313 if( iReduce<k ){
170314 pTerm->wtFlags |= TERM_HEURTRUTH;
170315 iReduce = k;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170316 }
170317 }
170318 }
170319 }
170320 }
@@ -172136,11 +172290,11 @@
172136 SrcItem *pItem;
172137 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
172138 sqlite3 *db = pWInfo->pParse->db;
172139 int rc = SQLITE_OK;
172140 int bFirstPastRJ = 0;
172141 int hasRightJoin = 0;
172142 WhereLoop *pNew;
172143
172144
172145 /* Loop over the tables in the join, from left to right */
172146 pNew = pBuilder->pNew;
@@ -172163,16 +172317,24 @@
172163 /* Add prerequisites to prevent reordering of FROM clause terms
172164 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
172165 ** prevents the right operand of a RIGHT JOIN from being swapped with
172166 ** other elements even further to the right.
172167 **
172168 ** The JT_LTORJ case and the hasRightJoin flag work together to
172169 ** prevent FROM-clause terms from moving from the right side of
172170 ** a LEFT JOIN over to the left side of that join if the LEFT JOIN
172171 ** is itself on the left side of a RIGHT JOIN.
 
 
 
 
172172 */
172173 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
 
 
 
 
172174 mPrereq |= mPrior;
172175 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
172176 }else if( pItem->fg.fromExists ){
172177 /* joins that result from the EXISTS-to-JOIN optimization should not
172178 ** be moved to the left of any of their dependencies */
@@ -172182,11 +172344,11 @@
172182 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
172183 if( (pNew->maskSelf & pTerm->prereqAll)!=0 ){
172184 mPrereq |= (pTerm->prereqAll & (pNew->maskSelf-1));
172185 }
172186 }
172187 }else if( !hasRightJoin ){
172188 mPrereq = 0;
172189 }
172190 #ifndef SQLITE_OMIT_VIRTUALTABLE
172191 if( IsVirtual(pItem->pSTab) ){
172192 SrcItem *p;
@@ -214034,32 +214196,36 @@
214034 jsonParseReset(&v);
214035 jsonParseReset(&ix);
214036 return rc;
214037 }
214038 }else if( zPath[0]=='[' ){
 
214039 x = pParse->aBlob[iRoot] & 0x0f;
214040 if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND;
214041 n = jsonbPayloadSize(pParse, iRoot, &sz);
214042 k = 0;
214043 i = 1;
214044 while( sqlite3Isdigit(zPath[i]) ){
214045 k = k*10 + zPath[i] - '0';
 
 
214046 i++;
214047 }
214048 if( i<2 || zPath[i]!=']' ){
214049 if( zPath[1]=='#' ){
214050 k = jsonbArrayCount(pParse, iRoot);
214051 i = 2;
214052 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
214053 unsigned int nn = 0;
214054 i = 3;
214055 do{
214056 nn = nn*10 + zPath[i] - '0';
 
 
214057 i++;
214058 }while( sqlite3Isdigit(zPath[i]) );
214059 if( nn>k ) return JSON_LOOKUP_NOTFOUND;
214060 k -= nn;
214061 }
214062 if( zPath[i]!=']' ){
214063 return JSON_LOOKUP_PATHERROR;
214064 }
214065 }else{
@@ -214067,22 +214233,22 @@
214067 }
214068 }
214069 j = iRoot+n;
214070 iEnd = j+sz;
214071 while( j<iEnd ){
214072 if( k==0 ){
214073 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
214074 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
214075 return rc;
214076 }
214077 k--;
214078 n = jsonbPayloadSize(pParse, j, &sz);
214079 if( n==0 ) return JSON_LOOKUP_ERROR;
214080 j += n+sz;
214081 }
214082 if( j>iEnd ) return JSON_LOOKUP_ERROR;
214083 if( k>0 ) return JSON_LOOKUP_NOTFOUND;
214084 if( pParse->eEdit>=JEDIT_INS ){
214085 JsonParse v;
214086 testcase( pParse->eEdit==JEDIT_INS );
214087 testcase( pParse->eEdit==JEDIT_AINS );
214088 testcase( pParse->eEdit==JEDIT_SET );
@@ -231358,10 +231524,11 @@
231358 struct carray_bind {
231359 void *aData; /* The data */
231360 int nData; /* Number of elements */
231361 int mFlags; /* Control flags */
231362 void (*xDel)(void*); /* Destructor for aData */
 
231363 };
231364
231365
231366 /* carray_cursor is a subclass of sqlite3_vtab_cursor which will
231367 ** serve as the underlying representation of a cursor that scans
@@ -231690,26 +231857,38 @@
231690 ** Destructor for the carray_bind object
231691 */
231692 static void carrayBindDel(void *pPtr){
231693 carray_bind *p = (carray_bind*)pPtr;
231694 if( p->xDel!=SQLITE_STATIC ){
231695 p->xDel(p->aData);
231696 }
231697 sqlite3_free(p);
231698 }
231699
231700 /*
231701 ** Invoke this interface in order to bind to the single-argument
231702 ** version of CARRAY().
 
 
 
 
 
 
 
 
 
 
 
231703 */
231704 SQLITE_API int sqlite3_carray_bind(
231705 sqlite3_stmt *pStmt,
231706 int idx,
231707 void *aData,
231708 int nData,
231709 int mFlags,
231710 void (*xDestroy)(void*)
 
231711 ){
231712 carray_bind *pNew = 0;
231713 int i;
231714 int rc = SQLITE_OK;
231715
@@ -231782,23 +231961,41 @@
231782 }
231783 }else{
231784 memcpy(pNew->aData, aData, sz);
231785 }
231786 pNew->xDel = sqlite3_free;
 
231787 }else{
231788 pNew->aData = aData;
231789 pNew->xDel = xDestroy;
 
231790 }
231791 return sqlite3_bind_pointer(pStmt, idx, pNew, "carray-bind", carrayBindDel);
231792
231793 carray_bind_error:
231794 if( xDestroy!=SQLITE_STATIC && xDestroy!=SQLITE_TRANSIENT ){
231795 xDestroy(aData);
231796 }
231797 sqlite3_free(pNew);
231798 return rc;
231799 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231800
231801 /*
231802 ** Invoke this routine to register the carray() function.
231803 */
231804 SQLITE_PRIVATE Module *sqlite3CarrayRegister(sqlite3 *db){
@@ -232157,10 +232354,24 @@
232157 ** bytes read.
232158 */
232159 static int sessionVarintGet(const u8 *aBuf, int *piVal){
232160 return getVarint32(aBuf, *piVal);
232161 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232162
232163 /* Load an unaligned and unsigned 32-bit integer */
232164 #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
232165
232166 /*
@@ -235370,11 +235581,12 @@
235370
235371 if( rc==SQLITE_OK ){
235372 u8 *aVal = &pIn->aData[pIn->iNext];
235373 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
235374 int nByte;
235375 pIn->iNext += sessionVarintGet(aVal, &nByte);
 
235376 rc = sessionInputBuffer(pIn, nByte);
235377 if( rc==SQLITE_OK ){
235378 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
235379 rc = SQLITE_CORRUPT_BKPT;
235380 }else{
@@ -235423,11 +235635,12 @@
235423 int nCol = 0;
235424 int nRead = 0;
235425
235426 rc = sessionInputBuffer(pIn, 9);
235427 if( rc==SQLITE_OK ){
235428 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
 
235429 /* The hard upper limit for the number of columns in an SQLite
235430 ** database table is, according to sqliteLimit.h, 32676. So
235431 ** consider any table-header that purports to have more than 65536
235432 ** columns to be corrupt. This is convenient because otherwise,
235433 ** if the (nCol>65536) condition below were omitted, a sufficiently
@@ -235583,14 +235796,14 @@
235583 sqlite3ValueFree(p->apValue[i]);
235584 }
235585 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
235586 }
235587
235588 /* Make sure the buffer contains at least 10 bytes of input data, or all
235589 ** remaining data if there are less than 10 bytes available. This is
235590 ** sufficient either for the 'T' or 'P' byte and the varint that follows
235591 ** it, or for the two single byte values otherwise. */
235592 p->rc = sessionInputBuffer(&p->in, 2);
235593 if( p->rc!=SQLITE_OK ) return p->rc;
235594
235595 p->in.iCurrent = p->in.iNext;
235596 sessionDiscardData(&p->in);
@@ -235616,15 +235829,17 @@
235616 ** corrupt changeset. */
235617 assert( p->in.iNext==1 || p->zTab );
235618 return (p->rc = SQLITE_CORRUPT_BKPT);
235619 }
235620
235621 p->op = op;
235622 p->bIndirect = p->in.aData[p->in.iNext++];
235623 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
235624 return (p->rc = SQLITE_CORRUPT_BKPT);
235625 }
 
 
235626
235627 if( paRec ){
235628 int nVal; /* Number of values to buffer */
235629 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
235630 nVal = p->nCol * 2;
@@ -261258,11 +261473,11 @@
261258 int nArg, /* Number of args */
261259 sqlite3_value **apUnused /* Function arguments */
261260 ){
261261 assert( nArg==0 );
261262 UNUSED_PARAM2(nArg, apUnused);
261263 sqlite3_result_text(pCtx, "fts5: 2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71", -1, SQLITE_TRANSIENT);
261264 }
261265
261266 /*
261267 ** Implementation of fts5_locale(LOCALE, TEXT) function.
261268 **
261269
--- 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 ** c476d956d0bd3065cf894de6f9d393b999ff with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -467,14 +467,14 @@
467 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468 ** [sqlite_version()] and [sqlite_source_id()].
469 */
470 #define SQLITE_VERSION "3.52.0"
471 #define SQLITE_VERSION_NUMBER 3052000
472 #define SQLITE_SOURCE_ID "2026-02-04 20:51:27 c476d956d0bd3065cf894de6f9d393b999ff7d2268a35f01a6d88804789ab58f"
473 #define SQLITE_SCM_BRANCH "trunk"
474 #define SQLITE_SCM_TAGS ""
475 #define SQLITE_SCM_DATETIME "2026-02-04T20:51:27.822Z"
476
477 /*
478 ** CAPI3REF: Run-Time Library Version Numbers
479 ** KEYWORDS: sqlite3_version sqlite3_sourceid
480 **
@@ -11571,23 +11571,45 @@
11571 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11572
11573 /*
11574 ** CAPI3REF: Bind array values to the CARRAY table-valued function
11575 **
11576 ** The sqlite3_carray_bind_v2(S,I,P,N,F,X,D) interface binds an array value to
11577 ** parameter that is the first argument of the [carray() table-valued function].
11578 ** The S parameter is a pointer to the [prepared statement] that uses the carray()
11579 ** functions. I is the parameter index to be bound. I must be the index of the
11580 ** parameter that is the first argument to the carray() table-valued function.
11581 ** P is a pointer to the array to be bound, and N is the number of elements in
11582 ** the array. The F argument is one of constants [SQLITE_CARRAY_INT32],
11583 ** [SQLITE_CARRAY_INT64], [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT],
11584 ** or [SQLITE_CARRAY_BLOB] to indicate the datatype of the array P.
11585 **
11586 ** If the X argument is not a NULL pointer or one of the special
11587 ** values [SQLITE_STATIC] or [SQLITE_TRANSIENT], then SQLite will invoke
11588 ** the function X with argument D when it is finished using the data in P.
11589 ** The call to X(D) is a destructor for the array P. The destructor X(D)
11590 ** is invoked even if the call to sqlite3_carray_bind() fails. If the X
11591 ** parameter is the special-case value [SQLITE_STATIC], then SQLite assumes
11592 ** that the data static and the destructor is never invoked. If the X
11593 ** parameter is the special-case value [SQLITE_TRANSIENT], then
11594 ** sqlite3_carray_bind_v2() makes its own private copy of the data prior
11595 ** to returning and never invokes the destructor X.
11596 **
11597 ** The sqlite3_carray_bind() function works the same as sqlite_carray_bind_v2()
11598 ** with a D parameter set to P. In other words,
11599 ** sqlite3_carray_bind(S,I,P,N,F,X) is same as
11600 ** sqlite3_carray_bind(S,I,P,N,F,X,P).
11601 */
11602 SQLITE_API int sqlite3_carray_bind_v2(
11603 sqlite3_stmt *pStmt, /* Statement to be bound */
11604 int i, /* Parameter index */
11605 void *aData, /* Pointer to array data */
11606 int nData, /* Number of data elements */
11607 int mFlags, /* CARRAY flags */
11608 void (*xDel)(void*), /* Destructor for aData */
11609 void *pDel /* Optional argument to xDel() */
11610 );
11611 SQLITE_API int sqlite3_carray_bind(
11612 sqlite3_stmt *pStmt, /* Statement to be bound */
11613 int i, /* Parameter index */
11614 void *aData, /* Pointer to array data */
11615 int nData, /* Number of data elements */
@@ -21094,10 +21116,11 @@
21116 u16 mWFlags; /* Use-dependent flags */
21117 union { /* Extra data for callback */
21118 NameContext *pNC; /* Naming context */
21119 int n; /* A counter */
21120 int iCur; /* A cursor number */
21121 int sz; /* String literal length */
21122 SrcList *pSrcList; /* FROM clause */
21123 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
21124 struct RefSrcList *pRefSrcList; /* sqlite3ReferencesSrcList() */
21125 int *aiCol; /* array of column indexes */
21126 struct IdxCover *pIdxCover; /* Check for index coverage */
@@ -21877,10 +21900,11 @@
21900 SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
21901 #endif
21902 SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*, Parse*);
21903 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
21904 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
21905 SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr*);
21906 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
21907 SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab);
21908 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
21909 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
21910 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
@@ -86688,10 +86712,15 @@
86712 ** the column value into *ppVal. If *ppVal is initially NULL then a new
86713 ** sqlite3_value object is allocated.
86714 **
86715 ** If *ppVal is initially NULL then the caller is responsible for
86716 ** ensuring that the value written into *ppVal is eventually freed.
86717 **
86718 ** If the buffer does not contain a well-formed record, this routine may
86719 ** read several bytes past the end of the buffer. Callers must therefore
86720 ** ensure that any buffer which may contain a corrupt record is padded
86721 ** with at least 8 bytes of addressable memory.
86722 */
86723 SQLITE_PRIVATE int sqlite3Stat4Column(
86724 sqlite3 *db, /* Database handle */
86725 const void *pRec, /* Pointer to buffer containing record */
86726 int nRec, /* Size of buffer pRec in bytes */
@@ -118827,11 +118856,14 @@
118856 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
118857 }
118858 if( pIEpr==0 ) break;
118859 if( NEVER(!ExprUseYTab(pExpr)) ) break;
118860 for(i=0; i<pSrcList->nSrc; i++){
118861 if( pSrcList->a[i].iCursor==pIEpr->iDataCur ){
118862 testcase( i>0 );
118863 break;
118864 }
118865 }
118866 if( i>=pSrcList->nSrc ) break;
118867 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
118868 if( pParse->nErr ){ return WRC_Abort; }
118869
@@ -141374,10 +141406,11 @@
141406 int (*db_status64)(sqlite3*,int,sqlite3_int64*,sqlite3_int64*,int);
141407 /* Version 3.52.0 and later */
141408 void (*str_truncate)(sqlite3_str*,int);
141409 void (*str_free)(sqlite3_str*);
141410 int (*carray_bind)(sqlite3_stmt*,int,void*,int,int,void(*)(void*));
141411 int (*carray_bind_v2)(sqlite3_stmt*,int,void*,int,int,void(*)(void*),void*);
141412 };
141413
141414 /*
141415 ** This is the function signature used for all extension entry points. It
141416 ** is also defined in the file "loadext.c".
@@ -141716,10 +141749,11 @@
141749 #define sqlite3_db_status64 sqlite3_api->db_status64
141750 /* Version 3.52.0 and later */
141751 #define sqlite3_str_truncate sqlite3_api->str_truncate
141752 #define sqlite3_str_free sqlite3_api->str_free
141753 #define sqlite3_carray_bind sqlite3_api->carray_bind
141754 #define sqlite3_carray_bind_v2 sqlite3_api->carray_bind_v2
141755 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
141756
141757 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
141758 /* This case when the file really is being compiled as a loadable
141759 ** extension */
@@ -142247,12 +142281,14 @@
142281 sqlite3_db_status64,
142282 /* Version 3.52.0 and later */
142283 sqlite3_str_truncate,
142284 sqlite3_str_free,
142285 #ifdef SQLITE_ENABLE_CARRAY
142286 sqlite3_carray_bind,
142287 sqlite3_carray_bind_v2
142288 #else
142289 0,
142290 0
142291 #endif
142292 };
142293
142294 /* True if x is the directory separator character
@@ -150108,11 +150144,11 @@
150144 ** function is responsible for ensuring that this structure is eventually
150145 ** freed.
150146 */
150147 static KeyInfo *multiSelectByMergeKeyInfo(Parse *pParse, Select *p, int nExtra){
150148 ExprList *pOrderBy = p->pOrderBy;
150149 int nOrderBy = (pOrderBy!=0) ? pOrderBy->nExpr : 0;
150150 sqlite3 *db = pParse->db;
150151 KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
150152 if( pRet ){
150153 int i;
150154 for(i=0; i<nOrderBy; i++){
@@ -150911,14 +150947,12 @@
150947 ** EofB: ...
150948 ** AltB: ...
150949 ** AeqB: ...
150950 ** AgtB: ...
150951 ** Init: initialize coroutine registers
150952 ** yield coA, on eof goto EofA
150953 ** yield coB, on eof goto EofB
 
 
150954 ** Cmpr: Compare A, B
150955 ** Jump AltB, AeqB, AgtB
150956 ** End: ...
150957 **
150958 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
@@ -151006,30 +151040,33 @@
151040 }
151041 }
151042 }
151043
151044 /* Compute the comparison permutation and keyinfo that is used with
151045 ** the permutation to determine if the next row of results comes
151046 ** from selectA or selectB. Also add literal collations to the
151047 ** ORDER BY clause terms so that when selectA and selectB are
151048 ** evaluated, they use the correct collation.
 
151049 */
151050 aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1));
151051 if( aPermute ){
151052 struct ExprList_item *pItem;
151053 int bKeep = 0;
151054 aPermute[0] = nOrderBy;
151055 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
151056 assert( pItem!=0 );
151057 assert( pItem->u.x.iOrderByCol>0 );
151058 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
151059 aPermute[i] = pItem->u.x.iOrderByCol - 1;
151060 if( aPermute[i]!=(u32)i-1 ) bKeep = 1;
151061 }
151062 if( bKeep==0 ){
151063 sqlite3DbFreeNN(db, aPermute);
151064 aPermute = 0;
151065 }
151066 }
151067 pKeyMerge = multiSelectByMergeKeyInfo(pParse, p, 1);
151068
151069 /* Allocate a range of temporary registers and the KeyInfo needed
151070 ** for the logic that removes duplicate result rows when the
151071 ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
151072 */
@@ -151104,11 +151141,11 @@
151141 /* Generate a coroutine to evaluate the SELECT statement to the
151142 ** left of the compound operator - the "A" select.
151143 */
151144 addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
151145 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
151146 VdbeComment((v, "SUBR: next-A"));
151147 pPrior->iLimit = regLimitA;
151148 ExplainQueryPlan((pParse, 1, "LEFT"));
151149 sqlite3Select(pParse, pPrior, &destA);
151150 sqlite3VdbeEndCoroutine(v, regAddrA);
151151 sqlite3VdbeJumpHere(v, addr1);
@@ -151116,11 +151153,11 @@
151153 /* Generate a coroutine to evaluate the SELECT statement on
151154 ** the right - the "B" select
151155 */
151156 addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
151157 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
151158 VdbeComment((v, "SUBR: next-B"));
151159 savedLimit = p->iLimit;
151160 savedOffset = p->iOffset;
151161 p->iLimit = regLimitB;
151162 p->iOffset = 0;
151163 ExplainQueryPlan((pParse, 1, "RIGHT"));
@@ -151130,20 +151167,20 @@
151167 sqlite3VdbeEndCoroutine(v, regAddrB);
151168
151169 /* Generate a subroutine that outputs the current row of the A
151170 ** select as the next output row of the compound select.
151171 */
151172 VdbeNoopComment((v, "SUBR: out-A"));
151173 addrOutA = generateOutputSubroutine(pParse,
151174 p, &destA, pDest, regOutA,
151175 regPrev, pKeyDup, labelEnd);
151176
151177 /* Generate a subroutine that outputs the current row of the B
151178 ** select as the next output row of the compound select.
151179 */
151180 if( op==TK_ALL || op==TK_UNION ){
151181 VdbeNoopComment((v, "SUBR: out-B"));
151182 addrOutB = generateOutputSubroutine(pParse,
151183 p, &destB, pDest, regOutB,
151184 regPrev, pKeyDup, labelEnd);
151185 }
151186 sqlite3KeyInfoUnref(pKeyDup);
@@ -151152,14 +151189,16 @@
151189 ** are exhausted and only data in select B remains.
151190 */
151191 if( op==TK_EXCEPT || op==TK_INTERSECT ){
151192 addrEofA_noB = addrEofA = labelEnd;
151193 }else{
151194 VdbeNoopComment((v, "SUBR: eof-A"));
151195 addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
151196 VdbeComment((v, "out-B"));
151197 addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
151198 VdbeCoverage(v);
151199 VdbeComment((v, "next-B"));
151200 sqlite3VdbeGoto(v, addrEofA);
151201 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
151202 }
151203
151204 /* Generate a subroutine to run when the results from select B
@@ -151167,21 +151206,24 @@
151206 */
151207 if( op==TK_INTERSECT ){
151208 addrEofB = addrEofA;
151209 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
151210 }else{
151211 VdbeNoopComment((v, "SUBR: eof-B"));
151212 addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
151213 VdbeComment((v, "out-A"));
151214 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
151215 VdbeComment((v, "next-A"));
151216 sqlite3VdbeGoto(v, addrEofB);
151217 }
151218
151219 /* Generate code to handle the case of A<B
151220 */
 
151221 addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
151222 VdbeComment((v, "out-A"));
151223 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
151224 VdbeComment((v, "next-A"));
151225 sqlite3VdbeGoto(v, labelCmpr);
151226
151227 /* Generate code to handle the case of A==B
151228 */
151229 if( op==TK_ALL ){
@@ -151188,40 +151230,52 @@
151230 addrAeqB = addrAltB;
151231 }else if( op==TK_INTERSECT ){
151232 addrAeqB = addrAltB;
151233 addrAltB++;
151234 }else{
151235 addrAeqB = addrAltB + 1;
 
 
 
151236 }
151237
151238 /* Generate code to handle the case of A>B
151239 */
 
151240 addrAgtB = sqlite3VdbeCurrentAddr(v);
151241 if( op==TK_ALL || op==TK_UNION ){
151242 sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
151243 VdbeComment((v, "out-B"));
151244 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
151245 VdbeComment((v, "next-B"));
151246 sqlite3VdbeGoto(v, labelCmpr);
151247 }else{
151248 addrAgtB++; /* Just do next-B. Might as well use the next-B call
151249 ** in the next code block */
151250 }
 
 
151251
151252 /* This code runs once to initialize everything.
151253 */
151254 sqlite3VdbeJumpHere(v, addr1);
151255 sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
151256 VdbeComment((v, "next-A"));
151257 /* v--- Also the A>B case for EXCEPT and INTERSECT */
151258 sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
151259 VdbeComment((v, "next-B"));
151260
151261 /* Implement the main merge loop
151262 */
151263 if( aPermute!=0 ){
151264 sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
151265 }
151266 sqlite3VdbeResolveLabel(v, labelCmpr);
 
151267 sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
151268 (char*)pKeyMerge, P4_KEYINFO);
151269 if( aPermute!=0 ){
151270 sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
151271 }
151272 sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
151273 VdbeCoverageIf(v, op==TK_ALL);
151274 VdbeCoverageIf(v, op==TK_UNION);
151275 VdbeCoverageIf(v, op==TK_EXCEPT);
151276 VdbeCoverageIf(v, op==TK_INTERSECT);
151277
151278 /* Jump to the this point in order to terminate the query.
151279 */
151280 sqlite3VdbeResolveLabel(v, labelEnd);
151281
@@ -165697,10 +165751,38 @@
165751 sqlite3ValueFree(pVal);
165752 return rc;
165753 }
165754 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
165755
165756 /*
165757 ** If pExpr is one of "like", "glob", "match", or "regexp", then
165758 ** return the corresponding SQLITE_INDEX_CONSTRAINT_xxxx value.
165759 ** If not, return 0.
165760 **
165761 ** pExpr is guaranteed to be a TK_FUNCTION.
165762 */
165763 SQLITE_PRIVATE int sqlite3ExprIsLikeOperator(const Expr *pExpr){
165764 static const struct {
165765 const char *zOp;
165766 unsigned char eOp;
165767 } aOp[] = {
165768 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
165769 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
165770 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
165771 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
165772 };
165773 int i;
165774 assert( pExpr->op==TK_FUNCTION );
165775 assert( !ExprHasProperty(pExpr, EP_IntValue) );
165776 for(i=0; i<ArraySize(aOp); i++){
165777 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
165778 return aOp[i].eOp;
165779 }
165780 }
165781 return 0;
165782 }
165783
165784
165785 #ifndef SQLITE_OMIT_VIRTUALTABLE
165786 /*
165787 ** Check to see if the pExpr expression is a form that needs to be passed
165788 ** to the xBestIndex method of virtual tables. Forms of interest include:
@@ -165733,19 +165815,10 @@
165815 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
165816 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
165817 Expr **ppRight /* Expression to left of MATCH/op2 */
165818 ){
165819 if( pExpr->op==TK_FUNCTION ){
 
 
 
 
 
 
 
 
 
165820 ExprList *pList;
165821 Expr *pCol; /* Column reference */
165822 int i;
165823
165824 assert( ExprUseXList(pExpr) );
@@ -165761,20 +165834,15 @@
165834 ** vtab_column MATCH expression
165835 ** MATCH(expression,vtab_column)
165836 */
165837 pCol = pList->a[1].pExpr;
165838 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
165839 if( ExprIsVtab(pCol) && (i = sqlite3ExprIsLikeOperator(pExpr))!=0 ){
165840 *peOp2 = i;
165841 *ppRight = pList->a[0].pExpr;
165842 *ppLeft = pCol;
165843 return 1;
 
 
 
 
 
165844 }
165845
165846 /* We can also match against the first column of overloaded
165847 ** functions where xFindFunction returns a value of at least
165848 ** SQLITE_INDEX_CONSTRAINT_FUNCTION.
@@ -170218,10 +170286,71 @@
170286 p->u.btree.pIndex = 0;
170287 }
170288 }
170289 return rc;
170290 }
170291
170292 /*
170293 ** Callback for estLikePatternLength().
170294 **
170295 ** If this node is a string literal that is longer pWalker->sz, then set
170296 ** pWalker->sz to the byte length of that string literal.
170297 **
170298 ** pWalker->eCode indicates how to count characters:
170299 **
170300 ** eCode==0 Count as a GLOB pattern
170301 ** eCode==1 Count as a LIKE pattern
170302 */
170303 static int exprNodePatternLengthEst(Walker *pWalker, Expr *pExpr){
170304 if( pExpr->op==TK_STRING ){
170305 int sz = 0; /* Pattern size in bytes */
170306 u8 *z = (u8*)pExpr->u.zToken; /* The pattern */
170307 u8 c; /* Next character of the pattern */
170308 u8 c1, c2, c3; /* Wildcards */
170309 if( pWalker->eCode ){
170310 c1 = '%';
170311 c2 = '_';
170312 c3 = 0;
170313 }else{
170314 c1 = '*';
170315 c2 = '?';
170316 c3 = '[';
170317 }
170318 while( (c = *(z++))!=0 ){
170319 if( c==c3 ){
170320 if( *z ) z++;
170321 while( *z && *z!=']' ) z++;
170322 }else if( c!=c1 && c!=c2 ){
170323 sz++;
170324 }
170325 }
170326 if( sz>pWalker->u.sz ) pWalker->u.sz = sz;
170327 }
170328 return WRC_Continue;
170329 }
170330
170331 /*
170332 ** Return the length of the longest string literal in the given
170333 ** expression.
170334 **
170335 ** eCode indicates how to count characters:
170336 **
170337 ** eCode==0 Count as a GLOB pattern
170338 ** eCode==1 Count as a LIKE pattern
170339 */
170340 static int estLikePatternLength(Expr *p, u16 eCode){
170341 Walker w;
170342 w.u.sz = 0;
170343 w.eCode = eCode;
170344 w.xExprCallback = exprNodePatternLengthEst;
170345 w.xSelectCallback = sqlite3SelectWalkFail;
170346 #ifdef SQLITE_DEBUG
170347 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
170348 #endif
170349 sqlite3WalkExpr(&w, p);
170350 return w.u.sz;
170351 }
170352
170353 /*
170354 ** Adjust the WhereLoop.nOut value downward to account for terms of the
170355 ** WHERE clause that reference the loop but which are not used by an
170356 ** index.
@@ -170247,10 +170376,17 @@
170376 ** of rows in the table. In other words, assume that x==EXPR will filter
170377 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
170378 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
170379 ** on the "x" column and so in that case only cap the output row estimate
170380 ** at 1/2 instead of 1/4.
170381 **
170382 ** Heuristic 3: If there is a LIKE or GLOB (or REGEXP or MATCH) operator
170383 ** with a large constant pattern, then reduce the size of the search
170384 ** space according to the length of the pattern, under the theory that
170385 ** longer patterns are less likely to match. This heuristic was added
170386 ** to give better output-row count estimates when preparing queries for
170387 ** the Join-Order Benchmarks. See forum thread 2026-01-30T09:57:54z
170388 */
170389 static void whereLoopOutputAdjust(
170390 WhereClause *pWC, /* The WHERE clause */
170391 WhereLoop *pLoop, /* The loop to adjust downward */
170392 LogEst nRow /* Number of rows in the entire table */
@@ -170296,25 +170432,43 @@
170432 ** then use the probability provided by the application. */
170433 pLoop->nOut += pTerm->truthProb;
170434 }else{
170435 /* In the absence of explicit truth probabilities, use heuristics to
170436 ** guess a reasonable truth probability. */
170437 Expr *pOpExpr = pTerm->pExpr;
170438 pLoop->nOut--;
170439 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
170440 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
170441 ){
170442 Expr *pRight = pOpExpr->pRight;
170443 int k = 0;
170444 testcase( pOpExpr->op==TK_IS );
170445 if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){
170446 k = 10;
170447 }else{
170448 k = 20;
170449 }
170450 if( iReduce<k ){
170451 pTerm->wtFlags |= TERM_HEURTRUTH;
170452 iReduce = k;
170453 }
170454 }else
170455 if( ExprHasProperty(pOpExpr, EP_InfixFunc)
170456 && pOpExpr->op==TK_FUNCTION
170457 ){
170458 int eOp;
170459 assert( ExprUseXList(pOpExpr) );
170460 assert( pOpExpr->x.pList->nExpr>=2 );
170461 eOp = sqlite3ExprIsLikeOperator(pOpExpr);
170462 if( ALWAYS(eOp>0) ){
170463 int szPattern;
170464 Expr *pRHS = pOpExpr->x.pList->a[0].pExpr;
170465 eOp = eOp==SQLITE_INDEX_CONSTRAINT_LIKE;
170466 szPattern = estLikePatternLength(pRHS, eOp);
170467 if( szPattern>0 ){
170468 pLoop->nOut -= szPattern*2;
170469 }
170470 }
170471 }
170472 }
170473 }
170474 }
@@ -172136,11 +172290,11 @@
172290 SrcItem *pItem;
172291 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
172292 sqlite3 *db = pWInfo->pParse->db;
172293 int rc = SQLITE_OK;
172294 int bFirstPastRJ = 0;
172295 int hasRightCrossJoin = 0;
172296 WhereLoop *pNew;
172297
172298
172299 /* Loop over the tables in the join, from left to right */
172300 pNew = pBuilder->pNew;
@@ -172163,16 +172317,24 @@
172317 /* Add prerequisites to prevent reordering of FROM clause terms
172318 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
172319 ** prevents the right operand of a RIGHT JOIN from being swapped with
172320 ** other elements even further to the right.
172321 **
172322 ** The hasRightCrossJoin flag prevent FROM-clause terms from moving
172323 ** from the right side of a LEFT JOIN or CROSS JOIN over to the
172324 ** left side of that same join. This is a required restriction in
172325 ** the case of LEFT JOIN - an incorrect answer may results if it is
172326 ** not enforced. This restriction is not required for CROSS JOIN.
172327 ** It is provided merely as a means of controlling join order, under
172328 ** the theory that no real-world queries that care about performance
172329 ** actually use the CROSS JOIN syntax.
172330 */
172331 if( pItem->fg.jointype & (JT_LTORJ|JT_CROSS) ){
172332 testcase( pItem->fg.jointype & JT_LTORJ );
172333 testcase( pItem->fg.jointype & JT_CROSS );
172334 hasRightCrossJoin = 1;
172335 }
172336 mPrereq |= mPrior;
172337 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
172338 }else if( pItem->fg.fromExists ){
172339 /* joins that result from the EXISTS-to-JOIN optimization should not
172340 ** be moved to the left of any of their dependencies */
@@ -172182,11 +172344,11 @@
172344 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
172345 if( (pNew->maskSelf & pTerm->prereqAll)!=0 ){
172346 mPrereq |= (pTerm->prereqAll & (pNew->maskSelf-1));
172347 }
172348 }
172349 }else if( !hasRightCrossJoin ){
172350 mPrereq = 0;
172351 }
172352 #ifndef SQLITE_OMIT_VIRTUALTABLE
172353 if( IsVirtual(pItem->pSTab) ){
172354 SrcItem *p;
@@ -214034,32 +214196,36 @@
214196 jsonParseReset(&v);
214197 jsonParseReset(&ix);
214198 return rc;
214199 }
214200 }else if( zPath[0]=='[' ){
214201 u64 kk = 0;
214202 x = pParse->aBlob[iRoot] & 0x0f;
214203 if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND;
214204 n = jsonbPayloadSize(pParse, iRoot, &sz);
 
214205 i = 1;
214206 while( sqlite3Isdigit(zPath[i]) ){
214207 if( kk<0xffffffff ) kk = kk*10 + zPath[i] - '0';
214208 /* ^^^^^^^^^^--- Allow kk to be bigger than any JSON array so that
214209 ** we get NOTFOUND instead of PATHERROR, without overflowing kk. */
214210 i++;
214211 }
214212 if( i<2 || zPath[i]!=']' ){
214213 if( zPath[1]=='#' ){
214214 kk = jsonbArrayCount(pParse, iRoot);
214215 i = 2;
214216 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
214217 u64 nn = 0;
214218 i = 3;
214219 do{
214220 if( nn<0xffffffff ) nn = nn*10 + zPath[i] - '0';
214221 /* ^^^^^^^^^^--- Allow nn to be bigger than any JSON array to
214222 ** get NOTFOUND instead of PATHERROR, without overflowing nn. */
214223 i++;
214224 }while( sqlite3Isdigit(zPath[i]) );
214225 if( nn>kk ) return JSON_LOOKUP_NOTFOUND;
214226 kk -= nn;
214227 }
214228 if( zPath[i]!=']' ){
214229 return JSON_LOOKUP_PATHERROR;
214230 }
214231 }else{
@@ -214067,22 +214233,22 @@
214233 }
214234 }
214235 j = iRoot+n;
214236 iEnd = j+sz;
214237 while( j<iEnd ){
214238 if( kk==0 ){
214239 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
214240 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
214241 return rc;
214242 }
214243 kk--;
214244 n = jsonbPayloadSize(pParse, j, &sz);
214245 if( n==0 ) return JSON_LOOKUP_ERROR;
214246 j += n+sz;
214247 }
214248 if( j>iEnd ) return JSON_LOOKUP_ERROR;
214249 if( kk>0 ) return JSON_LOOKUP_NOTFOUND;
214250 if( pParse->eEdit>=JEDIT_INS ){
214251 JsonParse v;
214252 testcase( pParse->eEdit==JEDIT_INS );
214253 testcase( pParse->eEdit==JEDIT_AINS );
214254 testcase( pParse->eEdit==JEDIT_SET );
@@ -231358,10 +231524,11 @@
231524 struct carray_bind {
231525 void *aData; /* The data */
231526 int nData; /* Number of elements */
231527 int mFlags; /* Control flags */
231528 void (*xDel)(void*); /* Destructor for aData */
231529 void *pDel; /* Alternative argument to xDel() */
231530 };
231531
231532
231533 /* carray_cursor is a subclass of sqlite3_vtab_cursor which will
231534 ** serve as the underlying representation of a cursor that scans
@@ -231690,26 +231857,38 @@
231857 ** Destructor for the carray_bind object
231858 */
231859 static void carrayBindDel(void *pPtr){
231860 carray_bind *p = (carray_bind*)pPtr;
231861 if( p->xDel!=SQLITE_STATIC ){
231862 p->xDel(p->pDel);
231863 }
231864 sqlite3_free(p);
231865 }
231866
231867 /*
231868 ** Invoke this interface in order to bind to the single-argument
231869 ** version of CARRAY().
231870 **
231871 ** pStmt The prepared statement to which to bind
231872 ** idx The index of the parameter of pStmt to which to bind
231873 ** aData The data to be bound
231874 ** nData The number of elements in aData
231875 ** mFlags One of SQLITE_CARRAY_xxxx indicating datatype of aData
231876 ** xDestroy Destructor for pDestroy or aData if pDestroy==NULL.
231877 ** pDestroy Invoke xDestroy on this pointer if not NULL
231878 **
231879 ** The destructor is called pDestroy if pDestroy!=NULL, or against
231880 ** aData if pDestroy==NULL.
231881 */
231882 SQLITE_API int sqlite3_carray_bind_v2(
231883 sqlite3_stmt *pStmt,
231884 int idx,
231885 void *aData,
231886 int nData,
231887 int mFlags,
231888 void (*xDestroy)(void*),
231889 void *pDestroy
231890 ){
231891 carray_bind *pNew = 0;
231892 int i;
231893 int rc = SQLITE_OK;
231894
@@ -231782,23 +231961,41 @@
231961 }
231962 }else{
231963 memcpy(pNew->aData, aData, sz);
231964 }
231965 pNew->xDel = sqlite3_free;
231966 pNew->pDel = pNew->aData;
231967 }else{
231968 pNew->aData = aData;
231969 pNew->xDel = xDestroy;
231970 pNew->pDel = pDestroy;
231971 }
231972 return sqlite3_bind_pointer(pStmt, idx, pNew, "carray-bind", carrayBindDel);
231973
231974 carray_bind_error:
231975 if( xDestroy!=SQLITE_STATIC && xDestroy!=SQLITE_TRANSIENT ){
231976 xDestroy(pDestroy);
231977 }
231978 sqlite3_free(pNew);
231979 return rc;
231980 }
231981
231982 /*
231983 ** Invoke this interface in order to bind to the single-argument
231984 ** version of CARRAY(). Same as sqlite3_carray_bind_v2() with the
231985 ** pDestroy parameter set to NULL.
231986 */
231987 SQLITE_API int sqlite3_carray_bind(
231988 sqlite3_stmt *pStmt,
231989 int idx,
231990 void *aData,
231991 int nData,
231992 int mFlags,
231993 void (*xDestroy)(void*)
231994 ){
231995 return sqlite3_carray_bind_v2(pStmt,idx,aData,nData,mFlags,xDestroy,aData);
231996 }
231997
231998 /*
231999 ** Invoke this routine to register the carray() function.
232000 */
232001 SQLITE_PRIVATE Module *sqlite3CarrayRegister(sqlite3 *db){
@@ -232157,10 +232354,24 @@
232354 ** bytes read.
232355 */
232356 static int sessionVarintGet(const u8 *aBuf, int *piVal){
232357 return getVarint32(aBuf, *piVal);
232358 }
232359
232360 /*
232361 ** Read a varint value from buffer aBuf[], size nBuf bytes, into *piVal.
232362 ** Return the number of bytes read.
232363 */
232364 static int sessionVarintGetSafe(const u8 *aBuf, int nBuf, int *piVal){
232365 u8 aCopy[5];
232366 const u8 *aRead = aBuf;
232367 if( nBuf<5 ){
232368 memcpy(aCopy, aBuf, nBuf);
232369 aRead = aCopy;
232370 }
232371 return getVarint32(aRead, *piVal);
232372 }
232373
232374 /* Load an unaligned and unsigned 32-bit integer */
232375 #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
232376
232377 /*
@@ -235370,11 +235581,12 @@
235581
235582 if( rc==SQLITE_OK ){
235583 u8 *aVal = &pIn->aData[pIn->iNext];
235584 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
235585 int nByte;
235586 int nRem = pIn->nData - pIn->iNext;
235587 pIn->iNext += sessionVarintGetSafe(aVal, nRem, &nByte);
235588 rc = sessionInputBuffer(pIn, nByte);
235589 if( rc==SQLITE_OK ){
235590 if( nByte<0 || nByte>pIn->nData-pIn->iNext ){
235591 rc = SQLITE_CORRUPT_BKPT;
235592 }else{
@@ -235423,11 +235635,12 @@
235635 int nCol = 0;
235636 int nRead = 0;
235637
235638 rc = sessionInputBuffer(pIn, 9);
235639 if( rc==SQLITE_OK ){
235640 int nBuf = pIn->nData - pIn->iNext;
235641 nRead += sessionVarintGetSafe(&pIn->aData[pIn->iNext], nBuf, &nCol);
235642 /* The hard upper limit for the number of columns in an SQLite
235643 ** database table is, according to sqliteLimit.h, 32676. So
235644 ** consider any table-header that purports to have more than 65536
235645 ** columns to be corrupt. This is convenient because otherwise,
235646 ** if the (nCol>65536) condition below were omitted, a sufficiently
@@ -235583,14 +235796,14 @@
235796 sqlite3ValueFree(p->apValue[i]);
235797 }
235798 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
235799 }
235800
235801 /* Make sure the buffer contains at least 2 bytes of input data, or all
235802 ** remaining data if there are less than 2 bytes available. This is
235803 ** sufficient either for the 'T' or 'P' byte that begins a new table,
235804 ** or for the "op" and "bIndirect" single bytes otherwise. */
235805 p->rc = sessionInputBuffer(&p->in, 2);
235806 if( p->rc!=SQLITE_OK ) return p->rc;
235807
235808 p->in.iCurrent = p->in.iNext;
235809 sessionDiscardData(&p->in);
@@ -235616,15 +235829,17 @@
235829 ** corrupt changeset. */
235830 assert( p->in.iNext==1 || p->zTab );
235831 return (p->rc = SQLITE_CORRUPT_BKPT);
235832 }
235833
235834 if( (op!=SQLITE_UPDATE && op!=SQLITE_DELETE && op!=SQLITE_INSERT)
235835 || (p->in.iNext>=p->in.nData)
235836 ){
235837 return (p->rc = SQLITE_CORRUPT_BKPT);
235838 }
235839 p->op = op;
235840 p->bIndirect = p->in.aData[p->in.iNext++];
235841
235842 if( paRec ){
235843 int nVal; /* Number of values to buffer */
235844 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
235845 nVal = p->nCol * 2;
@@ -261258,11 +261473,11 @@
261473 int nArg, /* Number of args */
261474 sqlite3_value **apUnused /* Function arguments */
261475 ){
261476 assert( nArg==0 );
261477 UNUSED_PARAM2(nArg, apUnused);
261478 sqlite3_result_text(pCtx, "fts5: 2026-02-04 18:10:49 e6902937ecdbeb449986469859b46631272fb0a9e7e1c31adea14cff072b6d67", -1, SQLITE_TRANSIENT);
261479 }
261480
261481 /*
261482 ** Implementation of fts5_locale(LOCALE, TEXT) function.
261483 **
261484
+36 -14
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.52.0"
150150
#define SQLITE_VERSION_NUMBER 3052000
151
-#define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
151
+#define SQLITE_SOURCE_ID "2026-02-04 20:51:27 c476d956d0bd3065cf894de6f9d393b999ff7d2268a35f01a6d88804789ab58f"
152152
#define SQLITE_SCM_BRANCH "trunk"
153153
#define SQLITE_SCM_TAGS ""
154
-#define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
154
+#define SQLITE_SCM_DATETIME "2026-02-04T20:51:27.822Z"
155155
156156
/*
157157
** CAPI3REF: Run-Time Library Version Numbers
158158
** KEYWORDS: sqlite3_version sqlite3_sourceid
159159
**
@@ -11250,23 +11250,45 @@
1125011250
#define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
1125111251
1125211252
/*
1125311253
** CAPI3REF: Bind array values to the CARRAY table-valued function
1125411254
**
11255
-** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to
11256
-** one of the first argument of the [carray() table-valued function]. The
11257
-** S parameter is a pointer to the [prepared statement] that uses the carray()
11258
-** functions. I is the parameter index to be bound. P is a pointer to the
11259
-** array to be bound, and N is the number of eements in the array. The
11260
-** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64],
11261
-** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to
11262
-** indicate the datatype of the array being bound. The X argument is not a
11263
-** NULL pointer, then SQLite will invoke the function X on the P parameter
11264
-** after it has finished using P, even if the call to
11265
-** sqlite3_carray_bind() fails. The special-case finalizer
11266
-** SQLITE_TRANSIENT has no effect here.
11255
+** The sqlite3_carray_bind_v2(S,I,P,N,F,X,D) interface binds an array value to
11256
+** parameter that is the first argument of the [carray() table-valued function].
11257
+** The S parameter is a pointer to the [prepared statement] that uses the carray()
11258
+** functions. I is the parameter index to be bound. I must be the index of the
11259
+** parameter that is the first argument to the carray() table-valued function.
11260
+** P is a pointer to the array to be bound, and N is the number of elements in
11261
+** the array. The F argument is one of constants [SQLITE_CARRAY_INT32],
11262
+** [SQLITE_CARRAY_INT64], [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT],
11263
+** or [SQLITE_CARRAY_BLOB] to indicate the datatype of the array P.
11264
+**
11265
+** If the X argument is not a NULL pointer or one of the special
11266
+** values [SQLITE_STATIC] or [SQLITE_TRANSIENT], then SQLite will invoke
11267
+** the function X with argument D when it is finished using the data in P.
11268
+** The call to X(D) is a destructor for the array P. The destructor X(D)
11269
+** is invoked even if the call to sqlite3_carray_bind() fails. If the X
11270
+** parameter is the special-case value [SQLITE_STATIC], then SQLite assumes
11271
+** that the data static and the destructor is never invoked. If the X
11272
+** parameter is the special-case value [SQLITE_TRANSIENT], then
11273
+** sqlite3_carray_bind_v2() makes its own private copy of the data prior
11274
+** to returning and never invokes the destructor X.
11275
+**
11276
+** The sqlite3_carray_bind() function works the same as sqlite_carray_bind_v2()
11277
+** with a D parameter set to P. In other words,
11278
+** sqlite3_carray_bind(S,I,P,N,F,X) is same as
11279
+** sqlite3_carray_bind(S,I,P,N,F,X,P).
1126711280
*/
11281
+SQLITE_API int sqlite3_carray_bind_v2(
11282
+ sqlite3_stmt *pStmt, /* Statement to be bound */
11283
+ int i, /* Parameter index */
11284
+ void *aData, /* Pointer to array data */
11285
+ int nData, /* Number of data elements */
11286
+ int mFlags, /* CARRAY flags */
11287
+ void (*xDel)(void*), /* Destructor for aData */
11288
+ void *pDel /* Optional argument to xDel() */
11289
+);
1126811290
SQLITE_API int sqlite3_carray_bind(
1126911291
sqlite3_stmt *pStmt, /* Statement to be bound */
1127011292
int i, /* Parameter index */
1127111293
void *aData, /* Pointer to array data */
1127211294
int nData, /* Number of data elements */
1127311295
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.52.0"
150 #define SQLITE_VERSION_NUMBER 3052000
151 #define SQLITE_SOURCE_ID "2026-01-26 10:53:24 4733d351ec2376291f093ba8d2ba71d82c6f100c68dc860eee0532986c154e71"
152 #define SQLITE_SCM_BRANCH "trunk"
153 #define SQLITE_SCM_TAGS ""
154 #define SQLITE_SCM_DATETIME "2026-01-26T10:53:24.426Z"
155
156 /*
157 ** CAPI3REF: Run-Time Library Version Numbers
158 ** KEYWORDS: sqlite3_version sqlite3_sourceid
159 **
@@ -11250,23 +11250,45 @@
11250 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11251
11252 /*
11253 ** CAPI3REF: Bind array values to the CARRAY table-valued function
11254 **
11255 ** The sqlite3_carray_bind(S,I,P,N,F,X) interface binds an array value to
11256 ** one of the first argument of the [carray() table-valued function]. The
11257 ** S parameter is a pointer to the [prepared statement] that uses the carray()
11258 ** functions. I is the parameter index to be bound. P is a pointer to the
11259 ** array to be bound, and N is the number of eements in the array. The
11260 ** F argument is one of constants [SQLITE_CARRAY_INT32], [SQLITE_CARRAY_INT64],
11261 ** [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT], or [SQLITE_CARRAY_BLOB] to
11262 ** indicate the datatype of the array being bound. The X argument is not a
11263 ** NULL pointer, then SQLite will invoke the function X on the P parameter
11264 ** after it has finished using P, even if the call to
11265 ** sqlite3_carray_bind() fails. The special-case finalizer
11266 ** SQLITE_TRANSIENT has no effect here.
 
 
 
 
 
 
 
 
 
 
 
 
 
11267 */
 
 
 
 
 
 
 
 
 
11268 SQLITE_API int sqlite3_carray_bind(
11269 sqlite3_stmt *pStmt, /* Statement to be bound */
11270 int i, /* Parameter index */
11271 void *aData, /* Pointer to array data */
11272 int nData, /* Number of data elements */
11273
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,14 +146,14 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.52.0"
150 #define SQLITE_VERSION_NUMBER 3052000
151 #define SQLITE_SOURCE_ID "2026-02-04 20:51:27 c476d956d0bd3065cf894de6f9d393b999ff7d2268a35f01a6d88804789ab58f"
152 #define SQLITE_SCM_BRANCH "trunk"
153 #define SQLITE_SCM_TAGS ""
154 #define SQLITE_SCM_DATETIME "2026-02-04T20:51:27.822Z"
155
156 /*
157 ** CAPI3REF: Run-Time Library Version Numbers
158 ** KEYWORDS: sqlite3_version sqlite3_sourceid
159 **
@@ -11250,23 +11250,45 @@
11250 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11251
11252 /*
11253 ** CAPI3REF: Bind array values to the CARRAY table-valued function
11254 **
11255 ** The sqlite3_carray_bind_v2(S,I,P,N,F,X,D) interface binds an array value to
11256 ** parameter that is the first argument of the [carray() table-valued function].
11257 ** The S parameter is a pointer to the [prepared statement] that uses the carray()
11258 ** functions. I is the parameter index to be bound. I must be the index of the
11259 ** parameter that is the first argument to the carray() table-valued function.
11260 ** P is a pointer to the array to be bound, and N is the number of elements in
11261 ** the array. The F argument is one of constants [SQLITE_CARRAY_INT32],
11262 ** [SQLITE_CARRAY_INT64], [SQLITE_CARRAY_DOUBLE], [SQLITE_CARRAY_TEXT],
11263 ** or [SQLITE_CARRAY_BLOB] to indicate the datatype of the array P.
11264 **
11265 ** If the X argument is not a NULL pointer or one of the special
11266 ** values [SQLITE_STATIC] or [SQLITE_TRANSIENT], then SQLite will invoke
11267 ** the function X with argument D when it is finished using the data in P.
11268 ** The call to X(D) is a destructor for the array P. The destructor X(D)
11269 ** is invoked even if the call to sqlite3_carray_bind() fails. If the X
11270 ** parameter is the special-case value [SQLITE_STATIC], then SQLite assumes
11271 ** that the data static and the destructor is never invoked. If the X
11272 ** parameter is the special-case value [SQLITE_TRANSIENT], then
11273 ** sqlite3_carray_bind_v2() makes its own private copy of the data prior
11274 ** to returning and never invokes the destructor X.
11275 **
11276 ** The sqlite3_carray_bind() function works the same as sqlite_carray_bind_v2()
11277 ** with a D parameter set to P. In other words,
11278 ** sqlite3_carray_bind(S,I,P,N,F,X) is same as
11279 ** sqlite3_carray_bind(S,I,P,N,F,X,P).
11280 */
11281 SQLITE_API int sqlite3_carray_bind_v2(
11282 sqlite3_stmt *pStmt, /* Statement to be bound */
11283 int i, /* Parameter index */
11284 void *aData, /* Pointer to array data */
11285 int nData, /* Number of data elements */
11286 int mFlags, /* CARRAY flags */
11287 void (*xDel)(void*), /* Destructor for aData */
11288 void *pDel /* Optional argument to xDel() */
11289 );
11290 SQLITE_API int sqlite3_carray_bind(
11291 sqlite3_stmt *pStmt, /* Statement to be bound */
11292 int i, /* Parameter index */
11293 void *aData, /* Pointer to array data */
11294 int nData, /* Number of data elements */
11295

Keyboard Shortcuts

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