Skip to content

Commit

Permalink
Query: Appropriately save bindings when grouping key is also part of …
Browse files Browse the repository at this point in the history
…Element selector

Resolves #11999
  • Loading branch information
smitpatel committed May 14, 2018
1 parent 124452f commit b15fd3c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
31 changes: 10 additions & 21 deletions src/EFCore.Relational/Query/Expressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,12 @@ public virtual SelectExpression PushDownSubquery()

var outerProjection = expressionToAdd.LiftExpressionFromSubquery(subquery);

var memberInfo = _memberInfoProjectionMapping.FirstOrDefault(
kvp => ExpressionEqualityComparer.Instance.Equals(kvp.Value, expression))
.Key;

if (memberInfo != null)
foreach (var memberInfoMapping
in _memberInfoProjectionMapping.Where(
kvp => ExpressionEqualityComparer.Instance.Equals(kvp.Value, expression))
.ToList())
{
_memberInfoProjectionMapping[memberInfo] = outerProjection;
_memberInfoProjectionMapping[memberInfoMapping.Key] = outerProjection;
}

outerProjections.Add(outerProjection);
Expand Down Expand Up @@ -945,15 +944,6 @@ public virtual void SetProjectionForMemberInfo([NotNull] MemberInfo memberInfo,
Check.NotNull(memberInfo, nameof(memberInfo));
Check.NotNull(projection, nameof(projection));

var existingMemberInfo = _memberInfoProjectionMapping.FirstOrDefault(
kvp => ExpressionEqualityComparer.Instance.Equals(kvp.Value, projection))
.Key;

if (existingMemberInfo != null)
{
_memberInfoProjectionMapping.Remove(existingMemberInfo);
}

_memberInfoProjectionMapping[memberInfo] = CreateUniqueProjection(projection, memberInfo.Name);
}

Expand Down Expand Up @@ -1349,15 +1339,14 @@ private void UpdateProjection(ExpressionVisitor visitor, List<Expression> projec
_orderBy.Insert(currentOrderingIndex, new Ordering(projection[i], oldOrdering.OrderingDirection));
}

var memberInfo = _memberInfoProjectionMapping.FirstOrDefault(
kvp => ExpressionEqualityComparer.Instance.Equals(kvp.Value, oldProjection))
.Key;
if (memberInfo != null)
foreach (var memberInfoMapping
in _memberInfoProjectionMapping.Where(
kvp => ExpressionEqualityComparer.Instance.Equals(kvp.Value, oldProjection))
.ToList())
{
_memberInfoProjectionMapping[memberInfo] = projection[i];
_memberInfoProjectionMapping[memberInfoMapping.Key] = projection[i];
}
}

}

private class SqlTableReferenceReplacingVisitor : ExpressionVisitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,32 @@ await AssertQueryScalar<Order>(
select g.Where(e => e.OrderID < 10300).Count());
}

[ConditionalFact]
public virtual async Task GroupBy_Key_as_part_of_element_selector()
{
await AssertQuery<Order>(
os => os.GroupBy(o => o.OrderID, o => new { o.OrderID, o.OrderDate })
.Select(g => new
{
g.Key,
Avg = g.Average(e => e.OrderID),
Max = g.Max(o => o.OrderDate)
}));
}

[ConditionalFact]
public virtual async Task GroupBy_composite_Key_as_part_of_element_selector()
{
await AssertQuery<Order>(
os => os.GroupBy(o => new { o.OrderID, o.CustomerID }, o => new { o.OrderID, o.OrderDate })
.Select(g => new
{
g.Key,
Avg = g.Average(e => e.OrderID),
Max = g.Max(o => o.OrderDate)
}));
}

#endregion

#region GroupByWithoutAggregate
Expand Down Expand Up @@ -1908,6 +1934,5 @@ public virtual async Task Double_GroupBy_with_aggregate()
}

#endregion

}
}
26 changes: 26 additions & 0 deletions src/EFCore.Specification.Tests/Query/GroupByQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,32 @@ public virtual void GroupBy_Where_in_aggregate()
select g.Where(e => e.OrderID < 10300).Count());
}

[ConditionalFact]
public virtual void GroupBy_Key_as_part_of_element_selector()
{
AssertQuery<Order>(
os => os.GroupBy(o => o.OrderID, o => new { o.OrderID, o.OrderDate })
.Select(g => new
{
g.Key,
Avg = g.Average(e => e.OrderID),
Max = g.Max(o => o.OrderDate)
}));
}

[ConditionalFact]
public virtual void GroupBy_composite_Key_as_part_of_element_selector()
{
AssertQuery<Order>(
os => os.GroupBy(o => new { o.OrderID, o.CustomerID }, o => new { o.OrderID, o.OrderDate })
.Select(g => new
{
g.Key,
Avg = g.Average(e => e.OrderID),
Max = g.Max(o => o.OrderDate)
}));
}

#endregion

#region GroupByWithoutAggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,26 @@ FROM [Orders] AS [o]
ORDER BY [o].[CustomerID]");
}

public override void GroupBy_Key_as_part_of_element_selector()
{
base.GroupBy_Key_as_part_of_element_selector();

AssertSql(
@"SELECT [o].[OrderID] AS [Key], AVG(CAST([o].[OrderID] AS float)) AS [Avg], MAX([o].[OrderDate]) AS [Max]
FROM [Orders] AS [o]
GROUP BY [o].[OrderID]");
}

public override void GroupBy_composite_Key_as_part_of_element_selector()
{
base.GroupBy_composite_Key_as_part_of_element_selector();

AssertSql(
@"SELECT [o].[OrderID], [o].[CustomerID], AVG(CAST([o].[OrderID] AS float)) AS [Avg], MAX([o].[OrderDate]) AS [Max]
FROM [Orders] AS [o]
GROUP BY [o].[OrderID], [o].[CustomerID]");
}

public override void GroupBy_anonymous()
{
base.GroupBy_anonymous();
Expand Down

0 comments on commit b15fd3c

Please sign in to comment.