Skip to content

Commit f98745a

Browse files
committed
Patch Doltlite to preserve pIdxKey->eqSeen across mutmap lookup
prollyBtCursorIndexMoveto's tree-scan loop sets pIdxKey->eqSeen when it finds an exact prefix match, and OP_SeekGE with BTREE_SEEK_EQ relies on that flag to decide whether the seek landed on an exact row. The subsequent findMatchingMutMapEntry call internally runs sqlite3VdbeRecordCompare against each candidate mutmap entry, and each probe resets eqSeen to 0 before the comparison — so after the lookup the flag is whatever the last mutmap probe left behind, not what the tree scan saw. The observable symptom: two sequential DELETEs on a composite NOCASE PK inside a SAVEPOINT. The first DELETE seeds the mutmap; the second goes through prollyBtCursorIndexMoveto, finds the matching tree row, sets eqSeen=1, then loses that flag when checking the mutmap. OP_SeekGE reads eqSeen=0, bails to seek_not_found, and the second DELETE silently does nothing. Patch saves eqSeen before the mutmap lookup and restores it when the lookup returns no override — checked-in as a .patch file and applied before build.
1 parent 60b4b76 commit f98745a

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Preserve pIdxKey->eqSeen across findMatchingMutMapEntry
2+
3+
findMatchingMutMapEntry's internal sqlite3VdbeRecordCompare probes reset
4+
and then re-set pIdxKey->eqSeen on every iteration, clobbering the flag
5+
that the tree-scan loop set to signal an exact prefix match. OP_SeekGE
6+
with BTREE_SEEK_EQ relies on that eqSeen flag to decide whether an
7+
exact match was found, so without this save/restore the second same-
8+
table DELETE inside a SAVEPOINT silently bails to seek_not_found and
9+
the DELETE is lost.
10+
11+
--- a/src/prolly_btree.c
12+
+++ b/src/prolly_btree.c
13+
@@ -4445,6 +4445,14 @@
14+
|| (pPending && pPending!=pCur->pMutMap
15+
&& !prollyMutMapIsEmpty(pPending)))
16+
&& !(treeFound && treeCmp==0) ){
17+
+ /* findMatchingMutMapEntry's internal sqlite3VdbeRecordCompare
18+
+ ** calls reset and re-set pIdxKey->eqSeen on every probe,
19+
+ ** clobbering the flag that the tree-scan loop above set.
20+
+ ** OP_SeekGE with BTREE_SEEK_EQ relies on that eqSeen to detect
21+
+ ** an exact prefix match; without save/restore, the second
22+
+ ** same-table DELETE inside a SAVEPOINT silently bails out of
23+
+ ** seek_not_found. */
24+
+ int savedEqSeen = pIdxKey->eqSeen;
25+
rc = findMatchingMutMapEntry((ProllyMutMap*)pCur->pMutMap,
26+
pCur->pKeyInfo,
27+
pIdxKey, pSortKey, nSortKey,
28+
@@ -4462,6 +4470,9 @@
29+
return rc;
30+
}
31+
}
32+
+ if( !mutE ){
33+
+ pIdxKey->eqSeen = savedEqSeen;
34+
+ }
35+
if( mutE ){
36+
37+
const u8 *pMutVal = mutE->pVal;

‎.github/workflows/phpunit-tests-doltlite.yml‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,22 @@ jobs:
3737
# 1) src/build.c — neutralize the auto-conversion of composite/TEXT-PK
3838
# tables to WITHOUT ROWID, so the driver's `ORDER BY ROWID`
3939
# tie-breaks in info-schema reads keep working.
40+
#
41+
# 2) src/prolly_btree.c — fix a write-visibility bug in
42+
# prollyBtCursorIndexMoveto. When a non-empty mutmap exists,
43+
# the function calls findMatchingMutMapEntry which internally
44+
# calls sqlite3VdbeRecordCompare with `pIdxKey->eqSeen = 0`
45+
# on every comparison, clobbering the eqSeen flag that the
46+
# tree scan loop above it set. OP_SeekGE with BTREE_SEEK_EQ
47+
# relies on eqSeen to detect prefix matches, so the second
48+
# same-table DELETE inside a SAVEPOINT lands on an exact
49+
# match in the tree but SeekGE reads eqSeen=0 and bails to
50+
# seek_not_found, skipping the actual delete. The patch
51+
# saves eqSeen before findMatchingMutMapEntry and restores
52+
# it when no mutmap entry overrides the tree result.
4053
run: |
4154
BUILD=doltlite-src/src/build.c
55+
MOVETO=doltlite-src/src/prolly_btree.c
4256
4357
# --- Patch 1: disable WITHOUT ROWID auto-conversion ---
4458
N=$(grep -cF ' tabOpts |= TF_WithoutRowid;' "$BUILD")
@@ -47,8 +61,12 @@ jobs:
4761
fi
4862
sed -i 's@^ tabOpts |= TF_WithoutRowid;$@ /* patched off in CI: do not auto-convert composite-PK tables */ (void)0;@' "$BUILD"
4963
64+
# --- Patch 2: preserve pIdxKey->eqSeen across findMatchingMutMapEntry ---
65+
patch -d doltlite-src -p1 --no-backup-if-mismatch \
66+
< .github/doltlite-patches/eqSeen-preservation.patch
67+
5068
echo "--- Patched lines ---"
51-
grep -n 'patched off in CI' "$BUILD"
69+
grep -n 'patched off in CI\|savedEqSeen' "$BUILD" "$MOVETO"
5270
5371
- name: Build Doltlite shared library
5472
run: |
@@ -116,6 +134,27 @@ jobs:
116134
echo "::error::rowid-preservation patch did not take effect: ${ROWID}"
117135
exit 1
118136
fi
137+
# 2) prolly_btree.c patch: two sequential DELETEs inside a
138+
# SAVEPOINT on a composite NOCASE PK no longer silently lose
139+
# the second DELETE.
140+
REMAINING=$(php -r '
141+
$db = new PDO("sqlite::memory:");
142+
$db->exec("CREATE TABLE t (ts TEXT NOT NULL COLLATE NOCASE, tn TEXT NOT NULL COLLATE NOCASE, cn TEXT NOT NULL COLLATE NOCASE, PRIMARY KEY (ts, tn, cn))");
143+
$db->exec("INSERT INTO t VALUES (\"s\", \"t\", \"id\")");
144+
$db->exec("INSERT INTO t VALUES (\"s\", \"t\", \"alpha\")");
145+
$db->exec("INSERT INTO t VALUES (\"s\", \"t\", \"beta\")");
146+
$db->exec("SAVEPOINT sp");
147+
$db->exec("DELETE FROM t WHERE ts = \"s\" AND tn = \"t\" AND cn = \"alpha\"");
148+
$db->exec("DELETE FROM t WHERE ts = \"s\" AND tn = \"t\" AND cn = \"beta\"");
149+
$db->exec("RELEASE SAVEPOINT sp");
150+
$rows = $db->query("SELECT cn FROM t ORDER BY cn")->fetchAll(PDO::FETCH_COLUMN);
151+
echo implode(",", $rows);
152+
' 2>&1)
153+
echo "post-SAVEPOINT remaining rows = ${REMAINING}"
154+
if [ "${REMAINING}" != "id" ]; then
155+
echo "::error::eqSeen-preservation patch did not take effect: expected only 'id', got '${REMAINING}'"
156+
exit 1
157+
fi
119158
120159
- name: Install Composer dependencies (root)
121160
uses: ramsey/composer-install@v3

0 commit comments

Comments
 (0)