Skip to content

feat: translate Clamp on Int32Range, Int64Range and DecimalRange (#406) - #440

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
feat/406-translate-clamp
Oct 25, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
feat/406-translate-clamp

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #406

The second addition of 10.3.0 (#408). main first, then cherry-picked into release/10.x.

Problem

Clamp(value) ran in memory only, so a query couldn't filter or order by a clamped value. #402 showed the old reason ("a CASE per shape") was overstated: GREATEST and LEAST skip NULL, so an unbounded side needs no branch.

Change

  • ValueRangesMethodCallTranslator translates RangeExtensions.Clamp, matched by its MethodInfo, on Int32Range, Int64Range and DecimalRange. On every other range type it declines, so those still run in memory:

    CASE WHEN x IS NULL OR v IS NULL OR isempty(x) THEN NULL
         ELSE LEAST(GREATEST(v, lower(x)), <upper>) END
    • <upper> is the bound UpperBound() already reads: upper(x) - 1 on int4range/int8range, whose stored upper bound is exclusive, and upper(x) on numrange. There memory returns an exclusive bound as it is.
    • GREATEST and LEAST skip the NULL that an unbounded side gives. They would also skip a NULL range or value, so both are tested first and the answer is NULL (rule (c)). EF Core drops those tests where the column is required or the value can't be NULL.
    • DecimalRange's NormalizeElement is the identity, so memory sees the value as given. numeric keeps the stored scale (2.500), which decimal equality ignores.
  • Measured on PostgreSQL 16 before writing it:

    • every shape;
    • (0,5] (stored [1,6)) clamps 0 to 1;
    • [-2147483648,2147483646] clamps int.MaxValue to 2147483646, and int8range's extremes clamp the same way;
    • numrange returns an exclusive bound;
    • a NULL range or value gives NULL.
  • Docs: docs/efcore.md#what-runs-where moves Clamp on the three types to "Runs as SQL" and adds a "Good to know" bullet (claim E74). The in-memory row for the other types now says only what is known: the same form would apply, but it has been checked against memory on these three only. docs: the in-memory reasons overstate what PostgreSQL lacks, and the Shift/Expand one is stale since #224 #402 gave no measured reason for the temporal or float types, so I didn't invent one. docs/ranges.md and the EF Core README follow.

  • Inventory and claims: translation-support.json keeps RangeExtensions.Clamp(* as client, with an except map making the three closed types server. Claim E31 now says Clamp stays in memory on the other types.

  • Changelogs: ### Added under [11.0.0-preview.1], in the root and EF Core changelogs.

  • Client-only tests: ClientSideOnlyTranslationTests and ClientSideOnlyProjectionTests used Seats.Clamp as their in-memory example. They now use Period.Clamp (DateRange).

Tests

  • ClampTranslationTests (no database): the SQL for a constant on Int32Range, a nullable value on Int64Range (its IS NULL test kept), and a column on DecimalRange.
  • ClampParityTests (integration, registered as an oracle with the seed clamp-reads-the-stored-exclusive-upper-bound.patch) covers Epic: 10.3.0 #408's parity list against memory over the loaded rows:
    • Shapes and extremes: every shape, exclusive numrange bounds, and the element types' minimum and maximum against the widest storable ranges.
    • Nullable operands: a NULL range column, a NULL value column read with .Value, and a missing outer-join child on either side.
    • Composition: each value in Where(== null), Where(!= null) and a final Select. Each predicate (equality with the value, comparisons, a compound one, a coalesce) in Where, Where(!) and Select, through CompositionRunner.Judge, which also checks that the filter and the projection agree.
    • Ordering: OrderBy and OrderByDescending, with NULL where PostgreSQL puts it.
    • The composition sweeps run over DateRange, whose Clamp stays in memory, so they don't reach these three.
  • Proofs:
    • The seed (reading the stored exclusive upper bound) fails it on every row whose value lies above a discrete range.
    • With the Clamp arm removed, the 3 translation tests fail, and every parity call is "Untranslated".
    • Keeping only the isempty guard fails it with a NULL range clamping to the value ("server 5" where rule (c) answers NULL).
  • Inventory and sweeps: TranslationSupportProbeTests passes with the three types server and the rest client. TranslatorDispatchSweepTests passes.
  • Full local runs on the .NET 11 RC2 SDK: core 1,399, NodaTime 239, conventions 417, EF translation 318, NodaTime EF translation 83, ASP.NET Core 92, integration on PostgreSQL 16 310. All passed.

🤖 Generated with Claude Code

Clamp(value) ran in memory only. On the three types whose bounds read as
memory's it now runs as SQL with memory's answers:

  CASE WHEN x IS NULL OR v IS NULL OR isempty(x) THEN NULL
       ELSE LEAST(GREATEST(v, lower(x)), <upper>) END

where <upper> is the bound UpperBound() reads: upper(x) - 1 on int4range and
int8range, whose stored upper bound is exclusive, and upper(x) on numrange,
where memory returns an exclusive bound as it is. GREATEST and LEAST skip the
NULL an unbounded side gives, so a NULL range or value is tested first and
answers NULL (rule (c)). Measured on PostgreSQL 16, the extremes included.

Clamp on the other range types stays in memory. ClampParityTests holds every
condition on #408's parity list; translation-support.json classifies the
three closed types server through its except map, and the docs move the row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.3.0 milestone Oct 25, 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 labels Oct 25, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 84dc121 into main Oct 25, 2026
14 of 16 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/406-translate-clamp branch October 25, 2026 15:45
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translate Clamp on Int32Range, Int64Range and DecimalRange

1 participant