Skip to content

feat: translate Contains over a captured collection of value sets - #75

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/list-contains-value-set
Oct 17, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/list-contains-value-set

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #46

Problem

wanted.Contains(r.Tags) over a captured collection of value sets (List<T>, T[], IEnumerable<T>, HashSet<T>) threw InvalidCastException in Npgsql for every value set family:

r."Tags" = ANY (@wanted)
InvalidCastException: Writing values of 'System.String[][]' is not supported …

Npgsql sends a parameterized collection as one array parameter. A value set is already a PostgreSQL array, so that parameter would be an array of arrays, which PostgreSQL doesn't have. Any/Count over the collection (unnest(@wanted)) failed the same way. A constant list, EF.Constant and an inline new[] { … } already translated to IN (…) and answered correctly. The full repro matrix is in the issue comment.

Change

  • ValueRangesQueryExpressionInterceptor switches a QueryParameterExpression whose element type is a registered value set, and whose effective mode is Parameter, to EF Core 10's ParameterTranslationMode.MultipleParameters. Each set is bound as its own parameter through its own mapping. Contains becomes IN (@wanted1, @wanted2, …), and Any/Count read from VALUES (@wanted1), (@wanted2). Equality of two canonical arrays is set equality, so the rows match the in-memory answer. This is the same SQL the constant and inline forms already produced.
  • A context set to Constant or MultipleParameters, and EF.Constant(wanted), keep their mode.
  • EF.Parameter(wanted) explicitly asks for the single array parameter that can't exist, so it now throws InvalidOperationException in translation and says to remove it.
  • A server-side Union stays refused under IN by ValueSetSqlGuard, with its equality message. Before, it was refused under = ANY with the generic one. ValueSetSqlGuard itself is unchanged.
  • Docs: new section Match a column against a list of sets, claim E36 in test/oracles/claims.json, and ### Added lines in the root and EF Core changelogs. No candidate was keyed to list.Contains(valueSetColumn) throws InvalidCastException in Npgsql #46.

Side finding, filed as #71: a captured List<Int32Range> fails too, with 42809 op ANY/ALL (array) requires array on right side.

Tests

  • SetCollectionTranslationTests (no database) checks the whole WHERE clause for a list, an array, a wrapper arity's IEnumerable, NOT IN and Any over VALUES. It also covers the EF.Parameter refusal, EF.Constant and the Constant context mode, which keep their literals, the Union refusal, and a List<string> that still binds as = ANY (@wanted).
  • SetCollectionContainsTests (live PostgreSQL) runs each value set family against two rows: all closed BCL families, the nullable column with null in the list, all five NodaTime sets and all fifteen wrapper arities. Each runs as an array, a List and an IEnumerable, together with its negation, and is compared with the in-memory answer. Where the family allows it, the wanted set is built differently from the stored one: another order, a duplicate, another offset, another decimal scale. Also covered: the issue's query, an empty list, Any/Count, the EF.Parameter refusal and the Union refusal, all executed.
  • Revert proof: with only the interceptor change reverted, all 11 live tests fail. Nine fail with InvalidCastException writing String[][], Int16[][], DateOnly[][], LocalDate[][] and so on. The EF.Parameter test catches InvalidCastException where it expects InvalidOperationException. The Union test gets the generic "This use of it" message. Seven of the ten translation tests fail too. The three that pass either way pin paths the fix must leave alone: EF.Constant, the Constant mode and List<string>.
  • dotnet test on the whole solution, integration included: green.

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 9.1.0 milestone Oct 17, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working area/value-sets Value set families and wrapper arities package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario labels Oct 17, 2026
wanted.Contains(r.Tags) over a captured list, array or IEnumerable of
value sets went out as one array parameter (r."Tags" = ANY (@wanted)).
A value set is already an array, so that parameter is an array of
arrays, which PostgreSQL does not have, and Npgsql threw
InvalidCastException writing T[][] for every family.

The query interceptor now switches such a parameter to EF Core's
MultipleParameters mode: one parameter per set, so Contains runs as
IN (@p1, @p2, ...) and Any/Count read from VALUES. Equality of canonical
arrays is set equality, so the rows match the in-memory answer. Only a
collection whose effective mode is Parameter is changed; Constant and
EF.Constant keep their literals. EF.Parameter(wanted), which asks for
the impossible single parameter, throws in translation and says to
remove it. A server-side Union stays refused under IN by
ValueSetSqlGuard.

Closes #46

Found-By: RV
Defect-Class: SQL
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/value-sets Value set families and wrapper arities bug Something isn't working package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL severity/medium Inconsistency or failure in a narrow scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

list.Contains(valueSetColumn) throws InvalidCastException in Npgsql

1 participant