Skip to content

Add Daitch-Mokotoff fuzzy string matching support - #3922

Merged
roji merged 2 commits into
npgsql:mainfrom
adomorn:feat/daitch-mokotoff
Oct 15, 2026
Merged

roji merged 2 commits into
npgsql:mainfrom
adomorn:feat/daitch-mokotoff

Conversation

@adomorn

@adomorn adomorn commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Adds EF.Functions.FuzzyStringMatchDaitchMokotoff(text), translated to PostgreSQL's daitch_mokotoff(text) from fuzzystrmatch (PostgreSQL 16+). The string[] 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:

  • Release build: no warnings or errors; changed-file whitespace and git diff --check clean.
  • Docker PostgreSQL 16.15: 19 fuzzy translation/unit tests passed, including the null-comparison regression (failed before the fix).
  • Docker PostgreSQL 15: 13 fuzzy/unit tests passed; PostgreSQL 16-only tests were excluded by the CI trait filter.
  • Full Release suite on the final commit with TZ=UTC against 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_DateTime failures (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:

  • Ubuntu 26.04: PostgreSQL 13, 14, 15, 16, 17, and 18 in Release, with PostGIS required.
  • Ubuntu 26.04: PostgreSQL 18 in Debug, with PostGIS required.
  • Windows Server 2022: PostgreSQL 17 in Release.

Both publish-ci and release were 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 upstream 930bd291, without this PR. All actual GitHub CI jobs, including PostgreSQL 18 Debug, passed.

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
Copilot AI lite review requested due to automatic review settings September 26, 2026 23:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Address the nullable API contract and PostgreSQL version gating issues.

Review effort: Lite
Findings: 1 Medium severity

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.
Copilot AI review requested due to automatic review settings September 26, 2026 23:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No unresolved blocking issues were identified.

Review effort: Lite
Findings: None

Resolved since last review (1)

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@roji

roji commented Oct 15, 2026

Copy link
Copy Markdown
Member

Thanks for this!

@roji
roji enabled auto-merge (squash) October 15, 2026 12:55
@roji
roji merged commit 0b3aa1c into npgsql:main Oct 15, 2026
10 checks passed
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.

Fuzzy text search: support for Daitch-Mokotoff function

3 participants