|
| 1 | +Keep mergeScan aligned with mutmap changes |
| 2 | + |
| 3 | +Doltlite merges committed prolly-tree rows with transaction-local mutmap |
| 4 | +entries during cursor scans. Two edge cases currently leak stale tree rows: |
| 5 | + |
| 6 | +1) A cursor can be positioned directly at a mutmap INSERT via |
| 7 | + setCursorToMutMapEntryPhys(), which jumps mmIdx past earlier mutmap |
| 8 | + entries. A later tree emission must still notice a mutmap DELETE for |
| 9 | + that same tree key, otherwise SQLite follows a rowid that the mutmap says |
| 10 | + is deleted and reports "database disk image is malformed". |
| 11 | + |
| 12 | +2) After emitting a mutmap-only row, the committed tree cursor can still be |
| 13 | + parked before the seek prefix used to find that mutmap row. The next scan |
| 14 | + step can then emit a committed tree row that does not satisfy the original |
| 15 | + prefix constraint. |
| 16 | + |
| 17 | +Fix both cases in mergeScan/mergeStep by checking tree keys against mutmap |
| 18 | +DELETE entries and by advancing the tree cursor past the emitted mutmap key. |
| 19 | + |
| 20 | +Reproduces deterministically on: |
| 21 | + |
| 22 | + CREATE TABLE c (ka TEXT, kb TEXT, kc TEXT, v TEXT, |
| 23 | + PRIMARY KEY (ka, kb, kc)); |
| 24 | + INSERT INTO c VALUES ('db','t','a',''); |
| 25 | + BEGIN IMMEDIATE; |
| 26 | + INSERT INTO c VALUES ('db','t','b',''); |
| 27 | + DELETE FROM c WHERE ka='db' AND kb='t' AND kc='a'; |
| 28 | + UPDATE c SET v = 'x' WHERE ka='db' AND kb='t'; -- malformed w/o fix |
| 29 | + |
| 30 | +And on: |
| 31 | + |
| 32 | + CREATE TABLE c (ts TEXT NOT NULL COLLATE NOCASE, |
| 33 | + tn TEXT NOT NULL COLLATE NOCASE, |
| 34 | + cn TEXT NOT NULL COLLATE NOCASE, |
| 35 | + ordinal INT NOT NULL, |
| 36 | + PRIMARY KEY (ts, tn, cn)) STRICT; |
| 37 | + BEGIN IMMEDIATE; |
| 38 | + INSERT INTO c VALUES ('sqlite_database', 't1', 'id', 1); |
| 39 | + COMMIT; |
| 40 | + BEGIN IMMEDIATE; |
| 41 | + INSERT INTO c VALUES ('sqlite_database', 't2', 'id', 1); |
| 42 | + SELECT tn FROM c WHERE ts = 'sqlite_database' AND tn = 't2'; |
| 43 | + -- returns t2,t1 without the tree-cursor alignment fix |
| 44 | + |
| 45 | +diff --git a/src/prolly_btree.c b/src/prolly_btree.c |
| 46 | +index 7085abb..f9e7d31 100644 |
| 47 | +--- a/src/prolly_btree.c |
| 48 | ++++ b/src/prolly_btree.c |
| 49 | +@@ -6026,0 +6027,33 @@ static int mergeCompare(BtCursor *pCur, ProllyMutMapEntry *e){ |
| 50 | ++static int skipDeletedTreeEntry(BtCursor *pCur, int dir, int *pSkipped){ |
| 51 | ++ ProllyMutMapEntry *delE = 0; |
| 52 | ++ int rc; |
| 53 | ++ *pSkipped = 0; |
| 54 | ++ if( pCur->curIntKey ){ |
| 55 | ++ rc = prollyMutMapFindRc(pCur->pMutMap, 0, 0, |
| 56 | ++ prollyCursorIntKey(&pCur->pCur), &delE); |
| 57 | ++ }else{ |
| 58 | ++ const u8 *pK; int nK; |
| 59 | ++ prollyCursorKey(&pCur->pCur, &pK, &nK); |
| 60 | ++ rc = prollyMutMapFindRc(pCur->pMutMap, pK, nK, 0, &delE); |
| 61 | ++ } |
| 62 | ++ if( rc!=SQLITE_OK ) return rc; |
| 63 | ++ if( delE && delE->op==PROLLY_EDIT_DELETE ){ |
| 64 | ++ rc = advanceTreeCursor(pCur, dir); |
| 65 | ++ if( rc!=SQLITE_OK ) return rc; |
| 66 | ++ *pSkipped = 1; |
| 67 | ++ } |
| 68 | ++ return SQLITE_OK; |
| 69 | ++} |
| 70 | ++ |
| 71 | ++static int advanceTreePastMutEntry(BtCursor *pCur, ProllyMutMapEntry *e, int dir){ |
| 72 | ++ int rc; |
| 73 | ++ if( !e ) return SQLITE_OK; |
| 74 | ++ while( pCur->pCur.eState==PROLLY_CURSOR_VALID ){ |
| 75 | ++ int cmp = mergeCompare(pCur, e); |
| 76 | ++ if( (dir>0 && cmp>0) || (dir<0 && cmp<0) ) break; |
| 77 | ++ rc = advanceTreeCursor(pCur, dir); |
| 78 | ++ if( rc!=SQLITE_OK ) return rc; |
| 79 | ++ } |
| 80 | ++ return SQLITE_OK; |
| 81 | ++} |
| 82 | ++ |
| 83 | +@@ -6044,0 +6078,4 @@ static int mergeScan(BtCursor *pCur, int dir, int *pRes){ |
| 84 | ++ int skipped = 0; |
| 85 | ++ int rc = skipDeletedTreeEntry(pCur, dir, &skipped); |
| 86 | ++ if( rc!=SQLITE_OK ) return rc; |
| 87 | ++ if( skipped ) continue; |
| 88 | +@@ -6057,0 +6095,11 @@ static int mergeScan(BtCursor *pCur, int dir, int *pRes){ |
| 89 | ++ /* Tree entry is ahead of mutmap[mmIdx] in scan direction. |
| 90 | ++ ** Check whether the mutmap has a DELETE entry for the tree |
| 91 | ++ ** key at an order index the iteration has already walked |
| 92 | ++ ** past (e.g. after setCursorToMutMapEntryPhys jumped mmIdx |
| 93 | ++ ** directly to a later INSERT). Without this check the scan |
| 94 | ++ ** would emit a logically-deleted tree row and SQLite would |
| 95 | ++ ** later TableMoveto a rowid that mutmap says is gone. */ |
| 96 | ++ int skipped = 0; |
| 97 | ++ int rc = skipDeletedTreeEntry(pCur, dir, &skipped); |
| 98 | ++ if( rc!=SQLITE_OK ) return rc; |
| 99 | ++ if( skipped ) continue; |
| 100 | +@@ -6115 +6163 @@ static int mergeStepForward(BtCursor *pCur){ |
| 101 | +- if( pCur->mergeSrc==MERGE_SRC_TREE || pCur->mergeSrc==MERGE_SRC_BOTH ){ |
| 102 | ++ if( pCur->mergeSrc==MERGE_SRC_TREE ){ |
| 103 | +@@ -6117,0 +6166,8 @@ static int mergeStepForward(BtCursor *pCur){ |
| 104 | ++ }else if( pCur->mergeSrc==MERGE_SRC_MUT |
| 105 | ++ || pCur->mergeSrc==MERGE_SRC_BOTH ){ |
| 106 | ++ ProllyMutMapEntry *e = 0; |
| 107 | ++ if( pCur->mmIdx >= 0 && pCur->mmIdx < pCur->pMutMap->nEntries ){ |
| 108 | ++ e = orderedMutMapEntryAt(pCur->pMutMap, pCur->mmIdx); |
| 109 | ++ } |
| 110 | ++ rc = advanceTreePastMutEntry(pCur, e, 1); |
| 111 | ++ if( rc!=SQLITE_OK ) return rc; |
| 112 | +@@ -6119 +6175 @@ static int mergeStepForward(BtCursor *pCur){ |
| 113 | +- if( pCur->mergeSrc==MERGE_SRC_MUT || pCur->mergeSrc==MERGE_SRC_BOTH ) |
| 114 | ++ if( pCur->mergeSrc==MERGE_SRC_MUT || pCur->mergeSrc==MERGE_SRC_BOTH ){ |
| 115 | +@@ -6120,0 +6177 @@ static int mergeStepForward(BtCursor *pCur){ |
| 116 | ++ } |
| 117 | +@@ -6129 +6186 @@ static int mergeStepBackward(BtCursor *pCur){ |
| 118 | +- if( pCur->mergeSrc==MERGE_SRC_TREE || pCur->mergeSrc==MERGE_SRC_BOTH ){ |
| 119 | ++ if( pCur->mergeSrc==MERGE_SRC_TREE ){ |
| 120 | +@@ -6131,0 +6189,8 @@ static int mergeStepBackward(BtCursor *pCur){ |
| 121 | ++ }else if( pCur->mergeSrc==MERGE_SRC_MUT |
| 122 | ++ || pCur->mergeSrc==MERGE_SRC_BOTH ){ |
| 123 | ++ ProllyMutMapEntry *e = 0; |
| 124 | ++ if( pCur->mmIdx >= 0 && pCur->mmIdx < pCur->pMutMap->nEntries ){ |
| 125 | ++ e = orderedMutMapEntryAt(pCur->pMutMap, pCur->mmIdx); |
| 126 | ++ } |
| 127 | ++ rc = advanceTreePastMutEntry(pCur, e, -1); |
| 128 | ++ if( rc!=SQLITE_OK ) return rc; |
| 129 | +@@ -6133 +6198 @@ static int mergeStepBackward(BtCursor *pCur){ |
| 130 | +- if( pCur->mergeSrc==MERGE_SRC_MUT || pCur->mergeSrc==MERGE_SRC_BOTH ) |
| 131 | ++ if( pCur->mergeSrc==MERGE_SRC_MUT || pCur->mergeSrc==MERGE_SRC_BOTH ){ |
| 132 | +@@ -6134,0 +6200 @@ static int mergeStepBackward(BtCursor *pCur){ |
| 133 | ++ } |
0 commit comments