Repository navigation
fix: CompareTo against a null range in a query returns the rows memory returns - #227
Merged
Merged
Conversation
…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]>
# Conflicts: # test/oracles/claims.json
This was referenced Oct 19, 2026
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 #220
Problem
EF Core rewrites
r.Seats.CompareTo(probe) > 0tor."Seats" > @probe. Withprobe == null, that isr."Seats" > NULL, which is SQLNULL, so the query returned no rows. In memory,nullsorts before every range, soCompareTo(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.
NullOperandCompareToRewriterinValueRangesQueryExpressionInterceptor, next to the other range rewrites:a.CompareTo(b) op 0, with 0 on either side:> 0,>= 0and!= 0becomeb IS NULL OR a op b.< 0,<= 0and== 0becomeb IS NOT NULL AND a op b.==is its value equality, which is not a null test.b."Period" > @some. A null one gives noWHEREorWHERE FALSE, which the translation test pins.docs/ranges.md#sort-rangesstates the rule for a null operand in a query.CompareTois 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 onnull >(the issue's reproduction). The revert is kept astest/oracles/seeds/compareto-null-operand-compared-as-sql-null.patch, anddotnet run tools/oracles.cs seeds compareto-nullreportsCAUGHT.QueryTranslationTests.CompareTo_AgainstANullOperand_IsDecidedAsInMemorypins the SQL for a null parameter, a null constant and a non-null parameter.🤖 Generated with Claude Code