Skip to content

fix: RangeAgg and RangeIntersectAgg skip a null input, as range_agg does - #368

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/361-aggregates-skip-null
Oct 24, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/361-aggregates-skip-null

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #361

Problem

docs/ranges.md#aggregate-a-sequence-of-ranges calls RangeAgg() and RangeIntersectAgg() "the in-memory versions of PostgreSQL's range_agg and range_intersect_agg". Those skip a NULL row; the in-memory versions didn't skip a null:

  • RangeIntersectAgg() over ranges folded a null in as Intersect(null). That is empty for every shape but (,), where it throws NullReferenceException; measured for the core and the NodaTime types alike. So [[1,10], [5,25], null] returned empty, silently, where range_intersect_agg returns [5,10].
  • RangeAgg() over ranges handed the null to RangeSet.From. That threw InvalidOperationException while sorting two or more inputs, or built a set holding null (Count 1, IsEmpty() false) from [null] alone.
  • Over sets, a null set made RangeAgg() throw NullReferenceException, and RangeIntersectAgg() too when it came after another set.

A consumer meets this when aggregating the loaded values of a nullable column or property in memory: a test double, a cache, or a grid that filters on the client.

Change

  • RangeAggregateExtensions and NodaTimeRangeAggregateExtensions: every per-type overload (nine in the core, four in the NodaTime package) and both set overloads drop nulls before folding, through a private WithoutNulls (OfType<TItem>()). A source of nothing but nulls is an empty source. So RangeIntersectAgg() answers null, as the server does, and RangeAgg() answers the empty set, which is the zero-rows difference E07 already documents. The XML docs say so.
  • The three aggregate seeds still apply. The filter sits where each overload calls its fold, and the set intersection's loop moved into IntersectSetsCore with its text unchanged, so their context lines are byte-identical.
  • docs/ranges.md: "A null range is skipped, as PostgreSQL skips a NULL row…", plus "A null set is skipped." for sets. Claim R48 quotes the first.
  • docs/migration.md: a bullet and a subsection under ## Migration from v10.x, with the measured before/after table.
  • Changelogs (root, core, NodaTime): a Ranges: line under ### Breaking in [11.0.0-preview.1], since a silent fix into a major's preview goes there. The 10.x port is 10.x: RangeIntersectAgg returns empty over a sequence holding a null range, where range_intersect_agg skips the NULL; RangeAgg throws (port of #361) #363, under ### Changed answers in 10.2.0.

Tests

Local, SDK 11.0.100-rc.2.26504.105, Docker (OrbStack).

  • AggregateNullInputs (new, in the core tests, linked into the NodaTime tests): a reflection sweep over every per-type RangeAgg/RangeIntersectAgg overload. It feeds each one [null], [(,), null], [null, (,)] and [(,), null, empty], and compares with the same input without the null. Held by RangeAggregateTests.EveryRangeOverload_SkipsANullRangeWhereverItStands (18 overloads at least) and NodaTimeRangeAggregateTests (8 at least).
  • RangeAggregateTests.TheSetOverloads_SkipANullSetWhereverItStands: the null at each of three positions, and a source of only nulls.
  • RangeAggregateParityTests.Aggregates_OverANullableColumn_SkipTheNullRows: a database of its own with a nullable Int32Range, RangeSet<Int32Range, int> and LocalDateRange column. It has one group per subset of a four-input universe, each with a NULL row added, and asserts the SQL holds range_agg(/range_intersect_agg(. The in-memory fold runs with the null at every position against the server's answer. The all-NULL group is checked as the documented zero-rows case.
  • Fix reverted (WithoutNulls returning its input): all four new tests fail. The integration test fails on 116 results, the reflection sweeps on every overload. That revert is kept as test/oracles/seeds/aggregates-fold-a-null-input.patch, and dotnet run tools/oracles.cs seeds fold-a-null reports it caught.
  • dotnet run tools/oracles.cs seeds intersect-agg: the three existing aggregate seeds still apply and are all caught.
  • Full suite on postgres:16-alpine: all eight projects pass (core 1399, NodaTime 239, EF 293, EF NodaTime 83, ASP.NET Core 92, conventions 407, integration 287, infinity-disabled 4).

🤖 Generated with Claude Code

…oes (#361)

PostgreSQL's range_agg and range_intersect_agg skip a NULL row, and the docs
call the two the in-memory versions of those aggregates. They didn't skip a null:
RangeIntersectAgg folded it in as Intersect(null), which is empty for every shape
but (,), where it throws, and RangeAgg handed it to RangeSet.From, which threw
while sorting or built a set holding null. Over sets, a null set threw.

Every per-type overload (nine in the core, four in the NodaTime package) and both
set overloads now drop nulls before folding. A source of nothing but nulls is an
empty source, which keeps the documented zero-rows difference for RangeAgg. The
filter sits where each overload calls its fold, so the three aggregate seeds
apply as they were; all three are still caught.

Tests: a reflection sweep over every per-type overload, linked into the NodaTime
tests; the set overloads; and RangeAggregateParityTests over a nullable range,
multirange and NodaTime range column with the NULL row at every position, which
fails on 116 results with the filter removed. That revert is kept as the seed
aggregates-fold-a-null-input.patch. Claim R48 quotes the new docs/ranges.md bullet.

Found-By: US
Defect-Class: ALG

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Oct 24, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred breaking Changes an answer or a signature — needs a major area/ranges Range unions, variants and the shape engines area/range-set RangeSet and multirange algebra package/nodatime Affects CodoMetis.ValueRanges.NodaTime package/core Affects CodoMetis.ValueRanges severity/low Cosmetic, message or documentation-level difference labels Oct 24, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit ab7458c into main Oct 24, 2026
11 of 13 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/361-aggregates-skip-null branch October 24, 2026 15:47
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 area/ranges Range unions, variants and the shape engines breaking Changes an answer or a signature — needs a major bug Something isn't working package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime severity/low Cosmetic, message or documentation-level difference silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RangeIntersectAgg returns empty over a sequence holding a null range, where range_intersect_agg skips the NULL; RangeAgg throws

1 participant