Skip to content

Commit

Permalink
Visit inside InlineQueryRootExpression in nav expansion (dotnet#32460)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Nov 30, 2023
1 parent 17fa62f commit 50a8ae5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,23 @@ protected override Expression VisitExtension(Expression extensionExpression)

return ApplyQueryFilter(entityType, navigationExpansionExpression);

// Inline query roots can contain arbitrary expressions, including subqueries with navigations; visit inside to process those.
case InlineQueryRootExpression inlineQueryRootExpression:
{
var visited = inlineQueryRootExpression.Update(this.VisitAndConvert(inlineQueryRootExpression.Values));
var currentTree = new NavigationTreeExpression(Expression.Default(inlineQueryRootExpression.ElementType));
var parameterName = GetParameterName("e");

return new NavigationExpansionExpression(visited, currentTree, currentTree, parameterName);
}

case QueryRootExpression queryRootExpression:
{
var currentTree = new NavigationTreeExpression(Expression.Default(queryRootExpression.ElementType));
var parameterName = GetParameterName("e");

return new NavigationExpansionExpression(queryRootExpression, currentTree, currentTree, parameterName);
}

case NavigationExpansionExpression:
case OwnedNavigationReference:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4606,6 +4606,14 @@ await AssertTranslationFailed(
AssertSql();
}

public override async Task Subquery_with_navigation_inside_inline_collection(bool async)
{
await AssertTranslationFailed(
() => base.Subquery_with_navigation_inside_inline_collection(async));

AssertSql();
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5652,4 +5652,11 @@ public virtual Task Collection_navigation_equal_to_null_for_subquery_using_Eleme
ss => ss.Set<Customer>().Where(c => c.Orders.OrderBy(o => o.OrderID).ElementAtOrDefault(prm).OrderDetails == null),
ss => ss.Set<Customer>().Where(c => c.Orders.OrderBy(o => o.OrderID).ElementAtOrDefault(prm) == null));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Subquery_with_navigation_inside_inline_collection(bool async)
=> AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => new[] { 100, c.Orders.Count }.Sum() > 101));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7260,6 +7260,23 @@ ORDER BY [o].[OrderID]
""");
}

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

AssertSql(
"""
SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE (
SELECT COALESCE(SUM([v].[Value]), 0)
FROM (VALUES (CAST(100 AS int)), ((
SELECT COUNT(*)
FROM [Orders] AS [o]
WHERE [c].[CustomerID] = [o].[CustomerID]))) AS [v]([Value])) > 101
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down

0 comments on commit 50a8ae5

Please sign in to comment.