Repository navigation
fix: RangeSet.Overlaps tests the last element that starts before the operand ends - #167
Merged
Merged
Conversation
…operand ends
On a continuous domain RangeSet.Overlaps(IRange<T>) chose its candidate element
by the operand's upper bound value alone: the last element whose lower bound is
at or below it. For {[1,4),[5,8)} against [3,5) that is [5,8), which starts
exactly where the operand stops and shares nothing with it, so the method
answered false while [1,4) before it overlapped. PostgreSQL's && answered
true, so a query and the same call in memory disagreed on those rows for every
continuous type. Overlaps(RangeSet) probes through the same method and
disagreed the same way. A discrete domain cannot reach the case: its closed
canonical form moves every exclusive end back onto a value.
The candidate is now the last element that starts *before* the operand ends —
a lower bound below the operand's upper bound, or at it when both are
inclusive. Every later element then starts where the operand has already
stopped, and if the candidate ends before the operand starts, so does every
earlier element, so one probe is still enough. Contains keeps the value-only
search, whose key is the operand's lower bound, where it is sound.
ContinuousMultirangeParityTests gains the predicate half it was already named
for by claim E02: every ordered pair of subsets through @>, &&, <<, >>, &<, &>
and -|-, and every subset against single ranges whose bound sits on a
fragment's with the other inclusivity. With the fix reverted it reports 132
Overlaps rows, and the new RangeSetTests case fails on the issue's example.
The seed rangeset-overlaps-skips-previous-element keeps that revert. The two
DifferentialFuzzParityTests candidates for #106 are removed.
Closes #106
Closes #134
Found-By: OT
Defect-Class: ALG
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
This was referenced Oct 18, 2026
Closed
…-previous-element # Conflicts: # CHANGELOG.md # docs/migration.md
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 #106
Closes #134
Problem
RangeSet<TRange, T>.Overlaps(IRange<T>), andOverlaps(RangeSet<TRange, T>)which probes through it, answeredfalseon a continuous domain when the operand's upper bound sat exactly on a later element's lower bound with one side exclusive, although an earlier element shared values with it.{[1,4),[5,8)}.Overlaps([3,5))wasfalse; PostgreSQL's&&answerstrue. Through EF Core the translation is right, so a query and the same call in memory disagreed on those rows, for every continuous type (DecimalRange,DateTimeRange,DateTimeOffsetRange,TimeRange,LocalDateTimeRange,InstantRange). A discrete domain cannot reach the case: its closed canonical form moves every exclusive end back onto a value.The cause was the candidate choice: the binary search picked the last element whose lower bound is at or below the operand's upper bound value, which for
[3,5)is[5,8), and tested only that element.Change
src/CodoMetis.ValueRanges/Internals/RangeSetHelpers.cs: newLastIndexStartingBefore, which keys the search on the operand's upper bound and its inclusivity. The candidate is the last element that starts before the operand ends: a lower bound below the upper bound, or at it when both are inclusive. Every later element starts where the operand has already stopped, and if the candidate ends before the operand starts, so does every earlier one, so one probe is still enough.src/CodoMetis.ValueRanges/RangeSet.cs:Overlaps(IRange<T>)uses it.Contains(IRange<T>)keeps the value-only search; its key is the operand's lower bound, where the value alone is sound.test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/ContinuousMultirangeParityTests.cs: the predicate half the sweep was already named for by claim E02 (Claim E02 names ContinuousMultirangeParityTests as a holder, but that sweep runs no predicate #134): every ordered pair of the 64 subsets through@>,&&,<<,>>,&<,&>and-|-, and every subset against 19 single ranges whose bounds sit on a fragment's with the other inclusivity (8,512 rows).test/oracles/: the twoDifferentialFuzzParityTestscandidates for RangeSet.Overlaps answers false on a continuous domain when the operand ends exactly where a later element begins #106 are removed;oracles.jsonanddocs/testing.mddescribe the widened sweep; seedrangeset-overlaps-skips-previous-element.patchkeeps the revert.CHANGELOG.md,src/CodoMetis.ValueRanges/CHANGELOG.md: the first 10.0.0 "Breaking" row and the "what can break you" line;docs/migration.md: theMigration from v9.xsection withCheck RangeSet.Overlaps on a continuous domain.Tests
RangeSetTests.Overlaps_ContinuousRangeEndingWhereALaterElementStarts_ReturnsTrue(and the_ReturnsFalseneighbour for the gap and the operand that stops where the first element starts). With the fix reverted it fails onset.Overlaps(DecimalRange.CreateFinite(3m, 5m)).ContinuousMultirangeParityTests.EveryOrderedPairOfSubsets_PredicatesAgreeWithPostgresandEverySubsetAgainstEveryRange_PredicatesAgreeWithPostgres. With the fix reverted:132 of 8512 nummultirange predicates disagree with PostgreSQL, allOverlapsrows such asOverlaps {[0,1), [2,2]} ⋄ (,2): model=False, PostgreSQL=True.DifferentialFuzzParityTests.Ranges_AgreeWithPostgrespasses with its two RangeSet.Overlaps answers false on a continuous domain when the operand ends exactly where a later element begins #106 candidates removed, so the fuzz rows that found it no longer disagree.dotnet run tools/oracles.cs seeds rangeset-overlapsproves the seed is caught by both named oracles.🤖 Generated with Claude Code