Skip to content

fix(query): match exact searches without InnoDB phrase positions - #14871

Merged
abnegate merged 1 commit into
mainfrom
fix/mysql-exact-search-out-of-order-commits
Oct 23, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix/mysql-exact-search-out-of-order-commits

Conversation

@abnegate

@abnegate abnegate commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Exact (quoted) full-text searches on MySQL and MariaDB can miss rows that clearly contain the phrase.

InnoDB records full-text document IDs in the order transactions assign them, but its phrase search assumes those IDs are also in commit order. When two transactions update indexed rows and the one holding the lower document ID commits last, that row stops matching phrase searches until it is written again. Plain word searches still find it. This is MySQL bug #110448. It reproduces on MySQL 8.4 and on MariaDB 10.11, 11.4 and 11.8.

With shared tables, every project's users live in one _users table, so writes from other projects regularly interleave with a user update. The shared MariaDB Users E2E lane hits this: UsersCustomServerTest::testUpdateUserEmailSearch searches "[email protected]" right after the email update and gets no users. The row's search value is correct, and a word search finds it, but the phrase search does not.

The MySQL builder no longer relies on InnoDB phrase positions for exact searches:

(MATCH(`search`) AGAINST('+users +service +updated' IN BOOLEAN MODE)
  AND `search` REGEXP '(^|[^[:alnum:]_])users[^[:alnum:]_]+service[^[:alnum:]_]+updated[^[:alnum:]_]+com([^[:alnum:]_]|$)')
  • The full-text index still narrows the candidates: it requires every word InnoDB indexes, which excludes the default stopwords and words shorter than the minimum token size of 3.
  • REGEXP checks that all the words appear in order as whole words, which is the phrase semantics. Matching follows the column collation, so it stays case-insensitive.
  • A term made only of stopwords or short words keeps the old phrase form, because InnoDB cannot require words it does not index.
  • notSearch negates the combined expression.

Non-exact searches are unchanged.

Test Plan

  • New MariaDBIntegrationTest and MySQLIntegrationTest cases open two connections, update two rows, and commit them in reverse document ID order. Without this change, the exact search returns only the second row on both engines. With it, it returns both. A second case checks that whole words must appear in order, that matching ignores case, and that the negated search returns the complementary rows.
  • The full MySQL (8.4) and MariaDB (11 and 10.11) integration suites pass.
  • MySQLTest covers the new SQL and bindings, including the stopword-only fallback and the negated form. The query package unit suite passes.
  • The Users E2E suite in the CI shared MariaDB configuration (_APP_DATABASE_SHARED_TABLES=database_db_main, --processes 4 --functional):
    • Before this change, testUpdateUserEmailSearch failed in 3 of 15 local runs on the fix(query): tighten function name and cast type parsing in the AST #14866 head, whose search code is the same as main, and in 0 of 14 runs on main. The failure only appears when another project's write interleaves with the update. While a failed stack was still up, the row held the right search value and a word search found it, but the phrase query found nothing.
    • Four concurrent clients updating a user's name and email and then running the exact search missed 1 to 3 of 40 lookups. Each missed lookup still returned nothing 4 seconds later.
    • With this change, all 8 runs of the Users suite passed, and the same four-client check found all 320 of 320 users.
  • Pint and PHPStan (package config, level max) pass on the changed files.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

InnoDB keeps full-text document IDs in the order transactions assign them, but its phrase search
assumes they are also in commit order. When two transactions update indexed rows and the one with
the lower document ID commits last, that row stops matching phrase searches until it is rewritten.
This is MySQL bug #110448 and reproduces on MySQL 8.4 and MariaDB 10.11 to 11.8.

Shared tables put every project's users in one table, so concurrent writes from other projects
regularly drop users from exact (quoted) searches. The Users E2E suite hits this in the shared
MariaDB lane, where testUpdateUserEmailSearch fails to find a user whose email was just updated.

Exact searches now require each indexed word through the full-text index and check the word order
with REGEXP against the stored value, so neither depends on the phrase position lists. Terms made
only of stopwords or words shorter than the minimum token size keep the phrase form, because
InnoDB cannot require words it does not index.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR adjusts full-text search query construction in the packages/query MySQL builder and expands its test coverage.

Files Change
packages/query/src/Builder/MySQL.php Updates the full-text MATCH predicate generation, including term escaping/binding and stopword filtering behavior.
packages/query/tests/Builder/MySQLTest.php Adds and updates unit expectations for the new full-text SQL output.
packages/query/tests/E2E/Builder/MySQLIntegrationTest.php and MariaDBIntegrationTest.php Refreshes integration coverage for full-text search on both engines.
packages/query/tests/E2E/IntegrationTestCase.php Minor shared test-case support for the new integration cases.

Review found no issues meeting the reporting threshold; the two candidates surfaced during scanning were rejected as not defensible.

Reviewed commit: 9d861fb

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Exact MySQL and MariaDB searches now use Boolean full-text matching to select indexed terms and a regular expression to verify phrase order, while phrases consisting only of unindexed words retain the previous full-text form. The query tests cover generated SQL and bindings, commit-order regressions, whole-word matching, case behavior, and negated searches on both engines.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
packages/query/src/Builder/MySQL.php Adds exact-search token filtering, Boolean full-text candidate matching, and ordered whole-word REGEXP verification.
packages/query/tests/Builder/MySQLTest.php Covers exact-search SQL and bindings, the unindexed-words fallback, and negated exact searches.
packages/query/tests/E2E/Builder/MariaDBIntegrationTest.php Adds MariaDB regressions for out-of-order commits, phrase matching, and negation.
packages/query/tests/E2E/Builder/MySQLIntegrationTest.php Adds MySQL regressions for out-of-order commits, phrase matching, and negation.
packages/query/tests/E2E/IntegrationTestCase.php Adds uncached MySQL and MariaDB connections for the concurrent-transaction integration tests.

Reviewed 9d861fb · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/mysql-exact-search-out-of-order-commits (after).

Metric Before After Change
🚀 Requests/sec 208.32 215 ⚪ +3.2%
⏱️ Latency P50 81.5 ms 80.2 ms ⚪ -1.6%
⏱️ Latency P95 195.14 ms 192.42 ms ⚪ -1.4%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 80.2 192.42 13,680 215 -2.72
Account 158.24 288.28 720 12.01 +0.9
TablesDB 76.88 144.52 7,440 120.36 -22.22
Storage 74.91 169.83 3,600 59.51 -1.62
Functions 119.25 232.13 1,920 32.42 +20.32

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 438.7
account.name.update 429.61
account.prefs.update 377.41
functions.delete 375.52
functions.variables.update 373.2

@abnegate
abnegate merged commit 6f27981 into main Oct 23, 2026
670 of 679 checks passed
@abnegate
abnegate deleted the fix/mysql-exact-search-out-of-order-commits branch October 23, 2026 04:03
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…h-out-of-order-commits

fix(query): match exact searches without InnoDB phrase positions

(cherry picked from commit 6f27981)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant