Skip to content

Commit a6c54b1

Browse files
committed
Bind members of entities from single-result subqueries that include navigations
Composing a Where or OrderBy over an entity produced by a single-result subquery (e.g. GroupBy(...).Select(g => g.First(...)).Where(...)) failed to translate when the entity includes navigations, such as owned types or auto-included navigations: the subquery's shaper was wrapped in IncludeExpressions. Unwrap the includes, and bind an included reference navigation over the same subquery, projecting the navigation's target. Moves the GroupBy element tests to the shared associations test base, so that they run for complex types, owned types and navigations.
1 parent 5fa47b5 commit a6c54b1

13 files changed

Lines changed: 1995 additions & 13 deletions

File tree

‎src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,23 @@ protected override Expression VisitExtension(Expression extensionExpression)
560560
innerExpression = unaryExpression.Operand;
561561
}
562562

563+
// A single-result subquery whose entity has included (e.g. owned) navigations, such as the element of a grouping.
564+
// The includes are kept aside, so that a reference navigation accessed on the entity can be bound over the subquery
565+
// (see TryBindMember).
566+
Dictionary<INavigationBase, Expression>? includedNavigations = null;
567+
while (innerExpression is IncludeExpression includeExpression)
568+
{
569+
includedNavigations ??= [];
570+
includedNavigations[includeExpression.Navigation] = includeExpression.NavigationExpression;
571+
innerExpression = includeExpression.EntityExpression;
572+
}
573+
563574
if (innerExpression is StructuralTypeShaperExpression ese
564575
&& (convertedType == null
565576
|| convertedType.IsAssignableFrom(ese.Type)))
566577
{
567-
return new StructuralTypeReferenceExpression(shapedQueryExpression.UpdateShaperExpression(innerExpression));
578+
return new StructuralTypeReferenceExpression(
579+
shapedQueryExpression.UpdateShaperExpression(innerExpression), includedNavigations);
568580
}
569581

