Repository navigation
feat: IComparable in PostgreSQL's range and multirange order - #183
Merged
Merged
Conversation
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]>
This was referenced Oct 18, 2026
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]>
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 #39
Problem
The range types and
RangeSethad no order.Comparer<Int32Range>.Default,List.Sort(),SortedSetand LINQ-to-objectsOrderBythrew, 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 eightpostgres-surface.jsonrows for those operators were open under this issue.Change
Internals/RangeOrder.cs— PostgreSQL'srange_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 throwsShapePair.Unreachablefrom both discards. It is kept apart fromRangeSetHelpers.CompareByLowerBound, whichFromsorts by.IRangeFactory<TRange, T>extendsIComparable<TRange>, and each of the eleven unions (seven core, four NodaTime) declarespublic int CompareTo(TRange? other). So a range type that forgets it does not compile.nullsorts first, as withComparer<T>.Default. The variants sort with the default comparer through contravariance (List<Int32Range.Finite>.Sort()).RangeSet<TRange, T>implementsIComparable<RangeSet<TRange, T>>, followingmultirange_cmp: element by element, and a shorter set sorts first.1.0mand1.00m). The discrete closed canonical form gives the same order as PostgreSQL's[).QueryOptimizingExpressionVisitoralready rewritesa.CompareTo(b) op 0toa op b, sob.Period.CompareTo(x) < 0runs asb."Period" < @xfor ranges and multiranges. The new translation tests pin that.docs/ranges.md#sort-ranges(new), the multirange order under Read and compare sets, and the generic-code note.docs/efcore.mdgets the operator list and the What runs where row.docs/architecture.mdlists the engine.RangeOrderParityTestsis registered as an oracle. The eight</<=/>/>=surface rows now name*Range.CompareTo/RangeSet.CompareTowith their translation fragments.### Addedunder 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 fromRangeProbes.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 checksEqualsagainst the server's=, and comparesORDER BYover the whole list withOrder(). A second test runsCompareToagainst 0 andOrderByas executed EF queries over a range and a multirange column, and compares the rows with memory.range-order-upper-tie-inclusive-first.patchbreaks upper-bound ties with the inclusive bound first, which is the lower-bound rule.dotnet run tools/oracles.cs seeds range-orderreportsCAUGHT: every continuous type and its multirange fail, and so doesORDER BY. The unit testAtTheSameValue_…fails under it too.RangeCompareToTests(core) checks the worked order, antisymmetry, transitivity,CompareTo == 0 ⇔ Equalsover range and multirange matrices,SortedSet, null, variant lists, and thatLowerBoundComparerstays separate.NodaTimeRangeOrderTestscovers the four NodaTime types.QueryTranslationTests: theCompareTooperators on a range and a multirange, andORDER BY.🤖 Generated with Claude Code