Skip to content

Commit

Permalink
Rename Cross/Outer Apply to Inner/Left Join Lateral
Browse files Browse the repository at this point in the history
To match with SQL standard
  • Loading branch information
smitpatel committed Aug 13, 2019
1 parent d11f607 commit a6dd1a6
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 42 deletions.
16 changes: 8 additions & 8 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,20 +706,20 @@ protected override Expression VisitCrossJoin(CrossJoinExpression crossJoinExpres
return crossJoinExpression;
}

protected override Expression VisitCrossApply(CrossApplyExpression crossApplyExpression)
protected override Expression VisitInnerJoinLateral(InnerJoinLateralExpression innerJoinLateralExpression)
{
_relationalCommandBuilder.Append("CROSS APPLY ");
Visit(crossApplyExpression.Table);
_relationalCommandBuilder.Append("INNER JOIN LATERAL ");
Visit(innerJoinLateralExpression.Table);

return crossApplyExpression;
return innerJoinLateralExpression;
}

protected override Expression VisitOuterApply(OuterApplyExpression outerApplyExpression)
protected override Expression VisitLeftJoinLateral(LeftJoinLateralExpression leftJoinLateralExpression)
{
_relationalCommandBuilder.Append("OUTER APPLY ");
Visit(outerApplyExpression.Table);
_relationalCommandBuilder.Append("LEFT JOIN LATERAL ");
Visit(leftJoinLateralExpression.Table);

return outerApplyExpression;
return leftJoinLateralExpression;
}

