Repository navigation
fix: CompareTo with a null range on either side compares as Comparer<T>.Default, for any number - #268
Merged
Merged
Conversation
…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]>
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 #256
Closes #261
Problem
CompareTocompared with 0 against a null operand. EF Core also turnsCompareTo(x) == 1,>= 1,> -1,!= -1and1 == CompareTo(x)into operators. With a null operand those returned 0 of 7 rows where memory returns 7 of 7, and anExecuteUpdatewithCompareTo(none) == 1updated none.CompareTo(null) > 0. The maintainer decided that null against null compares equal, asComparer<T>.Defaultdoes, and that a NULL column against a non-null operand compares lower (null first).Change
NullOperandCompareToRewriter(inValueRangesQueryExpressionInterceptor) now takes any comparison (==,!=,<,<=,>,>=) of a range or multirangea.CompareTo(b)with a number, the number on either side.It splits the comparison by nullness, deciding each case as
Comparer<T>.Defaultdoes:a(column)b(operand)0 op k-1 op k1 op kNo 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" > @somestays 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.CompareToanswers only −1, 0 or 1. EF Core's== 1→>rewrite relies on that. I first normalizedRangeOrderwithMath.Sign, but no element type answers another magnitude today: the new discovered test passed with and without it. So I leftRangeOrderunchanged 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
CompareTois 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== 1and!= -1with 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 nullableInt32Rangecolumn and a nullableRangeSetcolumn, 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 anExecuteUpdate, each compared row for row withComparer<T>.Default.Compare(column, operand).null == 1and the second onCompareTo(null) == 0(a NULL column) and== 1. That revert is kept as the seedcompareto-null-rewritten-only-against-zero.patch.dotnet run tools/oracles.cs seeds compareto-nullreports both CompareTo seeds asCAUGHT.RangeOrderSignContractTests(conventions, discovered) checks that every range type andRangeSetanswers −1, 0 or 1 for every shape pair. It fails withRangeOrderplanted to returnvalue * 2.🤖 Generated with Claude Code