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 @@ -23,6 +23,7 @@ One call answers differently, on the .NET 11 runtime only: [Changed answers in 1

- Value sets: `ToString` on every value set, in the core and NodaTime packages, runs in a `net11.0` Native AOT consumer, where the process ended with `SIGTRAP` on the first call ([Native AOT](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/serialization.md#native-aot)).
- EF Core: `Any` or `Count` over an empty captured list of ranges or value sets returns `false` or `0` on EF Core 10.0.2 and later, where it failed with `42883: operator does not exist: text && int4range`. The stopgap `AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37216", true)` also brings back EF Core's own failure on empty lists of other types ([EF Core](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#match-a-column-against-a-list-of-sets)).
- EF Core: `RangeSet.Contains` translates on the EF Core 10 patch that carries [dotnet/efcore#37081](https://github.com/dotnet/efcore/issues/37081) (10.0.15), where every overload threw `ArgumentException` inside EF Core before translation. The stopgap `AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37081", true)` also gives up EF Core's own fix for `IReadOnlySet` parameters, which inline their values again ([EF Core](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#query-ranges-with-linq)).

## [10.0.0] — 2026-10-19

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ covers all four packages, which share one version number and release together.

## [10.1.0]

No change in this package. The NodaTime ranges and sets get the EF Core package's fix for an empty captured list on EF Core 10.0.2 and later.
No change in this package. The NodaTime ranges and sets get the EF Core package's fixes for an empty captured list on EF Core 10.0.2 and later, and for `RangeSet.Contains` on EF Core 10.0.15 (dotnet/efcore#37081).

## [10.0.0] — 2026-10-19

Expand Down
1 change: 1 addition & 0 deletions src/CodoMetis.ValueRanges.EFCore.PostgreSQL/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ covers all four packages, which share one version number and release together.
### Fixed

- EF Core: `Any` or `Count` over an empty captured list of ranges or value sets returns `false` or `0` on EF Core 10.0.2 and later, where it failed with `42883: operator does not exist: text && int4range`. The stopgap `AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37216", true)` also brings back EF Core's own failure on empty lists of other types ([EF Core](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#match-a-column-against-a-list-of-sets)).
- EF Core: `RangeSet.Contains` translates on the EF Core 10 patch that carries [dotnet/efcore#37081](https://github.com/dotnet/efcore/issues/37081) (10.0.15), where every overload threw `ArgumentException` inside EF Core before translation. The stopgap `AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Issue37081", true)` also gives up EF Core's own fix for `IReadOnlySet` parameters, which inline their values again ([EF Core](https://github.com/CaffeinatedCoder/CodoMetis.ValueRanges/blob/main/docs/efcore.md#query-ranges-with-linq)).

## [10.0.0] — 2026-10-19

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Reflection;
using CodoMetis.ValueRanges.Core;

namespace CodoMetis.ValueRanges.EntityFrameworkCore.PostgreSQL.Query;

/// <summary>
/// The three <c>RangeSet&lt;TRange, T&gt;.Contains</c> overloads as static calls, which is the form a
/// query holds them in from the moment <see cref="ValueRangesQueryExpressionInterceptor"/> has seen it.
/// </summary>
/// <remarks>
/// <para>
/// <see cref="RangeSet{TRange,T}"/> is an <see cref="IReadOnlyList{T}"/> of its ranges. On the EF Core
/// 10 patch that carries dotnet/efcore#37081 (10.0.15, dotnet/efcore#39114), and from EF Core 11 on, a
/// method named <c>Contains</c> on any <see cref="IReadOnlyCollection{T}"/> is rewritten by EF Core,
/// before translation, into <c>Queryable.Contains</c> over the collection's <em>element</em> type —
/// <c>TRange</c> — so <c>set.Contains(value)</c> and <c>set.Contains(otherSet)</c>, whose operand is not
/// a range, failed inside EF Core with an <see cref="ArgumentException"/> (#288; #287 on the 11 line).
/// The rewrite looks at the method's declaring type, so the same call on a type that is not a
/// collection is left alone: the interceptor turns the instance call into the matching method here,
/// and <see cref="ValueRangesMethodCallTranslator"/> translates both forms to <c>@&gt;</c>.
/// </para>
/// <para>
/// Each method is the instance method it stands for, so a query that is evaluated on the client — a
/// captured set and a captured value — answers as the set does.
/// </para>
/// </remarks>
internal static class RangeSetContains
{
/// <summary><see cref="RangeSet{TRange,T}.Contains(T)"/>.</summary>
public static bool Value<TRange, T>(RangeSet<TRange, T> set, T value)
where TRange : IRangeFactory<TRange, T>, IRange<T>
where T : struct, IComparable<T>, IEquatable<T>
=> set.Contains(value);

/// <summary><see cref="RangeSet{TRange,T}.Contains(IRange{T})"/>.</summary>
public static bool Range<TRange, T>(RangeSet<TRange, T> set, IRange<T> range)
where TRange : IRangeFactory<TRange, T>, IRange<T>
where T : struct, IComparable<T>, IEquatable<T>
=> set.Contains(range);

/// <summary><see cref="RangeSet{TRange,T}.Contains(RangeSet{TRange,T})"/>.</summary>
public static bool Set<TRange, T>(RangeSet<TRange, T> set, RangeSet<TRange, T> other)
where TRange : IRangeFactory<TRange, T>, IRange<T>
where T : struct, IComparable<T>, IEquatable<T>
=> set.Contains(other);

private static readonly MethodInfo ValueMethod = typeof(RangeSetContains).GetMethod(nameof(Value))!;
private static readonly MethodInfo RangeMethod = typeof(RangeSetContains).GetMethod(nameof(Range))!;
private static readonly MethodInfo SetMethod = typeof(RangeSetContains).GetMethod(nameof(Set))!;

/// <summary>
/// The static form of a <c>RangeSet&lt;TRange, T&gt;.Contains</c> instance method, closed over the
/// set's type arguments, or <see langword="null"/> for any other method.
/// </summary>
public static MethodInfo? Equivalent(MethodInfo instanceMethod)
{
if (instanceMethod is not { Name: nameof(RangeSet<,>.Contains), IsStatic: false, DeclaringType: { IsGenericType: true } declaringType }
|| declaringType.GetGenericTypeDefinition() != typeof(RangeSet<,>)
|| instanceMethod.GetParameters() is not [{ ParameterType: var operand }])
return null;

var open = operand == declaringType ? SetMethod
: operand is { IsGenericType: true } && operand.GetGenericTypeDefinition() == typeof(IRange<>) ? RangeMethod
: ValueMethod;

return open.MakeGenericMethod(declaringType.GetGenericArguments());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ private static bool TakesSet(MethodInfo method)
return TranslateRangeExtensionCall(method, arguments);

if (declaringType is { IsGenericType: true } && declaringType.GetGenericTypeDefinition() == typeof(RangeSet<,>))
return TranslateRangeSetCall(instance, declaringType, method, arguments);
return TranslateRangeSetCall(instance, declaringType, method.Name, arguments);

// The static form of RangeSet.Contains the interceptor substitutes (RangeSetContains): the set
// is the first argument, and the method translates as the instance call it stands for.
if (declaringType == typeof(RangeSetContains) && arguments.Count == 2)
return TranslateRangeSetCall(arguments[0], method.GetParameters()[0].ParameterType, nameof(RangeSet<,>.Contains), [arguments[1]]);

if (declaringType is not null
&& RangeTypeRegistry.TryGetByClrType(declaringType, out var definition, out var isSet)
Expand Down Expand Up @@ -296,7 +301,7 @@ private static bool TryResolveDefinition(
private SqlExpression? TranslateRangeSetCall(
SqlExpression? instance,
Type declaringType,
MethodInfo method,
string methodName,
IReadOnlyList<SqlExpression> arguments
)
{
Expand All @@ -322,7 +327,7 @@ SqlExpression Operand(SqlExpression expression)
: expression.Type == definition.ElementClrType ? ApplyElementMapping(expression, definition)
: Range(expression);

switch (method.Name)
switch (methodName)
{
case nameof(RangeSet<,>.Contains) when instance is not null && arguments.Count == 1:
return sqlExpressionFactory.Contains(Set(instance), Operand(arguments[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace CodoMetis.ValueRanges.EntityFrameworkCore.PostgreSQL.Query;
/// one parameter per element, because neither can be bound as one array parameter — see
/// <see cref="CollectionParameterExpander"/>. A fourth makes <c>CompareTo</c> with a null range on
/// either side answer in SQL as <c>Comparer&lt;T&gt;.Default</c> does — see <see cref="NullOperandCompareToRewriter"/>.
/// A fifth keeps <c>RangeSet.Contains</c> out of EF Core's own rewrite of <c>Contains</c> on a
/// read-only collection — see <see cref="RangeSetContainsRewriter"/>.
/// </para>
/// <para>
/// The value set refusals — a server-computed <c>Union</c> consumed by an order- or
Expand All @@ -37,7 +39,8 @@ public sealed class ValueRangesQueryExpressionInterceptor : IQueryExpressionInte
public Expression QueryCompilationStarting(Expression queryExpression, QueryExpressionEventData eventData)
=> CollectionParameterExpander.Expand(
RangeVariantConstantRetyper.Instance.Visit(
NullOperandCompareToRewriter.Instance.Visit(RangeSetOperatorRewriter.Instance.Visit(queryExpression))),
NullOperandCompareToRewriter.Instance.Visit(
RangeSetContainsRewriter.Instance.Visit(RangeSetOperatorRewriter.Instance.Visit(queryExpression)))),
DefaultCollectionMode(eventData.Context));

private static ParameterTranslationMode DefaultCollectionMode(DbContext? context)
Expand Down Expand Up @@ -328,6 +331,32 @@ private static Expression Or(Expression left, Expression right)
};
}

/// <summary>
/// Turns <c>set.Contains(x)</c> on a <see cref="RangeSet{TRange,T}"/> into the static
/// <see cref="RangeSetContains"/> call that stands for it.
/// </summary>
/// <remarks>
/// The EF Core 10 patch that carries dotnet/efcore#37081 (10.0.15), and EF Core 11, rewrite every
/// method named <c>Contains</c> on an <see cref="IReadOnlyCollection{T}"/> into
/// <c>Queryable.Contains</c> over the element type before translation. A
/// <see cref="RangeSet{TRange,T}"/> is a read-only list of its ranges, so <c>Contains(T)</c>,
/// <c>Contains(IRange&lt;T&gt;)</c> and <c>Contains(RangeSet)</c> all matched, and the two whose
/// operand is not a range failed inside EF Core with an <see cref="ArgumentException"/> before any
/// translator ran (#288). The static form has a declaring type that is not a collection, which EF
/// Core's rewrite does not touch; <see cref="ValueRangesMethodCallTranslator"/> translates it to
/// <c>@&gt;</c> as before. On the 10.0.0 floor the instance call translated as it was, which is why
/// the SQL tests could not see the defect: <c>RangeSetContainsShapeTests</c> asserts the tree.
/// </remarks>
private sealed class RangeSetContainsRewriter : ExpressionVisitor
{
public static readonly RangeSetContainsRewriter Instance = new();

protected override Expression VisitMethodCall(MethodCallExpression node)
=> node is { Object: { } set, Arguments: [var operand] } && RangeSetContains.Equivalent(node.Method) is { } equivalent
? Expression.Call(equivalent, Visit(set), Visit(operand))
: base.VisitMethodCall(node);
}

private sealed class RangeSetOperatorRewriter : ExpressionVisitor
{
public static readonly RangeSetOperatorRewriter Instance = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting"/>
</ItemGroup>

<ItemGroup>
<!-- The post-interception query tree reader (#288), shared with the base package's translation tests; one copy, linked. -->
<Compile Include="..\CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests\InterceptedQuery.cs" Link="InterceptedQuery.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime\CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests;
using Microsoft.EntityFrameworkCore;
using NodaTime;

namespace CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime.Tests;

using CodoMetis.ValueRanges;

/// <summary>
/// <c>RangeSetContainsShapeTests</c> for the NodaTime range sets: the interceptor's rewrite keys off
/// <c>RangeSet&lt;,&gt;</c> itself, so the satellite's sets — the month-aligned
/// <c>RangeSet&lt;YearMonthRange, YearMonth&gt;</c> included — must leave no instance <c>Contains</c>
/// for the EF Core 10 patch that carries dotnet/efcore#37081 to rewrite (#288).
/// </summary>
[TestClass]
public sealed class NodaRangeSetContainsShapeTests
{
private static readonly LocalDate Day = new(2024, 6, 15);
private static readonly Instant At = Instant.FromUtc(2024, 6, 15, 12, 0);
private static readonly YearMonth Month = new(2024, 6);

[TestMethod]
public void LocalDateSet_Contains_Value()
=> AssertStaticForm(db => db.Reservations.Where(r => r.BlockedDays.Contains(Day)), "Value", "r.\"BlockedDays\" @> DATE '2024-06-15'");

[TestMethod]
public void LocalDateSet_Contains_RangeColumn()
=> AssertStaticForm(db => db.Reservations.Where(r => r.BlockedDays.Contains(r.Period)), "Range", "r.\"BlockedDays\" @> r.\"Period\"");

[TestMethod]
public void LocalDateSet_Contains_CapturedSet()
{
var other = RangeSet<LocalDateRange, LocalDate>.From([LocalDateRange.CreateFinite(Day, Day.PlusDays(2))]);

AssertStaticForm(db => db.Reservations.Where(r => r.BlockedDays.Contains(other)), "Set", "r.\"BlockedDays\" @> @other");
}

[TestMethod]
public void InstantSet_Contains_Value()
=> AssertStaticForm(db => db.Reservations.Where(r => r.Windows.Contains(At)), "Value", "r.\"Windows\" @> TIMESTAMPTZ '2024-06-15T12:00:00Z'");

[TestMethod]
public void InstantSet_Contains_RangeColumn()
=> AssertStaticForm(db => db.Reservations.Where(r => r.Windows.Contains(r.Window)), "Range", "r.\"Windows\" @> r.\"Window\"");

[TestMethod]
public void YearMonthSet_Contains_Value()
=> AssertStaticForm(db => db.Reservations.Where(r => r.BillingPeriods.Contains(Month)), "Value", "r.\"BillingPeriods\" @> DATE '2024-06-01'");

[TestMethod]
public void YearMonthSet_Contains_RangeColumn()
=> AssertStaticForm(db => db.Reservations.Where(r => r.BillingPeriods.Contains(r.BillingPeriod)), "Range", "r.\"BillingPeriods\" @> r.\"BillingPeriod\"");

[TestMethod]
public void YearMonthSet_Contains_CapturedSet()
{
var other = RangeSet<YearMonthRange, YearMonth>.From([YearMonthRange.CreateFinite(Month, Month.PlusMonths(2))]);

AssertStaticForm(db => db.Reservations.Where(r => r.BillingPeriods.Contains(other)), "Set", "r.\"BillingPeriods\" @> @other");
}

private static void AssertStaticForm(Func<NodaTestDbContext, IQueryable<Reservation>> query, string overload, string sql)
{
using var db = new NodaTestDbContext();
var queryable = query(db);

StringAssert.Contains(queryable.ToQueryString(), sql);

var tree = InterceptedQuery.Of(db, queryable);
var leftover = InterceptedQuery.CollectionContainsCalls(tree);
Assert.IsEmpty(
leftover,
$"An instance Contains on a collection type survived interception, which EF Core 10.0.15 rewrites "
+ $"into Queryable.Contains over the range type: {InterceptedQuery.Describe(leftover)}");

var statics = InterceptedQuery.StaticRangeSetContainsCalls(tree);
Assert.HasCount(1, statics, $"Expected one RangeSetContains call, found: {InterceptedQuery.Describe(statics)}");
Assert.AreEqual(overload, statics[0].Method.Name);
}
}
Loading