Fossil SCM

Update the built-in SQLite to the latest 3.47.0 alpha, including the percentile() and median() extensions in the CLI. Update to the latest Pikchr version.

drh 2024-07-24 14:47 trunk
Commit 4a26cb95d5dcdb0c04df867381357abaa4cbdb9e9e888cf3ebb05cb0afa9112f
+3 -2
--- extsrc/pikchr.c
+++ extsrc/pikchr.c
@@ -7213,11 +7213,12 @@
72137213
p->bbox.ne.y += margin + pik_value(p,"topmargin",9,0);
72147214
p->bbox.sw.x -= margin + pik_value(p,"leftmargin",10,0);
72157215
p->bbox.sw.y -= margin + pik_value(p,"bottommargin",12,0);
72167216
72177217
/* Output the SVG */
7218
- pik_append(p, "<svg xmlns='http://www.w3.org/2000/svg'",-1);
7218
+ pik_append(p, "<svg xmlns='http://www.w3.org/2000/svg'"
7219
+ " style='font-size:initial;'",-1);
72197220
if( p->zClass ){
72207221
pik_append(p, " class=\"", -1);
72217222
pik_append(p, p->zClass, -1);
72227223
pik_append(p, "\"", 1);
72237224
}
@@ -8241,6 +8242,6 @@
82418242
82428243
82438244
#endif /* PIKCHR_TCL */
82448245
82458246
8246
-#line 8271 "pikchr.c"
8247
+#line 8272 "pikchr.c"
82478248
--- extsrc/pikchr.c
+++ extsrc/pikchr.c
@@ -7213,11 +7213,12 @@
7213 p->bbox.ne.y += margin + pik_value(p,"topmargin",9,0);
7214 p->bbox.sw.x -= margin + pik_value(p,"leftmargin",10,0);
7215 p->bbox.sw.y -= margin + pik_value(p,"bottommargin",12,0);
7216
7217 /* Output the SVG */
7218 pik_append(p, "<svg xmlns='http://www.w3.org/2000/svg'",-1);
 
