Skip to content

fix(postgresql): render Substring and Trim as EF Core's queries do (#72) - #73

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/72-expression-rendering
Oct 20, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/72-expression-rendering

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #72

Problem

PostgreSQL uses an expression index only for a query whose expression matches it. Typed expression indexes rendered two methods differently from Npgsql's query translation of the same lambda:

Lambda Was EF Core sends Now
x.Code.Substring(1, 3) substr("Code", 2, 3) substring("Code", 2, 3) substring("Code", 2, 3)
x.Code.Trim() btrim("Code") btrim("Code", E' \t\n\r') btrim("Code", E' \t\n\r')

(Substring(start), TrimStart and TrimEnd likewise.) So the index applied and no query used it, and a unique index on Trim() accepted 'Y' next to 'Y\t', which Trim() treats as equal. Silent; the same class 5.4.0 fixed for JSON members.

Change

NpgsqlLinqIndexTranslator.VisitCall renders substring(…) and passes Npgsql's trim characters to btrim/ltrim/rtrim. The rest of the subset already matched EF Core and is pinned now.

Changelogs: a ### Changed answers row for #72. Upgrading: the first migrations add re-creates each typed expression index over these methods; a unique trimmed one may refuse rows that differ only in tabs or line breaks at the ends.

Merge note: #69 and #71 add the same ### Changed answers heading to the 5.5.0 changelog entries; whichever merges later needs a trivial changelog rebase, which I'll do as they land.

Tests

  • NpgsqlLinqExpressionIndexTests.Typed_expressions_match_ef_core_queries: seven lambdas (Substring both forms, Trim, TrimStart, TrimEnd, ToLower, Replace) render exactly what ToQueryString() selects.
  • PostgresIntegrationTests.Typed_trim_index_trims_what_trim_trims: on PostgreSQL 18, a unique Trim() index refuses E'Y\t' after 'Y'.
  • Three existing expectations updated (substr → substring, btrim with its characters).
  • verify-the-guard: with the translator change reverted, the two new tests and the three updated ones fail.
  • Full suite green locally, integration included.

🤖 Generated with Claude Code

PostgreSQL uses an expression index only for a query whose expression
matches it. Typed expression indexes rendered x.Code.Substring(1, 3) as
substr("Code", 2, 3) and x.Code.Trim() as btrim("Code"); Npgsql's query
translation sends substring(...) and btrim("Code", E' \t\n\r'). substr
and substring are different functions to the planner, so the indexes
applied and no query used them, and btrim(x) trims spaces only, so a
unique index on Trim() accepted 'Y' next to 'Y<tab>', which Trim()
treats as equal. The 5.4.0 JSON-member fix was the same class.

Substring now renders substring(...), and Trim/TrimStart/TrimEnd pass
the characters Npgsql passes. ToLower, ToUpper, Replace, ??, Length and
non-nullable concatenation already matched.

Verified by reverting the translator: the new agreement test against
ToQueryString() for seven lambdas and the live PostgreSQL 18 test (a
unique Trim() index refusing 'Y<tab>' after 'Y') fail, with the three
updated expectations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 5.5.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred severity/medium Inconsistency or failure in a narrow scenario package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL area/expression-indexes Expression, typed LINQ and JSON member indexes labels Oct 19, 2026
Keeps both new Postgres integration tests (Trim and nullable typed filter) and both changelog entries.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder merged commit 149e20a into main Oct 20, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/72-expression-rendering branch October 20, 2026 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/expression-indexes Expression, typed LINQ and JSON member indexes bug Something isn't working package/postgresql Affects EFCore.ComplexIndexes.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typed expression indexes render Substring and Trim differently from EF Core's queries, so no query uses them

1 participant