Skip to content

fix: a projected CompareTo against a null range returns 1, as in memory - #272

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/compareto-projection-null
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/compareto-projection-null

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #253

Stacked on #268 (#256/#261), which rewrote the same rewriter: merge #268 first, and this PR's base then moves to main.

Problem

Select(r => r.Seats.CompareTo(none)) with none == null translated to EF Core's CASE WHEN r."Seats" = NULL … END, which yields SQL NULL. Reading that into an int threw InvalidOperationException: Nullable object must have a value, where memory answers 1 (null sorts first). The comparison rewrite only covered CompareTo compared with a number.

Change

  • The rewriter now also rewrites a bare CompareTo. In NullOperandCompareToRewriter, a CompareTo that is not compared with a number, such as one projected or ordered by, becomes the same split by nullness as a comparison. It is one conditional following Comparer<T>.Default: (null, null) gives 0, (null, x) gives −1, (x, null) gives 1, and otherwise the call itself.
  • Constants and non-nullable columns fold it away. A null operand gives SELECT 1. A non-null one gives EF Core's own CASE, with no IS NULL, as the new translation test pins. Comparisons with a number still take fix: CompareTo with a null range on either side compares as Comparer<T>.Default, for any number #268's predicate form, so they keep the btree operator.
  • Docs (docs/ranges.md#sort-ranges): one sentence on a projected CompareTo. Claim R47 holds it.
  • Seeds. New projected-compareto-null-reads-sql-null.patch. compareto-null-rewritten-only-against-zero.patch from fix: CompareTo with a null range on either side compares as Comparer<T>.Default, for any number #268 is regenerated, because this PR changes the file it patches.
  • No changelog line, since CompareTo is new in this same 10.0.0.

Tests

  • RangeOrderParityTests.ProjectedCompareTo_ReturnsTheNumberTheDefaultComparerReturns (live). It runs over a nullable range and a nullable multirange column holding NULL and four shapes, against a null and a non-null operand. It projects CompareTo row for row against Comparer<T>.Default.Compare, and orders by it.
  • Proved by reverting. With src/ back at fix: CompareTo with a null range on either side compares as Comparer<T>.Default, for any number #268, the test throws Nullable object must have a value, the issue's symptom. dotnet run tools/oracles.cs seeds compareto reports all three CompareTo seeds as CAUGHT.
  • QueryTranslationTests.ProjectedCompareTo_… checks SELECT 1 for a null operand, and EF Core's CASE with no null test for a non-null one.
  • The EF and conventions tests and the full integration suite (267) pass.

🤖 Generated with Claude Code

Select(r => r.Seats.CompareTo(none)) with a null none translated to EF Core's CASE WHEN a = b ...
END, which is NULL for a null operand, and reading that into an int threw "Nullable object must
have a value" where memory answers 1. The interceptor's CompareTo rewriter now also rewrites a
CompareTo that is not compared with a number (projected, ordered by) into the same split by
nullness, as one conditional following Comparer<T>.Default: (null, null) 0, (null, x) -1, (x, null) 1,
otherwise the call. Constants and non-nullable columns fold it away: a null operand is SELECT 1, a
non-null one EF Core's own CASE.

RangeOrderParityTests.ProjectedCompareTo_ReturnsTheNumberTheDefaultComparerReturns projects and
orders by CompareTo over a nullable range and multirange column against a null and a non-null
operand, row for row against Comparer<T>.Default; it throws with the rewrite removed (seed
projected-compareto-null-reads-sql-null). The #256 seed is refreshed for the changed file.

Closes #253

Found-By: CA
Defect-Class: SQL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working 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 19, 2026
Base automatically changed from fix/compareto-null-forms to main October 19, 2026 05:35
@CaffeinatedCoder
CaffeinatedCoder merged commit 78953bf into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/compareto-projection-null branch October 19, 2026 05:39
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 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A projected raw CompareTo against a null range throws; memory answers 1

1 participant