Skip to content

Commit 67fd137

Browse files
authored
[release/8.0] Add missing check for predicate in primitive collection simplifications (#33933) (#33939)
Fixes #33932 (cherry picked from commit b84707f)
1 parent 9db7d0e commit 67fd137

6 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class SqlServerQueryableMethodTranslatingExpressionVisitor : RelationalQu
2828
private static readonly bool UseOldBehavior32374 =
2929
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32374", out var enabled32374) && enabled32374;
3030

31+
private static readonly bool UseOldBehavior33932 =
32+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue33932", out var enabled33932) && enabled33932;
33+
3134
/// <summary>
3235
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
3336
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -446,8 +449,9 @@ static IEnumerable<INavigation> GetAllNavigationsInHierarchy(IEntityType entityT
446449
Offset: null,
447450
Orderings: [],
448451
Projection: [{ Expression: ColumnExpression { Name: "value", Table: var projectionColumnTable } }]
449-
}
452+
} subquery
450453
}
454+
&& (UseOldBehavior33932 || subquery.Predicate is null)
451455
&& projectionColumnTable == openJsonExpression)
452456
{
453457
var newInExpression = _sqlExpressionFactory.In(translatedItem, parameter);
@@ -493,6 +497,7 @@ static IEnumerable<INavigation> GetAllNavigationsInHierarchy(IEntityType entityT
493497
]
494498
} selectExpression
495499
&& TranslateExpression(index) is { } translatedIndex
500+
&& (UseOldBehavior33932 || selectExpression.Predicate is null)
496501
&& orderingTable == openJsonExpression)
497502
{
498503
// Index on JSON array

‎src/EFCore.Sqlite.Core/Query/Internal/SqliteQueryableMethodTranslatingExpressionVisitor.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class SqliteQueryableMethodTranslatingExpressionVisitor : RelationalQuery
2121
private readonly SqliteSqlExpressionFactory _sqlExpressionFactory;
2222
private readonly bool _areJsonFunctionsSupported;
2323

24+
private static readonly bool UseOldBehavior33932 =
25+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue33932", out var enabled33932) && enabled33932;
26+
2427
/// <summary>
2528
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
2629
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -82,7 +85,8 @@ protected override QueryableMethodTranslatingExpressionVisitor CreateSubqueryVis
8285
IsDistinct: false,
8386
Limit: null,
8487
Offset: null
85-
})
88+
} subquery
89+
&& (UseOldBehavior33932 || subquery.Predicate is null))
8690
{
8791
var translation =
8892
_sqlExpressionFactory.GreaterThan(
@@ -180,7 +184,8 @@ protected override QueryableMethodTranslatingExpressionVisitor CreateSubqueryVis
180184
IsDistinct: false,
181185
Limit: null,
182186
Offset: null
183-
})
187+
} subquery
188+
&& (UseOldBehavior33932 || subquery.Predicate is null))
184189
{
185190
var translation = _sqlExpressionFactory.Function(
186191
"json_array_length",
@@ -456,7 +461,8 @@ static IEnumerable<INavigation> GetAllNavigationsInHierarchy(IEntityType entityT
456461
Offset: null
457462
} selectExpression
458463
&& orderingColumn.Table == jsonEachExpression
459-
&& TranslateExpression(index) is { } translatedIndex)
464+
&& TranslateExpression(index) is { } translatedIndex
465+
&& (UseOldBehavior33932 || selectExpression.Predicate is null))
460466
{
461467
// Index on JSON array
462468

‎test/EFCore.Specification.Tests/Query/PrimitiveCollectionsQueryTestBase.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,20 @@ public virtual Task Column_collection_Length(bool async)
386386
async,
387387
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Length == 2));
388388

389+
[ConditionalTheory]
390+
[MemberData(nameof(IsAsyncData))]
391+
public virtual Task Column_collection_Count_with_predicate(bool async)
392+
=> AssertQuery(
393+
async,
394+
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Count(i => i > 1) == 2));
395+
396+
[ConditionalTheory] // #33932
397+
[MemberData(nameof(IsAsyncData))]
398+
public virtual Task Column_collection_Where_Count(bool async)
399+
=> AssertQuery(
400+
async,
401+
ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => c.Ints.Where(i => i > 1).Count() == 2));
402+
389403
[ConditionalTheory]
390404
[MemberData(nameof(IsAsyncData))]
391405
public virtual Task Column_collection_index_int(bool async)

