Skip to content

Commit

Permalink
[release/7.0] Fix split query over GroupBy over parameter (#30024) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Jan 12, 2023
1 parent be8c0ef commit 895be6c
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,9 @@
<data name="NoDbCommand" xml:space="preserve">
<value>Cannot create a DbCommand for a non-relational query.</value>
</data>
<data name="NonConstantOrParameterAsInExpressionValues" xml:space="preserve">
<value>Expression of type '{type}' isn't supported as the Values of an InExpression; only constants and parameters are supported.</value>
</data>
<data name="NoneRelationalTypeMappingOnARelationalTypeMappingSource" xml:space="preserve">
<value>'FindMapping' was called on a 'RelationalTypeMappingSource' with a non-relational 'TypeMappingInfo'.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ private sealed class SqlRemappingVisitor : ExpressionVisitor
private readonly HashSet<SqlExpression> _correlatedTerms;
private bool _groupByDiscovery;

private static readonly bool QuirkEnabled30022
= AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue30022", out var enabled) && enabled;

public SqlRemappingVisitor(
Dictionary<SqlExpression, ColumnExpression> mappings,
SelectExpression subquery,
Expand Down Expand Up @@ -210,7 +213,9 @@ when _groupByDiscovery

case SqlExpression sqlExpression
when !_groupByDiscovery
&& sqlExpression is not SqlConstantExpression or SqlParameterExpression
&& (QuirkEnabled30022
? sqlExpression is not SqlConstantExpression or SqlParameterExpression
: sqlExpression is not SqlConstantExpression and not SqlParameterExpression)
&& _correlatedTerms.Contains(sqlExpression):
var outerColumn = _subquery.GenerateOuterColumn(_tableReferenceExpression, sqlExpression);
_mappings[sqlExpression] = outerColumn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,26 @@ public virtual Task Skip_Take_on_grouping_element_inside_collection_projection(b
});
});

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Collection_projection_over_GroupBy_over_parameter(bool async)
{
var validIds = new List<string> { "L1 01", "L1 02" };

return AssertQuery(
async,
ss => ss.Set<Level1>()
.Where(l1 => validIds.Contains(l1.Name))
.GroupBy(l => l.Date)
.Select(g => new { g.Key, Ids = g.Select(e => e.Id) }),
elementSorter: e => e.Key,
elementAsserter: (e, a) =>
{
AssertEqual(e.Key, a.Key);
AssertCollection(e.Ids, a.Ids);
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task SelectMany_over_conditional_null_source(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,28 @@ FROM [LevelOne] AS [l1]
""");
}

public override async Task Collection_projection_over_GroupBy_over_parameter(bool async)
{
await base.Collection_projection_over_GroupBy_over_parameter(async);

AssertSql(
"""
SELECT [t].[Date], [t0].[Id]
FROM (
SELECT [l].[Date]
FROM [LevelOne] AS [l]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
GROUP BY [l].[Date]
) AS [t]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date]
FROM [LevelOne] AS [l0]
WHERE [l0].[Name] IN (N'L1 01', N'L1 02')
) AS [t0] ON [t].[Date] = [t0].[Date]
ORDER BY [t].[Date]
""");
}

public override async Task Include_partially_added_before_Where_and_then_build_upon(bool async)
{
await base.Include_partially_added_before_Where_and_then_build_upon(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3003,6 +3003,28 @@ FROM [Level1] AS [l1]
""");
}

public override async Task Collection_projection_over_GroupBy_over_parameter(bool async)
{
await base.Collection_projection_over_GroupBy_over_parameter(async);

AssertSql(
"""
SELECT [t].[Date], [t0].[Id]
FROM (
SELECT [l].[Date]
FROM [Level1] AS [l]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
GROUP BY [l].[Date]
) AS [t]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date]
FROM [Level1] AS [l0]
WHERE [l0].[Name] IN (N'L1 01', N'L1 02')
) AS [t0] ON [t].[Date] = [t0].[Date]
ORDER BY [t].[Date]
""");
}

public override async Task Filtered_include_is_considered_loaded(bool async)
{
await base.Filtered_include_is_considered_loaded(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3728,6 +3728,36 @@ FROM [LevelOne] AS [l1]
""");
}

public override async Task Collection_projection_over_GroupBy_over_parameter(bool async)
{
await base.Collection_projection_over_GroupBy_over_parameter(async);

AssertSql(
"""
SELECT [l].[Date]
FROM [LevelOne] AS [l]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
GROUP BY [l].[Date]
ORDER BY [l].[Date]
""",
//
"""
SELECT [t0].[Id], [t].[Date]
FROM (
SELECT [l].[Date]
FROM [LevelOne] AS [l]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
GROUP BY [l].[Date]
) AS [t]
INNER JOIN (
SELECT [l0].[Id], [l0].[Date]
FROM [LevelOne] AS [l0]
WHERE [l0].[Name] IN (N'L1 01', N'L1 02')
) AS [t0] ON [t].[Date] = [t0].[Date]
ORDER BY [t].[Date]
""");
}

public override async Task Include_partially_added_before_Where_and_then_build_upon(bool async)
{
await base.Include_partially_added_before_Where_and_then_build_upon(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,28 @@ LEFT JOIN (
""");
}

public override async Task Collection_projection_over_GroupBy_over_parameter(bool async)
{
await base.Collection_projection_over_GroupBy_over_parameter(async);

AssertSql(
"""
SELECT [t].[Date], [t0].[Id]
FROM (
SELECT [l].[Date]
FROM [LevelOne] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [l]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
GROUP BY [l].[Date]
) AS [t]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date]
FROM [LevelOne] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [l0]
WHERE [l0].[Name] IN (N'L1 01', N'L1 02')
) AS [t0] ON [t].[Date] = [t0].[Date]
ORDER BY [t].[Date]
""");
}

public override async Task Multiple_SelectMany_navigation_property_followed_by_select_collection_navigation(bool async)
{
await base.Multiple_SelectMany_navigation_property_followed_by_select_collection_navigation(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,28 @@ LEFT JOIN (
""");
}

public override async Task Collection_projection_over_GroupBy_over_parameter(bool async)
{
await base.Collection_projection_over_GroupBy_over_parameter(async);

AssertSql(
"""
SELECT [t].[Date], [t0].[Id]
FROM (
SELECT [l].[Date]
FROM [Level1] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [l]
WHERE [l].[Name] IN (N'L1 01', N'L1 02')
GROUP BY [l].[Date]
) AS [t]
LEFT JOIN (
SELECT [l0].[Id], [l0].[Date]
FROM [Level1] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [l0]
WHERE [l0].[Name] IN (N'L1 01', N'L1 02')
) AS [t0] ON [t].[Date] = [t0].[Date]
ORDER BY [t].[Date]
""");
}

public override async Task Skip_Take_on_grouping_element_into_non_entity(bool async)
{
await base.Skip_Take_on_grouping_element_into_non_entity(async);
Expand Down

0 comments on commit 895be6c

Please sign in to comment.