Skip to content

feat: translate RangeSet.Count as a counted unnest of the multirange (#484) - #491

Merged
CaffeinatedCoder merged 3 commits into
mainfrom
feat/484-translate-rangeset-count
Oct 26, 2026
Merged

CaffeinatedCoder merged 3 commits into
mainfrom
feat/484-translate-rangeset-count

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #484

Problem

RangeSet.Count ran in memory only. bookings.Where(b => b.Blocked.Count > 2) ("how fragmented is this availability?") failed translation, and a projection loaded the multirange to count it. #94's spike found why: PostgreSQL has no multirange cardinality function, and a member translator can't build the subquery, because it has no alias manager.

Change

  • ValueRangesMemberTranslator: Count on a RangeSet<TRange, T> emits the placeholder valueranges_multirange_count_without_sql_guard(x), inside CASE WHEN x IS NULL THEN NULL. count(*) over unnest(NULL) is 0, not NULL (measured on 16 and 18). EF Core folds the test away for a required column.
  • MultirangeCount (new): builds (SELECT count(*)::int FROM unnest(x) AS vcN(r)) after EF Core's alias pass, run by ValueRangesParameterBasedSqlProcessorFactory next to ValueSetSqlGuard, as Translate value set Intersect, Except, Add and a canonical Union for the non-text families with composed SQL #478 builds its subqueries. The vcN aliases collide neither with EF Core's nor with the guard's vrN. It fails closed: without the decorator, PostgreSQL rejects the placeholder.
  • The domain boundary, documented. A stored multirange counts the same at the extremes. One the server computes from an operand at the element type's extreme can hold a range memory reads as empty: (,-2147483648) below int.MinValue, (infinity,) past the last date. It can also keep apart two date ranges that memory merges across the extreme. That's the existing boundary in docs/architecture.md#domain-width-and-the-boundary, which IsEmpty() over the same result shares; Count makes it visible. docs/efcore.md now says so in Good to know.
  • Docs: what-runs-where (a new row; the in-memory row keeps only the indexer), the Good to know entries, ranges.md. Changelogs: root, EF Core, EF Core NodaTime.
  • Oracles:
    • translation-support.json: RangeSet<TRange, T>.Count is now server.
    • postgres-surface.json: unnest(anymultirange) now names RangeSet.Count and its asserted SQL.
    • Claims: E84 (runs as SQL) and E85 (domain boundary) are new, and M06's quote follows the sentence added to ranges.md.
    • RangeSetCountParityTests is registered, with the new seed rangeset-count-of-a-null-multirange-is-zero.
    • sql-processor-reuses-its-first-output.patch is refreshed for the new Process body.

Out of scope, as #484 says: the indexer (memory throws past the end, SQL gives NULL), and Any/All/SelectMany, which stay on #94. The LINQ method Count() still fails translation loudly; the property is what an IReadOnlyList exposes.

Tests

  • RangeSetCountParityTests (integration, new). For each of the 13 range types: multiranges built from ranges that include the extremes, alone and in pairs and triples that overlap, touch or stand apart. Run through CompositionRunner.Judge: the column, a NULL column, Union, Except, Intersect, Complement(), Gaps(), two predicates and both orderings. Computed calls skip operands at the extremes, and the test reports how many (int4: 93 of 193). EveryRangeType_HasACountParityTest fails for a range type added without one.
  • RangeSetCountTranslationTests (no database): the subquery in a filter, one alias per subquery, a computed multirange, and the CASE over a nullable column. SqlGuardRegistrationTests.AReplacedProcessorFactory_SendsTheCountPlaceholder covers failing closed.
  • Proved by reverting: dropping the NULL wrapper fails 13 of 14 parity tests (tools/oracles.cs seeds rangeset-count-of-a-null-multirange-is-zero). The refreshed processor seed is still caught by RepeatedExecutionProbeTests.
  • Full suite green: unit, conventions, ASP.NET Core and EF no-database projects, and both integration projects on PostgreSQL 16 and 18 (MSTest.Parallelize.Workers=4 locally, see feat: translate Clamp on every range type (#486) #490).

This and #490 both add claims after E82 and lines to the same changelog sections, so whichever merges second needs a rebase.

🤖 Generated with Claude Code

CaffeinatedCoder and others added 2 commits October 26, 2026 12:31
…484)

RangeSet.Count ran in memory only: PostgreSQL has no multirange cardinality
function, and #94's spike found a member translator can't build the subquery
(no alias manager). #478 showed the way: the member translator emits a
placeholder, and MultirangeCount builds (SELECT count(*)::int FROM unnest(x)
AS vcN(r)) after EF Core's alias pass, from the same decorated processor as
ValueSetSqlGuard, so it fails closed too.

count(*) over unnest(NULL) is 0, so the placeholder sits inside
CASE WHEN x IS NULL THEN NULL, which EF Core folds away for a required column.

RangeSetCountParityTests measures every range type. A stored multirange counts
as memory counts it, the extremes included. One the server computes from an
operand at the element type's extreme can hold a range memory reads as empty,
(,-2147483648) or (infinity,), the documented domain boundary; those rows are
set aside and counted, and docs/efcore.md says so.

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/range-set RangeSet and multirange algebra package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime labels Oct 26, 2026
# Conflicts:
#	src/CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime/CHANGELOG.md
#	test/oracles/claims.json
@CaffeinatedCoder
CaffeinatedCoder merged commit 848fc0f into main Oct 26, 2026
14 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/484-translate-rangeset-count branch October 26, 2026 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/range-set RangeSet and multirange algebra 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 RangeSet.Count as a counted unnest of the multirange, built after the alias pass as #478 builds its subqueries (split from #94)

1 participant