Skip to content

fix: build a set from an inline array in a query (#583) - #591

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/583-inline-set-from-array
Nov 30, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/583-inline-set-from-array

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Closes #583

Problem

b.Codes.IsSubsetOf(Int32Set.From(new[] { 1, 2 })) threw "An exception was thrown while attempting to evaluate a LINQ query parameter expression", for every set and range set type. Since C# 14 the call binds From(params ReadOnlySpan<T>), so the tree holds ReadOnlySpan<T>.op_Implicit(array). EF Core evaluates query parameters with the LINQ interpreter, which cannot call a method returning a ref struct.

What EF Core 11 does itself: ExpressionTreeFuncletizer rewrites the span conversion away only for MemoryExtensions (Contains, Min, Max, SequenceEqual), hard-coded, before evaluation. It has no hook for that step. The query interceptor (RangeSetContainsRewriter's home) runs after parameter extraction, so it cannot fix this alone.

Change

  • Query/ArraySpanConversionFilterPlugin.cs (internal, new): an IEvaluatableExpressionFilterPlugin that declares Span<T>/ReadOnlySpan<T>.op_Implicit(T[]) not evaluatable. Every expression holding one fails in the interpreter today, so nothing that evaluates now changes. EF Core then keeps the array inline, with its elements as constants or parameters.
  • ValueRangesQueryExpressionInterceptor.SpanFromRewriter (new, runs first) handles XSet.From(op_Implicit(array)) for a registered set or range set type:
    • If the array holds only constants, it builds the set through From(IEnumerable<T>) and inlines it as a constant. The SQL is the same as XSet.Parse("{1,2}", null) inline.
    • If the array reads a column, it goes to the IEnumerable<T> overload. A final projection reads the array and builds the set in memory.
    • If the array holds captured variables, the query is refused with a message naming the two forms that work: capture the set, or pass IEnumerable<T>. Their values are bound after the rewrite runs. Before, EF Core threw the interpreter error.
  • NpgsqlValueRangesServiceCollectionExtensions: registers the plugin.
  • test/oracles/seeds/compareto-null-operand-compared-as-sql-null.patch and lower-bound-comparer-runs-as-sql.patch: refreshed. They edit the interceptor's rewriter chain, which gained a link. Their defects are unchanged.
  • Changelogs: one Fixed line in the root and EF changelogs, under the 11.0.0-preview.3 section feat: typed Overlaps for SchemaKit exclusion constraints in a fifth package (#540) #592 opened. Rebased on main after feat: typed Overlaps for SchemaKit exclusion constraints in a fifth package (#540) #592.

Tests

InlineSetFromArrayTests (EF translation, no database):

  • For every set and range set column of Booking (23 types, discovered), it builds From(op_Implicit(new[] { x })) as the compiler writes it. It expects the SQL of the same set inlined as a constant.
  • AsWrittenInCSharp checks the compiler's own tree against Int32Set.Parse.
  • One test covers an array that reads a column, and one covers the refusal of captured variables.

Reverted: with the plugin's registration removed, 25 of the 27 fail with the original interpreter exception.

Local: EF translation 479/479, EF NodaTime translation 114/114, conventions 431/431. Not run locally: the integration projects (no new SQL shape; the inlined constant is the existing constant path).

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 30, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working area/range-set RangeSet and multirange algebra area/value-sets Value set families and wrapper arities package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/low Cosmetic, message or documentation-level difference labels Nov 30, 2026
Since C# 14, Int32Set.From(new[] { 1, 2 }) binds From(params
ReadOnlySpan<T>), so the expression tree holds ReadOnlySpan<T>.op_Implicit
over the array. EF Core evaluates query parameters with the LINQ
interpreter before any plugin sees the query, and the interpreter cannot
call a method that returns a ref struct, so every such query threw. EF Core
rewrites the conversion away for MemoryExtensions only.

An IEvaluatableExpressionFilterPlugin now declares the array-to-span
conversion not evaluatable (the interpreter fails on every expression that
holds one, so nothing that evaluates today changes). The call then reaches
the query interceptor, which builds an array of constants into the set
through From(IEnumerable<T>) and inlines it, as EF Core does with an inline
Parse; hands an array that reads a column to the IEnumerable overload; and
refuses an array of captured variables, whose values are bound after the
rewrite, naming the two spellings that work.

Two seed patches touched the interceptor's rewriter chain and are
refreshed against it; their defects are unchanged.

Found-By: CA
Defect-Class: SQL

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CaffeinatedCoder
CaffeinatedCoder force-pushed the fix/583-inline-set-from-array branch from d6aec89 to 15c27f2 Compare November 30, 2026 12:41
@CaffeinatedCoder
CaffeinatedCoder merged commit 71b003e into main Nov 30, 2026
15 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/583-inline-set-from-array branch November 30, 2026 12:45
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/value-sets Value set families and wrapper arities bug Something isn't working package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/low Cosmetic, message or documentation-level difference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A set built inline in a query with From(new[] { … }) throws while EF Core evaluates the parameter

1 participant