7219 if( p->zClass ){
7220 pik_append(p, " class=\"", -1);
7221 pik_append(p, p->zClass, -1);
7222 pik_append(p, "\"", 1);
7223 }
@@ -8241,6 +8242,6 @@
8241
8242
8243 #endif /* PIKCHR_TCL */
8244
8245
8246 #line 8271 "pikchr.c"
8247
--- extsrc/pikchr.c
+++ extsrc/pikchr.c
@@ -7213,11 +7213,12 @@
7213 p->bbox.ne.y += margin + pik_value(p,"topmargin",9,0);
7214 p->bbox.sw.x -= margin + pik_value(p,"leftmargin",10,0);
7215 p->bbox.sw.y -= margin + pik_value(p,"bottommargin",12,0);
7216
7217 /* Output the SVG */
7218 pik_append(p, "<svg xmlns='http://www.w3.org/2000/svg'"
7219 " style='font-size:initial;'",-1);
7220 if( p->zClass ){
7221 pik_append(p, " class=\"", -1);
7222 pik_append(p, p->zClass, -1);
7223 pik_append(p, "\"", 1);
7224 }
@@ -8241,6 +8242,6 @@
8242
8243
8244 #endif /* PIKCHR_TCL */
8245
8246
8247 #line 8272 "pikchr.c"
8248
--- extsrc/pikchr.wasm
+++ extsrc/pikchr.wasm
cannot compute difference between binary files
11
--- extsrc/pikchr.wasm
+++ extsrc/pikchr.wasm
0 annot compute difference between binary files
1
--- extsrc/pikchr.wasm
+++ extsrc/pikchr.wasm
0 annot compute difference between binary files
1
+294 -1
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -4543,10 +4543,302 @@
45434543
}
45444544
return rc;
45454545
}
45464546
45474547
/************************* End ../ext/misc/decimal.c ********************/
4548
+/************************* Begin ../ext/misc/percentile.c ******************/
4549
+/*
4550
+** 2013-05-28
4551
+**
4552
+** The author disclaims copyright to this source code. In place of
4553
+** a legal notice, here is a blessing:
4554
+**
4555
+** May you do good and not evil.
4556
+** May you find forgiveness for yourself and forgive others.
4557
+** May you share freely, never taking more than you give.
4558
+**
4559
+******************************************************************************
4560
+**
4561
+** This file contains code to implement the percentile(Y,P) SQL function
4562
+** as described below:
4563
+**
4564
+** (1) The percentile(Y,P) function is an aggregate function taking
4565
+** exactly two arguments.
4566
+**
4567
+** (2) If the P argument to percentile(Y,P) is not the same for every
4568
+** row in the aggregate then an error is thrown. The word "same"
4569
+** in the previous sentence means that the value differ by less
4570
+** than 0.001.
4571
+**
4572
+** (3) If the P argument to percentile(Y,P) evaluates to anything other
4573
+** than a number in the range of 0.0 to 100.0 inclusive then an
4574
+** error is thrown.
4575
+**
4576
+** (4) If any Y argument to percentile(Y,P) evaluates to a value that
4577
+** is not NULL and is not numeric then an error is thrown.
4578
+**
4579
+** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus
4580
+** infinity then an error is thrown. (SQLite always interprets NaN
4581
+** values as NULL.)
4582
+**
4583
+** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions,
4584
+** including CASE WHEN expressions.
4585
+**
4586
+** (7) The percentile(Y,P) aggregate is able to handle inputs of at least
4587
+** one million (1,000,000) rows.
4588
+**
4589
+** (8) If there are no non-NULL values for Y, then percentile(Y,P)
4590
+** returns NULL.
4591
+**
4592
+** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P)
4593
+** returns the one Y value.
4594
+**
4595
+** (10) If there N non-NULL values of Y where N is two or more and
4596
+** the Y values are ordered from least to greatest and a graph is
4597
+** drawn from 0 to N-1 such that the height of the graph at J is
4598
+** the J-th Y value and such that straight lines are drawn between
4599
+** adjacent Y values, then the percentile(Y,P) function returns
4600
+** the height of the graph at P*(N-1)/100.
4601
+**
4602
+** (11) The percentile(Y,P) function always returns either a floating
4603
+** point number or NULL.
4604
+**
4605
+** (12) The percentile(Y,P) is implemented as a single C99 source-code
4606
+** file that compiles into a shared-library or DLL that can be loaded
4607
+** into SQLite using the sqlite3_load_extension() interface.
4608
+**
4609
+** (13) A separate median(Y) function is the equivalent percentile(Y,50).
4610
+**
4611
+** (14) A separate percentile_cond(Y,X) function is the equivalent of
4612
+** percentile(Y,X*100.0).
4613
+*/
4614
+/* #include "sqlite3ext.h" */
4615
+SQLITE_EXTENSION_INIT1
4616
+#include <assert.h>
4617
+#include <string.h>
4618
+#include <stdlib.h>
4619
+
4620
+/* The following object is the session context for a single percentile()
4621
+** function. We have to remember all input Y values until the very end.
4622
+** Those values are accumulated in the Percentile.a[] array.
4623
+*/
4624
+typedef struct Percentile Percentile;
4625
+struct Percentile {
4626
+ unsigned nAlloc; /* Number of slots allocated for a[] */
4627
+ unsigned nUsed; /* Number of slots actually used in a[] */
4628
+ double rPct; /* 1.0 more than the value for P */
4629
+ double *a; /* Array of Y values */
4630
+};
4631
+
4632
+/*
4633
+** Return TRUE if the input floating-point number is an infinity.
4634
+*/
4635
+static int isInfinity(double r){
4636
+ sqlite3_uint64 u;
4637
+ assert( sizeof(u)==sizeof(r) );
4638
+ memcpy(&u, &r, sizeof(u));
4639
+ return ((u>>52)&0x7ff)==0x7ff;
4640
+}
4641
+
4642
+/*
4643
+** Return TRUE if two doubles differ by 0.001 or less
4644
+*/
4645
+static int sameValue(double a, double b){
4646
+ a -= b;
4647
+ return a>=-0.001 && a<=0.001;
4648
+}
4649
+
4650
+/*
4651
+** The "step" function for percentile(Y,P) is called once for each
4652
+** input row.
4653
+*/
4654
+static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
4655
+ Percentile *p;
4656
+ double rPct;
4657
+ int eType;
4658
+ double y;
4659
+ assert( argc==2 || argc==1 );
4660
+
4661
+ if( argc==1 ){
4662
+ /* Requirement 13: median(Y) is the same as percentile(Y,50). */
4663
+ rPct = 50.0;
4664
+ }else if( sqlite3_user_data(pCtx)==0 ){
4665
+ /* Requirement 3: P must be a number between 0 and 100 */
4666
+ eType = sqlite3_value_numeric_type(argv[1]);
4667
+ rPct = sqlite3_value_double(argv[1]);
4668
+ if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
4669
+ || rPct<0.0 || rPct>100.0 ){
4670
+ sqlite3_result_error(pCtx, "2nd argument to percentile() is not "
4671
+ "a number between 0.0 and 100.0", -1);
4672
+ return;
4673
+ }
4674
+ }else{
4675
+ /* Requirement 3: P must be a number between 0 and 1 */
4676
+ eType = sqlite3_value_numeric_type(argv[1]);
4677
+ rPct = sqlite3_value_double(argv[1]);
4678
+ if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
4679
+ || rPct<0.0 || rPct>1.0 ){
4680
+ sqlite3_result_error(pCtx, "2nd argument to percentile_cont() is not "
4681
+ "a number between 0.0 and 1.0", -1);
4682
+ return;
4683
+ }
4684
+ rPct *= 100.0;
4685
+ }
4686
+
4687
+ /* Allocate the session context. */
4688
+ p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
4689
+ if( p==0 ) return;
4690
+
4691
+ /* Remember the P value. Throw an error if the P value is different
4692
+ ** from any prior row, per Requirement (2). */
4693
+ if( p->rPct==0.0 ){
4694
+ p->rPct = rPct+1.0;
4695
+ }else if( !sameValue(p->rPct,rPct+1.0) ){
4696
+ sqlite3_result_error(pCtx, "2nd argument to percentile() is not the "
4697
+ "same for all input rows", -1);
4698
+ return;
4699
+ }
4700
+
4701
+ /* Ignore rows for which Y is NULL */
4702
+ eType = sqlite3_value_type(argv[0]);
4703
+ if( eType==SQLITE_NULL ) return;
4704
+
4705
+ /* If not NULL, then Y must be numeric. Otherwise throw an error.
4706
+ ** Requirement 4 */
4707
+ if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
4708
+ sqlite3_result_error(pCtx, "1st argument to percentile() is not "
4709
+ "numeric", -1);
4710
+ return;
4711
+ }
4712
+
4713
+ /* Throw an error if the Y value is infinity or NaN */
4714
+ y = sqlite3_value_double(argv[0]);
4715
+ if( isInfinity(y) ){
4716
+ sqlite3_result_error(pCtx, "Inf input to percentile()", -1);
4717
+ return;
4718
+ }
4719
+
4720
+ /* Allocate and store the Y */
4721
+ if( p->nUsed>=p->nAlloc ){
4722
+ unsigned n = p->nAlloc*2 + 250;
4723
+ double *a = sqlite3_realloc64(p->a, sizeof(double)*n);
4724
+ if( a==0 ){
4725
+ sqlite3_free(p->a);
4726
+ memset(p, 0, sizeof(*p));
4727
+ sqlite3_result_error_nomem(pCtx);
4728
+ return;
4729
+ }
4730
+ p->nAlloc = n;
4731
+ p->a = a;
4732
+ }
4733
+ p->a[p->nUsed++] = y;
4734
+}
4735
+
4736
+/*
4737
+** Sort an array of doubles.
4738
+*/
4739
+static void sortDoubles(double *a, int n){
4740
+ int iLt; /* Entries with index less than iLt are less than rPivot */
4741
+ int iGt; /* Entries with index iGt or more are greater than rPivot */
4742
+ int i; /* Loop counter */
4743
+ double rPivot; /* The pivot value */
4744
+ double rTmp; /* Temporary used to swap two values */
4745
+
4746
+ if( n<2 ) return;
4747
+ if( n>5 ){
4748
+ rPivot = (a[0] + a[n/2] + a[n-1])/3.0;
4749
+ }else{
4750
+ rPivot = a[n/2];
4751
+ }
4752
+ iLt = i = 0;
4753
+ iGt = n;
4754
+ while( i<iGt ){
4755
+ if( a[i]<rPivot ){
4756
+ if( i>iLt ){
4757
+ rTmp = a[i];
4758
+ a[i] = a[iLt];
4759
+ a[iLt] = rTmp;
4760
+ }
4761
+ iLt++;
4762
+ i++;
4763
+ }else if( a[i]>rPivot ){
4764
+ do{
4765
+ iGt--;
4766
+ }while( iGt>i && a[iGt]>rPivot );
4767
+ rTmp = a[i];
4768
+ a[i] = a[iGt];
4769
+ a[iGt] = rTmp;
4770
+ }else{
4771
+ i++;
4772
+ }
4773
+ }
4774
+ if( iLt>=2 ) sortDoubles(a, iLt);
4775
+ if( n-iGt>=2 ) sortDoubles(a+iGt, n-iGt);
4776
+
4777
+/* Uncomment for testing */
4778
+#if 0
4779
+ for(i=0; i<n-1; i++){
4780
+ assert( a[i]<=a[i+1] );
4781
+ }
4782
+#endif
4783
+}
4784
+
4785
+/*
4786
+** Called to compute the final output of percentile() and to clean
4787
+** up all allocated memory.
4788
+*/
4789
+static void percentFinal(sqlite3_context *pCtx){
4790
+ Percentile *p;
4791
+ unsigned i1, i2;
4792
+ double v1, v2;
4793
+ double ix, vx;
4794
+ p = (Percentile*)sqlite3_aggregate_context(pCtx, 0);
4795
+ if( p==0 ) return;
4796
+ if( p->a==0 ) return;
4797
+ if( p->nUsed ){
4798
+ sortDoubles(p->a, p->nUsed);
4799
+ ix = (p->rPct-1.0)*(p->nUsed-1)*0.01;
4800
+ i1 = (unsigned)ix;
4801
+ i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1;
4802
+ v1 = p->a[i1];
4803
+ v2 = p->a[i2];
4804
+ vx = v1 + (v2-v1)*(ix-i1);
4805
+ sqlite3_result_double(pCtx, vx);
4806
+ }
4807
+ sqlite3_free(p->a);
4808
+ memset(p, 0, sizeof(*p));
4809
+}
4810
+
4811
+
4812
+#ifdef _WIN32
4813
+
4814
+#endif
4815
+int sqlite3_percentile_init(
4816
+ sqlite3 *db,
4817
+ char **pzErrMsg,
4818
+ const sqlite3_api_routines *pApi
4819
+){
4820
+ int rc = SQLITE_OK;
4821
+ SQLITE_EXTENSION_INIT2(pApi);
4822
+ (void)pzErrMsg; /* Unused parameter */
4823
+ rc = sqlite3_create_function(db, "percentile", 2,
4824
+ SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
4825
+ 0, percentStep, percentFinal);
4826
+ if( rc==SQLITE_OK ){
4827
+ rc = sqlite3_create_function(db, "median", 1,
4828
+ SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
4829
+ 0, percentStep, percentFinal);
4830
+ }
4831
+ if( rc==SQLITE_OK ){
4832
+ rc = sqlite3_create_function(db, "percentile_cont", 2,
4833
+ SQLITE_UTF8|SQLITE_INNOCUOUS, &percentStep,
4834
+ 0, percentStep, percentFinal);
4835
+ }
4836
+ return rc;
4837
+}
4838
+
4839
+/************************* End ../ext/misc/percentile.c ********************/
45484840
#undef sqlite3_base_init
45494841
#define sqlite3_base_init sqlite3_base64_init
45504842
/************************* Begin ../ext/misc/base64.c ******************/
45514843
/*
45524844
** 2022-11-18
@@ -8448,11 +8740,11 @@
84488740
while( sqlite3_step(pS2)==SQLITE_ROW ){
84498741
const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
84508742
zSql = sqlite3_mprintf(
84518743
"%z%s"
84528744
"SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
8453
- " JOIN pragma_table_info(sm.name,%Q) AS pti"
8745
+ " JOIN pragma_table_xinfo(sm.name,%Q) AS pti"
84548746
" WHERE sm.type='table'",
84558747
zSql, zSep, zDb, zDb
84568748
);
84578749
if( zSql==0 ) return SQLITE_NOMEM;
84588750
zSep = " UNION ";
@@ -23581,10 +23873,11 @@
2358123873
#endif
2358223874
sqlite3_shathree_init(p->db, 0, 0);
2358323875
sqlite3_uint_init(p->db, 0, 0);
2358423876
sqlite3_stmtrand_init(p->db, 0, 0);
2358523877
sqlite3_decimal_init(p->db, 0, 0);
23878
+ sqlite3_percentile_init(p->db, 0, 0);
2358623879
sqlite3_base64_init(p->db, 0, 0);
2358723880
sqlite3_base85_init(p->db, 0, 0);
2358823881
sqlite3_regexp_init(p->db, 0, 0);
2358923882
sqlite3_ieee_init(p->db, 0, 0);
2359023883
sqlite3_series_init(p->db, 0, 0);
2359123884
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -4543,10 +4543,302 @@
4543 }
4544 return rc;
4545 }
4546
4547 /************************* End ../ext/misc/decimal.c ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4548 #undef sqlite3_base_init
4549 #define sqlite3_base_init sqlite3_base64_init
4550 /************************* Begin ../ext/misc/base64.c ******************/
4551 /*
4552 ** 2022-11-18
@@ -8448,11 +8740,11 @@
8448 while( sqlite3_step(pS2)==SQLITE_ROW ){
8449 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
8450 zSql = sqlite3_mprintf(
8451 "%z%s"
8452 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
8453 " JOIN pragma_table_info(sm.name,%Q) AS pti"
8454 " WHERE sm.type='table'",
8455 zSql, zSep, zDb, zDb
8456 );
8457 if( zSql==0 ) return SQLITE_NOMEM;
8458 zSep = " UNION ";
@@ -23581,10 +23873,11 @@
23581 #endif
23582 sqlite3_shathree_init(p->db, 0, 0);
23583 sqlite3_uint_init(p->db, 0, 0);
23584 sqlite3_stmtrand_init(p->db, 0, 0);
23585 sqlite3_decimal_init(p->db, 0, 0);
 
23586 sqlite3_base64_init(p->db, 0, 0);
23587 sqlite3_base85_init(p->db, 0, 0);
23588 sqlite3_regexp_init(p->db, 0, 0);
23589 sqlite3_ieee_init(p->db, 0, 0);
23590 sqlite3_series_init(p->db, 0, 0);
23591
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -4543,10 +4543,302 @@
4543 }
4544 return rc;
4545 }
4546
4547 /************************* End ../ext/misc/decimal.c ********************/
4548 /************************* Begin ../ext/misc/percentile.c ******************/
4549 /*
4550 ** 2013-05-28
4551 **
4552 ** The author disclaims copyright to this source code. In place of
4553 ** a legal notice, here is a blessing:
4554 **
4555 ** May you do good and not evil.
4556 ** May you find forgiveness for yourself and forgive others.
4557 ** May you share freely, never taking more than you give.
4558 **
4559 ******************************************************************************
4560 **
4561 ** This file contains code to implement the percentile(Y,P) SQL function
4562 ** as described below:
4563 **
4564 ** (1) The percentile(Y,P) function is an aggregate function taking
4565 ** exactly two arguments.
4566 **
4567 ** (2) If the P argument to percentile(Y,P) is not the same for every
4568 ** row in the aggregate then an error is thrown. The word "same"
4569 ** in the previous sentence means that the value differ by less
4570 ** than 0.001.
4571 **
4572 ** (3) If the P argument to percentile(Y,P) evaluates to anything other
4573 ** than a number in the range of 0.0 to 100.0 inclusive then an
4574 ** error is thrown.
4575 **
4576 ** (4) If any Y argument to percentile(Y,P) evaluates to a value that
4577 ** is not NULL and is not numeric then an error is thrown.
4578 **
4579 ** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus
4580 ** infinity then an error is thrown. (SQLite always interprets NaN
4581 ** values as NULL.)
4582 **
4583 ** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions,
4584 ** including CASE WHEN expressions.
4585 **
4586 ** (7) The percentile(Y,P) aggregate is able to handle inputs of at least
4587 ** one million (1,000,000) rows.
4588 **
4589 ** (8) If there are no non-NULL values for Y, then percentile(Y,P)
4590 ** returns NULL.
4591 **
4592 ** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P)
4593 ** returns the one Y value.
4594 **
4595 ** (10) If there N non-NULL values of Y where N is two or more and
4596 ** the Y values are ordered from least to greatest and a graph is
4597 ** drawn from 0 to N-1 such that the height of the graph at J is
4598 ** the J-th Y value and such that straight lines are drawn between
4599 ** adjacent Y values, then the percentile(Y,P) function returns
4600 ** the height of the graph at P*(N-1)/100.
4601 **
4602 ** (11) The percentile(Y,P) function always returns either a floating
4603 ** point number or NULL.
4604 **
4605 ** (12) The percentile(Y,P) is implemented as a single C99 source-code
4606 ** file that compiles into a shared-library or DLL that can be loaded
4607 ** into SQLite using the sqlite3_load_extension() interface.
4608 **
4609 ** (13) A separate median(Y) function is the equivalent percentile(Y,50).
4610 **
4611 ** (14) A separate percentile_cond(Y,X) function is the equivalent of
4612 ** percentile(Y,X*100.0).
4613 */
4614 /* #include "sqlite3ext.h" */
4615 SQLITE_EXTENSION_INIT1
4616 #include <assert.h>
4617 #include <string.h>
4618 #include <stdlib.h>
4619
4620 /* The following object is the session context for a single percentile()
4621 ** function. We have to remember all input Y values until the very end.
4622 ** Those values are accumulated in the Percentile.a[] array.
4623 */
4624 typedef struct Percentile Percentile;
4625 struct Percentile {
4626 unsigned nAlloc; /* Number of slots allocated for a[] */
4627 unsigned nUsed; /* Number of slots actually used in a[] */
4628 double rPct; /* 1.0 more than the value for P */
4629 double *a; /* Array of Y values */
4630 };
4631
4632 /*
4633 ** Return TRUE if the input floating-point number is an infinity.
4634 */
4635 static int isInfinity(double r){
4636 sqlite3_uint64 u;
4637 assert( sizeof(u)==sizeof(r) );
4638 memcpy(&u, &r, sizeof(u));
4639 return ((u>>52)&0x7ff)==0x7ff;
4640 }
4641
4642 /*
4643 ** Return TRUE if two doubles differ by 0.001 or less
4644 */
4645 static int sameValue(double a, double b){
4646 a -= b;
4647 return a>=-0.001 && a<=0.001;
4648 }
4649
4650 /*
4651 ** The "step" function for percentile(Y,P) is called once for each
4652 ** input row.
4653 */
4654 static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){
4655 Percentile *p;
4656 double rPct;
4657 int eType;
4658 double y;
4659 assert( argc==2 || argc==1 );
4660
4661 if( argc==1 ){
4662 /* Requirement 13: median(Y) is the same as percentile(Y,50). */
4663 rPct = 50.0;
4664 }else if( sqlite3_user_data(pCtx)==0 ){
4665 /* Requirement 3: P must be a number between 0 and 100 */
4666 eType = sqlite3_value_numeric_type(argv[1]);
4667 rPct = sqlite3_value_double(argv[1]);
4668 if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
4669 || rPct<0.0 || rPct>100.0 ){
4670 sqlite3_result_error(pCtx, "2nd argument to percentile() is not "
4671 "a number between 0.0 and 100.0", -1);
4672 return;
4673 }
4674 }else{
4675 /* Requirement 3: P must be a number between 0 and 1 */
4676 eType = sqlite3_value_numeric_type(argv[1]);
4677 rPct = sqlite3_value_double(argv[1]);
4678 if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT)
4679 || rPct<0.0 || rPct>1.0 ){
4680 sqlite3_result_error(pCtx, "2nd argument to percentile_cont() is not "
4681 "a number between 0.0 and 1.0", -1);
4682 return;
4683 }
4684 rPct *= 100.0;
4685 }
4686
4687 /* Allocate the session context. */
4688 p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p));
4689 if( p==0 ) return;
4690
4691 /* Remember the P value. Throw an error if the P value is different
4692 ** from any prior row, per Requirement (2). */
4693 if( p->rPct==0.0 ){
4694 p->rPct = rPct+1.0;
4695 }else if( !sameValue(p->rPct,rPct+1.0) ){
4696 sqlite3_result_error(pCtx, "2nd argument to percentile() is not the "
4697 "same for all input rows", -1);
4698 return;
4699 }
4700
4701 /* Ignore rows for which Y is NULL */
4702 eType = sqlite3_value_type(argv[0]);
4703 if( eType==SQLITE_NULL ) return;
4704
4705 /* If not NULL, then Y must be numeric. Otherwise throw an error.
4706 ** Requirement 4 */
4707 if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){
4708 sqlite3_result_error(pCtx, "1st argument to percentile() is not "
4709 "numeric", -1);
4710 return;
4711 }
4712
4713 /* Throw an error if the Y value is infinity or NaN */
4714 y = sqlite3_value_double(argv[0]);
4715 if( isInfinity(y) ){
4716 sqlite3_result_error(pCtx, "Inf input to percentile()", -1);
4717 return;
4718 }
4719
4720 /* Allocate and store the Y */
4721 if( p->nUsed>=p->nAlloc ){
4722 unsigned n = p->nAlloc*2 + 250;
4723 double *a = sqlite3_realloc64(p->a, sizeof(double)*n);
4724 if( a==0 ){
4725 sqlite3_free(p->a);
4726 memset(p, 0, sizeof(*p));
4727 sqlite3_result_error_nomem(pCtx);
4728 return;
4729 }
4730 p->nAlloc = n;
4731 p->a = a;
4732 }
4733 p->a[p->nUsed++] = y;
4734 }
4735
4736 /*
4737 ** Sort an array of doubles.
4738 */
4739 static void sortDoubles(double *a, int n){
4740 int iLt; /* Entries with index less than iLt are less than rPivot */
4741 int iGt; /* Entries with index iGt or more are greater than rPivot */
4742 int i; /* Loop counter */
4743 double rPivot; /* The pivot value */
4744 double rTmp; /* Temporary used to swap two values */
4745
4746 if( n<2 ) return;
4747 if( n>5 ){
4748 rPivot = (a[0] + a[n/2] + a[n-1])/3.0;
4749 }else{
4750 rPivot = a[n/2];
4751 }
4752 iLt = i = 0;
4753 iGt = n;
4754 while( i<iGt ){
4755 if( a[i]<rPivot ){
4756 if( i>iLt ){
4757 rTmp = a[i];
4758 a[i] = a[iLt];
4759 a[iLt] = rTmp;
4760 }
4761 iLt++;
4762 i++;
4763 }else if( a[i]>rPivot ){
4764 do{
4765 iGt--;
4766 }while( iGt>i && a[iGt]>rPivot );
4767 rTmp = a[i];
4768 a[i] = a[iGt];
4769 a[iGt] = rTmp;
4770 }else{
4771 i++;
4772 }
4773 }
4774 if( iLt>=2 ) sortDoubles(a, iLt);
4775 if( n-iGt>=2 ) sortDoubles(a+iGt, n-iGt);
4776
4777 /* Uncomment for testing */
4778 #if 0
4779 for(i=0; i<n-1; i++){
4780 assert( a[i]<=a[i+1] );
4781 }
4782 #endif
4783 }
4784
4785 /*
4786 ** Called to compute the final output of percentile() and to clean
4787 ** up all allocated memory.
4788 */
4789 static void percentFinal(sqlite3_context *pCtx){
4790 Percentile *p;
4791 unsigned i1, i2;
4792 double v1, v2;
4793 double ix, vx;
4794 p = (Percentile*)sqlite3_aggregate_context(pCtx, 0);
4795 if( p==0 ) return;
4796 if( p->a==0 ) return;
4797 if( p->nUsed ){
4798 sortDoubles(p->a, p->nUsed);
4799 ix = (p->rPct-1.0)*(p->nUsed-1)*0.01;
4800 i1 = (unsigned)ix;
4801 i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1;
4802 v1 = p->a[i1];
4803 v2 = p->a[i2];
4804 vx = v1 + (v2-v1)*(ix-i1);
4805 sqlite3_result_double(pCtx, vx);
4806 }
4807 sqlite3_free(p->a);
4808 memset(p, 0, sizeof(*p));
4809 }
4810
4811
4812 #ifdef _WIN32
4813
4814 #endif
4815 int sqlite3_percentile_init(
4816 sqlite3 *db,
4817 char **pzErrMsg,
4818 const sqlite3_api_routines *pApi
4819 ){
4820 int rc = SQLITE_OK;
4821 SQLITE_EXTENSION_INIT2(pApi);
4822 (void)pzErrMsg; /* Unused parameter */
4823 rc = sqlite3_create_function(db, "percentile", 2,
4824 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
4825 0, percentStep, percentFinal);
4826 if( rc==SQLITE_OK ){
4827 rc = sqlite3_create_function(db, "median", 1,
4828 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
4829 0, percentStep, percentFinal);
4830 }
4831 if( rc==SQLITE_OK ){
4832 rc = sqlite3_create_function(db, "percentile_cont", 2,
4833 SQLITE_UTF8|SQLITE_INNOCUOUS, &percentStep,
4834 0, percentStep, percentFinal);
4835 }
4836 return rc;
4837 }
4838
4839 /************************* End ../ext/misc/percentile.c ********************/
4840 #undef sqlite3_base_init
4841 #define sqlite3_base_init sqlite3_base64_init
4842 /************************* Begin ../ext/misc/base64.c ******************/
4843 /*
4844 ** 2022-11-18
@@ -8448,11 +8740,11 @@
8740 while( sqlite3_step(pS2)==SQLITE_ROW ){
8741 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
8742 zSql = sqlite3_mprintf(
8743 "%z%s"
8744 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
8745 " JOIN pragma_table_xinfo(sm.name,%Q) AS pti"
8746 " WHERE sm.type='table'",
8747 zSql, zSep, zDb, zDb
8748 );
8749 if( zSql==0 ) return SQLITE_NOMEM;
8750 zSep = " UNION ";
@@ -23581,10 +23873,11 @@
23873 #endif
23874 sqlite3_shathree_init(p->db, 0, 0);
23875 sqlite3_uint_init(p->db, 0, 0);
23876 sqlite3_stmtrand_init(p->db, 0, 0);
23877 sqlite3_decimal_init(p->db, 0, 0);
23878 sqlite3_percentile_init(p->db, 0, 0);
23879 sqlite3_base64_init(p->db, 0, 0);
23880 sqlite3_base85_init(p->db, 0, 0);
23881 sqlite3_regexp_init(p->db, 0, 0);
23882 sqlite3_ieee_init(p->db, 0, 0);
23883 sqlite3_series_init(p->db, 0, 0);
23884
+182 -37
--- 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
-** baa83b460c677c210c7fa3f20314d7e05f30.
21
+** bcc31846964102385d5a21eb5e85d7db153b.
2222
*/
2323
#define SQLITE_CORE 1
2424
#define SQLITE_AMALGAMATION 1
2525
#ifndef SQLITE_PRIVATE
2626
# define SQLITE_PRIVATE static
@@ -462,11 +462,11 @@
462462
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463463
** [sqlite_version()] and [sqlite_source_id()].
464464
*/
465465
#define SQLITE_VERSION "3.47.0"
466466
#define SQLITE_VERSION_NUMBER 3047000
467
-#define SQLITE_SOURCE_ID "2024-07-03 20:19:33 baa83b460c677c210c7fa3f20314d7e05f305aed8a69026bc5fa106a3de4ea38"
467
+#define SQLITE_SOURCE_ID "2024-07-24 13:53:51 bcc31846964102385d5a21eb5e85d7db153b155e76b4e2847c9453d3d0e1af04"
468468
469469
/*
470470
** CAPI3REF: Run-Time Library Version Numbers
471471
** KEYWORDS: sqlite3_version sqlite3_sourceid
472472
**
@@ -16480,10 +16480,23 @@
1648016480
** The names of the following types declared in vdbeInt.h are required
1648116481
** for the VdbeOp definition.
1648216482
*/
1648316483
typedef struct sqlite3_value Mem;
1648416484
typedef struct SubProgram SubProgram;
16485
+typedef struct SubrtnSig SubrtnSig;
16486
+
16487
+/*
16488
+** A signature for a reusable subroutine that materializes the RHS of
16489
+** an IN operator.
16490
+*/
16491
+struct SubrtnSig {
16492
+ int selId; /* SELECT-id for the SELECT statement on the RHS */
16493
+ char *zAff; /* Affinity of the overall IN expression */
16494
+ int iTable; /* Ephemeral table generated by the subroutine */
16495
+ int iAddr; /* Subroutine entry address */
16496
+ int regReturn; /* Register used to hold return address */
16497
+};
1648516498
1648616499
/*
1648716500
** A single instruction of the virtual machine has an opcode
1648816501
** and as many as three operands. The instruction is recorded
1648916502
** as an instance of the following structure:
@@ -16508,10 +16521,11 @@
1650816521
VTable *pVtab; /* Used when p4type is P4_VTAB */
1650916522
KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
1651016523
u32 *ai; /* Used when p4type is P4_INTARRAY */
1651116524
SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
1651216525
Table *pTab; /* Used when p4type is P4_TABLE */
16526
+ SubrtnSig *pSubrtnSig; /* Used when p4type is P4_SUBRTNSIG */
1651316527
#ifdef SQLITE_ENABLE_CURSOR_HINTS
1651416528
Expr *pExpr; /* Used when p4type is P4_EXPR */
1651516529
#endif
1651616530
} p4;
1651716531
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -16575,10 +16589,11 @@
1657516589
#define P4_REAL (-12) /* P4 is a 64-bit floating point value */
1657616590
#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
1657716591
#define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
1657816592
#define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
1657916593
#define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
16594
+#define P4_SUBRTNSIG (-17) /* P4 is a SubrtnSig pointer */
1658016595
1658116596
/* Error message codes for OP_Halt */
1658216597
#define P5_ConstraintNotNull 1
1658316598
#define P5_ConstraintUnique 2
1658416599
#define P5_ConstraintCheck 3
@@ -19807,10 +19822,11 @@
1980719822
u8 okConstFactor; /* OK to factor out constants */
1980819823
u8 disableLookaside; /* Number of times lookaside has been disabled */
1980919824
u8 prepFlags; /* SQLITE_PREPARE_* flags */
1981019825
u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
1981119826
u8 bHasWith; /* True if statement contains WITH */
19827
+ u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
1981219828
#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
1981319829
u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
1981419830
#endif
1981519831
#ifdef SQLITE_DEBUG
1981619832
u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
@@ -32829,18 +32845,16 @@
3282932845
if( pItem->fg.isUsing ) n++;
3283032846
if( pItem->fg.isUsing ){
3283132847
sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
3283232848
}
3283332849
if( pItem->pSelect ){
32834
- sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc);
3283532850
if( pItem->pTab ){
3283632851
Table *pTab = pItem->pTab;
3283732852
sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
3283832853
}
3283932854
assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
3284032855
sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
32841
- sqlite3TreeViewPop(&pView);
3284232856
}
3284332857
if( pItem->fg.isTabFunc ){
3284432858
sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
3284532859
}
3284632860
sqlite3TreeViewPop(&pView);
@@ -32878,11 +32892,11 @@
3287832892
if( cnt++ ) sqlite3TreeViewPop(&pView);
3287932893
if( p->pPrior ){
3288032894
n = 1000;
3288132895
}else{
3288232896
n = 0;
32883
- if( p->pSrc && p->pSrc->nSrc ) n++;
32897
+ if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ) n++;
3288432898
if( p->pWhere ) n++;
3288532899
if( p->pGroupBy ) n++;
3288632900
if( p->pHaving ) n++;
3288732901
if( p->pOrderBy ) n++;
3288832902
if( p->pLimit ) n++;
@@ -32904,11 +32918,11 @@
3290432918
sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
3290532919
}
3290632920
sqlite3TreeViewPop(&pView);
3290732921
}
3290832922
#endif
32909
- if( p->pSrc && p->pSrc->nSrc ){
32923
+ if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ){
3291032924
sqlite3TreeViewPush(&pView, (n--)>0);
3291132925
sqlite3TreeViewLine(pView, "FROM");
3291232926
sqlite3TreeViewSrcList(pView, p->pSrc);
3291332927
sqlite3TreeViewPop(&pView);
3291432928
}
@@ -54746,10 +54760,11 @@
5474654760
memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
5474754761
pPgHdr->pPage = pPage;
5474854762
pPgHdr->pData = pPage->pBuf;
5474954763
pPgHdr->pExtra = (void *)&pPgHdr[1];
5475054764
memset(pPgHdr->pExtra, 0, 8);
54765
+ assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) );
5475154766
pPgHdr->pCache = pCache;
5475254767
pPgHdr->pgno = pgno;
5475354768
pPgHdr->flags = PGHDR_CLEAN;
5475454769
return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
5475554770
}
@@ -55492,11 +55507,12 @@
5549255507
if( zBulk ){
5549355508
int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
5549455509
do{
5549555510
PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
5549655511
pX->page.pBuf = zBulk;
55497
- pX->page.pExtra = &pX[1];
55512
+ pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX));
55513
+ assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) );
5549855514
pX->isBulkLocal = 1;
5549955515
pX->isAnchor = 0;
5550055516
pX->pNext = pCache->pFree;
5550155517
pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
5550255518
pCache->pFree = pX;
@@ -55629,11 +55645,12 @@
5562955645
pcache1EnterMutex(pCache->pGroup);
5563055646
#endif
5563155647
if( pPg==0 ) return 0;
5563255648
p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
5563355649
p->page.pBuf = pPg;
55634
- p->page.pExtra = &p[1];
55650
+ p->page.pExtra = (u8*)p + ROUND8(sizeof(*p));
55651
+ assert( EIGHT_BYTE_ALIGNMENT( p->page.pExtra ) );
5563555652
p->isBulkLocal = 0;
5563655653
p->isAnchor = 0;
5563755654
p->pLruPrev = 0; /* Initializing this saves a valgrind error */
5563855655
}
5563955656
(*pCache->pnPurgeable)++;
@@ -61188,10 +61205,11 @@
6118861205
if( p==0 ){
6118961206
sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
6119061207
return SQLITE_NOMEM_BKPT;
6119161208
}
6119261209
p->pExtra = (void *)&p[1];
61210
+ assert( EIGHT_BYTE_ALIGNMENT( p->pExtra ) );
6119361211
p->flags = PGHDR_MMAP;
6119461212
p->nRef = 1;
6119561213
p->pPager = pPager;
6119661214
}
6119761215
@@ -86272,10 +86290,16 @@
8627286290
}
8627386291
case P4_TABLEREF: {
8627486292
if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
8627586293
break;
8627686294
}
86295
+ case P4_SUBRTNSIG: {
86296
+ SubrtnSig *pSig = (SubrtnSig*)p4;
86297
+ sqlite3DbFree(db, pSig->zAff);
86298
+ sqlite3DbFree(db, pSig);
86299
+ break;
86300
+ }
8627786301
}
8627886302
}
8627986303
8628086304
/*
8628186305
** Free the space allocated for aOp and any p4 values allocated for the
@@ -86850,10 +86874,15 @@
8685086874
break;
8685186875
}
8685286876
case P4_TABLE: {
8685386877
zP4 = pOp->p4.pTab->zName;
8685486878
break;
86879
+ }
86880
+ case P4_SUBRTNSIG: {
86881
+ SubrtnSig *pSig = pOp->p4.pSubrtnSig;
86882
+ sqlite3_str_appendf(&x, "subrtnsig:%d,%s", pSig->selId, pSig->zAff);
86883
+ break;
8685586884
}
8685686885
default: {
8685786886
zP4 = pOp->p4.z;
8685886887
}
8685986888
}
@@ -100806,22 +100835,33 @@
100806100835
*/
100807100836
case OP_AggInverse:
100808100837
case OP_AggStep: {
100809100838
int n;
100810100839
sqlite3_context *pCtx;
100840
+ u64 nAlloc;
100811100841
100812100842
assert( pOp->p4type==P4_FUNCDEF );
100813100843
n = pOp->p5;
100814100844
assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100815100845
assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
100816100846
assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
100817
- pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
100818
- (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
100847
+
100848
+ /* Allocate space for (a) the context object and (n-1) extra pointers
100849
+ ** to append to the sqlite3_context.argv[1] array, and (b) a memory
100850
+ ** cell in which to store the accumulation. Be careful that the memory
100851
+ ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits.
100852
+ **
100853
+ ** Note: We could avoid this by using a regular memory cell from aMem[] for
100854
+ ** the accumulator, instead of allocating one here. */
100855
+ nAlloc = ROUND8P( sizeof(pCtx[0]) + (n-1)*sizeof(sqlite3_value*) );
100856
+ pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem));
100819100857
if( pCtx==0 ) goto no_mem;
100820
- pCtx->pMem = 0;
100821
- pCtx->pOut = (Mem*)&(pCtx->argv[n]);
100858
+ pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc);
100859
+ assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) );
100860
+
100822100861
sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
100862
+ pCtx->pMem = 0;
100823100863
pCtx->pFunc = pOp->p4.pFunc;
100824100864
pCtx->iOp = (int)(pOp - aOp);
100825100865
pCtx->pVdbe = p;
100826100866
pCtx->skipFlag = 0;
100827100867
pCtx->isError = 0;
@@ -112478,10 +112518,53 @@
112478112518
{
112479112519
sqlite3ErrorMsg(pParse, "row value misused");
112480112520
}
112481112521
}
112482112522
112523
+#ifndef SQLITE_OMIT_SUBQUERY
112524
+/*
112525
+** Scan all previously generated bytecode looking for an OP_BeginSubrtn
112526
+** that is compatible with pExpr. If found, add the y.sub values
112527
+** to pExpr and return true. If not found, return false.
112528
+*/
112529
+static int findCompatibleInRhsSubrtn(
112530
+ Parse *pParse, /* Parsing context */
112531
+ Expr *pExpr, /* IN operator with RHS that we want to reuse */
112532
+ SubrtnSig *pNewSig /* Signature for the IN operator */
112533
+){
112534
+ VdbeOp *pOp, *pEnd;
112535
+ SubrtnSig *pSig;
112536
+ Vdbe *v;
112537
+
112538
+ if( pNewSig==0 ) return 0;
112539
+ if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0;
112540
+ assert( pExpr->op==TK_IN );
112541
+ assert( !ExprUseYSub(pExpr) );
112542
+ assert( ExprUseXSelect(pExpr) );
112543
+ assert( pExpr->x.pSelect!=0 );
112544
+ assert( (pExpr->x.pSelect->selFlags & SF_All)==0 );
112545
+ v = pParse->pVdbe;
112546
+ assert( v!=0 );
112547
+ pOp = sqlite3VdbeGetOp(v, 1);
112548
+ pEnd = sqlite3VdbeGetLastOp(v);
112549
+ for(; pOp<pEnd; pOp++){
112550
+ if( pOp->p4type!=P4_SUBRTNSIG ) continue;
112551
+ assert( pOp->opcode==OP_BeginSubrtn );
112552
+ pSig = pOp->p4.pSubrtnSig;
112553
+ assert( pSig!=0 );
112554
+ if( pNewSig->selId!=pSig->selId ) continue;
112555
+ if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue;
112556
+ pExpr->y.sub.iAddr = pSig->iAddr;
112557
+ pExpr->y.sub.regReturn = pSig->regReturn;
112558
+ pExpr->iTable = pSig->iTable;
112559
+ ExprSetProperty(pExpr, EP_Subrtn);
112560
+ return 1;
112561
+ }
112562
+ return 0;
112563
+}
112564
+#endif /* SQLITE_OMIT_SUBQUERY */
112565
+
112483112566
#ifndef SQLITE_OMIT_SUBQUERY
112484112567
/*
112485112568
** Generate code that will construct an ephemeral table containing all terms
112486112569
** in the RHS of an IN operator. The IN operator can be in either of two
112487112570
** forms:
@@ -112527,15 +112610,32 @@
112527112610
**
112528112611
** If all of the above are false, then we can compute the RHS just once
112529112612
** and reuse it many names.
112530112613
*/
112531112614
if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
112532
- /* Reuse of the RHS is allowed */
112533
- /* If this routine has already been coded, but the previous code
112534
- ** might not have been invoked yet, so invoke it now as a subroutine.
112615
+ /* Reuse of the RHS is allowed
112616
+ **
112617
+ ** Compute a signature for the RHS of the IN operator to facility
112618
+ ** finding and reusing prior instances of the same IN operator.
112535112619
*/
112536
- if( ExprHasProperty(pExpr, EP_Subrtn) ){
112620
+ SubrtnSig *pSig = 0;
112621
+ assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 );
112622
+ if( ExprUseXSelect(pExpr) && (pExpr->x.pSelect->selFlags & SF_All)==0 ){
112623
+ pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0]));
112624
+ if( pSig ){
112625
+ pSig->selId = pExpr->x.pSelect->selId;
112626
+ pSig->zAff = exprINAffinity(pParse, pExpr);
112627
+ }
112628
+ }
112629
+
112630
+ /* Check to see if there is a prior materialization of the RHS of
112631
+ ** this IN operator. If there is, then make use of that prior
112632
+ ** materialization rather than recomputing it.
112633
+ */
112634
+ if( ExprHasProperty(pExpr, EP_Subrtn)
112635
+ || findCompatibleInRhsSubrtn(pParse, pExpr, pSig)
112636
+ ){
112537112637
addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112538112638
if( ExprUseXSelect(pExpr) ){
112539112639
ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
112540112640
pExpr->x.pSelect->selId));
112541112641
}
@@ -112543,10 +112643,14 @@
112543112643
sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112544112644
pExpr->y.sub.iAddr);
112545112645
assert( iTab!=pExpr->iTable );
112546112646
sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
112547112647
sqlite3VdbeJumpHere(v, addrOnce);
112648
+ if( pSig ){
112649
+ sqlite3DbFree(pParse->db, pSig->zAff);
112650
+ sqlite3DbFree(pParse->db, pSig);
112651
+ }
112548112652
return;
112549112653
}
112550112654
112551112655
/* Begin coding the subroutine */
112552112656
assert( !ExprUseYWin(pExpr) );
@@ -112553,11 +112657,17 @@
112553112657
ExprSetProperty(pExpr, EP_Subrtn);
112554112658
assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
112555112659
pExpr->y.sub.regReturn = ++pParse->nMem;
112556112660
pExpr->y.sub.iAddr =
112557112661
sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112558
-
112662
+ if( pSig ){
112663
+ pSig->iAddr = pExpr->y.sub.iAddr;
112664
+ pSig->regReturn = pExpr->y.sub.regReturn;
112665
+ pSig->iTable = iTab;
112666
+ pParse->mSubrtnSig = 1 << (pSig->selId&7);
112667
+ sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG);
112668
+ }
112559112669
addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112560112670
}
112561112671
112562112672
/* Check to see if this is a vector IN operator */
112563112673
pLeft = pExpr->pLeft;
@@ -112915,13 +113025,11 @@
112915113025
assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
112916113026
pLeft = pExpr->pLeft;
112917113027
if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
112918113028
zAff = exprINAffinity(pParse, pExpr);
112919113029
nVector = sqlite3ExprVectorSize(pExpr->pLeft);
112920
- aiMap = (int*)sqlite3DbMallocZero(
112921
- pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
112922
- );
113030
+ aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int));
112923113031
if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
112924113032
112925113033
/* Attempt to compute the RHS. After this step, if anything other than
112926113034
** IN_INDEX_NOOP is returned, the table opened with cursor iTab
112927113035
** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
@@ -120388,10 +120496,11 @@
120388120496
if( pIdx->aSample==0 ){
120389120497
sqlite3_finalize(pStmt);
120390120498
return SQLITE_NOMEM_BKPT;
120391120499
}
120392120500
pSpace = (tRowcnt*)&pIdx->aSample[nSample];
120501
+ assert( EIGHT_BYTE_ALIGNMENT( pSpace ) );
120393120502
pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
120394120503
pIdx->pTable->tabFlags |= TF_HasStat4;
120395120504
for(i=0; i<nSample; i++){
120396120505
pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
120397120506
pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
@@ -148069,11 +148178,12 @@
148069148178
** The hope is that the terms added to the inner query will make it more
148070148179
** efficient.
148071148180
**
148072148181
** NAME AMBIGUITY
148073148182
**
148074
-** This optimization is called the "WHERE-clause push-down optimization".
148183
+** This optimization is called the "WHERE-clause push-down optimization"
148184
+** or sometimes the "predicate push-down optimization".
148075148185
**
148076148186
** Do not confuse this optimization with another unrelated optimization
148077148187
** with a similar name: The "MySQL push-down optimization" causes WHERE
148078148188
** clause terms that can be evaluated using only the index and without
148079148189
** reference to the table are run first, so that if they are false,
@@ -163046,10 +163156,16 @@
163046163156
){
163047163157
Vdbe *v = pParse->pVdbe;
163048163158
VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
163049163159
int iEnd = sqlite3VdbeCurrentAddr(v);
163050163160
if( pParse->db->mallocFailed ) return;
163161
+#ifdef SQLITE_DEBUG
163162
+ if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
163163
+ printf("CHECKING for column-to-copy on cursor %d for %d..%d\n",
163164
+ iTabCur, iStart, iEnd);
163165
+ }
163166
+#endif
163051163167
for(; iStart<iEnd; iStart++, pOp++){
163052163168
if( pOp->p1!=iTabCur ) continue;
163053163169
if( pOp->opcode==OP_Column ){
163054163170
#ifdef SQLITE_DEBUG
163055163171
if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
@@ -232533,10 +232649,26 @@
232533232649
** Constants for the largest and smallest possible 64-bit signed integers.
232534232650
*/
232535232651
# define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
232536232652
# define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
232537232653
232654
+/* The uptr type is an unsigned integer large enough to hold a pointer
232655
+*/
232656
+#if defined(HAVE_STDINT_H)
232657
+ typedef uintptr_t uptr;
232658
+#elif SQLITE_PTRSIZE==4
232659
+ typedef u32 uptr;
232660
+#else
232661
+ typedef u64 uptr;
232662
+#endif
232663
+
232664
+#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
232665
+# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
232666
+#else
232667
+# define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
232668
+#endif
232669
+
232538232670
#endif
232539232671
232540232672
/* Truncate very long tokens to this many bytes. Hard limit is
232541232673
** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
232542232674
** field that occurs at the start of each leaf page (see fts5_index.c). */
@@ -241777,15 +241909,16 @@
241777241909
if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
241778241910
241779241911
if( rc==SQLITE_OK ){
241780241912
u8 *aOut = 0; /* Read blob data into this buffer */
241781241913
int nByte = sqlite3_blob_bytes(p->pReader);
241782
- sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
241914
+ int szData = (sizeof(Fts5Data) + 7) & ~7;
241915
+ sqlite3_int64 nAlloc = szData + nByte + FTS5_DATA_PADDING;
241783241916
pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
241784241917
if( pRet ){
241785241918
pRet->nn = nByte;
241786
- aOut = pRet->p = (u8*)&pRet[1];
241919
+ aOut = pRet->p = (u8*)pRet + szData;
241787241920
}else{
241788241921
rc = SQLITE_NOMEM;
241789241922
}
241790241923
241791241924
if( rc==SQLITE_OK ){
@@ -241804,10 +241937,11 @@
241804241937
p->rc = rc;
241805241938
p->nRead++;
241806241939
}
241807241940
241808241941
assert( (pRet==0)==(p->rc!=SQLITE_OK) );
241942
+ assert( pRet==0 || EIGHT_BYTE_ALIGNMENT( pRet->p ) );
241809241943
return pRet;
241810241944
}
241811241945
241812241946
241813241947
/*
@@ -251232,10 +251366,22 @@
251232251366
return sqlite3_value_int64(pVal);
251233251367
}
251234251368
}
251235251369
return iDefault;
251236251370
}
251371
+
251372
+/*
251373
+** Set the error message on the virtual table passed as the first argument.
251374
+*/
251375
+static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
251376
+ va_list ap; /* ... printf arguments */
251377
+ va_start(ap, zFormat);
251378
+ sqlite3_free(p->p.base.zErrMsg);
251379
+ p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
251380
+ va_end(ap);
251381
+}
251382
+
251237251383
251238251384
/*
251239251385
** This is the xFilter interface for the virtual table. See
251240251386
** the virtual table xFilter method documentation for additional
251241251387
** information.
@@ -251408,13 +251554,11 @@
251408251554
pCsr->ePlan = FTS5_PLAN_MATCH;
251409251555
rc = fts5CursorFirst(pTab, pCsr, bDesc);
251410251556
}
251411251557
}
251412251558
}else if( pConfig->zContent==0 ){
251413
- *pConfig->pzErrmsg = sqlite3_mprintf(
251414
- "%s: table does not support scanning", pConfig->zName
251415
- );
251559
+ fts5SetVtabError(pTab,"%s: table does not support scanning",pConfig->zName);
251416251560
rc = SQLITE_ERROR;
251417251561
}else{
251418251562
/* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
251419251563
** by rowid (ePlan==FTS5_PLAN_ROWID). */
251420251564
pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
@@ -251484,10 +251628,11 @@
251484251628
*pRowid = fts5CursorRowid(pCsr);
251485251629
}
251486251630
251487251631
return SQLITE_OK;
251488251632
}
251633
+
251489251634
251490251635
/*
251491251636
** If the cursor requires seeking (bSeekRequired flag is set), seek it.
251492251637
** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
251493251638
**
@@ -251521,28 +251666,25 @@
251521251666
CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
251522251667
}else{
251523251668
rc = sqlite3_reset(pCsr->pStmt);
251524251669
if( rc==SQLITE_OK ){
251525251670
rc = FTS5_CORRUPT;
251671
+ fts5SetVtabError((Fts5FullTable*)pTab,
251672
+ "fts5: missing row %lld from content table %s",
251673
+ fts5CursorRowid(pCsr),
251674
+ pTab->pConfig->zContent
251675
+ );
251526251676
}else if( pTab->pConfig->pzErrmsg ){
251527
- *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
251677
+ fts5SetVtabError((Fts5FullTable*)pTab,
251528251678
"%s", sqlite3_errmsg(pTab->pConfig->db)
251529251679
);
251530251680
}
251531251681
}
251532251682
}
251533251683
return rc;
251534251684
}
251535251685
251536
-static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
251537
- va_list ap; /* ... printf arguments */
251538
- va_start(ap, zFormat);
251539
- assert( p->p.base.zErrMsg==0 );
251540
- p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
251541
- va_end(ap);
251542
-}
251543
-
251544251686
/*
251545251687
** This function is called to handle an FTS INSERT command. In other words,
251546251688
** an INSERT statement of the form:
251547251689
**
251548251690
** INSERT INTO fts(fts) VALUES($pCmd)
@@ -252512,11 +252654,14 @@
252512252654
if( pCsr==0 || pCsr->ePlan==0 ){
252513252655
char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
252514252656
sqlite3_result_error(context, zErr, -1);
252515252657
sqlite3_free(zErr);
252516252658
}else{
252659
+ sqlite3_vtab *pTab = pCsr->base.pVtab;
252517252660
fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
252661
+ sqlite3_free(pTab->zErrMsg);
252662
+ pTab->zErrMsg = 0;
252518252663
}
252519252664
}
252520252665
252521252666
252522252667
/*
@@ -252968,11 +253113,11 @@
252968253113
int nArg, /* Number of args */
252969253114
sqlite3_value **apUnused /* Function arguments */
252970253115
){
252971253116
assert( nArg==0 );
252972253117
UNUSED_PARAM2(nArg, apUnused);
252973
- sqlite3_result_text(pCtx, "fts5: 2024-07-03 20:19:33 baa83b460c677c210c7fa3f20314d7e05f305aed8a69026bc5fa106a3de4ea38", -1, SQLITE_TRANSIENT);
253118
+ sqlite3_result_text(pCtx, "fts5: 2024-07-24 13:53:51 bcc31846964102385d5a21eb5e85d7db153b155e76b4e2847c9453d3d0e1af04", -1, SQLITE_TRANSIENT);
252974253119
}
252975253120
252976253121
/*
252977253122
** Return true if zName is the extension on one of the shadow tables used
252978253123
** by this module.
@@ -253590,11 +253735,11 @@
253590253735
ctx.szCol = 0;
253591253736
rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
253592253737
zText, nText, (void*)&ctx, fts5StorageInsertCallback
253593253738
);
253594253739
p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
253595
- if( p->aTotalSize[iCol-1]<0 ){
253740
+ if( p->aTotalSize[iCol-1]<0 && rc==SQLITE_OK ){
253596253741
rc = FTS5_CORRUPT;
253597253742
}
253598253743
}
253599253744
}
253600253745
if( rc==SQLITE_OK && p->nTotalRow<1 ){
253601253746
--- 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 ** baa83b460c677c210c7fa3f20314d7e05f30.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -462,11 +462,11 @@
462 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463 ** [sqlite_version()] and [sqlite_source_id()].
464 */
465 #define SQLITE_VERSION "3.47.0"
466 #define SQLITE_VERSION_NUMBER 3047000
467 #define SQLITE_SOURCE_ID "2024-07-03 20:19:33 baa83b460c677c210c7fa3f20314d7e05f305aed8a69026bc5fa106a3de4ea38"
468
469 /*
470 ** CAPI3REF: Run-Time Library Version Numbers
471 ** KEYWORDS: sqlite3_version sqlite3_sourceid
472 **
@@ -16480,10 +16480,23 @@
16480 ** The names of the following types declared in vdbeInt.h are required
16481 ** for the VdbeOp definition.
16482 */
16483 typedef struct sqlite3_value Mem;
16484 typedef struct SubProgram SubProgram;
 
 
 
 
 
 
 
 
 
 
 
 
 
16485
16486 /*
16487 ** A single instruction of the virtual machine has an opcode
16488 ** and as many as three operands. The instruction is recorded
16489 ** as an instance of the following structure:
@@ -16508,10 +16521,11 @@
16508 VTable *pVtab; /* Used when p4type is P4_VTAB */
16509 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
16510 u32 *ai; /* Used when p4type is P4_INTARRAY */
16511 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
16512 Table *pTab; /* Used when p4type is P4_TABLE */
 
16513 #ifdef SQLITE_ENABLE_CURSOR_HINTS
16514 Expr *pExpr; /* Used when p4type is P4_EXPR */
16515 #endif
16516 } p4;
16517 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -16575,10 +16589,11 @@
16575 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16576 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16577 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16578 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16579 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
 
16580
16581 /* Error message codes for OP_Halt */
16582 #define P5_ConstraintNotNull 1
16583 #define P5_ConstraintUnique 2
16584 #define P5_ConstraintCheck 3
@@ -19807,10 +19822,11 @@
19807 u8 okConstFactor; /* OK to factor out constants */
19808 u8 disableLookaside; /* Number of times lookaside has been disabled */
19809 u8 prepFlags; /* SQLITE_PREPARE_* flags */
19810 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
19811 u8 bHasWith; /* True if statement contains WITH */
 