protected override Expression VisitInnerJoin(InnerJoinExpression innerJoinExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,13 +744,13 @@ protected override ShapedQueryExpression TranslateSelectMany(
var innerShaperExpression = inner.ShaperExpression;
if (defaultIfEmpty)
{
((SelectExpression)source.QueryExpression).AddOuterApply(
((SelectExpression)source.QueryExpression).AddLeftJoinLateral(
(SelectExpression)inner.QueryExpression, transparentIdentifierType);
innerShaperExpression = MarkShaperNullable(innerShaperExpression);
}
else
{
((SelectExpression)source.QueryExpression).AddCrossApply(
((SelectExpression)source.QueryExpression).AddInnerJoinLateral(
(SelectExpression)inner.QueryExpression, transparentIdentifierType);
}

Expand Down
12 changes: 6 additions & 6 deletions src/EFCore.Relational/Query/SqlExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ protected override Expression VisitExtension(Expression extensionExpression)
case CrossJoinExpression crossJoinExpression:
return VisitCrossJoin(crossJoinExpression);

case CrossApplyExpression crossApplyExpression:
return VisitCrossApply(crossApplyExpression);
case InnerJoinLateralExpression innerJoinLateralExpression:
return VisitInnerJoinLateral(innerJoinLateralExpression);

case OuterApplyExpression outerApplyExpression:
return VisitOuterApply(outerApplyExpression);
case LeftJoinLateralExpression leftJoinLateralExpression:
return VisitLeftJoinLateral(leftJoinLateralExpression);

case ExistsExpression existsExpression:
return VisitExists(existsExpression);
Expand Down Expand Up @@ -86,8 +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 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
19 changes: 9 additions & 10 deletions src/EFCore.Relational/Query/SqlExpressions/CrossApplyExpression.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
// 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 class InnerJoinLateralExpression : JoinExpressionBase
{
public CrossApplyExpression(TableExpressionBase table)
public InnerJoinLateralExpression(TableExpressionBase table)
: base(table)
{
}

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

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

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

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

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

public override int GetHashCode() => base.GetHashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@

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

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

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

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

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

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

public override int GetHashCode() => base.GetHashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ public void AddCrossJoin(SelectExpression innerSelectExpression, Type transparen
_projectionMapping = projectionMapping;
}

public void AddCrossApply(SelectExpression innerSelectExpression, Type transparentIdentifierType)
public void AddInnerJoinLateral(SelectExpression innerSelectExpression, Type transparentIdentifierType)
{
var joinPredicate = TryExtractJoinKey(innerSelectExpression);
if (joinPredicate != null)
Expand Down Expand Up @@ -1121,7 +1121,7 @@ public void AddCrossApply(SelectExpression innerSelectExpression, Type transpare
}

_identifier.AddRange(innerSelectExpression._identifier);
var joinTable = new CrossApplyExpression(innerSelectExpression.Tables.Single());
var joinTable = new InnerJoinLateralExpression(innerSelectExpression.Tables.Single());
_tables.Add(joinTable);

var outerMemberInfo = transparentIdentifierType.GetTypeInfo().GetDeclaredField("Outer");
Expand All @@ -1140,7 +1140,7 @@ public void AddCrossApply(SelectExpression innerSelectExpression, Type transpare
_projectionMapping = projectionMapping;
}

public void AddOuterApply(SelectExpression innerSelectExpression, Type transparentIdentifierType)
public void AddLeftJoinLateral(SelectExpression innerSelectExpression, Type transparentIdentifierType)
{
var joinPredicate = TryExtractJoinKey(innerSelectExpression);
if (joinPredicate != null)
Expand Down Expand Up @@ -1168,7 +1168,7 @@ public void AddOuterApply(SelectExpression innerSelectExpression, Type transpare
}

_identifier.AddRange(innerSelectExpression._identifier);
var joinTable = new OuterApplyExpression(innerSelectExpression.Tables.Single());
var joinTable = new LeftJoinLateralExpression(innerSelectExpression.Tables.Single());
_tables.Add(joinTable);

var outerMemberInfo = transparentIdentifierType.GetTypeInfo().GetDeclaredField("Outer");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected override Expression VisitCrossJoin(CrossJoinExpression crossJoinExpres
return crossJoinExpression.Update(table);
}

protected override Expression VisitCrossApply(CrossApplyExpression crossApplyExpression)
protected override Expression VisitInnerJoinLateral(InnerJoinLateralExpression crossApplyExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
Expand All @@ -327,7 +327,7 @@ protected override Expression VisitCrossApply(CrossApplyExpression crossApplyExp
return crossApplyExpression.Update(table);
}

protected override Expression VisitOuterApply(OuterApplyExpression outerApplyExpression)
protected override Expression VisitLeftJoinLateral(LeftJoinLateralExpression outerApplyExpression)
{
var parentSearchCondition = _isSearchCondition;
_isSearchCondition = false;
Expand Down
16 changes: 16 additions & 0 deletions src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,21 @@ protected override Expression VisitSqlFunction(SqlFunctionExpression sqlFunction

return base.VisitSqlFunction(sqlFunctionExpression);
}

protected override Expression VisitInnerJoinLateral(InnerJoinLateralExpression innerJoinLateralExpression)
{
Sql.Append("CROSS APPLY ");
Visit(innerJoinLateralExpression.Table);

return innerJoinLateralExpression;
}

protected override Expression VisitLeftJoinLateral(LeftJoinLateralExpression leftJoinLateralExpression)
{
Sql.Append("OUTER APPLY ");
Visit(leftJoinLateralExpression.Table);

return leftJoinLateralExpression;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5236,7 +5236,7 @@ public virtual void Correlated_subquery_with_owned_navigation_being_compared_to_
Assert.Equal(10, partners[0].Addresses[0].Turnovers.AmountIn);

AssertSql(
@"SELECT [p].[Id], [t].[c], [t].[Turnovers_AmountIn], [t].[Id], [t].[Partner13157Id]
@"SELECT [p].[Id], [t].[c], [t].[Turnovers_AmountIn], [t].[Id]
FROM [Partners] AS [p]
LEFT JOIN (
SELECT CASE
Expand Down
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.Threading.Tasks;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Query
{
Expand Down Expand Up @@ -1006,5 +1007,11 @@ public override async Task SelectMany_without_result_selector_collection_navigat
FROM [Customers] AS [c]
INNER JOIN [Orders] AS [o] ON [c].[CustomerID] = [o].[CustomerID]");
}

[ConditionalFact(Skip = "Issue#17117")]
public override void Select_nested_projection()
{
base.Select_nested_projection();
}
}
}

0 comments on commit a6dd1a6

Please sign in to comment.