Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FrozenSet.Contains #3405

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
37 changes: 27 additions & 10 deletions src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Frozen;
using static System.Linq.Expressions.Expression;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.ValueConversion;
Expand Down Expand Up @@ -107,7 +108,15 @@ private static Expression<Func<TInput, TOutput>> ArrayConversionExpression<TInpu

var input = Parameter(typeof(TInput), "input");
var convertedInput = input;
var output = Parameter(typeof(TConcreteOutput), "result");

var outputIsImmutable = typeof(TConcreteOutput) is { IsGenericType: true } generic
&& generic == typeof(FrozenSet<>).MakeGenericType(outputElementType);
var mutableOutputType = outputIsImmutable
? typeof(HashSet<>).MakeGenericType(outputElementType)
: typeof(TConcreteOutput);

var output = Parameter(mutableOutputType, "result");

var lengthVariable = Variable(typeof(int), "length");

var expressions = new List<Expression>();
Expand Down Expand Up @@ -185,11 +194,11 @@ private static Expression<Func<TInput, TOutput>> ArrayConversionExpression<TInpu
// Allocate an output array or list
// var result = new int[length];
Assign(
output, typeof(TConcreteOutput).IsArray
output, mutableOutputType.IsArray
? NewArrayBounds(outputElementType, lengthVariable)
: typeof(TConcreteOutput).GetConstructor([typeof(int)]) is ConstructorInfo ctorWithLength
: mutableOutputType.GetConstructor([typeof(int)]) is ConstructorInfo ctorWithLength
? New(ctorWithLength, lengthVariable)
: New(typeof(TConcreteOutput).GetConstructor([])!))
: New(mutableOutputType.GetConstructor([])!))
]);

if (indexer is not null)
Expand All @@ -209,15 +218,15 @@ private static Expression<Func<TInput, TOutput>> ArrayConversionExpression<TInpu
condition: LessThan(counter, lengthVariable),
increment: AddAssign(counter, Constant(1)),
loopContent:
typeof(TConcreteOutput).IsArray
mutableOutputType.IsArray
? Assign(
ArrayAccess(output, counter),
elementConversionExpression is null
? indexer(counter)
: Invoke(elementConversionExpression, indexer(counter)))
: Call(
output,
typeof(TConcreteOutput).GetMethod("Add", [outputElementType])!,
mutableOutputType.GetMethod("Add", [outputElementType])!,
elementConversionExpression is null
? indexer(counter)
: Invoke(elementConversionExpression, indexer(counter)))));
Expand Down Expand Up @@ -256,7 +265,7 @@ elementConversionExpression is null
IfThenElse(
Equal(Call(enumeratorVariable, typeof(IEnumerator).GetMethod(nameof(IEnumerator.MoveNext))!), Constant(true)),
Block(
typeof(TConcreteOutput).IsArray
mutableOutputType.IsArray
// output[counter] = enumerator.Current;
? Assign(
ArrayAccess(output, counterVariable),
Expand All @@ -266,7 +275,7 @@ elementConversionExpression is null
// output.Add(enumerator.Current);
: Call(
output,
typeof(TConcreteOutput).GetMethod("Add", [outputElementType])!,
mutableOutputType.GetMethod("Add", [outputElementType])!,
elementConversionExpression is null
? Property(enumeratorVariable, "Current")
: Invoke(elementConversionExpression, Property(enumeratorVariable, "Current"))),
Expand All @@ -282,8 +291,16 @@ elementConversionExpression is null
Call(enumeratorVariable, typeof(IDisposable).GetMethod(nameof(IDisposable.Dispose))!)));
}

// return output;
expressions.Add(output);
expressions.Add(
outputIsImmutable
// return output.ToFrozenSet(null);
? Call(
typeof(FrozenSet), nameof(FrozenSet.ToFrozenSet),
[outputElementType],
output,
Constant(null, typeof(IEqualityComparer<>).MakeGenericType(outputElementType)))
// return output;
: output);

return Lambda<Func<TInput, TOutput>>(
// First, check if the given array value is null and return null immediately if so
Expand Down
Loading