Repository navigation
fix: RangeLowerBoundComparer.Compare runs in memory only in a query (#385) - #444
Merged
Merged
Conversation
…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]>
This was referenced Oct 25, 2026
Closed
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 #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, soRangeLowerBoundComparer<TRange, T>.Compareran in SQL comparing whole ranges, not lower bounds. Withp = [2026-03-01,2026-03-10], memory gives0,0,0,1for the issue's four rows and SQL gave0,1,-1,1;Where(… Compare(r.Period, p) == 0)selected row 1 where memory selects rows 1 to 3. The same throughIComparer<DateRange>andRangeSet.LowerBoundComparer. A NULL column threw in a projection.Change
ValueRangesQueryExpressionInterceptor: a sixth rewriter,RangeComparerRewriter, turnscomparer.Compare(x, y)over a range or set type (anyIComparer<TRange>for a registeredTRange) intoRangeComparerCall.RunInMemory(comparer, x, y), which no translator knows. A filter or ordering then fails translation (EF's message showsRangeComparerCall.RunInMemory(…)), and a finalSelectruns 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.IComparer<TRange>, asRangeSet.LowerBoundCompareris, 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>.Defaultincluded.CompareTostays the SQL range order. This is the one behaviour beyond the lower-bound comparer:Comparer<DateRange>.Default.Comparein a filter now fails translation where it ran (correctly, for non-null operands).test/oracles: theTranslationSupportProbeTestscandidate 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.jsonkeepsRangeLowerBoundComparer<TRange, T>.Compare(*asclient, with the new reason; claimE31covers the comparer and is held by the new test too; new seedlower-bound-comparer-runs-as-sql.patch;compareto-null-operand-compared-as-sql-null.patchrefreshed against the new pipeline (it removes the CompareTo rewriter from the same lines).### Breakingline in the root and EF Core changelogs under[11.0.0-preview.1], and the migration section MoveRangeLowerBoundComparerout of queries.Tests
LowerBoundComparerQueryTests(integration): the issue's four rows plus a NULL row. A projection throughRangeLowerBoundComparer.Instance,IComparer<DateRange>,RangeSet.LowerBoundComparerandComparer<DateRange>.Defaultanswers what memory answers (0,0,0,1, and-1for NULL); a filter through each path and an ordering fail translation. Written to cherry-pick ontorelease/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>, anOrderBy).LowerBoundComparerQueryTestsfails withserver 0,1,-1,1, and the filter runs without an error; the fourClientSideOnlyTranslationTestsrows fail;TranslationSupportProbeTestsreportsclassified client, but runs as SQL nowandSelect answers 1, memory 0.EFCore.PostgreSQL.IntegrationTestssuite, the EF translation tests, the NodaTime EF tests and the conventions tests pass.dotnet run tools/oracles.cs seedscatches 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