Skip to content

fix: RangeSet.Overlaps tests the last element that starts before the operand ends - #167

Merged
CaffeinatedCoder merged 3 commits into
mainfrom
fix/rangeset-overlaps-previous-element
Oct 18, 2026
Merged

CaffeinatedCoder merged 3 commits into
mainfrom
fix/rangeset-overlaps-previous-element

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #106
Closes #134

Problem

RangeSet<TRange, T>.Overlaps(IRange<T>), and Overlaps(RangeSet<TRange, T>) which probes through it, answered false on 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)) was false; PostgreSQL's && answers true. 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: new LastIndexStartingBefore, 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 two DifferentialFuzzParityTests candidates for RangeSet.Overlaps answers false on a continuous domain when the operand ends exactly where a later element begins #106 are removed; oracles.json and docs/testing.md describe the widened sweep; seed rangeset-overlaps-skips-previous-element.patch keeps 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: the Migration from v9.x section with Check RangeSet.Overlaps on a continuous domain.

Tests

  • RangeSetTests.Overlaps_ContinuousRangeEndingWhereALaterElementStarts_ReturnsTrue (and the _ReturnsFalse neighbour for the gap and the operand that stops where the first element starts). With the fix reverted it fails on set.Overlaps(DecimalRange.CreateFinite(3m, 5m)).
  • ContinuousMultirangeParityTests.EveryOrderedPairOfSubsets_PredicatesAgreeWithPostgres and EverySubsetAgainstEveryRange_PredicatesAgreeWithPostgres. With the fix reverted: 132 of 8512 nummultirange predicates disagree with PostgreSQL, all Overlaps rows such as Overlaps {[0,1), [2,2]} ⋄ (,2): model=False, PostgreSQL=True.
  • DifferentialFuzzParityTests.Ranges_AgreeWithPostgres passes 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.
  • The whole suite, including both integration projects against PostgreSQL in Docker, is green. dotnet run tools/oracles.cs seeds rangeset-overlaps proves the seed is caught by both named oracles.

🤖 Generated with Claude Code

…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]>
@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 breaking Changes an answer or a signature — needs a major found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) area/range-set RangeSet and multirange algebra package/core Affects CodoMetis.ValueRanges severity/high Wrong results, crash or failed writes in a realistic scenario labels Oct 18, 2026
…-previous-element

# Conflicts:
#	CHANGELOG.md
#	docs/migration.md
@CaffeinatedCoder
CaffeinatedCoder merged commit 14e431e into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/rangeset-overlaps-previous-element branch October 18, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/range-set RangeSet and multirange algebra breaking Changes an answer or a signature — needs a major bug Something isn't working found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) package/core Affects CodoMetis.ValueRanges severity/high Wrong results, crash or failed writes in a realistic scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

1 participant