Skip to content

Commit a964a63

Browse files
georg-jungroji
authored andcommitted
Translate bytea.Any() as length > 0 (#3817)
Fixes #3816 (cherry picked from commit 7ce26dd)
1 parent b2dce20 commit a964a63

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/EFCore.PG/Internal/EnumerableMethods.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static class EnumerableMethods
1010

1111
public static MethodInfo All { get; }
1212

13-
// public static MethodInfo AnyWithoutPredicate { get; }
13+
public static MethodInfo AnyWithoutPredicate { get; }
1414

1515
public static MethodInfo AnyWithPredicate { get; }
1616

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

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

265266
AnyWithPredicate = GetMethod(
266267
nameof(Enumerable.Any), 1,

src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlByteArrayMethodTranslator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ public NpgsqlByteArrayMethodTranslator(ISqlExpressionFactory sqlExpressionFactor
7979
_sqlExpressionFactory.Constant(0));
8080
}
8181

82+
if (method.GetGenericMethodDefinition().Equals(EnumerableMethods.AnyWithoutPredicate))
83+
{
84+
return _sqlExpressionFactory.GreaterThan(
85+
_sqlExpressionFactory.Function(
86+
"length",
87+
[arguments[0]],
88+
nullable: true,
89+
argumentsPropagateNullability: TrueArrays[1],
90+
typeof(int)),
91+
_sqlExpressionFactory.Constant(0));
92+
}
93+
8294
if (method.GetGenericMethodDefinition().Equals(EnumerableMethods.FirstWithoutPredicate))
8395
{
8496
return _sqlExpressionFactory.Convert(

test/EFCore.PG.FunctionalTests/Query/Translations/ByteArrayTranslationsNpgsqlTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.EntityFrameworkCore.TestModels.BasicTypesModel;
2+
13
namespace Microsoft.EntityFrameworkCore.Query.Translations;
24

35
public class ByteArrayTranslationsNpgsqlTest : ByteArrayTranslationsTestBase<BasicTypesQueryNpgsqlFixture>
@@ -98,6 +100,19 @@ public override async Task SequenceEqual()
98100
""");
99101
}
100102

103+
[ConditionalFact]
104+
public virtual async Task Any()
105+
{
106+
await AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(e => e.ByteArray.Any()));
107+
108+
AssertSql(
109+
"""
110+
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"
111+
FROM "BasicTypesEntities" AS b
112+
WHERE length(b."ByteArray") > 0
113+
""");
114+
}
115+
101116
private void AssertSql(params string[] expected)
102117
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
103118
}

0 commit comments

Comments
 (0)