Skip to content

feat: Split a range by a step or a calendar period, and Values(step) - #197

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
feat/100-split
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
feat/100-split

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #100

Stacked on #190 (#48, the calendar factories): the calendar split takes DateRange.Month, IsoWeek and the rest as its period, and shares their ISO 8601 week rule. Review and merge #190 first; this PR's diff against it is the one commit.

Problem

Values() walked a discrete range one element at a time, and nothing cut a range into sub-ranges. The billing months of a contract, the 30-minute slots of opening hours and the ISO weeks of a quarter were each a hand-written loop.

Change

Member On
Split(step) every range type: int, long, decimal, days (DateRange), TimeSpan (DateTimeRange, DateTimeOffsetRange, TimeRange), Period (LocalDateRange, LocalDateTimeRange), Duration (InstantRange), months (YearMonthRange)
Split(Func<T, TRange> period) DateRange, LocalDateRange, YearMonthRange
Values(step) the discrete types: Int32Range, Int64Range, DateRange, LocalDateRange (Period), YearMonthRange (months)

Design decisions

  • The calendar split takes a function, not a new step type. The issue sketched DateRange.Split(DateRangeStep step). A DateRangeStep (or a CalendarUnit enum) would be one more public type on the LTS line. It would also be closed: it could never express a fiscal year or a billing cycle. Split(Func<DateOnly, DateRange> period) takes the Calendar factories for DateRange (month, quarter, year, week) #48 factories as method groups, contract.Split(DateRange.Month) and quarter.Split(DateRange.IsoWeek), so the week rule is shared by construction. It also takes any function that returns the period holding a day. A function returning a range that is unbounded or doesn't hold the day throws ArgumentException, so a bad function can't loop forever.
  • A fixed step counts each boundary from the start, start + n·step, not from the previous boundary. A NodaTime period of months doesn't drift: from January 31 the pieces start February 28, March 31, April 30, where repeated addition gives February 28, March 28, April 28. NodaPeriods.Times scales a Period unit by unit.
  • The last piece keeps the range's end. On a continuous type every other piece is half-open, so a closed [0,10] split by 5 is [0,5) [5,10], not [0,5) [5,10) [10,10]. On a discrete type the cut is taken against the successor of the last value, so [1,10] split by 9 is [1,9] [10,10]. The oracle found that rule in the first draft (seed below).
  • Errors come at the call, as Values() does: an unbounded range throws NotSupportedException. A step of zero or less throws ArgumentOutOfRangeException, as do a Period with a negative unit and a step too small to move the start. Only the walk is deferred.
  • The engine keeps no state. A boundary is a function of (previous, n), so the same IEnumerable enumerates alike twice. My first draft held the counter in a closure and did not.
  • In memory only. generate_series returns rows, which is the documented reason for Values() (E31). docs/efcore.md#what-runs-where now lists Values(step) and Split on that row, and ClientSideOnlyTranslationTests and ClientSideOnlyProjectionTests hold both.

Findings while building it

  • decimal near its maximum: decimal.MaxValue - 3 + 0.5m rounds back to the start. A step below decimal's resolution at that size used to fail with a misleading "must move forward". It still throws ArgumentOutOfRangeException, now with a message that says why, and this is documented. A step of 1m there works.
  • Reflection on Values: two existing tests looked up GetMethod("Values"), which is ambiguous now that it has an overload. They now look the method up by name.

Tests

  • New oracle RangeSplitOracleTests (conventions, metamorphic, registered). It runs over every discovered range type, every bounded shape and inclusivity, and steps of 1, 2, 3 and 7 grid units. It checks that RangeSet.From(range.Split(step)) == RangeSet.From([range]), that consecutive pieces are adjacent and strictly left of each other, and that none is empty. Piece i must start i steps from the range's start, and the last must be at most one step long. Values(step) must be every k-th of Values(). Ranges ending at the domain maximum must split without overflow, and the rejections must come at the call. The calendar split must be one whole period per inner piece, clipped at the ends, and the same in DateRange and LocalDateRange for Month, Quarter, Year and IsoWeek.
  • Seeds, both caught (dotnet run tools/oracles.cs seeds split): split-stops-a-step-early.patch (the discrete cut rule) and split-pieces-share-their-boundary.patch (overlapping pieces).
  • RangeSplitTests and NodaTimeSplitTests hold the documented examples, months counted from the start, the decimal precision case, enumerating twice and the domain end.
  • dotnet test: unit 1039, NodaTime 205, EF translation 257 + 81, conventions 339, ASP.NET Core 84, integration ClientSideOnlyProjectionTests, all green.

🤖 Generated with Claude Code

Split(step) on every range type cuts a bounded range into consecutive
pieces of one step from its start, the last clipped to the range's end
and keeping its upper bound; on a continuous type the pieces are
half-open, so a closed range ends [5,10] rather than [5,10), [10,10].
DateRange, LocalDateRange and YearMonthRange also split at a calendar
period: range.Split(DateRange.Month), Split(DateRange.IsoWeek), or any
function returning the period that holds a value, which shares the ISO
week rule of #48. Values(step) on the discrete types skips ahead.

Boundaries are counted from the start (a NodaTime period of months does
not drift after a short month), past the domain's maximum is the end of
the last piece, and the engine keeps no state, so a result enumerates
alike twice. An unbounded range, a step of zero or less, or one too
small to move the start (0.5m near decimal.MaxValue) throws at the call.
In memory only: generate_series returns rows.

RangeSplitOracleTests holds every discovered type: the pieces' union is
the range, consecutive pieces are adjacent, each starts a whole number
of steps from the start, the last is at most one step, Values(step) is
every k-th of Values(), and the calendar split agrees across packages;
two seeds prove it.

Closes #100

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 package/nodatime Affects CodoMetis.ValueRanges.NodaTime package/core Affects CodoMetis.ValueRanges stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 18, 2026
Base automatically changed from feat/48-calendar-factories to main October 18, 2026 18:05
# Conflicts:
#	test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/ClientSideOnlyProjectionTests.cs
#	test/CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests/ClientSideOnlyTranslationTests.cs
#	test/oracles/claims.json
#	test/oracles/clone-waivers.json
@CaffeinatedCoder
CaffeinatedCoder merged commit 2017cc1 into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/100-split branch October 18, 2026 18:10
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/ranges Range unions, variants and the shape engines enhancement New feature or request package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime stacked Base is another open PR; merge in order, CI does not run until it targets main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split a range by a step or period, and Values(step) on the discrete types

1 participant