Repository navigation
Add Daitch-Mokotoff fuzzy string matching support - #3922
Conversation
Translate EF.Functions.FuzzyStringMatchDaitchMokotoff using provider array mapping and preserve null checks for empty inputs. Add PostgreSQL 16 integration and translator tests. Fixes npgsql#3524
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address the nullable API contract and PostgreSQL version gating issues.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Adds EF Core/Npgsql support for PostgreSQL 16’s daitch_mokotoff fuzzy string-matching function.
Changes:
- Adds the public DbFunctions API and SQL translation.
- Configures array mapping and server-side evaluation.
- Adds unit and PostgreSQL 16 functional tests.
Moderate findings remain regarding public API nullability annotations and PostgreSQL version-aware translation.
| File | Description |
|---|---|
test/EFCore.PG.Tests/Query/NpgsqlFuzzyStringMatchMethodTranslatorTest.cs |
Tests translation, mapping, nullability, and client rejection. |
test/EFCore.PG.FunctionalTests/Query/FuzzyStringMatchQueryNpgsqlTest.cs |
Adds PostgreSQL 16 functional coverage. |
src/EFCore.PG/Query/Internal/NpgsqlEvaluatableExpressionFilter.cs |
Prevents client-side evaluation. |
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlMethodCallTranslatorProvider.cs |
Supplies type mapping support. |
src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlFuzzyStringMatchMethodTranslator.cs |
Translates the function and applies array mapping. |
src/EFCore.PG/Extensions/DbFunctionsExtensions/NpgsqlFuzzyStringMatchDbFunctionsExtensions.cs |
Defines the public database-function API. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Follow the provider's version-aware translator pattern, test PostgreSQL 15 rejection and PostgreSQL 16/18 support, and annotate the nullable result.
roji
left a comment
There was a problem hiding this comment.
Recommendation and problem
Proceed with the narrowly scoped provider addition; I found no blocking correctness issues. The built-in API really is missing, and exposing PostgreSQL 16+'s daitch_mokotoff(text) function from fuzzystrmatch is consistent with the existing fuzzy-string APIs. This is a provider feature, not a reason to redesign collection translation.
Expected approach
I would expect one EF.Functions extension, a case in the existing translator, the provider's normal text[] mapping, appropriate server-version/nullability handling, and focused translation/result tests. The existing StringToArray translator provides the relevant array-mapping precedent. A companion entry in the translations documentation should show supported use and the current composition limitation.
Assessment of this implementation
The PR follows that approach without introducing a new abstraction. The nullable array result and non-propagating nullability metadata are important: both '' and '123' return SQL NULL even though their inputs are non-null, so reducing a function-null check to an input-null check would be incorrect. The evaluatable-expression-filter addition is justified too: calls whose arguments are only parameters must remain on the server rather than execute the throwing CLR stub. The PostgreSQL 16 gate follows existing version-sensitive translator patterns. The additional code is predominantly focused coverage rather than plumbing.
A supported example is:
var codes = await context.Names
.Select(n => EF.Functions.FuzzyStringMatchDaitchMokotoff(n.Text))
.ToArrayAsync();Remaining gap
Non-blocking documentation follow-up: do not present this as complete LINQ-based phonetic matching support yet. This natural predicate still fails translation:
context.Names.Where(n =>
EF.Functions.FuzzyStringMatchDaitchMokotoff(n.Text)!.Contains("595000"))Any(), Intersect(...).Any(), and array .Length also fail. I reproduced the corresponding Contains and .Length failures with the existing EF.Functions.StringToArray API, so these are not evidence that the new mapping is broken. Keep the general composition work tracked separately by dotnet/efcore#35980; a short caveat in the public docs is sufficient for this PR's scope.
Manual validation
I built the exact reviewed revision with its pinned .NET 11 SDK and wrote a separate consumer console program referencing the PR's provider project. Against an isolated PostgreSQL 18.4 database, I executed column and captured-parameter calls, verified single/multiple codes, empty/numeric/NULL inputs, checked both ordinary and non-null-input null-result predicates, and executed an array-index predicate. Those returned the expected values and SQL; the existing parameter-only Soundex API also worked. Configuring PostgreSQL 15 rejected the new translation as expected (this was a translation check, not a PostgreSQL 15 server run). I also ran the four new translator unit cases successfully. The composition failures above were exercised through the consumer API, not inferred solely from source or unit tests.
Posted by copilot
| /// <returns>The soundex codes, or <see langword="null" /> if the input contains no encodable characters.</returns> | ||
| /// <remarks> | ||
| /// The method call is translated to <c>daitch_mokotoff(text)</c>. | ||
| /// Requires PostgreSQL 16 or later and the fuzzystrmatch extension. |
There was a problem hiding this comment.
Non-blocking: please add a short composition caveat here or in a companion translations-docs change. In a consumer program, Where(n => EF.Functions.FuzzyStringMatchDaitchMokotoff(n.Text)!.Contains("595000")) still throws a translation exception; Any(), Intersect(...).Any(), and .Length also fail. Projection and array indexing work. This is the existing function-result composition limitation tracked by dotnet/efcore#35980, not a reason to expand this PR into query-pipeline work, but it is important for callers choosing this API for phonetic matching.
Posted by copilot
|
Thanks for this! |

Adds
EF.Functions.FuzzyStringMatchDaitchMokotoff(text), translated to PostgreSQL'sdaitch_mokotoff(text)from fuzzystrmatch (PostgreSQL 16+). Thestring[]result mapping is resolved through the provider's type mapping source.Keeps parameter-only calls on the server and preserves function null checks: Daitch-Mokotoff can return NULL for a non-null input, including the empty string. Tests cover column and parameter translations, single/multiple codes, empty/null inputs, null comparisons, the array mapping, and rejection of client execution. Translation is enabled only when the configured PostgreSQL version is 16 or later, following the existing version-aware translator pattern. Unit tests cover PostgreSQL 15 rejection and PostgreSQL 16/18 support. New database tests are gated on PostgreSQL 16. The new API annotates its potentially null result as
string[]?.Fixes #3524. LINQ Any/Intersect composition on function results remains outside this change (dotnet/efcore#35980).
Validation:
git diff --checkclean.TZ=UTCagainst Docker PostgreSQL 16.15: 28,622 passed, 226 skipped, 0 failed (dotnet test -c Release --no-build --filter-not-trait category=failing). PostGIS is not installed in this container; its tests are excluded by the repository's conditional filter.The initial full run exposed two unrelated timezone-sensitive
DateTimeFunction_subtract_DateTimefailures (host in Istanbul, PostgreSQL in UTC). Aligning the test process to UTC resolved both; the complete suite was then rerun successfully.Additional CI verification on the exact PR head
b0c73a6da258234e57c93f63906fc347035939a4: the unchanged Build workflow passed in the fork, with all 8 build/test jobs successful:Both
publish-ciandreleasewere skipped by their existing repository/event conditions. The upstream run still needs maintainer approval.An additional local macOS ARM64 Debug run against Docker PostgreSQL 18.6/PostGIS 3.6.4 had 29,014 passing tests, 227 skips, and one unrelated SQL baseline failure in
TPCInheritanceQueryNpgsqlTest.Can_insert_update_delete(generated ID 3 versus expected 4). The identical failure reproduces on clean upstream930bd291, without this PR. All actual GitHub CI jobs, including PostgreSQL 18 Debug, passed.