19812 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
19813 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
19814 #endif
19815 #ifdef SQLITE_DEBUG
19816 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
@@ -32829,18 +32845,16 @@
32829 if( pItem->fg.isUsing ) n++;
32830 if( pItem->fg.isUsing ){
32831 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
32832 }
32833 if( pItem->pSelect ){
32834 sqlite3TreeViewPush(&pView, i+1<pSrc->nSrc);
32835 if( pItem->pTab ){
32836 Table *pTab = pItem->pTab;
32837 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
32838 }
32839 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
32840 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
32841 sqlite3TreeViewPop(&pView);
32842 }
32843 if( pItem->fg.isTabFunc ){
32844 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
32845 }
32846 sqlite3TreeViewPop(&pView);
@@ -32878,11 +32892,11 @@
32878 if( cnt++ ) sqlite3TreeViewPop(&pView);
32879 if( p->pPrior ){
32880 n = 1000;
32881 }else{
32882 n = 0;
32883 if( p->pSrc && p->pSrc->nSrc ) n++;
32884 if( p->pWhere ) n++;
32885 if( p->pGroupBy ) n++;
32886 if( p->pHaving ) n++;
32887 if( p->pOrderBy ) n++;
32888 if( p->pLimit ) n++;
@@ -32904,11 +32918,11 @@
32904 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
32905 }
32906 sqlite3TreeViewPop(&pView);
32907 }
32908 #endif
32909 if( p->pSrc && p->pSrc->nSrc ){
32910 sqlite3TreeViewPush(&pView, (n--)>0);
32911 sqlite3TreeViewLine(pView, "FROM");
32912 sqlite3TreeViewSrcList(pView, p->pSrc);
32913 sqlite3TreeViewPop(&pView);
32914 }
@@ -54746,10 +54760,11 @@
54746 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
54747 pPgHdr->pPage = pPage;
54748 pPgHdr->pData = pPage->pBuf;
54749 pPgHdr->pExtra = (void *)&pPgHdr[1];
54750 memset(pPgHdr->pExtra, 0, 8);
 
