Skip to content

Relational: Translate SelectMany #17077

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

Merged
merged 2 commits into from
Aug 13, 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 @@ -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 VisitInnerJoinLateral(InnerJoinLateralExpression innerJoinLateralExpression)
{
_relationalCommandBuilder.Append("INNER JOIN LATERAL ");
Visit(innerJoinLateralExpression.Table);

return innerJoinLateralExpression;
}

protected override Expression VisitLeftJoinLateral(LeftJoinLateralExpression leftJoinLateralExpression)
{
_relationalCommandBuilder.Append("LEFT JOIN LATERAL ");
Visit(leftJoinLateralExpression.Table);

return leftJoinLateralExpression;
}

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).AddLeftJoinLateral(
(SelectExpression)inner.QueryExpression, transparentIdentifierType);
innerShaperExpression = MarkShaperNullable(innerShaperExpression);
}
else
{
((SelectExpression)source.QueryExpression).AddInnerJoinLateral(
(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 InnerJoinLateralExpression innerJoinLateralExpression:
return VisitInnerJoinLateral(innerJoinLateralExpression);

case LeftJoinLateralExpression leftJoinLateralExpression:
return VisitLeftJoinLateral(leftJoinLateralExpression);

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 VisitInnerJoinLateral(InnerJoinLateralExpression innerJoinLateralExpression);
protected abstract Expression VisitLeftJoinLateral(LeftJoinLateralExpression leftJoinLateralExpression);
protected abstract Expression VisitFromSql(FromSqlExpression fromSqlExpression);
protected abstract Expression VisitInnerJoin(InnerJoinExpression innerJoinExpression);
protected abstract Expression VisitLeftJoin(LeftJoinExpression leftJoinExpression);
Expand Down
40 changes: 40 additions & 0 deletions src/EFCore.Relational/Query/SqlExpressions/CrossApplyExpression.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 InnerJoinLateralExpression : JoinExpressionBase
{
public InnerJoinLateralExpression(TableExpressionBase table)
: base(table)
{
}

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

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

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.Append("INNER JOIN LATERAL ");
expressionPrinter.Visit(Table);
}

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

private bool Equals(InnerJoinLateralExpression innerJoinLateralExpression)
=> base.Equals(innerJoinLateralExpression);

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 LeftJoinLateralExpression : JoinExpressionBase
{
public LeftJoinLateralExpression(TableExpressionBase table)
: base(table)
{
}

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

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

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.Append("LEFT JOIN LATERAL ");
expressionPrinter.Visit(Table);
}

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

private bool Equals(LeftJoinLateralExpression leftJoinLateralExpression)
=> base.Equals(leftJoinLateralExpression);

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