Repository navigation
fix(postgresql): render Substring and Trim as EF Core's queries do (#72) - #73
Merged
Merged
Conversation
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]>
This was referenced Oct 19, 2026
Merged
Keeps both new Postgres integration tests (Trim and nullable typed filter) and both changelog entries. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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),TrimStartandTrimEndlikewise.) So the index applied and no query used it, and a unique index onTrim()accepted'Y'next to'Y\t', whichTrim()treats as equal. Silent; the same class 5.4.0 fixed for JSON members.Change
NpgsqlLinqIndexTranslator.VisitCallrenderssubstring(…)and passes Npgsql's trim characters tobtrim/ltrim/rtrim. The rest of the subset already matched EF Core and is pinned now.Changelogs: a
### Changed answersrow for #72. Upgrading: the firstmigrations addre-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 answersheading 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 (Substringboth forms,Trim,TrimStart,TrimEnd,ToLower,Replace) render exactly whatToQueryString()selects.PostgresIntegrationTests.Typed_trim_index_trims_what_trim_trims: on PostgreSQL 18, a uniqueTrim()index refusesE'Y\t'after'Y'.substr→substring,btrimwith its characters).🤖 Generated with Claude Code