Skip to content

feat: translate Clamp on every range type (#486) - #490

Merged
CaffeinatedCoder merged 3 commits into
mainfrom
feat/486-translate-clamp-remaining
Oct 26, 2026
Merged

CaffeinatedCoder merged 3 commits into
mainfrom
feat/486-translate-clamp-remaining

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #486

Problem

Clamp ran as SQL only on Int32Range, Int64Range and DecimalRange (#406). On the other ten range types it ran in memory: a filter or an ordering by the clamped value failed translation, and a projection loaded the rows. The reason given was verification, not a known disagreement.

Change

  • ValueRangesMethodCallTranslator: Clamp translates on every range type, with the same LEAST(GREATEST(v, lower(x)), upper) form. The type list is gone, because the core plugin can't name the NodaTime types. In its place, EveryRangeType_HasAClampParityTest fails for any range type added later without a parity test.
  • One pairing stays in memory. A DateTime stored as timestamptz (Npgsql's default for a DateTime property) against a DateTimeRange's tsrange. GREATEST and LEAST resolve to timestamptz through the session's time zone. In a Europe/Berlin session, [08:00,18:00) clamped 17:30Z to 17:00Z, where memory keeps 17:30. Contains and the range factories already fail on the server for that pair (operator does not exist), so Clamp now declines it (MixesTimeZones): a filter fails translation, and a projection runs in memory.
  • Docs: what-runs-where (both tables, and the Good to know entries), ranges.md and the EF Core README. Changelogs: root, EF Core and EF Core NodaTime, under 11.0.0-preview.1.
  • Oracles: translation-support.json reclassifies Clamp as server. Claims E31 and E74 are updated, E83 is new (precision), ClampDomainParityTests is registered, there's a new seed clamp-compares-a-timestamptz-value-through-the-session-zone, and the existing clamp seed now names both oracles.

What was measured, not guessed:

  • Temporal extremes. Npgsql writes them as ±infinity. A range the plugin writes ending at the last date is stored infinity], and infinity - 1 is still infinity, which memory reads back as the extreme. So clamping at the extreme agrees.
  • YearMonthRange. It steps back one day, to the last day of its last month, which reads as that month.
  • Precision. A value you pass in comes back at the store's microsecond (TimeOnly.MaxValue → 23:59:59.999999), as any value sent to PostgreSQL does. This is documented, and pinned by Clamp_ReturnsAnOperandAtTheStoresMicrosecond.

Found along the way, filed separately:

Tests

  • ClampDomainParityTests (integration, new). For each of the ten types, every range its factories build from points that include the extremes, crossed with every inclusiveness and every point as the value. Run as a value column, a parameter, an inlined constant and NULL range and value columns, through CompositionRunner.Judge, compared exactly (sign of zero, offset) and in both orderings. Also: the mixed-zone pair in UTC, Europe/Berlin and America/Los_Angeles sessions, the precision rule, and the coverage check.
  • ClampTranslationTests (no database): the SQL for DateRange, TimeRange, DateTimeRange and DateTimeOffsetRange, and the mixed-zone refusal. ClientSideOnlyTranslationTests now uses the mixed-zone pair as its in-memory Clamp.
  • DeclaredNullabilityTests: GREATEST/LEAST are conditional expressions rather than pg_proc functions, so they're treated as non-strict instead of failing the lookup.
  • Proved by reverting:
    • with the translation switched off, all ten domain tests fail (untranslated);
    • with the discrete step-back removed, the three discrete types and ClampParityTests fail;
    • with the time-zone decline removed, the mixed-zone test fails;
    • dotnet run tools/oracles.cs seeds catches both clamp seeds.
  • Full suite green: unit, conventions, ASP.NET Core and EF no-database projects, and both integration projects on PostgreSQL 16 and 18. Locally the integration project needs MSTest.Parallelize.Workers=4, because the shared container runs out of connections at full parallelism (53300), on clean main too (5 failures there).

🤖 Generated with Claude Code

CaffeinatedCoder and others added 3 commits October 26, 2026 12:13
Clamp ran as SQL only on Int32Range, Int64Range and DecimalRange, the three
#406 measured. The same LEAST(GREATEST(...)) form, with the discrete step back,
holds on the other ten types: ClampDomainParityTests measures each against
memory over every range its factories build from points that include the
element type's extremes, as a value column, a parameter and an inlined
constant, exactly and in both orderings.

One pairing stays in memory: a DateTime stored as timestamptz (Npgsql's
default) against a DateTimeRange's tsrange. GREATEST and LEAST convert the
range's bounds through the session's time zone; in a Europe/Berlin session
[08:00,18:00) clamped 17:30Z to 17:00Z. Contains and the factories already
fail on that pair, so Clamp now declines it too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…omain tests run alone (#486)

DeclaredNullabilityTests now records the GREATEST/LEAST Clamp emits on its
DateRange column; they are not pg_proc functions and skip a NULL argument.
The domain tests each seed a database of their own, so they run outside the
parallel batch rather than adding a dozen pools to the server's limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.4.0 milestone Oct 26, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request area/ranges Range unions, variants and the shape engines package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime labels Oct 26, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit ac9bd90 into main Oct 26, 2026
14 of 16 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/486-translate-clamp-remaining branch October 26, 2026 13:21
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/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translate Clamp on the remaining range types, each once it agrees with memory as Int32Range, Int64Range and DecimalRange do

1 participant