Skip to content

Commit b4d6edf

Browse files
committed
Patch Doltlite to preserve pIdxKey->eqSeen across mutmap lookup
Root cause of the ALTER TABLE failures: in prollyBtCursorIndexMoveto, when the cursor has a non-empty mutmap, findMatchingMutMapEntry gets called after the tree scan. Its internal sqlite3VdbeRecordCompare probes reset pIdxKey->eqSeen to 0 on each call, clobbering the flag that the tree scan loop set to 1 for a prefix match. OP_SeekGE with BTREE_SEEK_EQ then sees eqSeen=0 and bails to seek_not_found, silently skipping the DELETE. The fix saves eqSeen before findMatchingMutMapEntry and restores it whenever the mutmap branch isn't taken. Reproduced and traced to this exact line via source-level printf in a local Doltlite build; the fix makes the cross-check in the verify step (two sequential DELETEs inside a SAVEPOINT on a composite NOCASE PK) return the expected single remaining row. Drop the previous test exclusions; run the full PHPUnit suite.
1 parent 2222a12 commit b4d6edf

1 file changed

Lines changed: 90 additions & 34 deletions

File tree

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

Lines changed: 90 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,74 @@ jobs:
3131
repository: dolthub/doltlite
3232
path: doltlite-src
3333

34-
- name: Disable Doltlite's auto-conversion of PK tables to WITHOUT ROWID
35-
# Doltlite auto-converts tables with non-INTEGER/composite PK to
36-
# WITHOUT ROWID, which strips the `ROWID` column. The driver uses
37-
# `ORDER BY ROWID` as a tie-break in info-schema reads, so we
38-
# neutralize the single assignment in src/build.c that flips
39-
# TF_WithoutRowid on — those tables then take SQLite's normal
40-
# rowid + unique-PK-index path.
34+
- name: Patch Doltlite for mysql-on-sqlite compatibility
35+
# Two narrow, verifiable source-level patches applied before build:
36+
#
37+
# 1) src/build.c — neutralize the auto-conversion of composite/TEXT-PK
38+
# tables to WITHOUT ROWID, so the driver's `ORDER BY ROWID`
39+
# 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.
4153
run: |
42-
FILE=doltlite-src/src/build.c
43-
N=$(grep -cF ' tabOpts |= TF_WithoutRowid;' "$FILE")
54+
BUILD=doltlite-src/src/build.c
55+
MOVETO=doltlite-src/src/prolly_btree.c
56+
57+
# --- Patch 1: disable WITHOUT ROWID auto-conversion ---
58+
N=$(grep -cF ' tabOpts |= TF_WithoutRowid;' "$BUILD")
4459
if [ "$N" != "1" ]; then
45-
echo "::error::expected exactly 1 match, found $N"; exit 1
60+
echo "::error::build.c patch: expected exactly 1 match, found $N"; exit 1
4661
fi
47-
sed -i 's@^ tabOpts |= TF_WithoutRowid;$@ /* patched off in CI: do not auto-convert composite-PK tables */ (void)0;@' "$FILE"
48-
echo "Patched line:"
49-
grep -n 'patched off in CI' "$FILE"
62+
sed -i 's@^ tabOpts |= TF_WithoutRowid;$@ /* patched off in CI: do not auto-convert composite-PK tables */ (void)0;@' "$BUILD"
63+
64+
# --- Patch 2: preserve pIdxKey->eqSeen across findMatchingMutMapEntry ---
65+
MARKER=' rc = findMatchingMutMapEntry((ProllyMutMap\*)pCur->pMutMap,'
66+
N=$(grep -cE "$MARKER" "$MOVETO")
67+
if [ "$N" != "1" ]; then
68+
echo "::error::prolly_btree.c patch: expected exactly 1 match for marker, found $N"; exit 1
69+
fi
70+
patch -d doltlite-src -p1 --no-backup-if-mismatch <<'PATCH'
71+
--- a/src/prolly_btree.c
72+
+++ b/src/prolly_btree.c
73+
@@ -4445,6 +4445,14 @@
74+
|| (pPending && pPending!=pCur->pMutMap
75+
&& !prollyMutMapIsEmpty(pPending)))
76+
&& !(treeFound && treeCmp==0) ){
77+
+ /* findMatchingMutMapEntry's internal sqlite3VdbeRecordCompare
78+
+ ** calls reset then set pIdxKey->eqSeen on each probe, clobbering
79+
+ ** the flag that the tree-scan loop above set. OP_SeekGE with
80+
+ ** BTREE_SEEK_EQ relies on that eqSeen to detect an exact
81+
+ ** prefix match; without it a second same-table DELETE inside
82+
+ ** a SAVEPOINT silently bails out of seek_not_found. Save it
83+
+ ** here and restore it when the mutmap doesn't override. */
84+
+ int savedEqSeen = pIdxKey->eqSeen;
85+
rc = findMatchingMutMapEntry((ProllyMutMap*)pCur->pMutMap,
86+
pCur->pKeyInfo,
87+
pIdxKey, pSortKey, nSortKey,
88+
@@ -4462,6 +4470,9 @@
89+
return rc;
90+
}
91+
}
92+
+ if( !mutE ){
93+
+ pIdxKey->eqSeen = savedEqSeen;
94+
+ }
95+
if( mutE ){
96+
97+
const u8 *pMutVal = mutE->pVal;
98+
PATCH
99+
100+
echo "--- Patched lines ---"
101+
grep -n 'patched off in CI\|savedEqSeen' "$BUILD" "$MOVETO"
50102

51103
- name: Build Doltlite shared library
52104
run: |
@@ -91,7 +143,7 @@ jobs:
91143
echo "ldd pdo_sqlite:"
92144
ldd "$(php -r 'echo ini_get("extension_dir");')/pdo_sqlite.so"
93145
94-
- name: Verify Doltlite is active in PHP and rowid patch took effect
146+
- name: Verify Doltlite is active and both patches took effect
95147
run: |
96148
VERSION=$(php -r 'echo (new PDO("sqlite::memory:"))->query("SELECT SQLITE_VERSION()")->fetch()[0];')
97149
ENGINE=$(php -r 'echo (new PDO("sqlite::memory:"))->query("SELECT doltlite_engine()")->fetch()[0];')
@@ -101,9 +153,8 @@ jobs:
101153
echo "::error::Doltlite is not active (expected doltlite_engine() = 'prolly', got '${ENGINE}')"
102154
exit 1
103155
fi
104-
# Confirm the build.c patch took effect: a composite-PK table
105-
# would normally be auto-converted to WITHOUT ROWID (no rowid
106-
# column); with the patch, rowid should be accessible.
156+
# 1) build.c patch: composite-PK tables keep rowid accessible
157+
# instead of auto-converting to WITHOUT ROWID.
107158
ROWID=$(php -r '
108159
$db = new PDO("sqlite::memory:");
109160
$db->exec("CREATE TABLE t (a INT, b INT, PRIMARY KEY(a, b))");
@@ -115,6 +166,27 @@ jobs:
115166
echo "::error::rowid-preservation patch did not take effect: ${ROWID}"
116167
exit 1
117168
fi
169+
# 2) prolly_btree.c patch: two sequential DELETEs inside a
170+
# SAVEPOINT on a composite NOCASE PK no longer silently lose
171+
# the second DELETE.
172+
REMAINING=$(php -r '
173+
$db = new PDO("sqlite::memory:");
174+
$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))");
175+
$db->exec("INSERT INTO t VALUES (\"s\", \"t\", \"id\")");
176+
$db->exec("INSERT INTO t VALUES (\"s\", \"t\", \"alpha\")");
177+
$db->exec("INSERT INTO t VALUES (\"s\", \"t\", \"beta\")");
178+
$db->exec("SAVEPOINT sp");
179+
$db->exec("DELETE FROM t WHERE ts = \"s\" AND tn = \"t\" AND cn = \"alpha\"");
180+
$db->exec("DELETE FROM t WHERE ts = \"s\" AND tn = \"t\" AND cn = \"beta\"");
181+
$db->exec("RELEASE SAVEPOINT sp");
182+
$rows = $db->query("SELECT cn FROM t ORDER BY cn")->fetchAll(PDO::FETCH_COLUMN);
183+
echo implode(",", $rows);
184+
' 2>&1)
185+
echo "post-SAVEPOINT remaining rows = ${REMAINING}"
186+
if [ "${REMAINING}" != "id" ]; then
187+
echo "::error::eqSeen-preservation patch did not take effect: expected only 'id', got '${REMAINING}'"
188+
exit 1
189+
fi
118190
119191
- name: Install Composer dependencies (root)
120192
uses: ramsey/composer-install@v3
@@ -130,21 +202,5 @@ jobs:
130202
composer-options: "--optimize-autoloader"
131203

132204
- name: Run PHPUnit tests against Doltlite
133-
# Known Doltlite divergences on the rowid-patched storage path.
134-
# Every excluded test exercises the same underlying bug class:
135-
# inside a SAVEPOINT/BEGIN IMMEDIATE, two sequential writes
136-
# against a table with a COLLATE NOCASE composite PK lose the
137-
# second write. Reproduced as a minimal PDO script. Filtered out
138-
# here via a negative-lookahead regex so this job can run green
139-
# until the upstream fix lands.
140-
run: |
141-
EXCLUDE='testAlterTableDropMultipleColumns'
142-
EXCLUDE="${EXCLUDE}|testAlterTableAddAndDropColumns"
143-
EXCLUDE="${EXCLUDE}|testAlterTableWithMultiColumnFirstAndAfter"
144-
EXCLUDE="${EXCLUDE}|testAlterTableModifyColumnComplexChange"
145-
EXCLUDE="${EXCLUDE}|testColumnWithOnUpdate"
146-
EXCLUDE="${EXCLUDE}|testDynamicDatabaseNameWithUseStatement"
147-
EXCLUDE="${EXCLUDE}|testFullyQualifiedTableName"
148-
EXCLUDE="${EXCLUDE}|testWriteWithUsageOfInformationSchemaTables"
149-
php ./vendor/bin/phpunit -c ./phpunit.xml.dist --filter "/^(?!.*::(${EXCLUDE})( |\$)).+$/"
205+
run: php ./vendor/bin/phpunit -c ./phpunit.xml.dist
150206
working-directory: packages/mysql-on-sqlite

0 commit comments

Comments
 (0)