Repository navigation
feat: translate Length on Int32Range and Int64Range (#405) - #437
Merged
Merged
Conversation
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]>
This was referenced Oct 25, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #405
The first addition of 10.3.0 (#408).
mainfirst, then cherry-picked intorelease/10.x.Problem
LengthonInt32RangeandInt64Rangeran in memory only, so a query couldn't filter or order by it. #402 showed the old reason ("int4rangeoverflowsint4before a cast can widen it") doesn't hold: casting first works.Change
ValueRangesMemberTranslatortranslates the two members, matched by identity (HasSameMetadataDefinitionAs), so aLengthon another type is never taken for one of them (Sweep the EF translators for name-only dispatch and default operand classification #295):[lower, upper), soupper - loweris memory's count:[1,10]is[1,11)and measures 10.nulland rule (c).Int64Rangecount abovelong.MaxValuegives NULL, as memory does, never anint8overflow error.upper/lowerare declared as not propagating nullability, as forLowerBound/UpperBound.Measured on PostgreSQL 16 before writing it:
[-2147483648,2147483646]gives 4294967295;[0, long.MaxValue - 1]and[long.MinValue, -2]givelong.MaxValue, and one more gives NULL;ValueRangesMemberTranslatorPluginis a new public type, registered beside the other three plugins and named in the new claim's surfaces.Docs:
docs/efcore.md#what-runs-wheremovesLengthon the two types to "Runs as SQL" and adds a "Good to know" bullet (claim E73).Lengthon the other range types keeps its in-memory row with the temporal reason.docs/ranges.mdand the EF Core README follow.Inventory and claims:
translation-support.jsonclassifiesInt32Range.LengthandInt64Range.Lengthasserver, and claim E31 now saysLengthstays in memory on the other types.Changelogs:
### Addedunder[11.0.0-preview.1], in the root and EF Core changelogs.Client-only tests:
ClientSideOnlyTranslationTestsandClientSideOnlyProjectionTestsusedSeats.Lengthas their in-memory example. They now usePrice.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 seedinteger-length-empty-measures-null.patch) covers Epic: 10.3.0 #408's parity list against memory over the loaded rows:[x, int.MaxValue]can't be stored (docs/architecture.md#domain-width-and-the-boundary), so it isn't among them.Where(== null),Where(!= null)and a finalSelect. Each predicate (a comparison, a compound one, a coalesce, twoLengths compared) inWhere,Where(!)andSelect. These go throughCompositionRunner.Judge, which also checks that the filter and the projection agree (declared nullability).OrderByandOrderByDescending, 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).DateRangefamily only, so they don't reach these members. That's why this test exists.Int64Rangeguard (comparing withdecimal.MaxValue) fails it with22003: bigint out of range.TranslationSupportProbeTestspasses with both members classifiedserver.TranslatorDispatchSweepTestspasses, so both operands are mapped as their declared types.🤖 Generated with Claude Code