Skip to content

Commit

Permalink
Relational: Translate SelectMany
Browse files Browse the repository at this point in the history
SelectMany in linq look something like following (second clause is considered the collection selector)
```C#
from c in cs
from o in c.Orders
select....
```
Following are the translations of SelectMany

Unrelated collection selector
```C#
from c in cs
from o in os
```
Generates CROSS JOIN

Correlated collection selector with correlation being a predicate
```C#
from c in cs
from o in os.Where(o => o.CustomerID == c.CustomerID)
```
Such predicate can be lifted and used in generating a join. So this query generates JOIN.
If collection selector ends with DefaultIfEmpty then it is LEFT JOIN.

Correlated collection selector with correlation not a predicate
```C#
from c in cs
from o in os.Select(o => c.City)
```
Since we cannot generate a join predicate here, this translates to CROSS APPLY.
If collection selector ends with DefaultIfEmpty then it is OUTER APPLY.

- Add support for Cross Apply
- Add Support for Outer Apply
- Convert Cross Apply to Inner Join when possible
- Convert Outer Apply to Left Join when possible
- Add translation for DefaultIfEmpty
- Add translation for both overloads of SelectMany
- Handle DefaultIfEmpty & SelectMany without collectionSelector in Navigation Expansion
- Ensure columns are in projection when generating join predicate from a correlated subquery

Currently there are no tests for Cross/Outer Apply.
Our earlier Cross Apply got converted to join. N+1 evaluation tests are disabled right now, which would generate Cross Apply.
We never supported Outer Apply in past.

Resolves #15711
Resolves #12567
Resolves #12572
Resolves #12872
Resolves #16330
Resolves #15081
Resolves #16989

Re-enable tests for #12449
  • Loading branch information
smitpatel committed Aug 13, 2019
1 parent 449be3d commit d11f607
Show file tree
Hide file tree
Showing 46 changed files with 1,329 additions and 931 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ protected override Expression VisitUnary(UnaryExpression unaryExpression)

[DebuggerStepThrough]
private bool TranslationFailed(Expression original, Expression translation)
=> original != null && translation is EntityProjectionExpression;
=> original != null && (translation == null || translation is EntityProjectionExpression);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public InMemoryQueryableMethodTranslatingExpressionVisitor(
_model = model;
}

private static Type CreateTransparentIdentifierType(Type outerType, Type innerType)
=> typeof(TransparentIdentifier<,>).MakeGenericType(outerType, innerType);

public override ShapedQueryExpression TranslateSubquery(Expression expression)
{
return (ShapedQueryExpression)new InMemoryQueryableMethodTranslatingExpressionVisitor(
Expand Down Expand Up @@ -217,8 +214,7 @@ protected override ShapedQueryExpression TranslateJoin(ShapedQueryExpression out
outer,
resultSelector,
inner.ShaperExpression,
transparentIdentifierType,
false);
transparentIdentifierType);
}

protected override ShapedQueryExpression TranslateLastOrDefault(ShapedQueryExpression source, LambdaExpression predicate, Type returnType, bool returnDefault)
Expand Down Expand Up @@ -250,9 +246,8 @@ protected override ShapedQueryExpression TranslateLeftJoin(ShapedQueryExpression
return TranslateResultSelectorForJoin(
outer,
resultSelector,
inner.ShaperExpression,
transparentIdentifierType,
true);
MarkShaperNullable(inner.ShaperExpression),
transparentIdentifierType);
}

protected override ShapedQueryExpression TranslateLongCount(ShapedQueryExpression source, LambdaExpression predicate)
Expand Down Expand Up @@ -360,8 +355,7 @@ protected override ShapedQueryExpression TranslateSelectMany(
source,
resultSelector,
inner.ShaperExpression,
transparentIdentifierType,
false);
transparentIdentifierType);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,22 @@ protected override Expression VisitCrossJoin(CrossJoinExpression crossJoinExpres
return crossJoinExpression;
}

protected override Expression VisitCrossApply(CrossApplyExpression crossApplyExpression)
{
_relationalCommandBuilder.Append("CROSS APPLY ");
Visit(crossApplyExpression.Table);

return crossApplyExpression;
}

protected override Expression VisitOuterApply(OuterApplyExpression outerApplyExpression)
{
_relationalCommandBuilder.Append("OUTER APPLY ");
Visit(outerApplyExpression.Table);

return outerApplyExpression;
}

