1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Improvements and simplifications to the equality seek logic. Tests are

adjusted so that they all pass now.

FossilOrigin-Name: 997ce6c90b454c03cc2ef6934752ee8dd2e520e3
This commit is contained in:
drh
2015-11-05 22:30:54 +00:00
parent 70528d7868
commit b1d607de25
10 changed files with 84 additions and 63 deletions

View File

@@ -1,5 +1,5 @@
C The\stop\sof\san\sindex\sequality\sloop\snormally\sstarts\swith\sOP_SeekGE\sand\sOP_IdxGT.\nThis\scheck-in\sadds\sa\sflag\sto\sOP_SeekGE\ssuch\sthat\sit\sfails\simmediately\sif\nthe\skey\sis\snot\sequal,\sthen\sjumps\sover\sthe\sOP_IdxGT,\ssaving\sa\scall\sto\sthe\skey\ncomparison\sfunctions.\s\sConsider\sthis\scheck-in\sa\sproof-of-concept.\s\sIt\sneeds\s\nimprovement\sbefore\sgoing\son\strunk.\s\sSome\stests\sfail,\sbut\sonly\sbecause\sthey\nnew\suse\sfewer\skey\scomparisons\sthan\sexpected\s(which\sis\sa\sgood\sthing!).
D 2015-11-05T20:25:09.480
C Improvements\sand\ssimplifications\sto\sthe\sequality\sseek\slogic.\s\sTests\sare\nadjusted\sso\sthat\sthey\sall\spass\snow.
D 2015-11-05T22:30:54.990
F Makefile.in 3a705bb4bd12e194212ddbdbf068310d17153cdb
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc 702d3e98f3afc6587a78481257f3c4c900efc3a4
@@ -282,8 +282,8 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240
F src/backup.c 2869a76c03eb393ee795416e2387005553df72bc
F src/bitvec.c d1f21d7d91690747881f03940584f4cc548c9d3d
F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79
F src/btree.c 233c1536927b12d820cb0a19c657c59837f9334f
F src/btree.h b512723e4f27d7ba16b4b985cdecdb82c0f6d0c0
F src/btree.c 9e5d713bf54be8bfcae9b60210173dd53570f56d
F src/btree.h 1b8bf2818b5e256c25a5e09126720113b1d783da
F src/btreeInt.h 3ab435ed27adea54d040584b0bcc488ee7db1e38
F src/build.c ca574d33ffb1763cfd2979383f4d507095bfbe19
F src/callback.c 7b44ce59674338ad48b0e84e7b72f935ea4f68b0
@@ -345,7 +345,7 @@ F src/shell.c d25df04168d6ba5a4fa05bdbf859df667f9eb621
F src/sqlite.h.in 3cfc86c55e57c63d86b9e1e92869e2bfb162ca8e
F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
F src/sqlite3ext.h 4b66e3e3435da4b4c8c83696d0349f0c503b3924
F src/sqliteInt.h 8769e43c68421bdd6b4b830290965c9303056c4f
F src/sqliteInt.h 12ff325b433c4ded39995b74b01b78aacc19805a
F src/sqliteLimit.h 216557999cb45f2e3578ed53ebefe228d779cb46
F src/status.c 70912d7be68e9e2dbc4010c93d344af61d4c59ba
F src/table.c 51b46b2a62d1b3a959633d593b89bab5e2c9155e
@@ -403,7 +403,7 @@ F src/update.c 40e51cd0883cb5bfd6abb7d8a7cd8aa47fab2945
F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c
F src/util.c fc612367108b74573c5fd13a85d0a23027f438bd
F src/vacuum.c 2ddd5cad2a7b9cef7f9e431b8c7771634c6b1701
F src/vdbe.c 1f9ef1ef7e48e1d5268036a18b7c9b5cded49879
F src/vdbe.c a9b0ec9f8df86a805d593b625d1f21a8e71dcda5
F src/vdbe.h efb7a8c1459e31f3ea4377824c6a7e4cb5068637
F src/vdbeInt.h 33403622c6a8feaaac5f0f3f17f5d1bf6df42286
F src/vdbeapi.c 020681b943e77766b32ae1cddf86d7831b7374ca
@@ -419,7 +419,7 @@ F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
F src/walker.c 2e14d17f592d176b6dc879c33fbdec4fbccaa2ba
F src/where.c 6aceb72cc58dc06922a9e1604d559c8ca4c3e728
F src/whereInt.h 7892bb54cf9ca0ae5c7e6094491b94c9286dc647
F src/wherecode.c b45fe3878b0ea4962784d3072498ff63b233d958
F src/wherecode.c 4c96182e7b25e4be54008dee2da5b9c2f8480b9b
F src/whereexpr.c e63244ca06c503e5f3c5b7f3c9aea0db826089ed
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
@@ -519,7 +519,7 @@ F test/coalesce.test cee0dccb9fbd2d494b77234bccf9dc6c6786eb91
F test/collate1.test 08c18e7512a5a32c97938854263fa15362eeb846
F test/collate2.test 9aaa410a00734e48bcb27f3872617d6f69b2a621
F test/collate3.test 89defc49983ddfbf0a0555aca8c0521a676f56a5
F test/collate4.test f04d5168685f2eef637ecfa2d4ddf8ec0d600177
F test/collate4.test c953715fb498b87163e3e73dd94356bff1f317bd
F test/collate5.test 65d928034d30d2d263a80f6359f7549ee1598ec6
F test/collate6.test 8be65a182abaac8011a622131486dafb8076e907
F test/collate7.test 8ec29d98f3ee4ccebce6e16ce3863fb6b8c7b868
@@ -1301,10 +1301,10 @@ F test/walro.test 34422d1d95aaff0388f0791ec20edb34e2a3ed57
F test/walshared.test 0befc811dcf0b287efae21612304d15576e35417
F test/walslow.test e7be6d9888f83aa5d3d3c7c08aa9b5c28b93609a
F test/walthread.test de8dbaf6d9e41481c460ba31ca61e163d7348f8e
F test/where.test 66d4c107e82dfe86c01a96277b77e7a8809aff0b
F test/where.test 9902a3d84e9bc80357a2c54ed0e76c0d6d04a867
F test/where2.test af78c55589cbc82d793449493adba0dc3d659f23
F test/where3.test 1ad55ba900bd7747f98b6082e65bd3e442c5004e
F test/where4.test 44f506bf1737cf0fa4fc795e340208250f1fcd89
F test/where4.test 4a371bfcc607f41d233701bdec33ac2972908ba8
F test/where5.test fdf66f96d29a064b63eb543e28da4dfdccd81ad2
F test/where6.test 5da5a98cec820d488e82708301b96cb8c18a258b
F test/where7.test 5a4b0abc207d71da4deecd734ad8579e8dd40aa8
@@ -1398,10 +1398,7 @@ F tool/vdbe_profile.tcl 246d0da094856d72d2c12efec03250d71639d19f
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 445480095e6877cce8220b1c095f334bbb04c1c3
R 05aade661a6de043d2b7f27fd40fb41a
T *branch * seekeq-experiment
T *sym-seekeq-experiment *
T -sym-trunk *
P 32e31b9bc8664afcd326a1ff3892d86dc5202474
R 39eaab676629cc3889ae2b19a383806f
U drh
Z 34383bd81ccfcce5590ebe9ba011f808
Z 569a968eaa17fd575e8f13c4e6f1f518

