Skip to content

Commit 7ebdd78

Browse files
authored
Only translate static Compare methods declared on the operand type (#39491)
Fixes #39402
1 parent c685570 commit 7ebdd78

7 files changed

Lines changed: 225 additions & 3 deletions

File tree

‎src/EFCore.Cosmos/Query/Internal/Translators/CosmosComparisonTranslator.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ public CosmosComparisonTranslator(ISqlExpressionFactory sqlExpressionFactory)
4141
SqlExpression? left = null;
4242
SqlExpression? right = null;
4343
if (method.Name == nameof(string.Compare)
44+
&& method.IsStatic
4445
&& arguments.Count == 2
45-
&& arguments[0].Type == arguments[1].Type)
46+
&& arguments[0].Type == arguments[1].Type
47+
&& arguments[0].Type == method.DeclaringType)
4648
{
4749
left = arguments[0];
4850
right = arguments[1];

‎src/EFCore.Relational/Query/Internal/Translators/ComparisonTranslator.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ public ComparisonTranslator(ISqlExpressionFactory sqlExpressionFactory)
4242
SqlExpression? left = null;
4343
SqlExpression? right = null;
4444
if (method.Name == nameof(string.Compare)
45+
&& method.IsStatic
4546
&& arguments.Count == 2
46-
&& arguments[0].Type == arguments[1].Type)
47+
&& arguments[0].Type == arguments[1].Type
48+
&& arguments[0].Type == method.DeclaringType)
4749
{
4850
left = arguments[0];
4951
right = arguments[1];

‎test/EFCore.Cosmos.FunctionalTests/Query/Translations/MiscellaneousTranslationsCosmosTest.cs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,57 @@ FROM root c
354354
""");
355355
}
356356

357+
public override async Task DateTime_Compare_on_nullable_Value()
358+
{
359+
await base.DateTime_Compare_on_nullable_Value();
360+
361+
AssertSql(
362+
"""
363+
SELECT VALUE c
364+
FROM root c
365+
WHERE ((c["DateTime"] != null) AND (IIF((c["DateTime"] = c["DateTime"]), 0, IIF((c["DateTime"] > c["DateTime"]), 1, IIF((c["DateTime"] < c["DateTime"]), -1, null))) = 0))
366+
""");
367+
}
368+
369+
public override async Task StringComparer_Compare()
370+
{
371+
await base.StringComparer_Compare();
372+
373+
AssertSql();
374+
}
375+
376+
public override async Task Custom_Comparer_Compare()
377+
{
378+
await base.Custom_Comparer_Compare();
379+
380+
AssertSql();
381+
}
382+
383+
public override async Task Default_Comparer_Compare()
384+
{
385+
await base.Default_Comparer_Compare();
386+
387+
AssertSql();
388+
}
389+
390+
public override async Task Custom_Comparer_Compare_in_final_projection()
391+
{
392+
await base.Custom_Comparer_Compare_in_final_projection();
393+
394+
AssertSql(
395+
"""
396+
SELECT VALUE c["Int"]
397+
FROM root c
398+
""");
399+
}
400+
401+
public override async Task Nullable_Compare()
402+
{
403+
await base.Nullable_Compare();
404+
405+
AssertSql();
406+
}
407+
357408
#endregion Compare
358409

359410
[Fact]
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.EntityFrameworkCore.TestModels.BasicTypesModel;
5+
46
namespace Microsoft.EntityFrameworkCore.Query.Translations;
57

68
public class MiscellaneousTranslationsInMemoryTest(BasicTypesQueryInMemoryFixture fixture)
7-
: MiscellaneousTranslationsTestBase<BasicTypesQueryInMemoryFixture>(fixture);
9+
: MiscellaneousTranslationsTestBase<BasicTypesQueryInMemoryFixture>(fixture)
10+
{
11+
// The in-memory provider evaluates the comparer itself, so these queries run and honor its ordering.
12+
public override Task StringComparer_Compare()
13+
=> AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(c => StringComparer.OrdinalIgnoreCase.Compare(c.String, "seattle") == 0));
14+
15+
public override async Task Custom_Comparer_Compare()
16+
{
17+
var reversed = Comparer<int>.Create((x, y) => y.CompareTo(x));
18+
19+
await AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(c => reversed.Compare(c.Int, 8) > 0));
20+
21+
await AssertQuery(
22+
ss => ss.Set<BasicTypesEntity>().OrderBy(c => reversed.Compare(c.Int, 8)).ThenBy(c => c.Id),
23+
assertOrder: true);
24+
}
25+
26+
public override Task Default_Comparer_Compare()
27+
=> AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(c => Comparer<int>.Default.Compare(c.Int, 8) > 0));
28+
29+
public override Task Nullable_Compare()
30+
=> AssertQuery(ss => ss.Set<NullableBasicTypesEntity>().Where(c => Nullable.Compare(c.Int, 8) == 0));
31+
}

‎test/EFCore.Specification.Tests/Query/Translations/MiscellaneousTranslationsTestBase.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,5 +355,46 @@ public virtual async Task TimeSpan_Compare_to_simple_zero(bool compareTo)
355355
}
356356
}
357357

358+
[Fact]
359+
public virtual Task DateTime_Compare_on_nullable_Value()
360+
=> AssertQuery(ss => ss.Set<NullableBasicTypesEntity>()
361+
.Where(c => c.DateTime != null && DateTime.Compare(c.DateTime.Value, c.DateTime.Value) == 0));
362+
363+
[Fact]
364+
public virtual Task StringComparer_Compare()
365+
=> AssertTranslationFailed(() => AssertQuery(ss => ss.Set<BasicTypesEntity>()
366+
.Where(c => StringComparer.OrdinalIgnoreCase.Compare(c.String, "seattle") == 0)));
367+
368+
[Fact]
369+
public virtual async Task Custom_Comparer_Compare()
370+
{
371+
var reversed = Comparer<int>.Create((x, y) => y.CompareTo(x));
372+
373+
await AssertTranslationFailed(() => AssertQuery(ss => ss.Set<BasicTypesEntity>()
374+
.Where(c => reversed.Compare(c.Int, 8) > 0)));
375+
376+
await AssertTranslationFailed(() => AssertQuery(
377+
ss => ss.Set<BasicTypesEntity>().OrderBy(c => reversed.Compare(c.Int, 8)).ThenBy(c => c.Id),
378+
assertOrder: true));
379+
}
380+
381+
[Fact]
382+
public virtual Task Default_Comparer_Compare()
383+
=> AssertTranslationFailed(() => AssertQuery(ss => ss.Set<BasicTypesEntity>()
384+
.Where(c => Comparer<int>.Default.Compare(c.Int, 8) > 0)));
385+
386+
[Fact]
387+
public virtual Task Custom_Comparer_Compare_in_final_projection()
388+
{
389+
var reversed = Comparer<int>.Create((x, y) => y.CompareTo(x));
390+
391+
return AssertQuery(ss => ss.Set<BasicTypesEntity>().Select(c => reversed.Compare(c.Int, 8)));
392+
}
393+
394+
[Fact]
395+
public virtual Task Nullable_Compare()
396+
=> AssertTranslationFailed(() => AssertQuery(ss => ss.Set<NullableBasicTypesEntity>()
397+
.Where(c => Nullable.Compare(c.Int, 8) == 0)));
398+
358399
#endregion
359400
}

‎test/EFCore.SqlServer.FunctionalTests/Query/Translations/MiscellaneousTranslationsSqlServerTest.cs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,57 @@ FROM [BasicTypesEntities] AS [b]
842842
""");
843843
}
844844

845+
public override async Task DateTime_Compare_on_nullable_Value()
846+
{
847+
await base.DateTime_Compare_on_nullable_Value();
848+
849+
AssertSql(
850+
"""
851+
SELECT [n].[Id], [n].[Bool], [n].[Byte], [n].[ByteArray], [n].[DateOnly], [n].[DateTime], [n].[DateTimeOffset], [n].[Decimal], [n].[Double], [n].[Enum], [n].[FlagsEnum], [n].[Float], [n].[Guid], [n].[Int], [n].[Long], [n].[Short], [n].[String], [n].[TimeOnly], [n].[TimeSpan]
852+
FROM [NullableBasicTypesEntities] AS [n]
853+
WHERE [n].[DateTime] IS NOT NULL
854+
""");
855+
}
856+
857+
public override async Task StringComparer_Compare()
858+
{
859+
await base.StringComparer_Compare();
860+
861+
AssertSql();
862+
}
863+
864+
public override async Task Custom_Comparer_Compare()
865+
{
866+
await base.Custom_Comparer_Compare();
867+
868+
AssertSql();
869+
}
870+
871+
public override async Task Default_Comparer_Compare()
872+
{
873+
await base.Default_Comparer_Compare();
874+
875+
AssertSql();
876+
}
877+
878+
public override async Task Custom_Comparer_Compare_in_final_projection()
879+
{
880+
await base.Custom_Comparer_Compare_in_final_projection();
881+
882+
AssertSql(
883+
"""
884+
SELECT [b].[Int]
885+
FROM [BasicTypesEntities] AS [b]
886+
""");
887+
}
888+
889+
public override async Task Nullable_Compare()
890+
{
891+
await base.Nullable_Compare();
892+
893+
AssertSql();
894+
}
895+
845896
#endregion Compare
846897