protected override Expression VisitInnerJoin(InnerJoinExpression innerJoinExpression)
{
_relationalCommandBuilder.Append("INNER JOIN ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,18 @@ protected override ShapedQueryExpression TranslateCount(ShapedQueryExpression so
return source;
}

protected override ShapedQueryExpression TranslateDefaultIfEmpty(ShapedQueryExpression source, Expression defaultValue) => throw new NotImplementedException();
protected override ShapedQueryExpression TranslateDefaultIfEmpty(ShapedQueryExpression source, Expression defaultValue)
{
if (defaultValue == null)
{
((SelectExpression)source.QueryExpression).ApplyDefaultIfEmpty(_sqlExpressionFactory);
source.ShaperExpression = MarkShaperNullable(source.ShaperExpression);

return source;
}

throw new NotImplementedException();
}

protected override ShapedQueryExpression TranslateDistinct(ShapedQueryExpression source)
{
Expand Down Expand Up @@ -459,8 +470,7 @@ protected override ShapedQueryExpression TranslateJoin(
outer,
resultSelector,
inner.ShaperExpression,
transparentIdentifierType,
false);
transparentIdentifierType);
}

throw new NotImplementedException();
Expand All @@ -481,9 +491,8 @@ protected override ShapedQueryExpression TranslateLeftJoin(ShapedQueryExpression
return TranslateResultSelectorForJoin(
outer,
resultSelector,
inner.ShaperExpression,
transparentIdentifierType,
true);
MarkShaperNullable(inner.ShaperExpression),
transparentIdentifierType);
}

throw new NotImplementedException();
Expand Down Expand Up @@ -710,33 +719,57 @@ protected override ShapedQueryExpression TranslateSelect(ShapedQueryExpression s
return source;
}

private static readonly MethodInfo _defaultIfEmptyWithoutArgMethodInfo = typeof(Enumerable).GetTypeInfo()
.GetDeclaredMethods(nameof(Enumerable.DefaultIfEmpty)).Single(mi => mi.GetParameters().Length == 1);

protected override ShapedQueryExpression TranslateSelectMany(
ShapedQueryExpression source, LambdaExpression collectionSelector, LambdaExpression resultSelector)
{
var collectionSelectorBody = collectionSelector.Body;
//var defaultIfEmpty = false;

if (collectionSelectorBody is MethodCallExpression collectionEndingMethod
var defaultIfEmpty = false;
if (collectionSelector.Body is MethodCallExpression collectionEndingMethod
&& collectionEndingMethod.Method.IsGenericMethod
&& collectionEndingMethod.Method.GetGenericMethodDefinition() == _defaultIfEmptyWithoutArgMethodInfo)
&& collectionEndingMethod.Method.GetGenericMethodDefinition() == QueryableMethodProvider.DefaultIfEmptyWithoutArgumentMethodInfo)
{
//defaultIfEmpty = true;
collectionSelectorBody = collectionEndingMethod.Arguments[0];
defaultIfEmpty = true;
collectionSelector = Expression.Lambda(collectionEndingMethod.Arguments[0], collectionSelector.Parameters);
}

var correlated = new CorrelationFindingExpressionVisitor().IsCorrelated(collectionSelectorBody, collectionSelector.Parameters[0]);
var correlated = new CorrelationFindingExpressionVisitor().IsCorrelated(collectionSelector);
if (correlated)
{
// TODO visit inner with outer parameter;
throw new NotImplementedException();
var collectionSelectorBody = RemapLambdaBody(source, collectionSelector);
if (Visit(collectionSelectorBody) is ShapedQueryExpression inner)
{
var transparentIdentifierType = TransparentIdentifierFactory.Create(
resultSelector.Parameters[0].Type,
resultSelector.Parameters[1].Type);

var innerShaperExpression = inner.ShaperExpression;
if (defaultIfEmpty)
{
((SelectExpression)source.QueryExpression).AddOuterApply(
(SelectExpression)inner.QueryExpression, transparentIdentifierType);
innerShaperExpression = MarkShaperNullable(innerShaperExpression);
}
else
{
((SelectExpression)source.QueryExpression).AddCrossApply(
(SelectExpression)inner.QueryExpression, transparentIdentifierType);
}

return TranslateResultSelectorForJoin(
source,
resultSelector,
innerShaperExpression,
transparentIdentifierType);
}
}
else
{
if (Visit(collectionSelectorBody) is ShapedQueryExpression inner)
if (Visit(collectionSelector.Body) is ShapedQueryExpression inner)
{
if (defaultIfEmpty)
{
inner = TranslateDefaultIfEmpty(inner, null);
}

var transparentIdentifierType = TransparentIdentifierFactory.Create(
resultSelector.Parameters[0].Type,
resultSelector.Parameters[1].Type);
Expand All @@ -748,8 +781,7 @@ protected override ShapedQueryExpression TranslateSelectMany(
source,
resultSelector,
inner.ShaperExpression,
transparentIdentifierType,
false);
transparentIdentifierType);
}
}

