Skip to content

feat: Shift and Expand on every range type - #185

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
feat/101-shift-expand
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
feat/101-shift-expand

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #101

Problem

There was no way to move a range or grow it on both sides. "The same slot one week later" and "the booking plus 15 minutes either side" meant taking the range apart and rebuilding it, once per shape.

Change

  • Shift(delta) and Expand(amount) on all eleven range types, typed by the element's own step: int/long/decimal for the numeric ranges, days (int) for DateRange, TimeSpan for DateTimeRange, DateTimeOffsetRange and TimeRange, Period for LocalDateRange and LocalDateTimeRange, Duration for InstantRange, months (int) for YearMonthRange. LocalDateTimeRange was missing from the issue's list. It gets the same members, so the NodaTime package matches the core one.

  • One rebuild for every type: Internals/RangeArithmetic.Move rebuilds through the type's own factory. A discrete range canonicalizes, bounds that cross collapse to Empty, and Empty and Infinite come back as they are. It is a unary operation, so the per-shape switch is over one operand, and its discard throws ShapePair.Unreachable.

  • Overflow throws OverflowException on every type, whatever the element type throws (the BCL date types throw ArgumentOutOfRangeException, which RangeArithmetic.Checked converts). TimeRange uses AddWithinDay, because TimeOnly.Add wraps at midnight.

  • Runs in memory only. I measured the issue's CASE rebuild on PostgreSQL 18. Its answers differ from memory in four ways that one CASE on the flags does not fix:

    • int4range(lower('empty') + 1, upper('empty') + 1) is (,).
    • '23:00'::time + interval '2 hours' is 01:00, where TimeRange throws.
    • Under SET TIME ZONE 'Europe/Berlin', '2026-03-28 12:00+00'::timestamptz + interval '1 day' is 2026-03-29 13:00+02. That is 23 hours, where DateTimeOffset.Add adds 24.
    • 'infinity'::date + 1 is infinity, where DateOnly.MaxValue overflows.

    Both methods are listed in docs/efcore.md#what-runs-where with these reasons. ClientSideOnlyTranslationTests holds that they fail translation in a Where, and ClientSideOnlyProjectionTests that they run in memory in a Select (claim E31).

  • Docs: a new section in docs/ranges.md ("Move or widen a range"), the efcore row, package READMEs, changelogs under 10.0.0. Claims R28–R31, and E31 extended.

  • Clone sweep: DecimalRange uses checked(...) like the integer siblings. It is a no-op for decimal, but it keeps the family alike. Eight waivers cover what really differs: the TimeRange midnight rule, and the day, month and Duration step types.

Tests

  • New oracle RangeArithmeticOracleTests (conventions project, registered as metamorphic). It runs over every discovered range type: every shape and inclusivity of the RangeSpecs grid, moved by whole grid steps against the moved specification. It also checks Shift(d).Shift(-d) == r, that Expand(a) contains r, that Expand(a) is the hull of Shift(±a) and Expand(-a) their overlap, and that a bound moved past the domain's extreme throws OverflowException while moving away or an unbounded side does not. Its first run disagreed only because the model grew the bounds of a spec that builds Empty. That was the model's mistake: an empty range has no bounds. The model is fixed, not the code.
  • Seeds, both caught (dotnet run tools/oracles.cs seeds shift): shift-drops-inclusiveness.patch (11 of 12 types fail) and timerange-shift-wraps-at-midnight.patch.
  • Worked examples: RangeShiftExpandTests (core) and NodaTimeShiftExpandTests. The NodaTime tests cover month clipping, a period with a time part throwing ArgumentException, and YearMonth overflow.
  • dotnet test: unit 1032, NodaTime 202, EF translation 253 + 81, conventions 336, ASP.NET Core 84, integration ClientSideOnlyProjectionTests, all green.

🤖 Generated with Claude Code

Shift(delta) moves a range and Expand(amount) grows it on both sides,
keeping each bound's inclusiveness. Each type takes its element's own
step: an int, long or decimal, days for DateRange, a TimeSpan for the
BCL timestamps and TimeRange, a Period for LocalDateRange and
LocalDateTimeRange, a Duration for InstantRange, months for
YearMonthRange. Empty and Infinite come back as they are, a negative
Expand shrinks to Empty past the width, and a bound moved past the
element type's extreme throws OverflowException (a TimeRange past
midnight too, where TimeOnly.Add wraps).

They run in memory only. Measured on PostgreSQL 18: rebuilding from
lower(x) + d turns 'empty' into (,), time wraps at midnight,
timestamptz + interval '1 day' adds 23 hours across a DST change in the
session time zone, and infinity absorbs the step.

RangeArithmeticOracleTests sweeps every discovered type against the
shape matrix moved on its grid, the round-trip and hull/overlap laws,
and the overflow rule; two seeds prove it.

Closes #101

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/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/core Affects CodoMetis.ValueRanges labels Oct 18, 2026
# Conflicts:
#	CHANGELOG.md
#	src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md
#	src/CodoMetis.ValueRanges.NodaTime/InstantRange.cs
#	src/CodoMetis.ValueRanges.NodaTime/LocalDateRange.cs
#	src/CodoMetis.ValueRanges.NodaTime/LocalDateTimeRange.cs
#	src/CodoMetis.ValueRanges.NodaTime/YearMonthRange.cs
#	src/CodoMetis.ValueRanges/CHANGELOG.md
#	src/CodoMetis.ValueRanges/DateRange.cs
#	src/CodoMetis.ValueRanges/DateTimeOffsetRange.cs
#	src/CodoMetis.ValueRanges/DateTimeRange.cs
#	src/CodoMetis.ValueRanges/DecimalRange.cs
#	src/CodoMetis.ValueRanges/Int32Range.cs
#	src/CodoMetis.ValueRanges/Int64Range.cs
#	src/CodoMetis.ValueRanges/README.md
#	src/CodoMetis.ValueRanges/TimeRange.cs
#	test/oracles/claims.json
@CaffeinatedCoder
CaffeinatedCoder merged commit aa53e62 into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/101-shift-expand branch October 18, 2026 17:54
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/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.

Shift and Expand on ranges (move by a delta, buffer both sides)

1 participant