Repository navigation
Fix CreateParameter for collection shape mismatch with element value converters - #3894
hostage2222 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a failure in array parameter creation when an element value converter is present and the parameter value’s collection shape doesn’t match the inferred mapping shape (e.g. List<T> passed where an T[] mapping is applied), which previously could cause ValueConverter.Sanitize to throw InvalidCastException (notably in Intersect(...).Any() translated to &&).
Changes:
- Update
NpgsqlArrayTypeMapping.CreateParameterto normalize non-matching values to the supported concrete collection shape (array orList<T>) even when a converter exists, but without rewriting values that already match the converter’s model CLR type. - Add unit tests covering
CreateParameterwith value converters across array/list/immutable-list inputs. - Add functional tests covering
Intersect(...).Any()over value-converted primitive collections with array/list/immutable-list parameters.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/EFCore.PG/Storage/Internal/Mapping/NpgsqlArrayTypeMapping.cs | Normalizes mismatched collection-shape parameter values prior to converter sanitization to avoid invalid casts. |
| test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingSourceTest.cs | Unit coverage for CreateParameter behavior across list/array/immutable-list inputs with converters. |
| test/EFCore.PG.FunctionalTests/Query/ArrayListQueryTest.cs | Functional coverage for Intersect(...).Any() over a value-converted list when the parameter shape varies. |
| test/EFCore.PG.FunctionalTests/Query/ArrayArrayQueryTest.cs | Functional coverage for Intersect(...).Any() over a value-converted array when the parameter shape varies. |
roji
left a comment
There was a problem hiding this comment.
The approach is sound and correctly targets the collection-shape mismatch; I found no blocking issues.
One residual gap: in NpgsqlArrayTypeMapping.cs, the comment says the value is normalized to TConcreteCollection, but the non-array path always creates a List<TElement>. Concrete model collection types to which List<TElement> is not assignable (for example, HashSet<T>) could therefore still fail conversion. Either narrow the comment to describe the supported array/list normalization or instantiate the actual concrete collection.
Additional tests for the shape-only converter path and an arbitrary enumerable parameter over a value-converted array column would improve coverage.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (5)
test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingSourceTest.cs:374
- This helper asserts the exact order of the produced provider array. Since many callers pass unordered inputs (e.g. HashSet), the enumeration/materialization order is not guaranteed and can cause test flakiness. Compare using a deterministic ordering instead of strict positional equivalence.
private void CreateParameter_with_value_converter(Type mappingType, object value)
{
var mapping = CreateTypeMappingSource().FindMapping(mappingType)!;
Assert.NotNull(mapping.Converter);
var parameter = mapping.CreateParameter(new NpgsqlCommand(), "p", value);
Assert.Equivalent(new[] { "foo", "bar" }, Assert.IsType<string[]>(parameter.Value), strict: true);
}
test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingSourceTest.cs:382
- This helper assumes List preserves the original element order. When the input is an unordered collection (e.g. HashSet or other non-list IEnumerable), CreateParameter materializes via enumeration which has unspecified ordering, making the strict equivalence assertion potentially flaky. Compare using a deterministic ordering instead.
private void CreateParameter_without_converter(Type mappingType, object value)
{
var mapping = CreateTypeMappingSource().FindMapping(mappingType)!;
Assert.Null(mapping.Converter);
var parameter = mapping.CreateParameter(new NpgsqlCommand(), "p", value);
Assert.Equivalent(new[] { 1, 2 }, Assert.IsType<List<int>>(parameter.Value), strict: true);
}
test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingSourceTest.cs:340
- This assertion assumes a deterministic element order for a HashSet-based input. HashSet enumeration order is explicitly unspecified, so materializing it to an array can produce a different order and make this test flaky across runtimes/framework versions. Prefer an order-insensitive assertion (e.g. compare sorted sequences) for unordered inputs.
This issue also appears in the following locations of the same file:
- line 368
- line 376
var parameter = mapping.CreateParameter(new NpgsqlCommand(), "p", new HashSet<int> { 1, 2 });
Assert.Equivalent(new[] { 1, 2 }, Assert.IsType<int[]>(parameter.Value), strict: true);
test/EFCore.PG.FunctionalTests/Query/ArrayListQueryTest.cs:940
- HashSet enumeration order is unspecified, but this test asserts the parameter values in a specific order in the SQL baseline. This can make the test flaky if the HashSet gets enumerated in a different order. Consider using a deterministically ordered set (e.g. ImmutableSortedSet/SortedSet) while keeping the variable name (so the baseline parameter name stays the same).
public virtual async Task Intersect_parameter_hash_set_over_value_converted_list()
{
HashSet<SomeEnum> toFindHashSet = [SomeEnum.One, SomeEnum.Three, SomeEnum.Eight];
test/EFCore.PG.FunctionalTests/Query/ArrayArrayQueryTest.cs:934
- HashSet enumeration order is unspecified, but this test asserts the parameter values in a specific order in the SQL baseline. This can make the test flaky if the HashSet gets enumerated in a different order. Consider using a deterministically ordered set (e.g. ImmutableSortedSet/SortedSet) while keeping the variable name (so the baseline parameter name stays the same).
public virtual async Task Intersect_parameter_hash_set_over_value_converted_array()
{
HashSet<SomeEnum> toFindHashSet = [SomeEnum.One, SomeEnum.Three, SomeEnum.Eight];
5fe1443 to
a242d9e
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (3)
src/EFCore.PG/Storage/Internal/Mapping/NpgsqlArrayTypeMapping.cs:202
- CreateInstance() calls GetConstructor on every invocation, and the fallback exception message currently mentions only a parameterless constructor even though this method also supports an (int capacity) constructor. Since CreateParameter() can be a hot path, consider caching the constructor infos in static fields (generic-type specific) and update the exception message accordingly.
private static TConcreteCollection CreateInstance(int? count)
=> (count, typeof(TConcreteCollection)) switch
{
({ } c, var type) when type.GetConstructor([typeof(int)]) is { } ctorWithSize
=> (TConcreteCollection)ctorWithSize.Invoke([c]),
var (_, type) when type.GetConstructor([]) is { } ctor
=> (TConcreteCollection)ctor.Invoke(null),
var (_, type) => throw new InvalidOperationException(
$"Type {type.Name} cannot be instantiated as it does not have a public parameterless constructor")
};
test/EFCore.PG.FunctionalTests/Query/ArrayListQueryTest.cs:941
- This test captures a HashSet and asserts the exact parameter dump in AssertSql. Since HashSet enumeration order is not guaranteed, the logged parameter values can be nondeterministic across runtimes and make the baseline flaky. Consider using a deterministically-ordered set type here (while keeping separate coverage for HashSet elsewhere).
public virtual async Task Intersect_parameter_hash_set_over_value_converted_list()
{
HashSet<SomeEnum> toFindHashSet = [SomeEnum.One, SomeEnum.Three, SomeEnum.Eight];
await AssertQuery(ss => ss.Set<ArrayEntity>().Where(e => e.ValueConvertedListOfEnum.Intersect(toFindHashSet).Any()));
test/EFCore.PG.FunctionalTests/Query/ArrayArrayQueryTest.cs:936
- This test captures a HashSet and asserts the exact parameter dump in AssertSql. Since HashSet enumeration order is not guaranteed, the logged parameter values can be nondeterministic across runtimes and make the baseline flaky. Consider using a deterministically-ordered set type here (while keeping separate coverage for HashSet elsewhere).
[ConditionalFact]
public virtual async Task Intersect_parameter_hash_set_over_value_converted_array()
{
HashSet<SomeEnum> toFindHashSet = [SomeEnum.One, SomeEnum.Three, SomeEnum.Eight];
await AssertQuery(ss => ss.Set<ArrayEntity>().Where(e => e.ValueConvertedArrayOfEnum.Intersect(toFindHashSet).Any()));
|
Chose instantiating the actual Regarding Copilot’s feedback: narrowed the |
roji
left a comment
There was a problem hiding this comment.
Recommendation: The bug needs fixing, but I would address array type-mapping inference rather than add generic collection construction to parameter binding.
Problem and previous investigation. I independently reproduced #3805 with a console application issuing queries against PostgreSQL. Given an element converter MyId -> long, this fails when RelatedIds is MyId[]:
List<MyId> wanted = [new(42)];
await context.Users.Where(u => u.RelatedIds.Intersect(wanted).Any())
.Select(u => u.Id).ToArrayAsync();The original triage correctly identifies the collection/converter mismatch, and this array/list case fails in both 8.0.11 and 9.0.4. However, its no-regression conclusion is incomplete: with a List<MyId> property, ImmutableList<MyId>, HashSet<MyId>, and lazy-enumerable parameters work in 8.0.11 but fail in 9.0.4. This confirms the later reporter's correction. The unconverted long[]/List<long> control and the Any/Contains workaround succeed in both releases. I did not independently execute the SQL Server comparison.
Expected approach and scope. The binary-operator inference path applies one collection mapping to both operands, even when their CLR collection types differ. Infer the common element mapping, then obtain an array mapping for each operand's own CLR type, as the existing item/array mapping path already does. This should be a small C# query-inference change with mixed-shape converter tests; no ADO.NET/plugin changes, public API changes, or documentation expansion are needed.
PR assessment and gap. The PR fixes the reported array/list and immutable-parameter cases, and its functional coverage is useful. However, the three new helpers introduce reflection and an intermediate model collection solely to accommodate the wrong inferred mapping. They also leave the same problem unresolved for a mapped ReadOnlyCollection<MyId> property: the query above throws because that collection cannot be constructed here. Projecting only IDs with a matching read-only parameter already works; this is not a request to support materializing read-only properties. See the inline comment.
Manual validation. I executed the console repro against provider 8.0.11, 9.0.4, the exact PR base, and head a242d9ec, using real PostgreSQL 18.4 and temporary tables with matching, nonmatching, and empty-array rows. The PR passes all 118 cases in ArrayArrayQueryTest/ArrayListQueryTest and all 150 NpgsqlTypeMappingSourceTest cases. In a separate scratch copy of the PR base, a small inference-only change, leaving CreateParameter untouched, passes the same 118 functional cases (including this PR's added tests) and all 15 manual query/control cases, including the read-only projections. That supports replacing the new materialization machinery rather than extending it with more constructor handling.
Posted by copilot
| } | ||
|
|
||
| var count = elements.TryGetNonEnumeratedCount(out var c) ? c : (int?)null; | ||
| var collection = CreateInstance(count); |
There was a problem hiding this comment.
[P2] Infer the parameter's collection mapping instead of constructing the column's collection type
This still fails for an element-converted ReadOnlyCollection<MyId> property queried with a List<MyId> parameter:
List<MyId> wanted = [new(42)];
await context.Users.Where(u => u.RelatedIds.Intersect(wanted).Any())
.Select(u => u.Id).ToArrayAsync();I executed this against the PR: CreateInstance throws InvalidOperationException because ReadOnlyCollection<MyId> has no public parameterless constructor. The identical ID-only query with a matching read-only parameter succeeds, so reading/materializing that property is not required. This is another instance of the existing collection-shape bug, not a new regression introduced by this PR.
Could we instead fix NpgsqlSqlExpressionFactory.ApplyTypeMappingOnPostgresBinary to derive each operand's array mapping from its own CLR collection type plus the inferred element mapping? A small scratch implementation of that approach, with the original CreateParameter unchanged, fixes this case and the reported array/list/immutable cases, and passes all 118 array-query tests including the new ones. It avoids these three helpers, runtime reflection, and the extra intermediate collection. Please include this read-only ID-projection case in the query coverage rather than adding more collection-construction branches.
Posted by copilot
|
Closing in favor of #3991, which applies the fix at the type mapping inference level, preserving a more correct tree representation. |
When a primitive collection has an element value converter and a parameter uses a different collection shape (e.g. List vs T[]), type mapping inference for Intersect(...).Any() → && can apply the column mapping to that parameter. CreateParameter skipped materialization whenever a converter was present, so Sanitize threw InvalidCastException (IConvertible).
Normalize mismatched values to TConcreteCollection before Sanitize, without rewriting values that already match the converter model type. There is no extra materialization on existing paths — only the new mismatch cases take that branch; existing tests do not.
Fixes #3805