Expand All @@ -760,12 +792,14 @@ private class CorrelationFindingExpressionVisitor : ExpressionVisitor
{
private ParameterExpression _outerParameter;
private bool _isCorrelated;
public bool IsCorrelated(Expression tree, ParameterExpression outerParameter)

public bool IsCorrelated(LambdaExpression lambdaExpression)
{
Debug.Assert(lambdaExpression.Parameters.Count == 1, "Multiparameter lambda passed to CorrelationFindingExpressionVisitor");
_isCorrelated = false;
_outerParameter = outerParameter;
_outerParameter = lambdaExpression.Parameters[0];

Visit(tree);
Visit(lambdaExpression.Body);

return _isCorrelated;
}
Expand All @@ -783,7 +817,16 @@ protected override Expression VisitParameter(ParameterExpression parameterExpres

protected override ShapedQueryExpression TranslateSelectMany(ShapedQueryExpression source, LambdaExpression selector)
{
throw new NotImplementedException();
var innerParameter = Expression.Parameter(selector.ReturnType.TryGetSequenceType(), "i");
var resultSelector = Expression.Lambda(
innerParameter,
new[]
{
Expression.Parameter(source.Type.TryGetSequenceType()),
innerParameter
});

return TranslateSelectMany(source, selector, resultSelector);
}

protected override ShapedQueryExpression TranslateSingleOrDefault(ShapedQueryExpression source, LambdaExpression predicate, Type returnType, bool returnDefault)
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore.Relational/Query/SqlExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ protected override Expression VisitExtension(Expression extensionExpression)
case CrossJoinExpression crossJoinExpression:
return VisitCrossJoin(crossJoinExpression);

case CrossApplyExpression crossApplyExpression:
return VisitCrossApply(crossApplyExpression);

case OuterApplyExpression outerApplyExpression:
return VisitOuterApply(outerApplyExpression);

case ExistsExpression existsExpression:
return VisitExists(existsExpression);

Expand Down Expand Up @@ -80,6 +86,8 @@ protected override Expression VisitExtension(Expression extensionExpression)
protected abstract Expression VisitExists(ExistsExpression existsExpression);
protected abstract Expression VisitIn(InExpression inExpression);
protected abstract Expression VisitCrossJoin(CrossJoinExpression crossJoinExpression);
protected abstract Expression VisitCrossApply(CrossApplyExpression crossApplyExpression);
protected abstract Expression VisitOuterApply(OuterApplyExpression outerApplyExpression);
protected abstract Expression VisitFromSql(FromSqlExpression fromSqlExpression);
protected abstract Expression VisitInnerJoin(InnerJoinExpression innerJoinExpression);
protected abstract Expression VisitLeftJoin(LeftJoinExpression leftJoinExpression);
Expand Down
41 changes: 41 additions & 0 deletions src/EFCore.Relational/Query/SqlExpressions/CrossApplyExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;

namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions
{
public class CrossApplyExpression : JoinExpressionBase
{
public CrossApplyExpression(TableExpressionBase table)
: base(table)
{
}

protected override Expression VisitChildren(ExpressionVisitor visitor)
=> Update((TableExpressionBase)visitor.Visit(Table));

public virtual CrossApplyExpression Update(TableExpressionBase table)
=> table != Table
? new CrossApplyExpression(table)
: this;

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.Append("CROSS APPLY ");
expressionPrinter.Visit(Table);
}

public override bool Equals(object obj)
=> obj != null
&& (ReferenceEquals(this, obj)
|| obj is CrossApplyExpression crossApplyExpression
&& Equals(crossApplyExpression));

private bool Equals(CrossApplyExpression crossApplyExpression)
=> base.Equals(crossApplyExpression);

public override int GetHashCode() => base.GetHashCode();
}
}
40 changes: 40 additions & 0 deletions src/EFCore.Relational/Query/SqlExpressions/OuterApplyExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq.Expressions;

namespace Microsoft.EntityFrameworkCore.Query.SqlExpressions
{
public class OuterApplyExpression : JoinExpressionBase
{
public OuterApplyExpression(TableExpressionBase table)
: base(table)
{
}

protected override Expression VisitChildren(ExpressionVisitor visitor)
=> Update((TableExpressionBase)visitor.Visit(Table));

public virtual OuterApplyExpression Update(TableExpressionBase table)
=> table != Table
? new OuterApplyExpression(table)
: this;

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.Append("OUTER APPLY ");
expressionPrinter.Visit(Table);
}

public override bool Equals(object obj)
=> obj != null
&& (ReferenceEquals(this, obj)
|| obj is OuterApplyExpression outerApplyExpression
&& Equals(outerApplyExpression));

private bool Equals(OuterApplyExpression outerApplyExpression)
=> base.Equals(outerApplyExpression);

public override int GetHashCode() => base.GetHashCode();
}
}
Loading

0 comments on commit d11f607

Please sign in to comment.