‎test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQueryOldSqlServerTest.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ public override Task Column_collection_Count_method(bool async)
468468
public override Task Column_collection_Length(bool async)
469469
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_Length(async));
470470

471+
public override Task Column_collection_Count_with_predicate(bool async)
472+
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_Count_with_predicate(async));
473+
474+
public override Task Column_collection_Where_Count(bool async)
475+
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_Where_Count(async));
476+
471477
public override Task Column_collection_index_int(bool async)
472478
=> AssertCompatibilityLevelTooLow(() => base.Column_collection_index_int(async));
473479

‎test/EFCore.SqlServer.FunctionalTests/Query/PrimitiveCollectionsQuerySqlServerTest.cs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,36 @@ FROM OPENJSON([p].[Ints]) WITH ([value] int '$') AS [i]) = 2
663663
""");
664664
}
665665

666+
public override async Task Column_collection_Count_with_predicate(bool async)
667+
{
668+
await base.Column_collection_Count_with_predicate(async);
669+
670+
AssertSql(
671+
"""
672+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
673+
FROM [PrimitiveCollectionsEntity] AS [p]
674+
WHERE (
675+
SELECT COUNT(*)
676+
FROM OPENJSON([p].[Ints]) WITH ([value] int '$') AS [i]
677+
WHERE [i].[value] > 1) = 2
678+
""");
679+
}
680+
681+
public override async Task Column_collection_Where_Count(bool async)
682+
{
683+
await base.Column_collection_Where_Count(async);
684+
685+
AssertSql(
686+
"""
687+
SELECT [p].[Id], [p].[Bool], [p].[Bools], [p].[DateTime], [p].[DateTimes], [p].[Enum], [p].[Enums], [p].[Int], [p].[Ints], [p].[NullableInt], [p].[NullableInts], [p].[NullableString], [p].[NullableStrings], [p].[String], [p].[Strings]
688+
FROM [PrimitiveCollectionsEntity] AS [p]
689+
WHERE (
690+
SELECT COUNT(*)
691+
FROM OPENJSON([p].[Ints]) WITH ([value] int '$') AS [i]
692+
WHERE [i].[value] > 1) = 2
693+
""");
694+
}
695+
666696
public override async Task Column_collection_index_int(bool async)
667697
{
668698
await base.Column_collection_index_int(async);

‎test/EFCore.Sqlite.FunctionalTests/Query/PrimitiveCollectionsQuerySqliteTest.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,37 @@ WHERE json_array_length("p"."Ints") = 2
649649
""");
650650
}
651651

652+
public override async Task Column_collection_Count_with_predicate(bool async)
653+
{
654+
await base.Column_collection_Count_with_predicate(async);
655+
656+
AssertSql(
657+
"""
658+
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings"
659+
FROM "PrimitiveCollectionsEntity" AS "p"
660+
WHERE (
661+
SELECT COUNT(*)
662+
FROM json_each("p"."Ints") AS "i"
663+
WHERE "i"."value" > 1) = 2
664+
""");
665+
}
666+
667+
// #33932
668+
public override async Task Column_collection_Where_Count(bool async)
669+
{
670+
await base.Column_collection_Where_Count(async);
671+
672+
AssertSql(
673+
"""
674+
SELECT "p"."Id", "p"."Bool", "p"."Bools", "p"."DateTime", "p"."DateTimes", "p"."Enum", "p"."Enums", "p"."Int", "p"."Ints", "p"."NullableInt", "p"."NullableInts", "p"."NullableString", "p"."NullableStrings", "p"."String", "p"."Strings"
675+
FROM "PrimitiveCollectionsEntity" AS "p"
676+
WHERE (
677+
SELECT COUNT(*)
678+
FROM json_each("p"."Ints") AS "i"
679+
WHERE "i"."value" > 1) = 2
680+
""");
681+
}
682+
652683
public override async Task Column_collection_index_int(bool async)
653684
{
654685
await base.Column_collection_index_int(async);

0 commit comments

Comments
 (0)