54751 pPgHdr->pCache = pCache;
54752 pPgHdr->pgno = pgno;
54753 pPgHdr->flags = PGHDR_CLEAN;
54754 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
54755 }
@@ -55492,11 +55507,12 @@
55492 if( zBulk ){
55493 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
55494 do{
55495 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
55496 pX->page.pBuf = zBulk;
55497 pX->page.pExtra = &pX[1];
 
55498 pX->isBulkLocal = 1;
55499 pX->isAnchor = 0;
55500 pX->pNext = pCache->pFree;
55501 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
55502 pCache->pFree = pX;
@@ -55629,11 +55645,12 @@
55629 pcache1EnterMutex(pCache->pGroup);
55630 #endif
55631 if( pPg==0 ) return 0;
55632 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
55633 p->page.pBuf = pPg;
55634 p->page.pExtra = &p[1];
 
55635 p->isBulkLocal = 0;
55636 p->isAnchor = 0;
55637 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
55638 }
55639 (*pCache->pnPurgeable)++;
@@ -61188,10 +61205,11 @@
61188 if( p==0 ){
61189 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
61190 return SQLITE_NOMEM_BKPT;
61191 }
61192 p->pExtra = (void *)&p[1];
 
61193 p->flags = PGHDR_MMAP;
61194 p->nRef = 1;
61195 p->pPager = pPager;
61196 }
61197
@@ -86272,10 +86290,16 @@
86272 }
86273 case P4_TABLEREF: {
86274 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
86275 break;
86276 }
 
 
 
 
 
 
86277 }
86278 }
86279
86280 /*
86281 ** Free the space allocated for aOp and any p4 values allocated for the
@@ -86850,10 +86874,15 @@
86850 break;
86851 }
86852 case P4_TABLE: {
86853 zP4 = pOp->p4.pTab->zName;
86854 break;
 
 
 
 
 
86855 }
86856 default: {
86857 zP4 = pOp->p4.z;
86858 }
86859 }
@@ -100806,22 +100835,33 @@
100806 */
100807 case OP_AggInverse:
100808 case OP_AggStep: {
100809 int n;
100810 sqlite3_context *pCtx;
 
100811
100812 assert( pOp->p4type==P4_FUNCDEF );
100813 n = pOp->p5;
100814 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100815 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
100816 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
100817 pCtx = sqlite3DbMallocRawNN(db, n*sizeof(sqlite3_value*) +
100818 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
 
 
 
 
 
 
 
 
100819 if( pCtx==0 ) goto no_mem;
100820 pCtx->pMem = 0;
100821 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
 
100822 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
 
100823 pCtx->pFunc = pOp->p4.pFunc;
100824 pCtx->iOp = (int)(pOp - aOp);
100825 pCtx->pVdbe = p;
100826 pCtx->skipFlag = 0;
100827 pCtx->isError = 0;
@@ -112478,10 +112518,53 @@
112478 {
112479 sqlite3ErrorMsg(pParse, "row value misused");
112480 }
112481 }
112482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112483 #ifndef SQLITE_OMIT_SUBQUERY
112484 /*
112485 ** Generate code that will construct an ephemeral table containing all terms
112486 ** in the RHS of an IN operator. The IN operator can be in either of two
112487 ** forms:
@@ -112527,15 +112610,32 @@
112527 **
112528 ** If all of the above are false, then we can compute the RHS just once
112529 ** and reuse it many names.
112530 */
112531 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
112532 /* Reuse of the RHS is allowed */
112533 /* If this routine has already been coded, but the previous code
112534 ** might not have been invoked yet, so invoke it now as a subroutine.
 
112535 */
112536 if( ExprHasProperty(pExpr, EP_Subrtn) ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112537 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112538 if( ExprUseXSelect(pExpr) ){
112539 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
112540 pExpr->x.pSelect->selId));
112541 }
@@ -112543,10 +112643,14 @@
112543 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112544 pExpr->y.sub.iAddr);
112545 assert( iTab!=pExpr->iTable );
112546 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
112547 sqlite3VdbeJumpHere(v, addrOnce);
 
 
 
 
112548 return;
112549 }
112550
112551 /* Begin coding the subroutine */
112552 assert( !ExprUseYWin(pExpr) );
@@ -112553,11 +112657,17 @@
112553 ExprSetProperty(pExpr, EP_Subrtn);
112554 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
112555 pExpr->y.sub.regReturn = ++pParse->nMem;
112556 pExpr->y.sub.iAddr =
112557 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112558
 
 
 
 
 
 
112559 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112560 }
112561
112562 /* Check to see if this is a vector IN operator */
112563 pLeft = pExpr->pLeft;
@@ -112915,13 +113025,11 @@
112915 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
112916 pLeft = pExpr->pLeft;
112917 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
112918 zAff = exprINAffinity(pParse, pExpr);
112919 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
112920 aiMap = (int*)sqlite3DbMallocZero(
112921 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
112922 );
112923 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
112924
112925 /* Attempt to compute the RHS. After this step, if anything other than
112926 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
112927 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
@@ -120388,10 +120496,11 @@
120388 if( pIdx->aSample==0 ){
120389 sqlite3_finalize(pStmt);
120390 return SQLITE_NOMEM_BKPT;
120391 }
120392 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
 
120393 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
120394 pIdx->pTable->tabFlags |= TF_HasStat4;
120395 for(i=0; i<nSample; i++){
120396 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
120397 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
@@ -148069,11 +148178,12 @@
148069 ** The hope is that the terms added to the inner query will make it more
148070 ** efficient.
148071 **
148072 ** NAME AMBIGUITY
148073 **
148074 ** This optimization is called the "WHERE-clause push-down optimization".
 
148075 **
148076 ** Do not confuse this optimization with another unrelated optimization
148077 ** with a similar name: The "MySQL push-down optimization" causes WHERE
148078 ** clause terms that can be evaluated using only the index and without
148079 ** reference to the table are run first, so that if they are false,
@@ -163046,10 +163156,16 @@
163046 ){
163047 Vdbe *v = pParse->pVdbe;
163048 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
163049 int iEnd = sqlite3VdbeCurrentAddr(v);
163050 if( pParse->db->mallocFailed ) return;
 
 
 
 
 
 
163051 for(; iStart<iEnd; iStart++, pOp++){
163052 if( pOp->p1!=iTabCur ) continue;
163053 if( pOp->opcode==OP_Column ){
163054 #ifdef SQLITE_DEBUG
163055 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
@@ -232533,10 +232649,26 @@
232533 ** Constants for the largest and smallest possible 64-bit signed integers.
232534 */
232535 # define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
232536 # define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
232537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232538 #endif
232539
232540 /* Truncate very long tokens to this many bytes. Hard limit is
232541 ** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
232542 ** field that occurs at the start of each leaf page (see fts5_index.c). */
@@ -241777,15 +241909,16 @@
241777 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
241778
241779 if( rc==SQLITE_OK ){
241780 u8 *aOut = 0; /* Read blob data into this buffer */
241781 int nByte = sqlite3_blob_bytes(p->pReader);
241782 sqlite3_int64 nAlloc = sizeof(Fts5Data) + nByte + FTS5_DATA_PADDING;
 
241783 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
241784 if( pRet ){
241785 pRet->nn = nByte;
241786 aOut = pRet->p = (u8*)&pRet[1];
241787 }else{
241788 rc = SQLITE_NOMEM;
241789 }
241790
241791 if( rc==SQLITE_OK ){
@@ -241804,10 +241937,11 @@
241804 p->rc = rc;
241805 p->nRead++;
241806 }
241807
241808 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
 
241809 return pRet;
241810 }
241811
241812
241813 /*
@@ -251232,10 +251366,22 @@
251232 return sqlite3_value_int64(pVal);
251233 }
251234 }
251235 return iDefault;
251236 }
 
 
 
 
 
 
 
 
 
 
 
 
251237
251238 /*
251239 ** This is the xFilter interface for the virtual table. See
251240 ** the virtual table xFilter method documentation for additional
251241 ** information.
@@ -251408,13 +251554,11 @@
251408 pCsr->ePlan = FTS5_PLAN_MATCH;
251409 rc = fts5CursorFirst(pTab, pCsr, bDesc);
251410 }
251411 }
251412 }else if( pConfig->zContent==0 ){
251413 *pConfig->pzErrmsg = sqlite3_mprintf(
251414 "%s: table does not support scanning", pConfig->zName
251415 );
251416 rc = SQLITE_ERROR;
251417 }else{
251418 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
251419 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
251420 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
@@ -251484,10 +251628,11 @@
251484 *pRowid = fts5CursorRowid(pCsr);
251485 }
251486
251487 return SQLITE_OK;
251488 }
 
251489
251490 /*
251491 ** If the cursor requires seeking (bSeekRequired flag is set), seek it.
251492 ** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
251493 **
@@ -251521,28 +251666,25 @@
251521 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
251522 }else{
251523 rc = sqlite3_reset(pCsr->pStmt);
251524 if( rc==SQLITE_OK ){
251525 rc = FTS5_CORRUPT;
 
 
 
 
 
251526 }else if( pTab->pConfig->pzErrmsg ){
251527 *pTab->pConfig->pzErrmsg = sqlite3_mprintf(
251528 "%s", sqlite3_errmsg(pTab->pConfig->db)
251529 );
251530 }
251531 }
251532 }
251533 return rc;
251534 }
251535
251536 static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
251537 va_list ap; /* ... printf arguments */
251538 va_start(ap, zFormat);
251539 assert( p->p.base.zErrMsg==0 );
251540 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
251541 va_end(ap);
251542 }
251543
251544 /*
251545 ** This function is called to handle an FTS INSERT command. In other words,
251546 ** an INSERT statement of the form:
251547 **
251548 ** INSERT INTO fts(fts) VALUES($pCmd)
@@ -252512,11 +252654,14 @@
252512 if( pCsr==0 || pCsr->ePlan==0 ){
252513 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
252514 sqlite3_result_error(context, zErr, -1);
252515 sqlite3_free(zErr);
252516 }else{
 
252517 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
 
 
252518 }
252519 }
252520
252521
252522 /*
@@ -252968,11 +253113,11 @@
252968 int nArg, /* Number of args */
252969 sqlite3_value **apUnused /* Function arguments */
252970 ){
252971 assert( nArg==0 );
252972 UNUSED_PARAM2(nArg, apUnused);
252973 sqlite3_result_text(pCtx, "fts5: 2024-07-03 20:19:33 baa83b460c677c210c7fa3f20314d7e05f305aed8a69026bc5fa106a3de4ea38", -1, SQLITE_TRANSIENT);
252974 }
252975
252976 /*
252977 ** Return true if zName is the extension on one of the shadow tables used
252978 ** by this module.
@@ -253590,11 +253735,11 @@
253590 ctx.szCol = 0;
253591 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
253592 zText, nText, (void*)&ctx, fts5StorageInsertCallback
253593 );
253594 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
253595 if( p->aTotalSize[iCol-1]<0 ){
253596 rc = FTS5_CORRUPT;
253597 }
253598 }
253599 }
253600 if( rc==SQLITE_OK && p->nTotalRow<1 ){
253601
--- 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 ** bcc31846964102385d5a21eb5e85d7db153b.
22 */
23 #define SQLITE_CORE 1
24 #define SQLITE_AMALGAMATION 1
25 #ifndef SQLITE_PRIVATE
26 # define SQLITE_PRIVATE static
@@ -462,11 +462,11 @@
462 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
463 ** [sqlite_version()] and [sqlite_source_id()].
464 */
465 #define SQLITE_VERSION "3.47.0"
466 #define SQLITE_VERSION_NUMBER 3047000
467 #define SQLITE_SOURCE_ID "2024-07-24 13:53:51 bcc31846964102385d5a21eb5e85d7db153b155e76b4e2847c9453d3d0e1af04"
468
469 /*
470 ** CAPI3REF: Run-Time Library Version Numbers
471 ** KEYWORDS: sqlite3_version sqlite3_sourceid
472 **
@@ -16480,10 +16480,23 @@
16480 ** The names of the following types declared in vdbeInt.h are required
16481 ** for the VdbeOp definition.
16482 */
16483 typedef struct sqlite3_value Mem;
16484 typedef struct SubProgram SubProgram;
16485 typedef struct SubrtnSig SubrtnSig;
16486
16487 /*
16488 ** A signature for a reusable subroutine that materializes the RHS of
16489 ** an IN operator.
16490 */
16491 struct SubrtnSig {
16492 int selId; /* SELECT-id for the SELECT statement on the RHS */
16493 char *zAff; /* Affinity of the overall IN expression */
16494 int iTable; /* Ephemeral table generated by the subroutine */
16495 int iAddr; /* Subroutine entry address */
16496 int regReturn; /* Register used to hold return address */
16497 };
16498
16499 /*
16500 ** A single instruction of the virtual machine has an opcode
16501 ** and as many as three operands. The instruction is recorded
16502 ** as an instance of the following structure:
@@ -16508,10 +16521,11 @@
16521 VTable *pVtab; /* Used when p4type is P4_VTAB */
16522 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
16523 u32 *ai; /* Used when p4type is P4_INTARRAY */
16524 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
16525 Table *pTab; /* Used when p4type is P4_TABLE */
16526 SubrtnSig *pSubrtnSig; /* Used when p4type is P4_SUBRTNSIG */
16527 #ifdef SQLITE_ENABLE_CURSOR_HINTS
16528 Expr *pExpr; /* Used when p4type is P4_EXPR */
16529 #endif
16530 } p4;
16531 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -16575,10 +16589,11 @@
16589 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
16590 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16591 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16592 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16593 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
16594 #define P4_SUBRTNSIG (-17) /* P4 is a SubrtnSig pointer */
16595
16596 /* Error message codes for OP_Halt */
16597 #define P5_ConstraintNotNull 1
16598 #define P5_ConstraintUnique 2
16599 #define P5_ConstraintCheck 3
@@ -19807,10 +19822,11 @@
19822 u8 okConstFactor; /* OK to factor out constants */
19823 u8 disableLookaside; /* Number of times lookaside has been disabled */
19824 u8 prepFlags; /* SQLITE_PREPARE_* flags */
19825 u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
19826 u8 bHasWith; /* True if statement contains WITH */
19827 u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
19828 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
19829 u8 earlyCleanup; /* OOM inside sqlite3ParserAddCleanup() */
19830 #endif
19831 #ifdef SQLITE_DEBUG
19832 u8 ifNotExists; /* Might be true if IF NOT EXISTS. Assert()s only */
@@ -32829,18 +32845,16 @@
32845 if( pItem->fg.isUsing ) n++;
32846 if( pItem->fg.isUsing ){
32847 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
32848 }
32849 if( pItem->pSelect ){
 
32850 if( pItem->pTab ){
32851 Table *pTab = pItem->pTab;
32852 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
32853 }
32854 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
32855 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
 
32856 }
32857 if( pItem->fg.isTabFunc ){
32858 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
32859 }
32860 sqlite3TreeViewPop(&pView);
@@ -32878,11 +32892,11 @@
32892 if( cnt++ ) sqlite3TreeViewPop(&pView);
32893 if( p->pPrior ){
32894 n = 1000;
32895 }else{
32896 n = 0;
32897 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ) n++;
32898 if( p->pWhere ) n++;
32899 if( p->pGroupBy ) n++;
32900 if( p->pHaving ) n++;
32901 if( p->pOrderBy ) n++;
32902 if( p->pLimit ) n++;
@@ -32904,11 +32918,11 @@
32918 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
32919 }
32920 sqlite3TreeViewPop(&pView);
32921 }
32922 #endif
32923 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ){
32924 sqlite3TreeViewPush(&pView, (n--)>0);
32925 sqlite3TreeViewLine(pView, "FROM");
32926 sqlite3TreeViewSrcList(pView, p->pSrc);
32927 sqlite3TreeViewPop(&pView);
32928 }
@@ -54746,10 +54760,11 @@
54760 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
54761 pPgHdr->pPage = pPage;
54762 pPgHdr->pData = pPage->pBuf;
54763 pPgHdr->pExtra = (void *)&pPgHdr[1];
54764 memset(pPgHdr->pExtra, 0, 8);
54765 assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) );
54766 pPgHdr->pCache = pCache;
54767 pPgHdr->pgno = pgno;
54768 pPgHdr->flags = PGHDR_CLEAN;
54769 return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
54770 }
@@ -55492,11 +55507,12 @@
55507 if( zBulk ){
55508 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
55509 do{
55510 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
55511 pX->page.pBuf = zBulk;
55512 pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX));
55513 assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) );
55514 pX->isBulkLocal = 1;
55515 pX->isAnchor = 0;
55516 pX->pNext = pCache->pFree;
55517 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
55518 pCache->pFree = pX;
@@ -55629,11 +55645,12 @@
55645 pcache1EnterMutex(pCache->pGroup);
55646 #endif
55647 if( pPg==0 ) return 0;
55648 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
55649 p->page.pBuf = pPg;
55650 p->page.pExtra = (u8*)p + ROUND8(sizeof(*p));
55651 assert( EIGHT_BYTE_ALIGNMENT( p->page.pExtra ) );
55652 p->isBulkLocal = 0;
55653 p->isAnchor = 0;
55654 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
55655 }
55656 (*pCache->pnPurgeable)++;
@@ -61188,10 +61205,11 @@
61205 if( p==0 ){
61206 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
61207 return SQLITE_NOMEM_BKPT;
61208 }
61209 p->pExtra = (void *)&p[1];
61210 assert( EIGHT_BYTE_ALIGNMENT( p->pExtra ) );
61211 p->flags = PGHDR_MMAP;
61212 p->nRef = 1;
61213 p->pPager = pPager;
61214 }
61215
@@ -86272,10 +86290,16 @@
86290 }
86291 case P4_TABLEREF: {
86292 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
86293 break;
86294 }
86295 case P4_SUBRTNSIG: {
86296 SubrtnSig *pSig = (SubrtnSig*)p4;
86297 sqlite3DbFree(db, pSig->zAff);
86298 sqlite3DbFree(db, pSig);
86299 break;
86300 }
86301 }
86302 }
86303
86304 /*
86305 ** Free the space allocated for aOp and any p4 values allocated for the
@@ -86850,10 +86874,15 @@
86874 break;
86875 }
86876 case P4_TABLE: {
86877 zP4 = pOp->p4.pTab->zName;
86878 break;
86879 }
86880 case P4_SUBRTNSIG: {
86881 SubrtnSig *pSig = pOp->p4.pSubrtnSig;
86882 sqlite3_str_appendf(&x, "subrtnsig:%d,%s", pSig->selId, pSig->zAff);
86883 break;
86884 }
86885 default: {
86886 zP4 = pOp->p4.z;
86887 }
86888 }
@@ -100806,22 +100835,33 @@
100835 */
100836 case OP_AggInverse:
100837 case OP_AggStep: {
100838 int n;
100839 sqlite3_context *pCtx;
100840 u64 nAlloc;
100841
100842 assert( pOp->p4type==P4_FUNCDEF );
100843 n = pOp->p5;
100844 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
100845 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
100846 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
100847
100848 /* Allocate space for (a) the context object and (n-1) extra pointers
100849 ** to append to the sqlite3_context.argv[1] array, and (b) a memory
100850 ** cell in which to store the accumulation. Be careful that the memory
100851 ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits.
100852 **
100853 ** Note: We could avoid this by using a regular memory cell from aMem[] for
100854 ** the accumulator, instead of allocating one here. */
100855 nAlloc = ROUND8P( sizeof(pCtx[0]) + (n-1)*sizeof(sqlite3_value*) );
100856 pCtx = sqlite3DbMallocRawNN(db, nAlloc + sizeof(Mem));
100857 if( pCtx==0 ) goto no_mem;
100858 pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc);
100859 assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) );
100860
100861 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
100862 pCtx->pMem = 0;
100863 pCtx->pFunc = pOp->p4.pFunc;
100864 pCtx->iOp = (int)(pOp - aOp);
100865 pCtx->pVdbe = p;
100866 pCtx->skipFlag = 0;
100867 pCtx->isError = 0;
@@ -112478,10 +112518,53 @@
112518 {
112519 sqlite3ErrorMsg(pParse, "row value misused");
112520 }
112521 }
112522
112523 #ifndef SQLITE_OMIT_SUBQUERY
112524 /*
112525 ** Scan all previously generated bytecode looking for an OP_BeginSubrtn
112526 ** that is compatible with pExpr. If found, add the y.sub values
112527 ** to pExpr and return true. If not found, return false.
112528 */
112529 static int findCompatibleInRhsSubrtn(
112530 Parse *pParse, /* Parsing context */
112531 Expr *pExpr, /* IN operator with RHS that we want to reuse */
112532 SubrtnSig *pNewSig /* Signature for the IN operator */
112533 ){
112534 VdbeOp *pOp, *pEnd;
112535 SubrtnSig *pSig;
112536 Vdbe *v;
112537
112538 if( pNewSig==0 ) return 0;
112539 if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0;
112540 assert( pExpr->op==TK_IN );
112541 assert( !ExprUseYSub(pExpr) );
112542 assert( ExprUseXSelect(pExpr) );
112543 assert( pExpr->x.pSelect!=0 );
112544 assert( (pExpr->x.pSelect->selFlags & SF_All)==0 );
112545 v = pParse->pVdbe;
112546 assert( v!=0 );
112547 pOp = sqlite3VdbeGetOp(v, 1);
112548 pEnd = sqlite3VdbeGetLastOp(v);
112549 for(; pOp<pEnd; pOp++){
112550 if( pOp->p4type!=P4_SUBRTNSIG ) continue;
112551 assert( pOp->opcode==OP_BeginSubrtn );
112552 pSig = pOp->p4.pSubrtnSig;
112553 assert( pSig!=0 );
112554 if( pNewSig->selId!=pSig->selId ) continue;
112555 if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue;
112556 pExpr->y.sub.iAddr = pSig->iAddr;
112557 pExpr->y.sub.regReturn = pSig->regReturn;
112558 pExpr->iTable = pSig->iTable;
112559 ExprSetProperty(pExpr, EP_Subrtn);
112560 return 1;
112561 }
112562 return 0;
112563 }
112564 #endif /* SQLITE_OMIT_SUBQUERY */
112565
112566 #ifndef SQLITE_OMIT_SUBQUERY
112567 /*
112568 ** Generate code that will construct an ephemeral table containing all terms
112569 ** in the RHS of an IN operator. The IN operator can be in either of two
112570 ** forms:
@@ -112527,15 +112610,32 @@
112610 **
112611 ** If all of the above are false, then we can compute the RHS just once
112612 ** and reuse it many names.
112613 */
112614 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
112615 /* Reuse of the RHS is allowed
112616 **
112617 ** Compute a signature for the RHS of the IN operator to facility
112618 ** finding and reusing prior instances of the same IN operator.
112619 */
112620 SubrtnSig *pSig = 0;
112621 assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 );
112622 if( ExprUseXSelect(pExpr) && (pExpr->x.pSelect->selFlags & SF_All)==0 ){
112623 pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0]));
112624 if( pSig ){
112625 pSig->selId = pExpr->x.pSelect->selId;
112626 pSig->zAff = exprINAffinity(pParse, pExpr);
112627 }
112628 }
112629
112630 /* Check to see if there is a prior materialization of the RHS of
112631 ** this IN operator. If there is, then make use of that prior
112632 ** materialization rather than recomputing it.
112633 */
112634 if( ExprHasProperty(pExpr, EP_Subrtn)
112635 || findCompatibleInRhsSubrtn(pParse, pExpr, pSig)
112636 ){
112637 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112638 if( ExprUseXSelect(pExpr) ){
112639 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
112640 pExpr->x.pSelect->selId));
112641 }
@@ -112543,10 +112643,14 @@
112643 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
112644 pExpr->y.sub.iAddr);
112645 assert( iTab!=pExpr->iTable );
112646 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
112647 sqlite3VdbeJumpHere(v, addrOnce);
112648 if( pSig ){
112649 sqlite3DbFree(pParse->db, pSig->zAff);
112650 sqlite3DbFree(pParse->db, pSig);
112651 }
112652 return;
112653 }
112654
112655 /* Begin coding the subroutine */
112656 assert( !ExprUseYWin(pExpr) );
@@ -112553,11 +112657,17 @@
112657 ExprSetProperty(pExpr, EP_Subrtn);
112658 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
112659 pExpr->y.sub.regReturn = ++pParse->nMem;
112660 pExpr->y.sub.iAddr =
112661 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
112662 if( pSig ){
112663 pSig->iAddr = pExpr->y.sub.iAddr;
112664 pSig->regReturn = pExpr->y.sub.regReturn;
112665 pSig->iTable = iTab;
112666 pParse->mSubrtnSig = 1 << (pSig->selId&7);
112667 sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG);
112668 }
112669 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
112670 }
112671
112672 /* Check to see if this is a vector IN operator */
112673 pLeft = pExpr->pLeft;
@@ -112915,13 +113025,11 @@
113025 assert( !ExprHasVVAProperty(pExpr,EP_Immutable) );
113026 pLeft = pExpr->pLeft;
113027 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
113028 zAff = exprINAffinity(pParse, pExpr);
113029 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
113030 aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int));
 
 
113031 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
113032
113033 /* Attempt to compute the RHS. After this step, if anything other than
113034 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
113035 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
@@ -120388,10 +120496,11 @@
120496 if( pIdx->aSample==0 ){
120497 sqlite3_finalize(pStmt);
120498 return SQLITE_NOMEM_BKPT;
120499 }
120500 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
120501 assert( EIGHT_BYTE_ALIGNMENT( pSpace ) );
120502 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
120503 pIdx->pTable->tabFlags |= TF_HasStat4;
120504 for(i=0; i<nSample; i++){
120505 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
120506 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
@@ -148069,11 +148178,12 @@
148178 ** The hope is that the terms added to the inner query will make it more
148179 ** efficient.
148180 **
148181 ** NAME AMBIGUITY
148182 **
148183 ** This optimization is called the "WHERE-clause push-down optimization"
148184 ** or sometimes the "predicate push-down optimization".
148185 **
148186 ** Do not confuse this optimization with another unrelated optimization
148187 ** with a similar name: The "MySQL push-down optimization" causes WHERE
148188 ** clause terms that can be evaluated using only the index and without
148189 ** reference to the table are run first, so that if they are false,
@@ -163046,10 +163156,16 @@
163156 ){
163157 Vdbe *v = pParse->pVdbe;
163158 VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart);
163159 int iEnd = sqlite3VdbeCurrentAddr(v);
163160 if( pParse->db->mallocFailed ) return;
163161 #ifdef SQLITE_DEBUG
163162 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
163163 printf("CHECKING for column-to-copy on cursor %d for %d..%d\n",
163164 iTabCur, iStart, iEnd);
163165 }
163166 #endif
163167 for(; iStart<iEnd; iStart++, pOp++){
163168 if( pOp->p1!=iTabCur ) continue;
163169 if( pOp->opcode==OP_Column ){
163170 #ifdef SQLITE_DEBUG
163171 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
@@ -232533,10 +232649,26 @@
232649 ** Constants for the largest and smallest possible 64-bit signed integers.
232650 */
232651 # define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
232652 # define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
232653
232654 /* The uptr type is an unsigned integer large enough to hold a pointer
232655 */
232656 #if defined(HAVE_STDINT_H)
232657 typedef uintptr_t uptr;
232658 #elif SQLITE_PTRSIZE==4
232659 typedef u32 uptr;
232660 #else
232661 typedef u64 uptr;
232662 #endif
232663
232664 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
232665 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
232666 #else
232667 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
232668 #endif
232669
232670 #endif
232671
232672 /* Truncate very long tokens to this many bytes. Hard limit is
232673 ** (65536-1-1-4-9)==65521 bytes. The limiting factor is the 16-bit offset
232674 ** field that occurs at the start of each leaf page (see fts5_index.c). */
@@ -241777,15 +241909,16 @@
241909 if( rc==SQLITE_ERROR ) rc = FTS5_CORRUPT;
241910
241911 if( rc==SQLITE_OK ){
241912 u8 *aOut = 0; /* Read blob data into this buffer */
241913 int nByte = sqlite3_blob_bytes(p->pReader);
241914 int szData = (sizeof(Fts5Data) + 7) & ~7;
241915 sqlite3_int64 nAlloc = szData + nByte + FTS5_DATA_PADDING;
241916 pRet = (Fts5Data*)sqlite3_malloc64(nAlloc);
241917 if( pRet ){
241918 pRet->nn = nByte;
241919 aOut = pRet->p = (u8*)pRet + szData;
241920 }else{
241921 rc = SQLITE_NOMEM;
241922 }
241923
241924 if( rc==SQLITE_OK ){
@@ -241804,10 +241937,11 @@
241937 p->rc = rc;
241938 p->nRead++;
241939 }
241940
241941 assert( (pRet==0)==(p->rc!=SQLITE_OK) );
241942 assert( pRet==0 || EIGHT_BYTE_ALIGNMENT( pRet->p ) );
241943 return pRet;
241944 }
241945
241946
241947 /*
@@ -251232,10 +251366,22 @@
251366 return sqlite3_value_int64(pVal);
251367 }
251368 }
251369 return iDefault;
251370 }
251371
251372 /*
251373 ** Set the error message on the virtual table passed as the first argument.
251374 */
251375 static void fts5SetVtabError(Fts5FullTable *p, const char *zFormat, ...){
251376 va_list ap; /* ... printf arguments */
251377 va_start(ap, zFormat);
251378 sqlite3_free(p->p.base.zErrMsg);
251379 p->p.base.zErrMsg = sqlite3_vmprintf(zFormat, ap);
251380 va_end(ap);
251381 }
251382
251383
251384 /*
251385 ** This is the xFilter interface for the virtual table. See
251386 ** the virtual table xFilter method documentation for additional
251387 ** information.
@@ -251408,13 +251554,11 @@
251554 pCsr->ePlan = FTS5_PLAN_MATCH;
251555 rc = fts5CursorFirst(pTab, pCsr, bDesc);
251556 }
251557 }
251558 }else if( pConfig->zContent==0 ){
251559 fts5SetVtabError(pTab,"%s: table does not support scanning",pConfig->zName);
 
 
251560 rc = SQLITE_ERROR;
251561 }else{
251562 /* This is either a full-table scan (ePlan==FTS5_PLAN_SCAN) or a lookup
251563 ** by rowid (ePlan==FTS5_PLAN_ROWID). */
251564 pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
@@ -251484,10 +251628,11 @@
251628 *pRowid = fts5CursorRowid(pCsr);
251629 }
251630
251631 return SQLITE_OK;
251632 }
251633
251634
251635 /*
251636 ** If the cursor requires seeking (bSeekRequired flag is set), seek it.
251637 ** Return SQLITE_OK if no error occurs, or an SQLite error code otherwise.
251638 **
@@ -251521,28 +251666,25 @@
251666 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_CONTENT);
251667 }else{
251668 rc = sqlite3_reset(pCsr->pStmt);
251669 if( rc==SQLITE_OK ){
251670 rc = FTS5_CORRUPT;
251671 fts5SetVtabError((Fts5FullTable*)pTab,
251672 "fts5: missing row %lld from content table %s",
251673 fts5CursorRowid(pCsr),
251674 pTab->pConfig->zContent
251675 );
251676 }else if( pTab->pConfig->pzErrmsg ){
251677 fts5SetVtabError((Fts5FullTable*)pTab,
251678 "%s", sqlite3_errmsg(pTab->pConfig->db)
251679 );
251680 }
251681 }
251682 }
251683 return rc;
251684 }
251685
 
 
 
 
 
 
 
 
251686 /*
251687 ** This function is called to handle an FTS INSERT command. In other words,
251688 ** an INSERT statement of the form:
251689 **
251690 ** INSERT INTO fts(fts) VALUES($pCmd)
@@ -252512,11 +252654,14 @@
252654 if( pCsr==0 || pCsr->ePlan==0 ){
252655 char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
252656 sqlite3_result_error(context, zErr, -1);
252657 sqlite3_free(zErr);
252658 }else{
252659 sqlite3_vtab *pTab = pCsr->base.pVtab;
252660 fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
252661 sqlite3_free(pTab->zErrMsg);
252662 pTab->zErrMsg = 0;
252663 }
252664 }
252665
252666
252667 /*
@@ -252968,11 +253113,11 @@
253113 int nArg, /* Number of args */
253114 sqlite3_value **apUnused /* Function arguments */
253115 ){
253116 assert( nArg==0 );
253117 UNUSED_PARAM2(nArg, apUnused);
253118 sqlite3_result_text(pCtx, "fts5: 2024-07-24 13:53:51 bcc31846964102385d5a21eb5e85d7db153b155e76b4e2847c9453d3d0e1af04", -1, SQLITE_TRANSIENT);
253119 }
253120
253121 /*
253122 ** Return true if zName is the extension on one of the shadow tables used
253123 ** by this module.
@@ -253590,11 +253735,11 @@
253735 ctx.szCol = 0;
253736 rc = sqlite3Fts5Tokenize(pConfig, FTS5_TOKENIZE_DOCUMENT,
253737 zText, nText, (void*)&ctx, fts5StorageInsertCallback
253738 );
253739 p->aTotalSize[iCol-1] -= (i64)ctx.szCol;
253740 if( p->aTotalSize[iCol-1]<0 && rc==SQLITE_OK ){
253741 rc = FTS5_CORRUPT;
253742 }
253743 }
253744 }
253745 if( rc==SQLITE_OK && p->nTotalRow<1 ){
253746
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.47.0"
150150
#define SQLITE_VERSION_NUMBER 3047000
151
-#define SQLITE_SOURCE_ID "2024-07-03 20:19:33 baa83b460c677c210c7fa3f20314d7e05f305aed8a69026bc5fa106a3de4ea38"
151
+#define SQLITE_SOURCE_ID "2024-07-24 13:53:51 bcc31846964102385d5a21eb5e85d7db153b155e76b4e2847c9453d3d0e1af04"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
157157
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.47.0"
150 #define SQLITE_VERSION_NUMBER 3047000
151 #define SQLITE_SOURCE_ID "2024-07-03 20:19:33 baa83b460c677c210c7fa3f20314d7e05f305aed8a69026bc5fa106a3de4ea38"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
157
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.47.0"
150 #define SQLITE_VERSION_NUMBER 3047000
151 #define SQLITE_SOURCE_ID "2024-07-24 13:53:51 bcc31846964102385d5a21eb5e85d7db153b155e76b4e2847c9453d3d0e1af04"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
157

Keyboard Shortcuts

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