Skip to content

feat: translate Length on Int32Range and Int64Range (#405) - #437

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
feat/405-translate-integer-length
Oct 25, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
feat/405-translate-integer-length

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #405

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

Problem

Length on Int32Range and Int64Range ran in memory only, so a query couldn't filter or order by it. #402 showed the old reason ("int4range overflows int4 before a cast can widen it") doesn't hold: casting first works.

Change

  • ValueRangesMemberTranslator translates the two members, matched by identity (HasSameMetadataDefinitionAs), so a Length on another type is never taken for one of them (Sweep the EF translators for name-only dispatch and default operand classification #295):

    -- Int32Range
    CASE WHEN isempty(x) THEN 0 ELSE upper(x)::bigint - lower(x)::bigint END
    -- Int64Range
    CASE WHEN isempty(x) THEN 0
         WHEN upper(x)::numeric - lower(x)::numeric <= 9223372036854775807.0
         THEN CAST(upper(x)::numeric - lower(x)::numeric AS bigint) END
    • PostgreSQL stores an integer range as [lower, upper), so upper - lower is memory's count: [1,10] is [1,11) and measures 10.
    • An unbounded side and a NULL range give NULL, as memory's null and rule (c).
    • An Int64Range count above long.MaxValue gives NULL, as memory does, never an int8 overflow error.
    • upper/lower are declared as not propagating nullability, as for LowerBound/UpperBound.
  • Measured on PostgreSQL 16 before writing it:

    • every shape;
    • [-2147483648,2147483646] gives 4294967295;
    • [0, long.MaxValue - 1] and [long.MinValue, -2] give long.MaxValue, and one more gives NULL;
    • parameters and inlined constants too large to count raise no planning error, because the planner drops the arm whose condition folds to false.
  • ValueRangesMemberTranslatorPlugin is a new public type, registered beside the other three plugins and named in the new claim's surfaces.

  • Docs: docs/efcore.md#what-runs-where moves Length on the two types to "Runs as SQL" and adds a "Good to know" bullet (claim E73). Length on the other range types keeps its in-memory row with the temporal reason. docs/ranges.md and the EF Core README follow.

  • Inventory and claims: translation-support.json classifies Int32Range.Length and Int64Range.Length as server, and claim E31 now says Length 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.Length as their in-memory example. They now use Price.Length (DecimalRange).

Tests

  • IntegerLengthTranslationTests (no database): the SQL in a filter, a projection, a null test and an ordering.
  • IntegerLengthParityTests (integration, registered as an oracle with the seed integer-length-empty-measures-null.patch) covers Epic: 10.3.0 #408's parity list against memory over the loaded rows:
    • Shapes and extremes: the rows hold every shape and the widest storable ranges. [x, int.MaxValue] can't be stored (docs/architecture.md#domain-width-and-the-boundary), so it isn't among them.
    • Nullable operands: a nullable column, and a missing child through a navigation (LEFT JOIN).
    • Composition: each value in Where(== null), Where(!= null) and a final Select. Each predicate (a comparison, a compound one, a coalesce, two Lengths compared) in Where, Where(!) and Select. These go through CompositionRunner.Judge, which also checks that the filter and the projection agree (declared nullability).
    • Ordering: OrderBy and OrderByDescending, with NULL where PostgreSQL puts it (Docs: ordering by LowerBound() or UpperBound() puts open-ended and empty rows where PostgreSQL puts NULLs, also on a required column #359).
    • The composition sweeps run over the DateRange family only, so they don't reach these members. That's why this test exists.
  • Proofs:
    • The seed (the empty range measures NULL) fails the parity test with "server NULL, rule (c) 0" on the empty rows.
    • With the plugin unregistered, the 4 translation tests fail, and every parity call is "Untranslated, where it must run as SQL".
    • Dropping the Int64Range guard (comparing with decimal.MaxValue) fails it with 22003: bigint out of range.
  • Inventory and sweeps: TranslationSupportProbeTests passes with both members classified server. TranslatorDispatchSweepTests passes, so both operands are mapped as their declared types.
  • Full local runs on the .NET 11 RC2 SDK: core 1,399, NodaTime 239, conventions 417, EF translation 315, NodaTime EF translation 83, ASP.NET Core 92, integration on PostgreSQL 16 308. All passed.

🤖 Generated with Claude Code

Length on the two integer ranges ran in memory only, so a query could not
filter or order by it. It now runs as SQL with memory's answers:

  Int32Range: CASE WHEN isempty(x) THEN 0
              ELSE upper(x)::bigint - lower(x)::bigint END
  Int64Range: CASE WHEN isempty(x) THEN 0
              WHEN upper(x)::numeric - lower(x)::numeric <= 9223372036854775807
              THEN (upper(x)::numeric - lower(x)::numeric)::bigint END

PostgreSQL stores an integer range as [lower, upper), so upper - lower is
the count memory returns. An unbounded side and a NULL range give NULL, and
an Int64Range count above long.MaxValue gives NULL rather than an int8
overflow, as in memory. Measured on PostgreSQL 16 over columns, parameters
and inlined constants.

ValueRangesMemberTranslator matches the two members by identity and is
registered through a new public ValueRangesMemberTranslatorPlugin.
IntegerLengthParityTests holds every condition on #408's parity list against
memory over the loaded rows; translation-support.json classifies both server
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 67628e2 into main Oct 25, 2026
14 of 16 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/405-translate-integer-length branch October 25, 2026 15:03
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 Length on Int32Range and Int64Range

1 participant