847898
[Fact]

‎test/EFCore.Sqlite.FunctionalTests/Query/Translations/MiscellaneousTranslationsSqliteTest.cs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,57 @@ public override async Task TimeSpan_Compare_to_simple_zero(bool compareTo)
284284
""");
285285
}
286286

287+
public override async Task DateTime_Compare_on_nullable_Value()
288+
{
289+
await base.DateTime_Compare_on_nullable_Value();
290+
291+
AssertSql(
292+
"""
293+
SELECT "n"."Id", "n"."Bool", "n"."Byte", "n"."ByteArray", "n"."DateOnly", "n"."DateTime", "n"."DateTimeOffset", "n"."Decimal", "n"."Double", "n"."Enum", "n"."FlagsEnum", "n"."Float", "n"."Guid", "n"."Int", "n"."Long", "n"."Short", "n"."String", "n"."TimeOnly", "n"."TimeSpan"
294+
FROM "NullableBasicTypesEntities" AS "n"
295+
WHERE "n"."DateTime" IS NOT NULL
296+
""");
297+
}
298+
299+
public override async Task StringComparer_Compare()
300+
{
301+
await base.StringComparer_Compare();
302+
303+
AssertSql();
304+
}
305+
306+
public override async Task Custom_Comparer_Compare()
307+
{
308+
await base.Custom_Comparer_Compare();
309+
310+
AssertSql();
311+
}
312+
313+
public override async Task Default_Comparer_Compare()
314+
{
315+
await base.Default_Comparer_Compare();
316+
317+
AssertSql();
318+
}
319+
320+
public override async Task Custom_Comparer_Compare_in_final_projection()
321+
{
322+
await base.Custom_Comparer_Compare_in_final_projection();
323+
324+
AssertSql(
325+
"""
326+
SELECT "b"."Int"
327+
FROM "BasicTypesEntities" AS "b"
328+
""");
329+
}
330+
331+
public override async Task Nullable_Compare()
332+
{
333+
await base.Nullable_Compare();
334+
335+
AssertSql();
336+
}
337+
287338
#endregion Compare
288339

289340
[Fact]

0 commit comments

Comments
 (0)