Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0.1] Query: Pushdown into subquery when applying group by over a group by #23297

Merged
merged 1 commit into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,23 @@ protected override ShapedQueryExpression TranslateGroupBy(
Check.NotNull(keySelector, nameof(keySelector));

var selectExpression = (SelectExpression)source.QueryExpression;
selectExpression.PrepareForAggregate();
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23265", out var isEnabled) && isEnabled)
{
selectExpression.PrepareForAggregate();
}
else
{
// This has it's own set of condition since it is different scenario from below.
// Aggregate operators need pushdown for skip/limit/offset covered by selectExpression.PrepareForAggregate.
// Aggregate operators need special processing beyond pushdown when applying over group by for client eval.
if (selectExpression.Limit != null
|| selectExpression.Offset != null
|| selectExpression.IsDistinct
|| selectExpression.GroupBy.Count > 0)
{
selectExpression.PushdownIntoSubquery();
}
}

var remappedKeySelector = RemapLambdaBody(source, keySelector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1288,9 +1288,10 @@ public virtual Task Element_selector_with_case_block_repeated_inside_another_cas
async,
ss => from order in ss.Set<Order>()
group new
{
IsAlfki = order.CustomerID == "ALFKI", OrderId = order.OrderID > 1000 ? order.OrderID : -order.OrderID
} by
{
IsAlfki = order.CustomerID == "ALFKI",
OrderId = order.OrderID > 1000 ? order.OrderID : -order.OrderID
} by
new { order.OrderID }
into g
select new { g.Key.OrderID, Aggregate = g.Sum(s => s.IsAlfki ? s.OrderId : -s.OrderId) });
Expand Down Expand Up @@ -2326,6 +2327,20 @@ public virtual Task GroupBy_aggregate_join_another_GroupBy_aggregate(bool async)
elementSorter: o => o.Key);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_aggregate_followed_another_GroupBy_aggregate(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Order>()
.GroupBy(o => new { o.CustomerID, o.OrderDate.Value.Year })
.Select(g => new { g.Key.CustomerID, g.Key.Year })
.GroupBy(e => e.CustomerID)
.Select(g => new { g.Key, Count = g.Count() }),
elementSorter: o => o.Key);
}

#endregion

#region GroupByAggregateChainComposition
Expand Down Expand Up @@ -2523,7 +2538,8 @@ public virtual Task GroupBy_group_Distinct_Select_Distinct_aggregate(bool async)
g =>
new
{
g.Key, Max = g.Distinct().Select(e => e.OrderDate).Distinct().Max(),
g.Key,
Max = g.Distinct().Select(e => e.OrderDate).Distinct().Max(),
}),
elementSorter: e => e.Key);
}
Expand All @@ -2540,7 +2556,8 @@ public virtual Task GroupBy_group_Where_Select_Distinct_aggregate(bool async)
g =>
new
{
g.Key, Max = g.Where(e => e.OrderDate.HasValue).Select(e => e.OrderDate).Distinct().Max(),
g.Key,
Max = g.Where(e => e.OrderDate.HasValue).Select(e => e.OrderDate).Distinct().Max(),
}),
elementSorter: e => e.Key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,20 @@ GROUP BY [o0].[CustomerID]
) AS [t0] ON [t].[CustomerID] = [t0].[CustomerID]");
}

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

AssertSql(
@"SELECT [t].[CustomerID] AS [Key], COUNT(*) AS [Count]
FROM (
SELECT [o].[CustomerID]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID], DATEPART(year, [o].[OrderDate])
) AS [t]
GROUP BY [t].[CustomerID]");
}

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