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

Query: Skip constants in GROUP BY #17114

Merged
merged 3 commits into from
Aug 15, 2019
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 @@ -285,7 +285,10 @@ private void AppendGroupBy(Expression keySelector)
switch (keySelector)
{
case SqlExpression sqlExpression:
_groupBy.Add(sqlExpression);
if (!(sqlExpression is SqlConstantExpression))
{
_groupBy.Add(sqlExpression);
}
break;

case NewExpression newExpression:
Expand Down Expand Up @@ -1102,7 +1105,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)

var groupBy = _groupBy.ToList();
_groupBy.Clear();
_groupBy.AddRange(GroupBy.Select(e => (SqlExpression)visitor.Visit(e)));
_groupBy.AddRange(GroupBy.Select(e => (SqlExpression)visitor.Visit(e)).Where(e => !(e is SqlConstantExpression)));

Having = (SqlExpression)visitor.Visit(Having);

Expand Down
17 changes: 17 additions & 0 deletions test/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
Expand Down Expand Up @@ -262,6 +263,22 @@ public virtual Task GroupBy_Property_Select_key_multiple_times_and_aggregate(boo
e => e.Key1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_Property_Select_Key_with_constant(bool isAsync)
{
return AssertQuery<Order>(
isAsync,
os => os.GroupBy(o => new { Name = "CustomerID", Value = o.CustomerID }).Select(
g =>
new
{
g.Key,
Count = g.Count()
}),
e => e.Key.Value);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_aggregate_projecting_conditional_expression_based_on_group_key(bool isAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]");
}

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

AssertSql(
@"SELECT N'CustomerID' AS [Name], [o].[CustomerID] AS [Value], COUNT(*) AS [Count]
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]");
}

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