Skip to content

fix: RangeLowerBoundComparer.Compare runs in memory only in a query (#385) - #444

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/385-lower-bound-comparer-refused
Oct 25, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/385-lower-bound-comparer-refused

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Closes #385

Was stacked on #443, which merged; now based on main.

Problem

EF Core translates any Compare(x, y) as the natural comparison of its operands, whatever the comparer (dotnet/efcore#39402). For a range that is the range order, so RangeLowerBoundComparer<TRange, T>.Compare ran in SQL comparing whole ranges, not lower bounds. With p = [2026-03-01,2026-03-10], memory gives 0,0,0,1 for the issue's four rows and SQL gave 0,1,-1,1; Where(… Compare(r.Period, p) == 0) selected row 1 where memory selects rows 1 to 3. The same through IComparer<DateRange> and RangeSet.LowerBoundComparer. A NULL column threw in a projection.

Change

  • ValueRangesQueryExpressionInterceptor: a sixth rewriter, RangeComparerRewriter, turns comparer.Compare(x, y) over a range or set type (any IComparer<TRange> for a registered TRange) into RangeComparerCall.RunInMemory(comparer, x, y), which no translator knows. A filter or ordering then fails translation (EF's message shows RangeComparerCall.RunInMemory(…)), and a final Select runs the comparer on the loaded rows. This is the refusal planned on RangeLowerBoundComparer.Compare runs as SQL in the range order: EF Core rewrites any Compare(a, b) to the natural comparison #385.
  • Scope: once the query compiles, the comparer is a query parameter. Typed as IComparer<TRange>, as RangeSet.LowerBoundComparer is, its value is unknown, and one compiled query serves every comparer. So every comparer over a range or set is kept out of SQL, Comparer<T>.Default included. CompareTo stays the SQL range order. This is the one behaviour beyond the lower-bound comparer: Comparer<DateRange>.Default.Compare in a filter now fails translation where it ran (correctly, for non-null operands).
  • test/oracles: the TranslationSupportProbeTests candidate against RangeLowerBoundComparer.Compare runs as SQL in the range order: EF Core rewrites any Compare(a, b) to the natural comparison #385 is removed; translation-support.json keeps RangeLowerBoundComparer<TRange, T>.Compare(* as client, with the new reason; claim E31 covers the comparer and is held by the new test too; new seed lower-bound-comparer-runs-as-sql.patch; compareto-null-operand-compared-as-sql-null.patch refreshed against the new pipeline (it removes the CompareTo rewriter from the same lines).
  • Docs: a row in the in-memory table of What runs where, a pointer under Sort ranges the way a set does, a ### Breaking line in the root and EF Core changelogs under [11.0.0-preview.1], and the migration section Move RangeLowerBoundComparer out of queries.

Tests

  • New LowerBoundComparerQueryTests (integration): the issue's four rows plus a NULL row. A projection through RangeLowerBoundComparer.Instance, IComparer<DateRange>, RangeSet.LowerBoundComparer and Comparer<DateRange>.Default answers what memory answers (0,0,0,1, and -1 for NULL); a filter through each path and an ordering fail translation. Written to cherry-pick onto release/10.x (10.x: RangeLowerBoundComparer.Compare in a query runs in the range order (port of #385) #396).
  • ClientSideOnlyTranslationTests: four more rows (direct, RangeSet.LowerBoundComparer, IComparer<RangeSet>, an OrderBy).
  • With the rewriter removed from the pipeline (the seed): LowerBoundComparerQueryTests fails with server 0,1,-1,1, and the filter runs without an error; the four ClientSideOnlyTranslationTests rows fail; TranslationSupportProbeTests reports classified client, but runs as SQL now and Select answers 1, memory 0.
  • With the fix, on PostgreSQL 16: the full EFCore.PostgreSQL.IntegrationTests suite, the EF translation tests, the NodaTime EF tests and the conventions tests pass. dotnet run tools/oracles.cs seeds catches the new seed, the refreshed one and fix: value set Union and Remove over a NULL operand are NULL in a query (#360) #443's seed.

🤖 Generated with Claude Code

…385)

EF Core translates any Compare(x, y) as the natural order of its operands,
whatever the comparer (dotnet/efcore#39402). For a range that is the range
order, so RangeLowerBoundComparer ran in SQL comparing whole ranges rather
than lower bounds: with p = [2026-03-01,2026-03-10], [2026-03-01,) and
[2026-03-01,2026-03-03] compared as 1 and -1 instead of 0, and
Where(... Compare(r.Period, p) == 0) selected one row of three.

The query interceptor now turns any IComparer<TRange>.Compare over a range
or set type into RangeComparerCall.RunInMemory, which no translator knows:
a filter or an ordering fails translation, and a final projection runs the
comparer on the loaded rows. Once compiled the comparer is a query
parameter, so a comparer typed as IComparer<TRange> (as
RangeSet.LowerBoundComparer is) can't be told apart from another, and
Comparer<T>.Default goes the same way; CompareTo stays the SQL range order.

The TranslationSupportProbeTests candidate against #385 is removed, the
inventory entry stays client with the new reason, and docs/efcore.md lists
the comparer under what runs in memory. A new seed plants the defect, and
compareto-null-operand-compared-as-sql-null.patch is refreshed against the
new interceptor pipeline.

Found-By: OT
Defect-Class: SQL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Oct 25, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) area/ranges Range unions, variants and the shape engines package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/low Cosmetic, message or documentation-level difference stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 25, 2026
Base automatically changed from fix/360-value-set-null-operand to main October 25, 2026 16:50
@CaffeinatedCoder CaffeinatedCoder removed the stacked Base is another open PR; merge in order, CI does not run until it targets main label Oct 25, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 9522c1a into main Oct 25, 2026
14 of 15 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/385-lower-bound-comparer-refused branch October 25, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ranges Range unions, variants and the shape engines bug Something isn't working found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/low Cosmetic, message or documentation-level difference 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.

RangeLowerBoundComparer.Compare runs as SQL in the range order: EF Core rewrites any Compare(a, b) to the natural comparison

1 participant