View File

@@ -1 +1 @@
32e31b9bc8664afcd326a1ff3892d86dc5202474
997ce6c90b454c03cc2ef6934752ee8dd2e520e3

View File

@@ -5041,6 +5041,8 @@ int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
** *pRes>0 The cursor is left pointing at an entry that
** is larger than intKey/pIdxKey.
**
** For index tables, the pIdxKey->eqSeen field is set to 1 if there
** exists an entry in the table that exactly matches pIdxKey.
*/
int sqlite3BtreeMovetoUnpacked(
BtCursor *pCur, /* The cursor to be moved */
@@ -9644,7 +9646,6 @@ int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
return rc;
}
#ifdef SQLITE_DEBUG
/*
** Return true if the cursor has a hint specified. This routine is
** only used from within assert() statements
@@ -9652,7 +9653,6 @@ int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
return (pCsr->hints & mask)!=0;
}
#endif
/*
** Return true if the given Btree is read-only.

View File

@@ -257,9 +257,7 @@ int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
void sqlite3BtreeIncrblobCursor(BtCursor *);
void sqlite3BtreeClearCursor(BtCursor *);
int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
#ifdef SQLITE_DEBUG
int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
#endif
int sqlite3BtreeIsReadonly(Btree *pBt);
int sqlite3HeaderSizeBtree(void);

View File

@@ -1812,9 +1812,8 @@ struct KeyInfo {
};
/*
** An instance of the following structure holds information about a
** single index record that has already been parsed out into individual
** values.
** This object holds a record which has been parsed out into individual
** fields, for the purposes of doing a comparison.
**
** A record is an object that contains one or more fields of data.
** Records are used to store the content of a table row and to store
@@ -1822,11 +1821,30 @@ struct KeyInfo {
** the OP_MakeRecord opcode of the VDBE and is disassembled by the
** OP_Column opcode.
**
** This structure holds a record that has already been disassembled
** into its constituent fields.
** An instance of this object serves as a "key" for doing a search on
** an index b+tree. The goal of the search is to find the entry that
** is closed to the key described by this object. This object might hold
** just a prefix of the key. The number of fields is given by
** pKeyInfo->nField.
**
** The r1 and r2 member variables are only used by the optimized comparison
** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
** The r1 and r2 fields are the values to return if this key is less than
** or greater than a key in the btree, respectively. These are normally
** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
** is in DESC order.
**
** The key comparison functions actually return default_rc when they find
** an equals comparison. default_rc can be -1, 0, or +1. If there are
** multiple entries in the b-tree with the same key (when only looking
** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
** cause the search to find the last match, or +1 to cause the search to
** find the first match.
**
** The key comparison functions will set eqSeen to true if they ever
** get and equal results when comparing this structure to a b-tree record.
** When default_rc!=0, the search might end up on the record immediately
** before the first match or immediately after the last match. The
** eqSeen field will indicate whether or not an exact match exists in the
** b-tree.
*/
struct UnpackedRecord {
KeyInfo *pKeyInfo; /* Collation and sort-order information */

View File

@@ -3597,6 +3597,13 @@ case OP_ColumnsUsed: {
** is greater than or equal to the key value. If there are no records
** greater than or equal to the key and P2 is not zero, then jump to P2.
**
** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
** opcode will always land on a record that equally equals the key, or
** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
** opcode must be followed by an IdxLE opcode with the same arguments.
** The IdxLE opcode will be skipped if this opcode succeeds, but the
** IdxLE opcode will be used on subsequent loop iterations.
**
** This opcode leaves the cursor configured to move in forward order,
** from the beginning toward the end. In other words, the cursor is
** configured to use Next, not Prev.
@@ -3655,18 +3662,26 @@ case OP_ColumnsUsed: {
** from the end toward the beginning. In other words, the cursor is
** configured to use Prev, not Next.
**
** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
** opcode will always land on a record that equally equals the key, or
** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
** opcode must be followed by an IdxGE opcode with the same arguments.
** The IdxGE opcode will be skipped if this opcode succeeds, but the
** IdxGE opcode will be used on subsequent loop iterations.
**
** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
*/
case OP_SeekLT: /* jump, in3 */
case OP_SeekLE: /* jump, in3 */
case OP_SeekGE: /* jump, in3 */
case OP_SeekGT: { /* jump, in3 */
int res;
int oc;
VdbeCursor *pC;
UnpackedRecord r;
int nField;
int res; /* Comparison result */
int oc; /* Opcode */
VdbeCursor *pC; /* The cursor to seek */
UnpackedRecord r; /* The key to seek for */
int nField; /* Number of columns or fields in the key */
i64 iKey; /* The rowid we are to seek to */
int eqOnly = 0; /* Only interested in == results */
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
assert( pOp->p2!=0 );
@@ -3688,18 +3703,15 @@ case OP_SeekGT: { /* jump, in3 */
** OP_SeekLE opcodes are allowed, and these must be immediately followed
** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
*/
#ifdef SQLITE_DEBUG
if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
eqOnly = 1;
assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
#if 0
assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
assert( pOp[1].p1==pOp[0].p1 );
assert( pOp[1].p2==pOp[0].p2 );
assert( pOp[1].p3==pOp[0].p3 );
assert( pOp[1].p4.i==pOp[0].p4.i );
#endif
}
#endif
if( pC->isTable ){
/* The input value in P3 might be of any type: integer, real, string,
@@ -3749,6 +3761,7 @@ case OP_SeekGT: { /* jump, in3 */
if( rc!=SQLITE_OK ){
goto abort_due_to_error;
}
if( eqOnly && res ) goto seek_not_found;
}else{
nField = pOp->p4.i;
assert( pOp->p4type==P4_INT32 );
@@ -3779,8 +3792,9 @@ case OP_SeekGT: { /* jump, in3 */
if( rc!=SQLITE_OK ){
goto abort_due_to_error;
}
if( (pOp->p5 & OPFLAG_SEEKEQ)!=0 && r.eqSeen==0 ){
goto take_the_jump;
if( eqOnly && r.eqSeen==0 ){
assert( res!=0 );
goto seek_not_found;
}
}
pC->deferredMoveto = 0;
@@ -3809,11 +3823,14 @@ case OP_SeekGT: { /* jump, in3 */
res = sqlite3BtreeEof(pC->pCursor);
}
}
take_the_jump:
seek_not_found:
assert( pOp->p2>0 );
VdbeBranchTaken(res!=0,2);
if( res ){
goto jump_to_p2;
}else if( eqOnly ){
assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
}
break;
}

View File

@@ -1026,7 +1026,6 @@ Bitmask sqlite3WhereCodeOneLoopStart(
WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */
int startEq; /* True if range start uses ==, >= or <= */
int endEq; /* True if range end uses ==, >= or <= */
int eqOnly; /* True if uses only == */
int start_constraints; /* Start of range is constrained */
int nConstraint; /* Number of constraint terms */
Index *pIdx; /* The index we will be using */
@@ -1096,8 +1095,6 @@ Bitmask sqlite3WhereCodeOneLoopStart(
}
}
assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
eqOnly = nEq>0 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)==0
&& (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0;
/* If we are doing a reverse order scan on an ascending index, or
** a forward order scan on a descending index, interchange the
@@ -1170,7 +1167,6 @@ Bitmask sqlite3WhereCodeOneLoopStart(
VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE );
VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE );
VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT );
if( eqOnly ) sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ);
/* Load the value for the inequality constraint at the end of the
** range (if any).
@@ -1206,11 +1202,6 @@ Bitmask sqlite3WhereCodeOneLoopStart(
/* Check if the index cursor is past the end of the range. */
if( nConstraint ){
if( eqOnly ){
int bx = sqlite3VdbeCurrentAddr(v);
sqlite3VdbeAddOp2(v, OP_Goto, 0, bx+2);
pLevel->p2 = bx+1;
}
op = aEndOp[bRev*2 + endEq];
sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT );

View File

@@ -352,7 +352,7 @@ do_test collate4-2.1.2 {
count {
SELECT * FROM collate4t2, collate4t1 WHERE a = b;
}
} {A a A A 5}
} {A a A A 4}
do_test collate4-2.1.3 {
count {
SELECT * FROM collate4t2, collate4t1 WHERE b = a;
@@ -372,7 +372,7 @@ do_test collate4-2.1.5 {
count {
SELECT * FROM collate4t2, collate4t1 WHERE b = a;
}
} {A A 4}
} {A A 3}
ifcapable subquery {
do_test collate4-2.1.6 {
count {
@@ -389,12 +389,12 @@ ifcapable subquery {
SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
ORDER BY rowid
}
} {a A 6}
} {a A 5}
do_test collate4-2.1.8 {
count {
SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
}
} {a A 5}
} {a A 4}
do_test collate4-2.1.9 {
execsql {
DROP INDEX collate4i1;

View File

@@ -412,22 +412,22 @@ ifcapable subquery {
count {
SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
}
} {1 0 4 2 1 9 3 1 16 13}
} {1 0 4 2 1 9 3 1 16 12}
do_test where-5.3b {
count {
SELECT * FROM t1 WHERE w IN (3,-1,1,2) order by 1;
}
} {1 0 4 2 1 9 3 1 16 13}
} {1 0 4 2 1 9 3 1 16 12}
do_test where-5.3c {
count {
SELECT * FROM t1 WHERE w IN (3,2,-1,1,2) order by 1;
}
} {1 0 4 2 1 9 3 1 16 13}
} {1 0 4 2 1 9 3 1 16 12}
do_test where-5.3d {
count {
SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC;
}
} {3 1 16 2 1 9 1 0 4 12}
} {3 1 16 2 1 9 1 0 4 11}
do_test where-5.4 {
count {
SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
@@ -465,7 +465,7 @@ ifcapable subquery {
count {
SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
}
} {2 1 9 3 1 16 7}
} {2 1 9 3 1 16 6}
do_test where-5.10 {
count {
SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
@@ -485,17 +485,17 @@ ifcapable subquery {
count {
SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
}
} {2 1 9 3 1 16 7}
} {2 1 9 3 1 16 6}
do_test where-5.14 {
count {
SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
}
} {2 1 9 8}
} {2 1 9 5}
do_test where-5.15 {
count {
SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
}
} {2 1 9 3 1 16 11}
} {2 1 9 3 1 16 9}
do_test where-5.100 {
db eval {
SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)

View File

@@ -91,7 +91,7 @@ do_test where4-1.10 {
} {6 2}
do_test where4-1.11 {
count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=123}
} {1}
} {0}
do_test where4-1.12 {
count {SELECT rowid FROM t1 WHERE w=x'78' AND x IS NULL AND y=x'7A'}
} {6 2}