** and a very large cost if either start or stop are unavailable. This
** encourages the query planner to order joins such that the bounds of the
** series are well-defined.
+**
+** Update on 2024-08-22:
+** xBestIndex now also looks for equality and inequality constraints against
+** the value column and uses those constraints as additional bounds against
+** the sequence range. Thus, a query like this:
+**
+** SELECT value FROM generate_series($SA,$EA)
+** WHERE value BETWEEN $SB AND $EB;
+**
+** Is logically the same as:
+**
+** SELECT value FROM generate_series(max($SA,$SB),min($EA,$EB));
+**
+** Constraints on the value column can server as substitutes for constraints
+** on the hidden start and stop columns. So, the following two queries
+** are equivalent:
+**
+** SELECT value FROM generate_series($S,$E);
+** SELECT value FROM generate_series WHERE value BETWEEN $S and $E;
+**
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
typedef unsigned char u8;
typedef struct SequenceSpec {
- sqlite3_int64 iBase; /* Starting value ("start") */
- sqlite3_int64 iTerm; /* Given terminal value ("stop") */
+ sqlite3_int64 iOBase; /* Original starting value ("start") */
+ sqlite3_int64 iOTerm; /* Original terminal value ("stop") */
+ sqlite3_int64 iBase; /* Starting value to actually use */
+ sqlite3_int64 iTerm; /* Terminal value to actually use */
sqlite3_int64 iStep; /* Increment ("step") */
sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */
sqlite3_uint64 uSeqIndexNow; /* Current index during generation */
series_cursor *pCur = (series_cursor*)cur;
sqlite3_int64 x = 0;
switch( i ){
- case SERIES_COLUMN_START: x = pCur->ss.iBase; break;
- case SERIES_COLUMN_STOP: x = pCur->ss.iTerm; break;
- case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
+ case SERIES_COLUMN_START: x = pCur->ss.iOBase; break;
+ case SERIES_COLUMN_STOP: x = pCur->ss.iOTerm; break;
+ case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break;
default: x = pCur->ss.iValueNow; break;
}
sqlite3_result_int64(ctx, x);
}
#ifndef LARGEST_UINT64
+#define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
#define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32))
+#define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
#endif
/*
** parameter. (idxStr is not used in this implementation.) idxNum
** is a bitmask showing which constraints are available:
**
-** 0x01: start=VALUE
-** 0x02: stop=VALUE
-** 0x04: step=VALUE
-** 0x08: descending order
-** 0x10: ascending order
-** 0x20: LIMIT VALUE
-** 0x40: OFFSET VALUE
+** 0x0001: start=VALUE
+** 0x0002: stop=VALUE
+** 0x0004: step=VALUE
+** 0x0008: descending order
+** 0x0010: ascending order
+** 0x0020: LIMIT VALUE
+** 0x0040: OFFSET VALUE
+** 0x0080: value=VALUE
+** 0x0100: value>=VALUE
+** 0x0200: value>VALUE
+** 0x1000: value<=VALUE
+** 0x2000: value<VALUE
**
** This routine should initialize the cursor and position it so that it
** is pointing at the first row, or pointing off the end of the table
){
series_cursor *pCur = (series_cursor *)pVtabCursor;
int i = 0;
+ int returnNoRows = 0;
+ sqlite3_int64 iMin = SMALLEST_INT64;
+ sqlite3_int64 iMax = LARGEST_INT64;
+ sqlite3_int64 iLimit = 0;
+ sqlite3_int64 iOffset = 0;
+
(void)idxStrUnused;
if( idxNum & 0x01 ){
pCur->ss.iBase = sqlite3_value_int64(argv[i++]);
}else{
pCur->ss.iStep = 1;
}
+
+ /* If there are constraints on the value column but there are
+ ** no constraints on the start, stop, and step columns, then
+ ** initialize the default range to be the entire range of 64-bit signed
+ ** integers. This range will contracted by the value column constraints
+ ** further below.
+ */
+ if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){
+ pCur->ss.iBase = SMALLEST_INT64;
+ }
+ if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){
+ pCur->ss.iTerm = LARGEST_INT64;
+ }
+ pCur->ss.iOBase = pCur->ss.iBase;
+ pCur->ss.iOTerm = pCur->ss.iTerm;
+
+ /* Extract the LIMIT and OFFSET values, but do not apply them yet.
+ ** The range must first be constrained by the limits on value.
+ */
if( idxNum & 0x20 ){
- sqlite3_int64 iLimit = sqlite3_value_int64(argv[i++]);
- sqlite3_int64 iTerm;
+ iLimit = sqlite3_value_int64(argv[i++]);
if( idxNum & 0x40 ){
- sqlite3_int64 iOffset = sqlite3_value_int64(argv[i++]);
- if( iOffset>0 ){
- pCur->ss.iBase += pCur->ss.iStep*iOffset;
+ iOffset = sqlite3_value_int64(argv[i++]);
+ }
+ }
+
+ if( idxNum & 0x3380 ){
+ /* Extract the maximum range of output values determined by
+ ** constraints on the "value" column.
+ */
+ if( idxNum & 0x0080 ){
+ iMin = iMax = sqlite3_value_int64(argv[i++]);
+ }else{
+ if( idxNum & 0x0300 ){
+ iMin = sqlite3_value_int64(argv[i++]);
+ if( idxNum & 0x0200 ){
+ if( iMin==LARGEST_INT64 ){
+ returnNoRows = 1;
+ }else{
+ iMin++;
+ }
+ }
+ }
+ if( idxNum & 0x3000 ){
+ iMax = sqlite3_value_int64(argv[i++]);
+ if( idxNum & 0x2000 ){
+ if( iMax==SMALLEST_INT64 ){
+ returnNoRows = 1;
+ }else{
+ iMax--;
+ }
+ }
+ }
+ if( iMin>iMax ){
+ returnNoRows = 1;
}
}
+
+ /* Try to reduce the range of values to be generated based on
+ ** constraints on the "value" column.
+ */
+ if( pCur->ss.iStep>0 ){
+ sqlite3_int64 szStep = pCur->ss.iStep;
+ if( pCur->ss.iBase<iMin ){
+ sqlite3_uint64 d = iMin - pCur->ss.iBase;
+ pCur->ss.iBase += ((d+szStep-1)/szStep)*szStep;
+ }
+ if( pCur->ss.iTerm>iMax ){
+ sqlite3_uint64 d = pCur->ss.iTerm - iMax;
+ pCur->ss.iTerm -= ((d+szStep-1)/szStep)*szStep;
+ }
+ }else{
+ sqlite3_int64 szStep = -pCur->ss.iStep;
+ assert( szStep>0 );
+ if( pCur->ss.iBase>iMax ){
+ sqlite3_uint64 d = pCur->ss.iBase - iMax;
+ pCur->ss.iBase -= ((d+szStep-1)/szStep)*szStep;
+ }
+ if( pCur->ss.iTerm<iMin ){
+ sqlite3_uint64 d = iMin - pCur->ss.iTerm;
+ pCur->ss.iTerm += ((d+szStep-1)/szStep)*szStep;
+ }
+ }
+ }
+
+ /* Apply LIMIT and OFFSET constraints, if any */
+ if( idxNum & 0x20 ){
+ if( iOffset>0 ){
+ pCur->ss.iBase += pCur->ss.iStep*iOffset;
+ }
if( iLimit>=0 ){
+ sqlite3_int64 iTerm;
iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep;
if( pCur->ss.iStep<0 ){
if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm;
}
}
}
+
+
for(i=0; i<argc; i++){
if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
/* If any of the constraints have a NULL value, then return no rows.
** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
- pCur->ss.iBase = 1;
- pCur->ss.iTerm = 0;
- pCur->ss.iStep = 1;
+ returnNoRows = 1;
break;
}
}
+ if( returnNoRows ){
+ pCur->ss.iBase = 1;
+ pCur->ss.iTerm = 0;
+ pCur->ss.iStep = 1;
+ }
if( idxNum & 0x08 ){
pCur->ss.isReversing = pCur->ss.iStep > 0;
}else{
**
** The query plan is represented by bits in idxNum:
**
-** 0x01 start = $value -- constraint exists
-** 0x02 stop = $value -- constraint exists
-** 0x04 step = $value -- constraint exists
-** 0x08 output is in descending order
-** 0x10 output is in ascending order
-** 0x20 LIMIT $value -- constraint exists
-** 0x40 OFFSET $value -- constraint exists
+** 0x0001 start = $num
+** 0x0002 stop = $num
+** 0x0004 step = $num
+** 0x0008 output is in descending order
+** 0x0010 output is in ascending order
+** 0x0020 LIMIT $num
+** 0x0040 OFFSET $num
+** 0x0080 value = $num
+** 0x0100 value >= $num
+** 0x0200 value > $num
+** 0x1000 value <= $num
+** 0x2000 value < $num
+**
+** Only one of 0x0100 or 0x0200 will be returned. Similarly, only
+** one of 0x1000 or 0x2000 will be returned. If the 0x0080 is set, then
+** none of the 0xff00 bits will be set.
+**
+** The order of parameters passed to xFilter is as follows:
+**
+** * The argument to start= if bit 0x0001 is in the idxNum mask
+** * The argument to stop= if bit 0x0002 is in the idxNum mask
+** * The argument to step= if bit 0x0004 is in the idxNum mask
+** * The argument to LIMIT if bit 0x0020 is in the idxNum mask
+** * The argument to OFFSET if bit 0x0040 is in the idxNum mask
+** * The argument to value=, or value>= or value> if any of
+** bits 0x0380 are in the idxNum mask
+** * The argument to value<= or value< if either of bits 0x3000
+** are in the mask
+**
*/
static int seriesBestIndex(
sqlite3_vtab *pVTab,
#endif
int unusableMask = 0; /* Mask of unusable constraints */
int nArg = 0; /* Number of arguments that seriesFilter() expects */
- int aIdx[5]; /* Constraints on start, stop, step, LIMIT, OFFSET */
+ int aIdx[7]; /* Constraints on start, stop, step, LIMIT, OFFSET,
+ ** and value. aIdx[5] covers value=, value>=, and
+ ** value>, aIdx[6] covers value<= and value< */
const struct sqlite3_index_constraint *pConstraint;
/* This implementation assumes that the start, stop, and step columns
assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
- aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = -1;
+ aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = aIdx[5] = aIdx[6] = -1;
pConstraint = pIdxInfo->aConstraint;
for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
int iCol; /* 0 for start, 1 for stop, 2 for step */
}
continue;
}
- if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
+ if( pConstraint->iColumn==SERIES_COLUMN_VALUE ){
+ switch( op ){
+ case SQLITE_INDEX_CONSTRAINT_EQ:
+ case SQLITE_INDEX_CONSTRAINT_IS: {
+ idxNum |= 0x0080;
+ idxNum &= ~0x3300;
+ aIdx[5] = i;
+ aIdx[6] = -1;
+ bStartSeen = 1;
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_GE: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x0100;
+ idxNum &= ~0x0200;
+ aIdx[5] = i;
+ bStartSeen = 1;
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_GT: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x0200;
+ idxNum &= ~0x0100;
+ aIdx[5] = i;
+ bStartSeen = 1;
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_LE: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x1000;
+ idxNum &= ~0x2000;
+ aIdx[6] = i;
+ break;
+ }
+ case SQLITE_INDEX_CONSTRAINT_LT: {
+ if( idxNum & 0x0080 ) break;
+ idxNum |= 0x2000;
+ idxNum &= ~0x1000;
+ aIdx[6] = i;
+ break;
+ }
+ }
+ continue;
+ }
iCol = pConstraint->iColumn - SERIES_COLUMN_START;
assert( iCol>=0 && iCol<=2 );
iMask = 1 << iCol;
idxNum &= ~0x60;
aIdx[4] = 0;
}
- for(i=0; i<5; i++){
+ for(i=0; i<7; i++){
if( (j = aIdx[i])>=0 ){
pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
pIdxInfo->aConstraintUsage[j].omit =
pIdxInfo->estimatedRows = 2147483647;
}
pIdxInfo->idxNum = idxNum;
+#ifdef SQLITE_INDEX_SCAN_HEX
+ pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_HEX;
+#endif
return SQLITE_OK;
}
-C Add\sthe\sSQLITE_INDEX_SCAN_HEX\sbit\sto\sthe\ssqlite3_index_info.idxFlags\sbitmask.\nWhen\sset,\sthis\sbit\scauses\sthe\sEXPLAIN\sQUERY\sPLAN\soutput\sto\sshow\sthe\sidxNum\nvalue\sin\shex\srather\sthan\sin\sdecimal.\s\sThis\sis\spurely\sa\sdebugging\said.
-D 2024-08-22T16:22:08.900
+C Enhance\sthe\sgenerate_series()\stable-valued\sfunction\ssuch\sthat\sit\sis\sable\sto\nrecognize\sequality\sand\sinequality\sconstraints\son\sthe\s"value"\scolumn\sand\noptimize\sits\soperating\saccordingly.
+D 2024-08-22T18:12:10.402
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F ext/misc/remember.c add730f0f7e7436cd15ea3fd6a90fd83c3f706ab44169f7f048438b7d6baa69c
F ext/misc/rot13.c 51ac5f51e9d5fd811db58a9c23c628ad5f333c173f1fc53c8491a3603d38556c
F ext/misc/scrub.c 2a44b0d44c69584c0580ad2553f6290a307a49df4668941d2812135bfb96a946
-F ext/misc/series.c d96e5aac21658c6b5d54f918ac140460ec7197734c1a4fba806950831a7b1e7a
+F ext/misc/series.c a6089b5e8e3002bd1e5d9877cee6aead0b9a6426e406c09a399817db9e9ae823
F ext/misc/sha1.c 4011aef176616872b2a0d5bccf0ecfb1f7ce3fe5c3d107f3a8e949d8e1e3f08d
F ext/misc/shathree.c 1821d90a0040c9accdbe3e3527d378d30569475d758aa70f6848924c0b430e8c
F ext/misc/showauth.c 732578f0fe4ce42d577e1c86dc89dd14a006ab52
F test/memleak.test 10b9c6c57e19fc68c32941495e9ba1c50123f6e2
F test/memsubsys1.test 86b8158752af9188ed5b32a30674a1ef71183e6bc4e6808e815cd658ca9058a6
F test/memsubsys2.test 774b93cb09ca50d1b759bb7c645baa2a9ce172edc3a3da67d5150a26a9fc2a08
-F test/merge1.test 2de6d6ef8d25402764b1aab49d8f9d7f89208c89a6674e437f76de4c812157b8
+F test/merge1.test 7dd9dc6838bcd0623a069485fe3a8dd498a051c16e1877cf84f506c0d6a29b43
F test/minmax.test fe638b55d77d2375531a8f549b338eafcd9adfbd2f72df37ed77d9b26ca0a71a
F test/minmax2.test cf9311babb6f0518d04e42fd6a42c619531c4309a9dd790a2c4e9b3bc595e0de
F test/minmax3.test cc1e8b010136db0d01a6f2a29ba5a9f321034354
F test/sync2.test 8f9f7d4f6d5be8ca8941a8dadcc4299e558cb6a1ff653a9469146c7a76ef2039
F test/syscall.test a067468b43b8cb2305e9f9fe414e5f40c875bb5d2cba5f00b8154396e95fcf37
F test/sysfault.test c9f2b0d8d677558f74de750c75e12a5454719d04
-F test/tabfunc01.test f150d206294471d20f50029e6b46b76b87a7a010b16dc57eb44245c76dd02802
+F test/tabfunc01.test 6002a5f37b76355f173c75c2b3b03173b19d6a8b078c5baaa4c78bbcd0fa6323
F test/table.test 7862a00b58b5541511a26757ea9c5c7c3f8298766e98aa099deec703d9c0a8e0
F test/tableapi.test ecbcc29c4ab62c1912c3717c48ea5c5e59f7d64e4a91034e6148bd2b82f177f4
F test/tableopts.test dba698ba97251017b7c80d738c198d39ab747930
F test/temptable2.test 76821347810ecc88203e6ef0dd6897b6036ac788e9dd3e6b04fd4d1631311a16
F test/temptable3.test d11a0974e52b347e45ee54ef1923c91ed91e4637
F test/temptrigger.test 38f0ca479b1822d3117069e014daabcaacefffcc
-F test/tester.tcl e88c498c369cff6bf0898db6d04088685066730be51821ef775ef13fd2b1d077
+F test/tester.tcl 2c203a2dd664298f239f0ec3ce22fbc65b5f021c1e09edbae8452af8a694e052
F test/testrunner.tcl 5d02deeba7a53baeadae6aa7641d90aac58fdfa3a7bcac85cfcfd752b1aab87c
F test/testrunner_data.tcl c5ae2b1f9a99210b0600d002fb3af1fee350997cee9416551e83b93501360ebf
F test/thread001.test a0985c117eab62c0c65526e9fa5d1360dd1cac5b03bde223902763274ce21899
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 6500baa9914b08ce19c361a69f19a80e5d800fcb82579d1c1e03601c7a664338
-R 7b7b9fd0e23c278e007dcb15333f71d2
+P 6c00e88ebdb41d6317bb8758825521614dedc2e6e6289ff415c5f0406eed815b
+R c06569ccdd3e5d2b3b096a4379f23ac8
U drh
-Z e92224536d6b88aa809210ea20e1cd7b
+Z e410ad1cfdbbb6e926b30bd406bc8816
# Remove this line to create a well-formed Fossil manifest.