Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Some calls answer differently: [Changed answers in 10.2.0](https://github.com/Ca

### Changed answers

- Ranges: `RangeAgg()` and `RangeIntersectAgg()` skip a `null` range or set, as `range_agg` and `range_intersect_agg` skip a NULL row. With a `null` after another range, `RangeIntersectAgg()` returned `empty`, or threw after `(,)`, and `RangeAgg()` threw unless an input was `(,)` ([#363](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/363), [Migration](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1020)).
- EF Core: `LowerBound()` and `UpperBound()` in a query are `NULL` for an empty range or set and on an unbounded side, as in memory. So `== null` selects those rows, where it selected none on a required column, and `?? fallback` returns the fallback, where the row was missing ([#354](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/354), [Migration](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1020)).

## [10.1.0] — 2026-10-19
Expand Down
30 changes: 30 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,33 @@ float ranges and `YearMonthRange`. Code that runs in memory is unchanged.
so the extra test is now optional.
- **Check reports built on the old rows.** On a required column, a count of rows with no end date
was 0, and a filter on a date with a `??` fallback left out every open-ended row.

### Check `RangeAgg` and `RangeIntersectAgg` over `null` ranges

Check code that aggregates ranges in memory from a source that can hold `null`: the values of a
nullable property, or query results you load first and aggregate afterwards.

PostgreSQL's `range_agg` and `range_intersect_agg` skip a `NULL` row, so a `GroupBy` over a
nullable column aggregates the ranges it has. Before 10.2.0 the in-memory versions didn't. With `a`
as `[1,10]` and `b` as `[5,25]`:

| Sequence | Before 10.2.0 | Now |
|---|---|---|
| `[a, b, null].RangeIntersectAgg()` | `empty` | `[5,10]` |
| `[null, a, b].RangeIntersectAgg()` | `[5,10]` | `[5,10]` |
| `[(,), null].RangeIntersectAgg()` | throws `NullReferenceException` | `(,)` |
| `[a, null].RangeAgg()` | throws `InvalidOperationException` | `{[1,10]}` |
| `[null].RangeAgg()` | a set holding `null`: `Count` is 1, `IsEmpty()` is `false` | `{}` |

Over sets, a `null` set made `RangeAgg()` throw `NullReferenceException`, and `RangeIntersectAgg()`
too when it came after another set. Now it is skipped. This holds for every range type, the NodaTime
ones included. Queries are unchanged, because the database already skipped NULL rows.

- **Search for `RangeIntersectAgg()` over a sequence that can hold `null`.** A `!` in
`rows.Select(r => r.Period!).RangeIntersectAgg()` often marks the place: it silenced the nullable
warning for a property that can be `null`.
- **Remove a `Where(r => r.Period != null)` you added before aggregating, if you like.** The answer is
the same with or without it.
- **A group of nothing but `null` still differs from the database.** In memory `RangeAgg()` returns
the empty set, where the query returns `null`, as for zero rows
([Read bounds, merge ranges and aggregate](efcore.md#read-bounds-merge-ranges-and-aggregate)).
6 changes: 4 additions & 2 deletions docs/ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ new[] { Int32Range.CreateFinite(1, 10), Int32Range.CreateFinite(5, 15) }
- **`RangeAgg()`** returns a normalized `RangeSet` with every value of every range.
- **`RangeIntersectAgg()`** returns the intersection of all the ranges, or `null` when the
sequence is empty.
- **A `null` range is skipped**, as PostgreSQL skips a `NULL` row. So a group over a nullable
column aggregates the ranges it has, and a sequence of nothing but `null` counts as empty.

Both also take a sequence of sets, as PostgreSQL's aggregates also take multiranges:

Expand All @@ -695,8 +697,8 @@ blocked.RangeIntersectAgg(); // { [3, 5], [20, 22] } — the values every set h
```

Over sets, `RangeIntersectAgg()` returns a `RangeSet`, again `null` for an empty sequence. An empty
set among the inputs makes it the empty set. These two are generic, so they work for every range
type, the NodaTime ones included.
set among the inputs makes it the empty set. A `null` set is skipped. These two are generic, so they
work for every range type, the NodaTime ones included.

In an EF Core query, all four run as the SQL aggregates inside a `GroupBy` projection. See
[Entity Framework Core](efcore.md).
Expand Down
4 changes: 3 additions & 1 deletion src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ covers all four packages, which share one version number and release together.

## [10.2.0]

No change in this package.
### Changed answers

- Ranges: `RangeAgg()` and `RangeIntersectAgg()` over `LocalDateRange`, `LocalDateTimeRange`, `InstantRange` and `YearMonthRange` skip a `null` range, as `range_agg` and `range_intersect_agg` skip a NULL row. With a `null` after another range, `RangeIntersectAgg()` returned `empty`, or threw after `(,)`, and `RangeAgg()` threw unless an input was `(,)` ([#363](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/363), [Migration](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1020)).

## [10.1.0] — 2026-10-19

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ public static class NodaTimeRangeAggregateExtensions
/// An empty source yields <see cref="RangeSet{TRange,T}.Empty"/>. Note that PostgreSQL
/// aggregates return <c>NULL</c> over zero rows, so the translated SQL can materialize
/// <see langword="null"/> where the in-memory operation returns the empty set.
/// A <see langword="null"/> range is skipped, as PostgreSQL skips a <c>NULL</c> input, so a source
/// of nothing but nulls is an empty source.
/// </remarks>
/// <param name="source">The ranges to aggregate.</param>
/// <returns>A normalized set covering every value of every input range.</returns>
public static RangeSet<LocalDateRange, LocalDate> RangeAgg(this IEnumerable<LocalDateRange> source)
=> RangeSet<LocalDateRange, LocalDate>.From(source);
=> RangeSet<LocalDateRange, LocalDate>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{LocalDateRange})"/>
public static RangeSet<LocalDateTimeRange, LocalDateTime> RangeAgg(this IEnumerable<LocalDateTimeRange> source)
=> RangeSet<LocalDateTimeRange, LocalDateTime>.From(source);
=> RangeSet<LocalDateTimeRange, LocalDateTime>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{LocalDateRange})"/>
public static RangeSet<InstantRange, Instant> RangeAgg(this IEnumerable<InstantRange> source)
=> RangeSet<InstantRange, Instant>.From(source);
=> RangeSet<InstantRange, Instant>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{LocalDateRange})"/>
public static RangeSet<YearMonthRange, YearMonth> RangeAgg(this IEnumerable<YearMonthRange> source)
=> RangeSet<YearMonthRange, YearMonth>.From(source);
=> RangeSet<YearMonthRange, YearMonth>.From(WithoutNulls(source));

/// <summary>
/// Intersects all ranges of the sequence into a single range — equivalent to the
Expand All @@ -48,25 +50,27 @@ public static RangeSet<YearMonthRange, YearMonth> RangeAgg(this IEnumerable<Year
/// An empty source yields <see langword="null"/>, matching the <c>NULL</c> PostgreSQL
/// returns for aggregates over zero rows. Disjoint inputs collapse to
/// <see cref="IRangeFactory{TRange,T}.Empty"/>.
/// A <see langword="null"/> range is skipped, as PostgreSQL skips a <c>NULL</c> input, so a source
/// of nothing but nulls is an empty source.
/// </remarks>
/// <param name="source">The ranges to intersect.</param>
/// <returns>
/// The intersection of all input ranges, or <see langword="null"/> for an empty source.
/// </returns>
public static LocalDateRange? RangeIntersectAgg(this IEnumerable<LocalDateRange> source)
=> IntersectAggCore<LocalDateRange, LocalDate>(source);
=> IntersectAggCore<LocalDateRange, LocalDate>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{LocalDateRange})"/>
public static LocalDateTimeRange? RangeIntersectAgg(this IEnumerable<LocalDateTimeRange> source)
=> IntersectAggCore<LocalDateTimeRange, LocalDateTime>(source);
=> IntersectAggCore<LocalDateTimeRange, LocalDateTime>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{LocalDateRange})"/>
public static InstantRange? RangeIntersectAgg(this IEnumerable<InstantRange> source)
=> IntersectAggCore<InstantRange, Instant>(source);
=> IntersectAggCore<InstantRange, Instant>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{LocalDateRange})"/>
public static YearMonthRange? RangeIntersectAgg(this IEnumerable<YearMonthRange> source)
=> IntersectAggCore<YearMonthRange, YearMonth>(source);
=> IntersectAggCore<YearMonthRange, YearMonth>(WithoutNulls(source));

private static TRange? IntersectAggCore<TRange, T>(IEnumerable<TRange> source)
where TRange : class, IRangeFactory<TRange, T>, IRange<T>
Expand All @@ -80,4 +84,12 @@ public static RangeSet<YearMonthRange, YearMonth> RangeAgg(this IEnumerable<Year

return accumulator;
}

/// <summary>
/// The source without its <see langword="null"/> elements. PostgreSQL's aggregates skip a
/// <c>NULL</c> input, so a group over a nullable column aggregates the values it has.
/// </summary>
private static IEnumerable<TItem> WithoutNulls<TItem>(IEnumerable<TItem?> source)
where TItem : class
=> source.OfType<TItem>();
}
4 changes: 3 additions & 1 deletion src/CodoMetis.ValueRanges/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ covers all four packages, which share one version number and release together.

## [10.2.0]

No change in this package.
### Changed answers

- Ranges: `RangeAgg()` and `RangeIntersectAgg()` skip a `null` range or set, as `range_agg` and `range_intersect_agg` skip a NULL row. With a `null` after another range, `RangeIntersectAgg()` returned `empty`, or threw after `(,)`, and `RangeAgg()` threw unless an input was `(,)` ([#363](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/issues/363), [Migration](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/migration.md#changed-answers-in-1020)).

## [10.1.0] — 2026-10-19

Expand Down
59 changes: 39 additions & 20 deletions src/CodoMetis.ValueRanges/RangeAggregateExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,45 @@ public static class RangeAggregateExtensions
/// An empty source yields <see cref="RangeSet{TRange,T}.Empty"/>. Note that PostgreSQL
/// aggregates return <c>NULL</c> over zero rows, so the translated SQL can materialize
/// <see langword="null"/> where the in-memory operation returns the empty set.
/// A <see langword="null"/> range is skipped, as PostgreSQL skips a <c>NULL</c> input, so a source
/// of nothing but nulls is an empty source.
/// </remarks>
/// <param name="source">The ranges to aggregate.</param>
/// <returns>A normalized set covering every value of every input range.</returns>
public static RangeSet<Int32Range, int> RangeAgg(this IEnumerable<Int32Range> source)
=> RangeSet<Int32Range, int>.From(source);
=> RangeSet<Int32Range, int>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<Int64Range, long> RangeAgg(this IEnumerable<Int64Range> source)
=> RangeSet<Int64Range, long>.From(source);
=> RangeSet<Int64Range, long>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<DecimalRange, decimal> RangeAgg(this IEnumerable<DecimalRange> source)
=> RangeSet<DecimalRange, decimal>.From(source);
=> RangeSet<DecimalRange, decimal>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<Float64Range, double> RangeAgg(this IEnumerable<Float64Range> source)
=> RangeSet<Float64Range, double>.From(source);
=> RangeSet<Float64Range, double>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<Float32Range, float> RangeAgg(this IEnumerable<Float32Range> source)
=> RangeSet<Float32Range, float>.From(source);
=> RangeSet<Float32Range, float>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<DateRange, DateOnly> RangeAgg(this IEnumerable<DateRange> source)
=> RangeSet<DateRange, DateOnly>.From(source);
=> RangeSet<DateRange, DateOnly>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<DateTimeRange, DateTime> RangeAgg(this IEnumerable<DateTimeRange> source)
=> RangeSet<DateTimeRange, DateTime>.From(source);
=> RangeSet<DateTimeRange, DateTime>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<DateTimeOffsetRange, DateTimeOffset> RangeAgg(this IEnumerable<DateTimeOffsetRange> source)
=> RangeSet<DateTimeOffsetRange, DateTimeOffset>.From(source);
=> RangeSet<DateTimeOffsetRange, DateTimeOffset>.From(WithoutNulls(source));

/// <inheritdoc cref="RangeAgg(IEnumerable{Int32Range})"/>
public static RangeSet<TimeRange, TimeOnly> RangeAgg(this IEnumerable<TimeRange> source)
=> RangeSet<TimeRange, TimeOnly>.From(source);
=> RangeSet<TimeRange, TimeOnly>.From(WithoutNulls(source));

/// <summary>
/// Intersects all ranges of the sequence into a single range — equivalent to the
Expand All @@ -70,52 +72,55 @@ public static RangeSet<TimeRange, TimeOnly> RangeAgg(this IEnumerable<TimeRange>
/// An empty source yields <see langword="null"/>, matching the <c>NULL</c> PostgreSQL
/// returns for aggregates over zero rows. Disjoint inputs collapse to
/// <see cref="IRangeFactory{TRange,T}.Empty"/>.
/// A <see langword="null"/> range is skipped, as PostgreSQL skips a <c>NULL</c> input, so a source
/// of nothing but nulls is an empty source.
/// </remarks>
/// <param name="source">The ranges to intersect.</param>
/// <returns>
/// The intersection of all input ranges, or <see langword="null"/> for an empty source.
/// </returns>
public static Int32Range? RangeIntersectAgg(this IEnumerable<Int32Range> source)
=> IntersectAggCore<Int32Range, int>(source);
=> IntersectAggCore<Int32Range, int>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static Int64Range? RangeIntersectAgg(this IEnumerable<Int64Range> source)
=> IntersectAggCore<Int64Range, long>(source);
=> IntersectAggCore<Int64Range, long>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static DecimalRange? RangeIntersectAgg(this IEnumerable<DecimalRange> source)
=> IntersectAggCore<DecimalRange, decimal>(source);
=> IntersectAggCore<DecimalRange, decimal>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static Float64Range? RangeIntersectAgg(this IEnumerable<Float64Range> source)
=> IntersectAggCore<Float64Range, double>(source);
=> IntersectAggCore<Float64Range, double>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static Float32Range? RangeIntersectAgg(this IEnumerable<Float32Range> source)
=> IntersectAggCore<Float32Range, float>(source);
=> IntersectAggCore<Float32Range, float>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static DateRange? RangeIntersectAgg(this IEnumerable<DateRange> source)
=> IntersectAggCore<DateRange, DateOnly>(source);
=> IntersectAggCore<DateRange, DateOnly>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static DateTimeRange? RangeIntersectAgg(this IEnumerable<DateTimeRange> source)
=> IntersectAggCore<DateTimeRange, DateTime>(source);
=> IntersectAggCore<DateTimeRange, DateTime>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static DateTimeOffsetRange? RangeIntersectAgg(this IEnumerable<DateTimeOffsetRange> source)
=> IntersectAggCore<DateTimeOffsetRange, DateTimeOffset>(source);
=> IntersectAggCore<DateTimeOffsetRange, DateTimeOffset>(WithoutNulls(source));

/// <inheritdoc cref="RangeIntersectAgg(IEnumerable{Int32Range})"/>
public static TimeRange? RangeIntersectAgg(this IEnumerable<TimeRange> source)
=> IntersectAggCore<TimeRange, TimeOnly>(source);
=> IntersectAggCore<TimeRange, TimeOnly>(WithoutNulls(source));

/// <summary>
/// Aggregates the sets into one normalized <see cref="RangeSet{TRange,T}"/>, the union of all of
/// them — equivalent to the PostgreSQL <c>range_agg</c> aggregate over multiranges.
/// </summary>
/// <remarks>
/// An empty source yields <see cref="RangeSet{TRange,T}.Empty"/>, and an empty set adds nothing.
/// A <see langword="null"/> set is skipped, as PostgreSQL skips a <c>NULL</c> input.
/// As with the range overloads, PostgreSQL returns <c>NULL</c> over zero rows.
/// </remarks>
/// <param name="source">The sets to aggregate.</param>
Expand All @@ -125,7 +130,7 @@ public static RangeSet<TimeRange, TimeOnly> RangeAgg(this IEnumerable<TimeRange>
public static RangeSet<TRange, T> RangeAgg<TRange, T>(this IEnumerable<RangeSet<TRange, T>> source)
where TRange : IRangeFactory<TRange, T>, IRange<T>
where T : struct, IComparable<T>, IEquatable<T>
=> RangeSet<TRange, T>.From(source.SelectMany(set => set));
=> RangeSet<TRange, T>.From(WithoutNulls(source).SelectMany(set => set));

/// <summary>
/// Intersects all sets of the sequence into one set — equivalent to the PostgreSQL
Expand All @@ -134,7 +139,8 @@ public static RangeSet<TRange, T> RangeAgg<TRange, T>(this IEnumerable<RangeSet<
/// <remarks>
/// An empty source yields <see langword="null"/>, matching the <c>NULL</c> PostgreSQL returns
/// for aggregates over zero rows. Sets with no value in common, or an empty set among the
/// inputs, give <see cref="RangeSet{TRange,T}.Empty"/>.
/// inputs, give <see cref="RangeSet{TRange,T}.Empty"/>. A <see langword="null"/> set is skipped, as
/// PostgreSQL skips a <c>NULL</c> input, so a source of nothing but nulls is an empty source.
/// </remarks>
/// <param name="source">The sets to intersect.</param>
/// <typeparam name="TRange">The range type of the sets.</typeparam>
Expand All @@ -145,6 +151,11 @@ public static RangeSet<TRange, T> RangeAgg<TRange, T>(this IEnumerable<RangeSet<
public static RangeSet<TRange, T>? RangeIntersectAgg<TRange, T>(this IEnumerable<RangeSet<TRange, T>> source)
where TRange : IRangeFactory<TRange, T>, IRange<T>
where T : struct, IComparable<T>, IEquatable<T>
=> IntersectSetsCore<TRange, T>(WithoutNulls(source));

private static RangeSet<TRange, T>? IntersectSetsCore<TRange, T>(IEnumerable<RangeSet<TRange, T>> source)
where TRange : IRangeFactory<TRange, T>, IRange<T>
where T : struct, IComparable<T>, IEquatable<T>
{
RangeSet<TRange, T>? accumulator = null;
foreach (var set in source)
Expand All @@ -167,4 +178,12 @@ public static RangeSet<TRange, T> RangeAgg<TRange, T>(this IEnumerable<RangeSet<

return accumulator;
}

/// <summary>
/// The source without its <see langword="null"/> elements. PostgreSQL's aggregates skip a
/// <c>NULL</c> input, so a group over a nullable column aggregates the values it has.
/// </summary>
private static IEnumerable<TItem> WithoutNulls<TItem>(IEnumerable<TItem?> source)
where TItem : class
=> source.OfType<TItem>();
}
Loading
Loading