Skip to content

fix: CompareTo with a null range on either side compares as Comparer<T>.Default, for any number - #268

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

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

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #256
Closes #261

Problem

Change

NullOperandCompareToRewriter (in ValueRangesQueryExpressionInterceptor) now takes any comparison (==, !=, <, <=, >, >=) of a range or multirange a.CompareTo(b) with a number, the number on either side.

  • It splits the comparison by nullness, deciding each case as Comparer<T>.Default does:

    a (column) b (operand) decided as
    null null 0 op k
    null not null -1 op k
    not null null 1 op k
    not null not null the comparison itself, which EF Core still turns into the btree operator
  • No change in SQL for two non-null operands. Constant cases are decided at rewrite time. The null tests are reference comparisons: folded here for a constant, and by EF Core for a parameter's value or a non-nullable column. So b."Period" > @some stays the bare operator, and fix: CompareTo against a null range in a query returns the rows memory returns #227's translation tests are unchanged and green.

  • CompareTo answers only −1, 0 or 1. EF Core's == 1 → > rewrite relies on that. I first normalized RangeOrder with Math.Sign, but no element type answers another magnitude today: the new discovered test passed with and without it. So I left RangeOrder unchanged and kept the test as the guard, shown to fail with a planted doubled answer.

  • Docs (docs/ranges.md#sort-ranges): the ±1 forms, the −1/0/1 answer, and a Good to know bullet on how a NULL column compares. Claims R44, R45 and R46 are new, numbered past R43, which feat: the non-generic IComparable on every range union and RangeSet #230 uses; E45's claim covers the NULL column.

  • No changelog line, since CompareTo is new in this same 10.0.0.

Tests

  • RangeOrderParityTests.CompareToInAQuery_ReturnsTheRowsMemoryReturns (live) adds the five predicates from CompareTo(null) compared with ±1 in a query still returns no rows #256 with a null operand, plus == -1, < 1, <= -1, and == 1 and != -1 with a non-null operand. The 0-forms were already there.
  • CompareToOverANullableColumn_AgreesWithTheDefaultComparer_RowForRow (live, new; its own database, so the shared schema is untouched). A nullable Int32Range column and a nullable RangeSet column, each holding NULL and six shapes, run against a null and a non-null operand. It covers every operator × k ∈ {−1, 0, 1} × both orientations (144 queries) and an ExecuteUpdate, each compared row for row with Comparer<T>.Default.Compare(column, operand).
  • Proved by reverting. With fix: CompareTo against a null range in a query returns the rows memory returns #227's rewriter restored, the first test fails on null == 1 and the second on CompareTo(null) == 0 (a NULL column) and == 1. That revert is kept as the seed compareto-null-rewritten-only-against-zero.patch. dotnet run tools/oracles.cs seeds compareto-null reports both CompareTo seeds as CAUGHT.
  • RangeOrderSignContractTests (conventions, discovered) checks that every range type and RangeSet answers −1, 0 or 1 for every shape pair. It fails with RangeOrder planted to return value * 2.
  • Unit, conventions, EF, EF NodaTime and the full integration suite (266) pass.

🤖 Generated with Claude Code

…T>.Default, for any number

#227 rewrote a range CompareTo compared with 0 against a null operand. EF Core also turns
CompareTo(x) == 1, >= 1, > -1, != -1 and 1 == CompareTo(x) into operators, and with a null operand
those still returned no rows, ExecuteUpdate included (#256). And #227's rewrite made a NULL column
match CompareTo(null) > 0 (#261).

The interceptor now splits any comparison of a range or multirange CompareTo with a number by
nullness, deciding each case as Comparer<T>.Default does, as the maintainer decided for #261: both
null, 0 op k; only the column, -1 op k; only the operand, 1 op k; neither, the comparison itself,
which EF Core still turns into the btree operator. The null tests are reference comparisons, folded
here for a constant and by EF Core for a parameter or a non-nullable column, so two non-null operands
still give the bare operator.

RangeOrderParityTests: the ±1 forms with a null operand in CompareToInAQuery, and a nullable range
and multirange column against a null and a non-null operand through every operator, k in -1, 0, 1,
on either side, in a predicate and ExecuteUpdate, row for row against Comparer<T>.Default. Seed
compareto-null-rewritten-only-against-zero restores #227's rewriter. RangeOrderSignContractTests
holds that CompareTo answers only -1, 0 or 1, which EF Core's == 1 rewrite assumes.

Closes #256
Closes #261

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 silent Answers wrongly without throwing — fixes go in the next major, never deferred area/ranges Range unions, variants and the shape engines package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/low Cosmetic, message or documentation-level difference labels Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit fdc1a15 into main Oct 19, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/compareto-null-forms branch October 19, 2026 05:35
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 silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

1 participant