Skip to content

Commit

Permalink
Allow set operations over other mapping types
Browse files Browse the repository at this point in the history
Including different mapping types on the two sides, as long as the
result is compatible.

Fixes #16725
  • Loading branch information
roji committed Jul 29, 2019
1 parent 057d15e commit 1956bd4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,19 @@ public Expression ApplySetOperation(
continue;
}

if (joinedMapping.Value1 is ColumnExpression && joinedMapping.Value2 is ColumnExpression
|| joinedMapping.Value1 is ScalarSubqueryExpression && joinedMapping.Value2 is ScalarSubqueryExpression)
if (joinedMapping.Value1 is SqlExpression innerColumn1
&& joinedMapping.Value2 is SqlExpression innerColumn2)
{
handleColumnMapping(
joinedMapping.Key,
select1, (SqlExpression)joinedMapping.Value1,
select2, (SqlExpression)joinedMapping.Value2);
// The actual columns may actually be different, but we don't care as long as the type and alias
// coming out of the two operands are the same
var alias = joinedMapping.Key.Last?.Name;
select1.AddToProjection(innerColumn1, alias);
select2.AddToProjection(innerColumn2, alias);
_projectionMapping[joinedMapping.Key] = innerColumn1;
continue;
}

throw new InvalidOperationException("Non-matching or unknown projection mapping type in set operation");
throw new InvalidOperationException($"Non-matching or unknown projection mapping type in set operation ({joinedMapping.Value1.GetType().Name} and {joinedMapping.Value2.GetType().Name})");
}
}

Expand Down Expand Up @@ -520,19 +522,6 @@ ColumnExpression addSetOperationColumnProjections(

return column1;
}

void handleColumnMapping(
ProjectionMember projectionMember,
SelectExpression select1, SqlExpression innerColumn1,
SelectExpression select2, SqlExpression innerColumn2)
{
// The actual columns may actually be different, but we don't care as long as the type and alias
// coming out of the two operands are the same
var alias = projectionMember.Last?.Name;
select1.AddToProjection(innerColumn1, alias);
select2.AddToProjection(innerColumn2, alias);
_projectionMapping[projectionMember] = innerColumn1;
}
}

public IDictionary<SqlExpression, ColumnExpression> PushdownIntoSubquery()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ public override void Include_Union_only_on_one_side_throws() {}
public override void Include_Union_different_includes_throws() {}
public override Task SubSelect_Union(bool isAsync) => Task.CompletedTask;
public override Task Client_eval_Union_FirstOrDefault(bool isAsync) => Task.CompletedTask;
public override Task GroupBy_Select_Union(bool isAsync) => Task.CompletedTask;
public override Task Union_over_different_projection_types(bool isAsync) => Task.CompletedTask;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ public override Task Select_Except_reference_projection(bool isAsync)
return Task.CompletedTask; //base.Select_Except_reference_projection(isAsync);
}

public override Task GroupBy_Select_Union(bool isAsync)
{
return Task.CompletedTask; //base.GroupBy_Select_Union(isAsync);
}

public override Task Union_over_different_projection_types(bool isAsync)
{
return Task.CompletedTask; //base.Union_over_different_projection_types(isAsync);
}

#endregion

[ConditionalFact(Skip = "Issue#16564")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,28 @@ public virtual Task Client_eval_Union_FirstOrDefault(bool isAsync)
.Union(cs));

private static Customer ClientSideMethod(Customer c) => c;

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_Select_Union(bool isAsync)
=> AssertQuery<Customer>(isAsync, cs => cs
.Where(c => c.City == "Berlin")
.GroupBy(c => c.CustomerID)
.Select(g => new { CustomerID = g.Key, Count = g.Count() })
.Union(cs
.Where(c => c.City == "London")
.GroupBy(c => c.CustomerID)
.Select(g => new { CustomerID = g.Key, Count = g.Count() })));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Union_over_different_projection_types(bool isAsync)
=> AssertQuery<Customer>(isAsync, cs => cs
.Where(c => c.City == "Berlin")
.GroupBy(c => c.CustomerID)
.Select(g => new { CustomerID = g.Key, Count = g.Count() })
.Union(cs
.Where(c => c.City == "London")
.Select(c => new { c.CustomerID, Count = c.ContactName.Length })));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,36 @@ FROM [Orders] AS [o0]
WHERE ([c0].[CustomerID] = [o0].[CustomerID]) AND [o0].[CustomerID] IS NOT NULL) AS [Orders]
FROM [Customers] AS [c0]");
}

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

AssertSql(
@"SELECT [c].[CustomerID], COUNT(*) AS [Count]
FROM [Customers] AS [c]
WHERE ([c].[City] = N'Berlin') AND [c].[City] IS NOT NULL
GROUP BY [c].[CustomerID]
UNION
SELECT [c0].[CustomerID], COUNT(*) AS [Count]
FROM [Customers] AS [c0]
WHERE ([c0].[City] = N'London') AND [c0].[City] IS NOT NULL
GROUP BY [c0].[CustomerID]");
}

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

AssertSql(
@"SELECT [c].[CustomerID], COUNT(*) AS [Count]
FROM [Customers] AS [c]
WHERE ([c].[City] = N'Berlin') AND [c].[City] IS NOT NULL
GROUP BY [c].[CustomerID]
UNION
SELECT [c0].[CustomerID], CAST(LEN([c0].[ContactName]) AS int) AS [Count]
FROM [Customers] AS [c0]
WHERE ([c0].[City] = N'London') AND [c0].[City] IS NOT NULL");
}
}
}

0 comments on commit 1956bd4

Please sign in to comment.