Skip to content

fix: CompareTo against a null range in a query returns the rows memory returns - #227

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/compareto-null-operand
Oct 18, 2026
Merged

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

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #220

Problem

EF Core rewrites r.Seats.CompareTo(probe) > 0 to r."Seats" > @probe. With probe == null, that is r."Seats" > NULL, which is SQL NULL, so the query returned no rows. In memory, null sorts before every range, so CompareTo(null) is positive and every row matches. The same held for >= and !=. It broke claim E45 ("the rows returned match … in memory") silently.

Change

I took the rewrite route the issue prefers, since it keeps the same-answer promise. NullOperandCompareToRewriter in ValueRangesQueryExpressionInterceptor, next to the other range rewrites:

  • The rewrite. For a range or multirange a.CompareTo(b) op 0, with 0 on either side:
    • > 0, >= 0 and != 0 become b IS NULL OR a op b.
    • < 0, <= 0 and == 0 become b IS NOT NULL AND a op b.
    • Negation then comes out right too.
  • The null test is a reference comparison. A range record's == is its value equality, which is not a null test.
  • No change in SQL for a non-null operand. A non-null constant operand is left alone. A null constant decides the comparison outright. For a parameter, EF Core folds the test away by its value, so a non-null operand still runs as b."Period" > @some. A null one gives no WHERE or WHERE FALSE, which the translation test pins.
  • Docs. docs/ranges.md#sort-ranges states the rule for a null operand in a query.
  • Claims. E45's claim now includes the null operand. The new claim R42 holds the sentence. R30, R31 and R40 are already used twice or taken on main, so it takes the next free number, and nothing is renumbered.
  • Changelog. CompareTo is new in this same 10.0.0, so per CONTRIBUTING ("leave out … fixes to something new in the same release") this adds no changelog line. The existing 10.0.0 EF line ("returns the rows the same comparison returns in memory") becomes true.

Tests

  • RangeOrderParityTests.CompareToInAQuery_ReturnsTheRowsMemoryReturns (live) now covers a null operand for <, <=, >, >=, == and !=. It also covers 0 on the left, a negated comparison, the multirange > and <=, and a non-null operand through the same rewrite. With the rewriter dropped from the chain, it fails on null > (the issue's reproduction). The revert is kept as test/oracles/seeds/compareto-null-operand-compared-as-sql-null.patch, and dotnet run tools/oracles.cs seeds compareto-null reports CAUGHT.
  • QueryTranslationTests.CompareTo_AgainstANullOperand_IsDecidedAsInMemory pins the SQL for a null parameter, a null constant and a non-null parameter.
  • The EF, EF NodaTime and conventions tests and the full integration suite (256) pass locally.

🤖 Generated with Claude Code

…y returns

EF Core rewrites a.CompareTo(b) op 0 to a op b, and a > NULL is SQL NULL, so a null operand
returned no rows for every comparison, while memory, where null sorts first, returns every row
for > 0, >= 0 and != 0. The query interceptor now adds the null test memory applies:
b IS NULL OR a op b for those three, b IS NOT NULL AND a op b for < 0, <= 0 and == 0, with 0 on
either side. EF Core folds the test away by the parameter's value, so a non-null operand still
runs as the bare operator, and a null constant decides the comparison outright.

RangeOrderParityTests.CompareToInAQuery_ReturnsTheRowsMemoryReturns has a null-operand case for
every comparison, both orientations, a negation and a multirange; it fails on "null >" without
the rewrite (seed compareto-null-operand-compared-as-sql-null).

Closes #220

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 18, 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 18, 2026
# Conflicts:
#	test/oracles/claims.json
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

Development

Successfully merging this pull request may close these issues.

CompareTo against a null range in an EF predicate returns no rows; memory returns every row

1 participant