Skip to content

fix: keep RangeSet.Contains out of EF Core's rewrite of Contains on a read-only collection - #310

Merged
CaffeinatedCoder merged 2 commits into
release/10.xfrom
fix/288-rangeset-contains-ef-core-patches
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
release/10.xfrom
fix/288-rangeset-contains-ef-core-patches

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #288

#288 is fixed only on release/10.x (main carries the 11-line twin #287 through #292), so Closes will not act: close it by hand after the merge, with a link here.

Problem

The EF Core 10 patch that carries dotnet/efcore#37081 (dotnet/efcore#39114, milestone 10.0.15) makes QueryableMethodNormalizingExpressionVisitor rewrite every one-argument method named Contains on an IReadOnlyCollection<T> into Queryable.Contains over the collection's element type, before any translator runs. RangeSet<TRange, T> is an IReadOnlyList<TRange> whose Contains overloads take a T, an IRange<T> or a RangeSet, so on that patch every RangeSet.Contains in a query throws ArgumentException inside EF Core. EF Core 10.0.0 through 10.0.13 rewrite only ICollection<T>, which is why the floor this repository's CI pins never sees it (#297); Npgsql EF 10.0.1–10.0.3 require EF Core ≥ 10.0.4, so a consumer reaches 10.0.15 with a plain restore or a Dependabot bump. Reported upstream as dotnet/efcore#39329.

Change

  • src/CodoMetis.ValueRanges.EFCore.PostgreSQL/Query/RangeSetContains.cs (new, internal): the three RangeSet.Contains overloads as static methods Value, Range and Set, each calling the instance method it stands for, plus Equivalent(MethodInfo), which picks the static form by the operand's type.
  • ValueRangesQueryExpressionInterceptor.cs: a fifth rewrite, RangeSetContainsRewriter, turns set.Contains(x) into the matching static call. Its declaring type is not a collection, so EF Core's rewrite leaves it alone on every patch.
  • ValueRangesMethodCallTranslator.cs: translates the static form to @> exactly as the instance call (TranslateRangeSetCall now takes the method name, and the static form passes the set argument as the instance).
  • This is the fix PR build: retarget main to .NET 11 for 11.0.0-preview.1 #292 made on main (231f23a), ported by hand: build: retarget main to .NET 11 for 11.0.0-preview.1 #292 is one commit holding the whole .NET 11 retarget, so there is no commit to cherry-pick -x. Public API unchanged.
  • test/oracles/seeds/compareto-null-*.patch: the two seeds that diff the interceptor's chain and remarks are refreshed to the new context (as build: retarget main to .NET 11 for 11.0.0-preview.1 #292 did on main); OracleSeedTests requires every seed to apply.
  • Changelogs: one line under ## [10.1.0] in the root and the EF Core package changelog, naming the patch and the stopgap AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37081", true) with its cost (EF Core's own fix for IReadOnlySet parameters is lost, they inline their values again); the EF Core NodaTime changelog's intro line notes the satellite's sets get the fix through the base package.

No docs change: nothing a consumer writes changes.

Tests

No published EF Core 10 patch carries the change yet, so the proof is the query tree, not the SQL. test/CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests/InterceptedQuery.cs (linked into the NodaTime translation tests) runs the context's interceptor chain through the same logger call QueryCompilationContext makes and lists the calls EF Core's normalization rewrites: a one-argument instance Contains whose declaring type is, or implements, ICollection<> or IReadOnlyCollection<>.

  • RangeSetContainsShapeTests (5 + 1): for Contains(T), Contains(IRange<T>) and Contains(RangeSet) on the date set, a captured set as receiver, and the integer set — no such call survives, exactly one RangeSetContains.<overload> stands in its place, and the SQL is still @> with the whole operand asserted (b."BlockedDays" @> DATE '2024-06-15', @> b."Period", @> @other, @other @> b."Period", b."SeatBlocks" @> 5). StaticForms_AnswerAsTheInstanceMethods checks Equivalent picks Value/Range/Set by operand type, answers as the instance methods, and returns null for Overlaps and List<int>.Contains.
  • NodaRangeSetContainsShapeTests (8): the same for RangeSet<LocalDateRange, LocalDate>, RangeSet<InstantRange, Instant> (@> TIMESTAMPTZ '2024-06-15T12:00:00Z') and the month-aligned RangeSet<YearMonthRange, YearMonth> (@> DATE '2024-06-01').
  • Every existing RangeSet.Contains translation and integration test is unchanged.
Suite Floor (EF Core 10.0.0 / Npgsql EF 10.0.0) Float (EF Core 10.0.13 / Npgsql EF 10.0.3, src/ on the floor)
EFCore.PostgreSQL.Tests 295 / 295 295 / 295
EFCore.PostgreSQL.NodaTime.Tests 91 / 91 91 / 91
EFCore.PostgreSQL.IntegrationTests (PostgreSQL 16, Docker) 280 / 280 —
EFCore.PostgreSQL.InfinityDisabled.IntegrationTests 4 / 4 —
Conventions.Tests 403 / 403 —

The float used a scratch test/Directory.Build.targets (not committed) adding Microsoft.EntityFrameworkCore* and Npgsql.EntityFrameworkCore.PostgreSQL(.NodaTime) references to the test hosts (-p:EfFloat=10.0.13 -p:NpgsqlEfFloat=10.0.3; project.assets.json confirmed Microsoft.EntityFrameworkCore/10.0.13, Npgsql.EntityFrameworkCore.PostgreSQL/10.0.3).

