Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 4 additions & 3 deletions src/EFCore.PG/Internal/EnumerableMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class EnumerableMethods

public static MethodInfo All { get; }

// public static MethodInfo AnyWithoutPredicate { get; }
public static MethodInfo AnyWithoutPredicate { get; }

public static MethodInfo AnyWithPredicate { get; }

Expand Down Expand Up @@ -259,8 +259,9 @@ static EnumerableMethods()
nameof(Enumerable.All), 1,
types => [typeof(IEnumerable<>).MakeGenericType(types[0]), typeof(Func<,>).MakeGenericType(types[0], typeof(bool))]);

// AnyWithoutPredicate = GetMethod(nameof(Enumerable.Any), 1,
// types => new[] { typeof(IEnumerable<>).MakeGenericType(types[0]) });
AnyWithoutPredicate = GetMethod(
nameof(Enumerable.Any), 1,
types => [typeof(IEnumerable<>).MakeGenericType(types[0])]);

AnyWithPredicate = GetMethod(
nameof(Enumerable.Any), 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public NpgsqlByteArrayMethodTranslator(ISqlExpressionFactory sqlExpressionFactor
// Note: we only translate if the array argument is a column mapped to bytea. There are various other
// cases (e.g. Where(b => new byte[] { 1, 2, 3 }.Contains(b.SomeByte))) where we prefer to translate via
// regular PostgreSQL array logic.
if (method.GetGenericMethodDefinition().Equals(EnumerableMethods.AnyWithoutPredicate))
Comment thread
georg-jung marked this conversation as resolved.
Outdated
Comment thread
georg-jung marked this conversation as resolved.
Outdated
{
return _sqlExpressionFactory.GreaterThan(
_sqlExpressionFactory.Function(
"length",
[arguments[0]],
nullable: true,
argumentsPropagateNullability: TrueArrays[1],
typeof(int)),
_sqlExpressionFactory.Constant(0));
}
Comment thread
georg-jung marked this conversation as resolved.
Outdated

if (method.GetGenericMethodDefinition().Equals(EnumerableMethods.Contains))
{
var source = arguments[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.EntityFrameworkCore.TestModels.BasicTypesModel;

namespace Microsoft.EntityFrameworkCore.Query.Translations;

public class ByteArrayTranslationsNpgsqlTest : ByteArrayTranslationsTestBase<BasicTypesQueryNpgsqlFixture>
Expand Down Expand Up @@ -98,6 +100,32 @@ public override async Task SequenceEqual()
""");
}

[ConditionalFact]
public virtual async Task Length_greater_than_zero()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this test is necessary, given that we already have coverage for Length and Any? This tests a composite tree pattern (so both ByteArray.Length and greater-than-zero), but we usually do that only when there's a special casing - here it seems like it's just the regular translation for Length, and then the regular translation for greater-than-zero.

Another way to think about it, is that AFAICT this doesn't add any more coverage than Length_greater_than_one, Length_greater_than_two would (and obviously we wouldn't do them).

(I understand that we now normalize Length > 0 to Any() - which is slightly "special" - but we already have the direct Any() test below to cover that)

So unless you think there's a specific reason for this test, we can consider removing it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw #3817 (comment) - I'm still not sure that test really adds anything, but I'm also OK with leaving it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I agree that one of them is sufficient to cover the relevant code paths, so I removed the Length > 0 test in e2ad4b0

{
await AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(e => e.ByteArray.Length > 0));

AssertSql(
"""
SELECT b."Id", b."Bool", b."Byte", b."ByteArray", b."DateOnly", b."DateTime", b."DateTimeOffset", b."Decimal", b."Double", b."Enum", b."FlagsEnum", b."Float", b."Guid", b."Int", b."Long", b."Short", b."String", b."TimeOnly", b."TimeSpan"
FROM "BasicTypesEntities" AS b
WHERE length(b."ByteArray") > 0
""");
}

[ConditionalFact]
public virtual async Task Any()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I'll "promote" this test to the EF test suite later as it's not very PG-specific.

{
await AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(e => e.ByteArray.Any()));

AssertSql(
"""
SELECT b."Id", b."Bool", b."Byte", b."ByteArray", b."DateOnly", b."DateTime", b."DateTimeOffset", b."Decimal", b."Double", b."Enum", b."FlagsEnum", b."Float", b."Guid", b."Int", b."Long", b."Short", b."String", b."TimeOnly", b."TimeSpan"
FROM "BasicTypesEntities" AS b
WHERE length(b."ByteArray") > 0
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Loading