Skip to content

Commit

Permalink
Query: Pushdown into subquery when applying group by over a group by
Browse files Browse the repository at this point in the history
Resolves #23265

Everyone wants their own set of conditions for pushdown. ¯\_(ツ)_/¯
  • Loading branch information
smitpatel committed Nov 17, 2020
1 parent d185ae4 commit 09c6f31
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
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

0 comments on commit 09c6f31

Please sign in to comment.