Skip to content

feat: IComparable in PostgreSQL's range and multirange order - #183

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
feat/range-order
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
feat/range-order

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #39

Problem

The range types and RangeSet had no order. Comparer<Int32Range>.Default, List.Sort(), SortedSet and LINQ-to-objects OrderBy threw, and TypeKit could not generate comparison for a value object wrapping a range. PostgreSQL has a btree order on every range and multirange (<, <=, >, >=, range_cmp, multirange_cmp); the eight postgres-surface.json rows for those operators were open under this issue.

Change

  • Internals/RangeOrder.cs — PostgreSQL's range_cmp: empty first, then lower bound, then upper bound. An absent bound is the smallest lower and the largest upper bound. At the same value an inclusive lower bound sorts first and an inclusive upper bound sorts last (range_cmp_bounds). It reads the two bounds it compares and throws ShapePair.Unreachable from both discards. It is kept apart from RangeSetHelpers.CompareByLowerBound, which From sorts by.
  • IRangeFactory<TRange, T> extends IComparable<TRange>, and each of the eleven unions (seven core, four NodaTime) declares public int CompareTo(TRange? other). So a range type that forgets it does not compile. null sorts first, as with Comparer<T>.Default. The variants sort with the default comparer through contravariance (List<Int32Range.Finite>.Sort()).
  • RangeSet<TRange, T> implements IComparable<RangeSet<TRange, T>>, following multirange_cmp: element by element, and a shorter set sorts first.
  • CompareTo is 0 exactly when Equals. The two ranges must be the same variant with the same bounds and inclusivity, which is what record equality checks. That includes equal values spelled differently (two offsets of one instant, 1.0m and 1.00m). The discrete closed canonical form gives the same order as PostgreSQL's [).
  • EF Core needs no translator change. EF Core's QueryOptimizingExpressionVisitor already rewrites a.CompareTo(b) op 0 to a op b, so b.Period.CompareTo(x) < 0 runs as b."Period" < @x for ranges and multiranges. The new translation tests pin that.
  • Docs — docs/ranges.md#sort-ranges (new), the multirange order under Read and compare sets, and the generic-code note. docs/efcore.md gets the operator list and the What runs where row. docs/architecture.md lists the engine.
  • Catalogues — claims R26–R28, M12 and E45. RangeOrderParityTests is registered as an oracle. The eight </<=/>/>= surface rows now name *Range.CompareTo / RangeSet.CompareTo with their translation fragments.
  • Changelogs — ### Added under 10.0.0 in the root, core, NodaTime and EF Core changelogs.

Tests

  • RangeOrderParityTests (integration, new, registered oracle). For every discovered range type, it builds 16 shapes from RangeProbes.Bounds, with every inclusivity at each shared value, plus 20 multiranges. It asks PostgreSQL 17 for the sign of </=/> over every ordered pair. It also checks Equals against the server's =, and compares ORDER BY over the whole list with Order(). A second test runs CompareTo against 0 and OrderBy as executed EF queries over a range and a multirange column, and compares the rows with memory.
  • Seed range-order-upper-tie-inclusive-first.patch breaks upper-bound ties with the inclusive bound first, which is the lower-bound rule. dotnet run tools/oracles.cs seeds range-order reports CAUGHT: every continuous type and its multirange fail, and so does ORDER BY. The unit test AtTheSameValue_… fails under it too.
  • RangeCompareToTests (core) checks the worked order, antisymmetry, transitivity, CompareTo == 0 ⇔ Equals over range and multirange matrices, SortedSet, null, variant lists, and that LowerBoundComparer stays separate. NodaTimeRangeOrderTests covers the four NodaTime types.
  • QueryTranslationTests: the CompareTo operators on a range and a multirange, and ORDER BY.
  • Unit, NodaTime, EF translation, EF NodaTime, conventions (clone sweep, surface coverage and catalogue included), ASP.NET Core and the full integration suite all pass locally.

🤖 Generated with Claude Code

Every range union implements IComparable<TRange> (IRangeFactory now extends it) and RangeSet
implements IComparable<RangeSet<TRange, T>>, in range_cmp / multirange_cmp order: empty first,
then lower bound, then upper bound; an inclusive lower bound sorts first and an inclusive upper
bound last at the same value; a shorter set sorts first. CompareTo is 0 exactly when Equals,
and null sorts first. The order lives in Internals/RangeOrder, separate from the lower-bound
comparer RangeSet.From sorts by.

EF Core's own optimizer rewrites a.CompareTo(b) op 0 to a op b, so the predicate runs as the
btree operator with no translator change; the translation tests assert it.

RangeOrderParityTests asks PostgreSQL for <, = and > over every ordered pair of range and
multirange shapes of every discovered type, and for ORDER BY over the whole list, and runs the
comparisons as an EF query. Seeded with range-order-upper-tie-inclusive-first.patch.

Closes #39

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 enhancement New feature or request area/ranges Range unions, variants and the shape engines area/range-set RangeSet and multirange algebra package/nodatime Affects CodoMetis.ValueRanges.NodaTime package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/core Affects CodoMetis.ValueRanges labels Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit a99cce5 into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/range-order branch October 18, 2026 17:28
CaffeinatedCoder added a commit that referenced this pull request Oct 18, 2026
The closed-union copy constructor (#213), CompareTo in PostgreSQL's range
order (#183), Shift/Expand (#185) and Split(step) (#197). Values(step)
stays with the discrete types.

Float arithmetic overflows to an infinity rather than throwing, so a moved
bound or split boundary past the finite values throws OverflowException as
for the other types, and a NaN or infinite step throws ArgumentException.
Each split boundary is Start + n * step, rounded once.

The arithmetic and split oracles gain a double grid step and an edge step
of whole ulps at MaxValue, where half a grid unit is absorbed by rounding.
Claims are renumbered onto the free ids after main's (E54-E57, M17, S22),
with R40 and R41 for the new sentences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 area/ranges Range unions, variants and the shape engines enhancement New feature or request package/core Affects CodoMetis.ValueRanges package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/nodatime Affects CodoMetis.ValueRanges.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IComparable<TRange> in PostgreSQL's range order

1 participant