570582
if (innerExpression is ProjectionBindingExpression pbe
@@ -1284,6 +1296,22 @@ public virtual bool TryBindMember(
12841296
return true;
12851297
}
12861298

1299+
// A reference navigation on an entity produced by a single-result subquery,
1300+
// which the subquery already includes (e.g. an owned navigation):
1301+
// bind it over the same subquery, projecting the navigation's target instead of the entity.
1302+
if (typeReference is { Subquery: { } subquery, IncludedNavigations: { } includedNavigations }
1303+
&& structuralType is IEntityType entityType
1304+
&& (member.MemberInfo != null
1305+
? entityType.FindNavigation(member.MemberInfo)
1306+
: entityType.FindNavigation(member.Name!)) is { IsCollection: false } navigation
1307+
&& includedNavigations.TryGetValue(navigation, out var navigationExpression)
1308+
&& Visit(subquery.UpdateShaperExpression(navigationExpression)) is StructuralTypeReferenceExpression navigationReference)
1309+
{
1310+
expression = navigationReference;
1311+
property = navigation;
1312+
return true;
1313+
}
1314+
12871315
AddTranslationErrorDetails(
12881316
CoreStrings.QueryUnableToTranslateMember(
12891317
member.Name,
@@ -2026,21 +2054,28 @@ public StructuralTypeReferenceExpression(StructuralTypeShaperExpression paramete
20262054
StructuralType = parameter.StructuralType;
20272055
}
20282056

2029-
public StructuralTypeReferenceExpression(ShapedQueryExpression subquery)
2057+
public StructuralTypeReferenceExpression(
2058+
ShapedQueryExpression subquery,
2059+
IReadOnlyDictionary<INavigationBase, Expression>? includedNavigations = null)
20302060
{
20312061
Subquery = subquery;
2062+
IncludedNavigations = includedNavigations;
20322063
StructuralType = ((StructuralTypeShaperExpression)subquery.ShaperExpression).StructuralType;
20332064
}
20342065

20352066
private StructuralTypeReferenceExpression(StructuralTypeReferenceExpression typeReference, ITypeBase structuralType)
20362067
{
20372068
Parameter = typeReference.Parameter;
20382069
Subquery = typeReference.Subquery;
2070+
IncludedNavigations = typeReference.IncludedNavigations;
20392071
StructuralType = structuralType;
20402072
}
20412073

20422074
public new StructuralTypeShaperExpression? Parameter { get; }
20432075
public ShapedQueryExpression? Subquery { get; }
2076+
2077+
// For a subquery reference: the navigations the subquery includes, with the shapers of their targets.
2078+
public IReadOnlyDictionary<INavigationBase, Expression>? IncludedNavigations { get; }
20442079
public ITypeBase StructuralType { get; }
20452080

20462081
public override Type Type

‎test/EFCore.Cosmos.FunctionalTests/Query/Associations/ComplexProperties/ComplexPropertiesMiscellaneousCosmosTest.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ FROM root c
9494
public override Task Where_on_associate_scalar_property_on_GroupBy_First()
9595
=> AssertTranslationFailed(base.Where_on_associate_scalar_property_on_GroupBy_First);
9696

97+
public override Task Where_on_root_property_on_GroupBy_First()
98+
=> AssertTranslationFailed(base.Where_on_root_property_on_GroupBy_First);
99+
100+
public override Task Select_multiple_associates_on_GroupBy_OrderByDescending_First()
101+
=> AssertTranslationFailed(base.Select_multiple_associates_on_GroupBy_OrderByDescending_First);
102+
103+
public override Task Select_multiple_associates_on_GroupBy_MaxBy()
104+
=> AssertTranslationFailed(base.Select_multiple_associates_on_GroupBy_MaxBy);
105+
public override Task OrderBy_associate_scalar_property_on_GroupBy_First()
106+
=> AssertTranslationFailed(base.OrderBy_associate_scalar_property_on_GroupBy_First);
107+
97108
#endregion Subquery
98109

99110
[Fact]

‎test/EFCore.Cosmos.FunctionalTests/Query/Associations/OwnedComplexProperties/OwnedComplexPropertiesMiscellaneousCosmosTest.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ FROM root c
5151
private void AssertSql(params string[] expected)
5252
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
5353

54+
#region GroupBy element
55+
56+
public override Task Where_on_root_property_on_GroupBy_First()
57+
=> AssertTranslationFailed(base.Where_on_root_property_on_GroupBy_First);
58+
59+
public override Task Where_on_associate_scalar_property_on_GroupBy_First()
60+
=> AssertTranslationFailed(base.Where_on_associate_scalar_property_on_GroupBy_First);
61+
62+
public override Task Select_multiple_associates_on_GroupBy_OrderByDescending_First()
63+
=> AssertTranslationFailed(base.Select_multiple_associates_on_GroupBy_OrderByDescending_First);
64+
65+
public override Task Select_multiple_associates_on_GroupBy_MaxBy()
66+
=> AssertTranslationFailed(base.Select_multiple_associates_on_GroupBy_MaxBy);
67+
public override Task OrderBy_associate_scalar_property_on_GroupBy_First()
68+
=> AssertTranslationFailed(base.OrderBy_associate_scalar_property_on_GroupBy_First);
69+
70+
#endregion GroupBy element
71+
5472
[Fact]
5573
public virtual void Check_all_tests_overridden()
5674
=> TestHelpers.AssertAllMethodsOverridden(GetType());

‎test/EFCore.Cosmos.FunctionalTests/Query/Associations/OwnedNavigations/OwnedNavigationsMiscellaneousCosmosTest.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ FROM root c
4848
""");
4949
}
5050

51+
#region GroupBy element
52+
53+
public override Task Where_on_root_property_on_GroupBy_First()
54+
=> AssertTranslationFailed(base.Where_on_root_property_on_GroupBy_First);
55+
56+
public override Task Where_on_associate_scalar_property_on_GroupBy_First()
57+
=> AssertTranslationFailed(base.Where_on_associate_scalar_property_on_GroupBy_First);
58+
59+
public override Task Select_multiple_associates_on_GroupBy_OrderByDescending_First()
60+
=> AssertTranslationFailed(base.Select_multiple_associates_on_GroupBy_OrderByDescending_First);
61+
62+
public override Task Select_multiple_associates_on_GroupBy_MaxBy()
63+
=> AssertTranslationFailed(base.Select_multiple_associates_on_GroupBy_MaxBy);
64+
public override Task OrderBy_associate_scalar_property_on_GroupBy_First()
65+
=> AssertTranslationFailed(base.OrderBy_associate_scalar_property_on_GroupBy_First);
66+
67+
#endregion GroupBy element
68+
5169
[Fact]
5270
public virtual void Check_all_tests_overridden()
5371
=> TestHelpers.AssertAllMethodsOverridden(GetType());

‎test/EFCore.Specification.Tests/Query/Associations/AssociationsMiscellaneousTestBase.cs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,66 @@ public virtual Task Where_on_nested_associate_scalar_property()
2121
=> AssertQuery(ss => ss.Set<RootEntity>().Where(e => e.RequiredAssociate.RequiredNestedAssociate.Int == 8));
2222

2323
#endregion Simple filters
24+
25+
#region GroupBy element
26+
27+
[Fact]
28+
public virtual Task Where_on_root_property_on_GroupBy_First()
29+
=> AssertQuery(ss => ss.Set<RootEntity>()
30+
.GroupBy(e => e.Id % 2)
31+
.Select(g => g.OrderBy(e => e.Id).First(e => e.Id > 0))
32+
.Where(e => e.Id > 1));
33+
34+
[Fact]
35+
public virtual Task Where_on_associate_scalar_property_on_GroupBy_First()
36+
=> AssertQuery(ss => ss.Set<RootEntity>()
37+
.GroupBy(e => e.Id % 2)
38+
.Select(g => g.OrderBy(e => e.Id).First(e => e.Id > 0))
39+
.Where(e => e.RequiredAssociate.Int == 8));
40+
41+
[Fact]
42+
public virtual Task OrderBy_associate_scalar_property_on_GroupBy_First()
43+
=> AssertQuery(
44+
ss => ss.Set<RootEntity>()
45+
.GroupBy(e => e.Id % 2)
46+
.Select(g => g.OrderByDescending(e => e.Id).First())
47+
.OrderBy(e => e.RequiredAssociate.Int)
48+
.ThenBy(e => e.Id),
49+
assertOrder: true);
50+
51+
[Fact]
52+
public virtual Task Select_multiple_associates_on_GroupBy_OrderByDescending_First()
53+
=> AssertQuery(
54+
ss => ss.Set<RootEntity>()
55+
.GroupBy(e => e.Id % 2)
56+
.Select(g => g.OrderByDescending(e => e.Id).First())
57+
.OrderByDescending(e => e.RequiredAssociate.Int)
58+
.ThenBy(e => e.Id)
59+
.Select(e => new { e.Id, e.RequiredAssociate, e.RequiredAssociate.RequiredNestedAssociate }),
60+
assertOrder: true,
61+
elementAsserter: (e, a) =>
62+
{
63+
Assert.Equal(e.Id, a.Id);
64+
AssertEqual(e.RequiredAssociate, a.RequiredAssociate);
65+
AssertEqual(e.RequiredNestedAssociate, a.RequiredNestedAssociate);
66+
});
67+
68+
[Fact]
69+
public virtual Task Select_multiple_associates_on_GroupBy_MaxBy()
70+
=> AssertQuery(
71+
ss => ss.Set<RootEntity>()
72+
.GroupBy(e => e.Id % 2)
73+
.Select(g => g.MaxBy(e => e.Id)!)
74+
.OrderByDescending(e => e.RequiredAssociate.Int)
75+
.ThenBy(e => e.Id)
76+
.Select(e => new { e.Id, e.RequiredAssociate, e.RequiredAssociate.RequiredNestedAssociate }),
77+
assertOrder: true,
78+
elementAsserter: (e, a) =>
79+
{
80+
Assert.Equal(e.Id, a.Id);
81+
AssertEqual(e.RequiredAssociate, a.RequiredAssociate);
82+
AssertEqual(e.RequiredNestedAssociate, a.RequiredNestedAssociate);
83+
});
84+
85+
#endregion GroupBy element
2486
}

‎test/EFCore.Specification.Tests/Query/Associations/ComplexProperties/ComplexPropertiesMiscellaneousTestBase.cs‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,4 @@ public virtual Task Where_HasValue_on_nullable_value_type()
2424
=> AssertQuery(ss => ss.Set<ValueRootEntity>().Where(e => e.OptionalAssociate.HasValue));
2525

2626
#endregion Value types
27-
28-
#region Subquery
29-
30-
[Fact]
31-
public virtual Task Where_on_associate_scalar_property_on_GroupBy_First()
32-
=> AssertQuery(ss => ss.Set<RootEntity>()
33-
.GroupBy(e => e.Id % 2)
34-
.Select(g => g.OrderBy(e => e.Id).First(e => e.Id > 0))
35-
.Where(e => e.RequiredAssociate.Int == 8));
36-
37-
#endregion Subquery
3827
}

‎test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexJson/ComplexJsonMiscellaneousSqlServerTest.cs‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,158 @@ WHERE [r1].[Id] > 0
359359
}
360360
}
361361

362+
public override async Task Where_on_root_property_on_GroupBy_First()
363+
{
364+
await base.Where_on_root_property_on_GroupBy_First();
365+
366+
AssertSql(
367+
"""
368+
SELECT [r7].[Id], [r7].[Name], [r7].[c], [r7].[c0], [r7].[c1]
369+
FROM (
370+
SELECT [r0].[Key]
371+
FROM (
372+
SELECT [r].[Id] % 2 AS [Key]
373+
FROM [RootEntity] AS [r]
374+
) AS [r0]
375+
GROUP BY [r0].[Key]
376+
HAVING (
377+
SELECT TOP(1) [r3].[Id]
378+
FROM (
379+
SELECT [r4].[Id], [r4].[Id] % 2 AS [Key]
380+
FROM [RootEntity] AS [r4]
381+
) AS [r3]
382+
WHERE ([r0].[Key] = [r3].[Key] OR ([r0].[Key] IS NULL AND [r3].[Key] IS NULL)) AND [r3].[Id] > 0
383+
ORDER BY [r3].[Id]) > 1
384+
) AS [r5]
385+
LEFT JOIN (
386+
SELECT [r6].[Id], [r6].[Name], [r6].[c] AS [c], [r6].[c0] AS [c0], [r6].[c1] AS [c1], [r6].[Key]
387+
FROM (
388+
SELECT [r1].[Id], [r1].[Name], [r1].[c] AS [c], [r1].[c0] AS [c0], [r1].[c1] AS [c1], [r1].[Key], ROW_NUMBER() OVER(PARTITION BY [r1].[Key] ORDER BY [r1].[Id]) AS [row]
389+
FROM (
390+
SELECT [r2].[Id], [r2].[Name], [r2].[AssociateCollection] AS [c], [r2].[OptionalAssociate] AS [c0], [r2].[RequiredAssociate] AS [c1], [r2].[Id] % 2 AS [Key]
391+
FROM [RootEntity] AS [r2]
392+
) AS [r1]
393+
WHERE [r1].[Id] > 0
394+
) AS [r6]
395+
WHERE [r6].[row] <= 1
396+
) AS [r7] ON [r5].[Key] = [r7].[Key] OR ([r5].[Key] IS NULL AND [r7].[Key] IS NULL)
397+
""");
398+
}
399+
400+
public override async Task Select_multiple_associates_on_GroupBy_OrderByDescending_First()
401+
{
402+
await base.Select_multiple_associates_on_GroupBy_OrderByDescending_First();
403+
404+
if (Fixture.UsingJsonType)
405+
{
406+
AssertSql(
407+
"""
408+
SELECT [r1].[Id], [r1].[c1], JSON_QUERY([r1].[c1], '$.RequiredNestedAssociate')
409+
FROM (
410+
SELECT [r0].[Id], [r0].[c1] AS [c1], ROW_NUMBER() OVER(PARTITION BY [r0].[Key] ORDER BY [r0].[Id] DESC) AS [row]
411+
FROM (
412+
SELECT [r].[Id], [r].[RequiredAssociate] AS [c1], [r].[Id] % 2 AS [Key]
413+
FROM [RootEntity] AS [r]
414+
) AS [r0]
415+
) AS [r1]
416+
WHERE [r1].[row] <= 1
417+
ORDER BY JSON_VALUE([r1].[c1], '$.Int' RETURNING int) DESC, [r1].[Id]
418+
""");
419+
}
420+
else
421+
{
422+
AssertSql(
423+
"""
424+
SELECT [r1].[Id], [r1].[c1], JSON_QUERY([r1].[c1], '$.RequiredNestedAssociate')
425+
FROM (
426+
SELECT [r0].[Id], [r0].[c1] AS [c1], ROW_NUMBER() OVER(PARTITION BY [r0].[Key] ORDER BY [r0].[Id] DESC) AS [row]
427+
FROM (
428+
SELECT [r].[Id], [r].[RequiredAssociate] AS [c1], [r].[Id] % 2 AS [Key]
429+
FROM [RootEntity] AS [r]
430+
) AS [r0]
431+
) AS [r1]
432+
WHERE [r1].[row] <= 1
433+
ORDER BY CAST(JSON_VALUE([r1].[c1], '$.Int') AS int) DESC, [r1].[Id]
434+
""");
435+
}
436+
}
437+
438+
public override async Task Select_multiple_associates_on_GroupBy_MaxBy()
439+
{
440+
await base.Select_multiple_associates_on_GroupBy_MaxBy();
441+
442+
if (Fixture.UsingJsonType)
443+
{
444+
AssertSql(
445+
"""
446+
SELECT [r1].[Id], [r1].[c1], JSON_QUERY([r1].[c1], '$.RequiredNestedAssociate')
447+
FROM (
448+
SELECT [r0].[Id], [r0].[c1] AS [c1], ROW_NUMBER() OVER(PARTITION BY [r0].[Key] ORDER BY [r0].[Id] DESC) AS [row]
449+
FROM (
450+
SELECT [r].[Id], [r].[RequiredAssociate] AS [c1], [r].[Id] % 2 AS [Key]
451+
FROM [RootEntity] AS [r]
452+
) AS [r0]
453+
) AS [r1]
454+
WHERE [r1].[row] <= 1
455+
ORDER BY JSON_VALUE([r1].[c1], '$.Int' RETURNING int) DESC, [r1].[Id]
456+
""");
457+
}
458+
else
459+
{
460+
AssertSql(
461+
"""
462+
SELECT [r1].[Id], [r1].[c1], JSON_QUERY([r1].[c1], '$.RequiredNestedAssociate')
463+
FROM (
464+
SELECT [r0].[Id], [r0].[c1] AS [c1], ROW_NUMBER() OVER(PARTITION BY [r0].[Key] ORDER BY [r0].[Id] DESC) AS [row]
465+
FROM (
466+
SELECT [r].[Id], [r].[RequiredAssociate] AS [c1], [r].[Id] % 2 AS [Key]
467+
FROM [RootEntity] AS [r]
468+
) AS [r0]
469+
) AS [r1]
470+
WHERE [r1].[row] <= 1
471+
ORDER BY CAST(JSON_VALUE([r1].[c1], '$.Int') AS int) DESC, [r1].[Id]
472+
""");
473+
}
474+
}
475+
476+
public override async Task OrderBy_associate_scalar_property_on_GroupBy_First()
477+
{
478+
await base.OrderBy_associate_scalar_property_on_GroupBy_First();
479+
480+
if (Fixture.UsingJsonType)
481+
{
482+
AssertSql(
483+
"""
484+
SELECT [r1].[Id], [r1].[Name], [r1].[c], [r1].[c0], [r1].[c1]
485+
FROM (
486+
SELECT [r0].[Id], [r0].[Name], [r0].[c] AS [c], [r0].[c0] AS [c0], [r0].[c1] AS [c1], ROW_NUMBER() OVER(PARTITION BY [r0].[Key] ORDER BY [r0].[Id] DESC) AS [row]
487+
FROM (
488+
SELECT [r].[Id], [r].[Name], [r].[AssociateCollection] AS [c], [r].[OptionalAssociate] AS [c0], [r].[RequiredAssociate] AS [c1], [r].[Id] % 2 AS [Key]
489+
FROM [RootEntity] AS [r]
490+
) AS [r0]
491+
) AS [r1]
492+
WHERE [r1].[row] <= 1
493+
ORDER BY JSON_VALUE([r1].[c1], '$.Int' RETURNING int), [r1].[Id]
494+
""");
495+
}
496+
else
497+
{
498+
AssertSql(
499+
"""
500+
SELECT [r1].[Id], [r1].[Name], [r1].[c], [r1].[c0], [r1].[c1]
501+
FROM (
502+
SELECT [r0].[Id], [r0].[Name], [r0].[c] AS [c], [r0].[c0] AS [c0], [r0].[c1] AS [c1], ROW_NUMBER() OVER(PARTITION BY [r0].[Key] ORDER BY [r0].[Id] DESC) AS [row]
503+
FROM (
504+
SELECT [r].[Id], [r].[Name], [r].[AssociateCollection] AS [c], [r].[OptionalAssociate] AS [c0], [r].[RequiredAssociate] AS [c1], [r].[Id] % 2 AS [Key]
505+
FROM [RootEntity] AS [r]
506+
) AS [r0]
507+
) AS [r1]
508+
WHERE [r1].[row] <= 1
509+
ORDER BY CAST(JSON_VALUE([r1].[c1], '$.Int') AS int), [r1].[Id]
510+
""");
511+
}
512+
}
513+
362514
#endregion Subquery
363515

364516
[Fact]

0 commit comments

Comments
 (0)