Daily servicing build (#297): the dnceng feed https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet10/nuget/v3/index.json answers (HTTP 200) but its Microsoft.EntityFrameworkCore listing ends at 10.0.0-rtm.25513.102 — it holds no 10.0.x servicing build, and dotnet10-transport does not list the package at all. So the run against a build carrying dotnet/efcore#39114 could not be made from here; the first published 10.0.15 is the moment the newest-patch leg of #297 proves this on CI.

Revert proof (verify-the-guard): with the one line that calls RangeSetContainsRewriter stripped from the interceptor's chain and everything else kept, the tree tests fail and every @> SQL test passes — on the floor and on the float alike, which is exactly why the defect was invisible to the SQL tests:

### REVERTED floor: full projects
Failed!  - Failed:     5, Passed:   290, Skipped:     0, Total:   295 - CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests.dll
Failed!  - Failed:     8, Passed:    83, Skipped:     0, Total:    91 - CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime.Tests.dll
### REVERTED floor: RangeSet.Contains tests, per test
Failed Contains_CapturedSet_IsHandedOverAsAStaticCall
Failed Contains_OnACapturedSet_IsHandedOverAsAStaticCall
Failed Contains_OnAnIntegerSet_IsHandedOverAsAStaticCall
Failed Contains_RangeColumn_IsHandedOverAsAStaticCall
Failed Contains_Value_IsHandedOverAsAStaticCall
Passed RangeSet_Contains_RangeColumn
Passed RangeSet_Contains_Set
Passed RangeSet_Contains_Value
Passed StaticForms_AnswerAsTheInstanceMethods
Failed InstantSet_Contains_RangeColumn
Failed InstantSet_Contains_Value
Failed LocalDateSet_Contains_CapturedSet
Failed LocalDateSet_Contains_RangeColumn
Failed LocalDateSet_Contains_Value
Failed YearMonthSet_Contains_CapturedSet
Failed YearMonthSet_Contains_RangeColumn
Failed YearMonthSet_Contains_Value
### REVERTED float: full projects
Failed!  - Failed:     5, Passed:   290, Skipped:     0, Total:   295 - CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests.dll
Failed!  - Failed:     8, Passed:    83, Skipped:     0, Total:    91 - CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime.Tests.dll
### FIXED floor / FIXED float
Passed!  - Failed:     0, Passed:   295, Skipped:     0, Total:   295 - CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests.dll
Passed!  - Failed:     0, Passed:    91, Skipped:     0, Total:    91 - CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime.Tests.dll

The failure each tree test gives with the rewrite stripped:

Assertion failed. Expected collection to be empty.
An instance Contains on a collection type survived interception, which EF Core 10.0.15 rewrites into Queryable.Contains over the range type: RangeSet`2.Contains

🤖 Generated with Claude Code

… read-only collection

The EF Core 10 patch that carries dotnet/efcore#37081 (dotnet/efcore#39114, milestone
10.0.15) makes QueryableMethodNormalizingExpressionVisitor rewrite every one-argument
method named Contains on an IReadOnlyCollection<T> into Queryable.Contains over the
collection's element type, before any translator runs. RangeSet<TRange, T> is an
IReadOnlyList<TRange> whose Contains overloads take a T, an IRange<T> or a RangeSet, so
on that patch every RangeSet.Contains in a query threw ArgumentException inside EF Core.
EF Core 10.0.0 through 10.0.13 rewrite only ICollection<T>, which is why the floor this
repository's CI pins never saw it (#297); Npgsql EF 10.0.1-10.0.3 require EF Core >= 10.0.4,
so a consumer reaches the patch with a plain restore. Reported upstream as
dotnet/efcore#39329.

The query interceptor now turns the three instance overloads into static methods on
RangeSetContains, a declaring type that is not a collection and that EF Core's rewrite
therefore leaves alone, and ValueRangesMethodCallTranslator translates that form to @>
exactly as the instance call. Each static method calls the instance method it stands for,
so a client-evaluated call answers as the set does. Public API unchanged. This is the fix
PR #292 made on main for the 11 line (#287), ported by hand: #292 is one commit holding
the whole .NET 11 retarget, so there is no commit to cherry-pick.

No published EF Core 10 patch carries the change yet (the dnceng dotnet10 feed answers
but lists no 10.0.x servicing build), so the proof is the query tree rather than the SQL.
RangeSetContainsShapeTests and NodaRangeSetContainsShapeTests read the query as the
interceptor hands it over (InterceptedQuery, through the logger call
QueryCompilationContext makes) and assert that no instance Contains whose declaring type
implements IReadOnlyCollection<> survives, that exactly one RangeSetContains call stands
in its place, and that the SQL is still @>, for the three overloads on the BCL date and
integer sets and on the NodaTime LocalDate, Instant and YearMonth sets. With the rewrite
stripped from the interceptor the 5 and 8 tree tests fail and every @> SQL test still
passes, on EF Core 10.0.0 and with the test hosts floated to EF Core 10.0.13 / Npgsql EF
10.0.3, which is exactly why the SQL tests could not see the defect. With it, 295/295 and
91/91 translation tests pass on both, and 280 + 4 integration tests pass on PostgreSQL 16.

The two CompareTo seeds under test/oracles/seeds that diff the interceptor's chain and
remarks are refreshed to the new context, as #292 did on main. The changelog line names
the patch and the stopgap AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37081",
true), whose cost is EF Core's own fix for IReadOnlySet parameters.

Issue #288, fixed on release/10.x only (main has #287 through #292); closed by hand after
the merge, since a closing keyword acts only on main.

Found-By: OT
Defect-Class: SQL
Co-Authored-By: Claude Fable 5.1 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.1.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working area/range-set RangeSet and multirange algebra package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime severity/high Wrong results, crash or failed writes in a realistic scenario labels Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/range-set RangeSet and multirange algebra bug Something isn't working package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime severity/high Wrong results, crash or failed writes in a realistic scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant