Skip to content

Commit

Permalink
Query: Match types in conditional during client evaluation (#23323)
Browse files Browse the repository at this point in the history
Resolves #23309
  • Loading branch information
smitpatel committed Nov 18, 2020
1 parent da1c05f commit 2fa06f4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ protected override Expression VisitConditional(ConditionalExpression conditional
test = Expression.Equal(test, Expression.Constant(true, typeof(bool?)));
}

if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23309", out var isEnabled) && isEnabled))
{
ifTrue = MatchTypes(ifTrue, conditionalExpression.IfTrue.Type);
ifFalse = MatchTypes(ifFalse, conditionalExpression.IfFalse.Type);
}

return conditionalExpression.Update(test, ifTrue, ifFalse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ protected override Expression VisitConditional(ConditionalExpression conditional
test = Expression.Equal(test, Expression.Constant(true, typeof(bool?)));
}

if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23309", out var isEnabled) && isEnabled))
{
ifTrue = MatchTypes(ifTrue, conditionalExpression.IfTrue.Type);
ifFalse = MatchTypes(ifFalse, conditionalExpression.IfFalse.Type);
}

return conditionalExpression.Update(test, ifTrue, ifFalse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,17 @@ public override Task Do_not_erase_projection_mapping_when_adding_single_projecti
return base.Do_not_erase_projection_mapping_when_adding_single_projection(async);
}

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

AssertSql(
@"SELECT VALUE {""CustomerID"" : c[""CustomerID""], ""OrderDate"" : c[""OrderDate""], ""c"" : (c[""OrderID""] - 10000)}
FROM root c
WHERE ((c[""Discriminator""] = ""Order"") AND (c[""OrderID""] < 10300))
ORDER BY c[""OrderID""]");
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1876,5 +1876,32 @@ public virtual Task Do_not_erase_projection_mapping_when_adding_single_projectio
},
entryCount: 446);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Ternary_in_client_eval_assigns_correct_types(bool async)
{ return AssertQuery(
async,
ss => ss.Set<Order>()
.Where(o => o.OrderID < 10300)
.OrderBy(e => e.OrderID)
.Select(
o => new
{
CustomerID = ClientMethod(o.CustomerID),
OrderDate = o.OrderDate.HasValue ? o.OrderDate.Value : new DateTime(o.OrderID - 10000, 1, 1),
OrderDate2 = o.OrderDate.HasValue == false ? new DateTime(o.OrderID - 10000, 1, 1) : o.OrderDate.Value
}),
assertOrder: true,
elementAsserter: (e, a) =>
{
AssertEqual(e.CustomerID, a.CustomerID);
AssertEqual(e.OrderDate, a.OrderDate);
AssertEqual(e.OrderDate2, a.OrderDate2);
});
}

private static string ClientMethod(string s) => s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,23 @@ WHERE [o].[OrderID] < 10350
ORDER BY [o].[OrderID], [t0].[OrderID], [t0].[ProductID], [t0].[ProductID0], [t1].[OrderID], [t1].[ProductID], [t1].[ProductID0], [t2].[OrderID], [t2].[ProductID], [t2].[ProductID0]");
}

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

AssertSql(
@"SELECT [o].[CustomerID], CASE
WHEN [o].[OrderDate] IS NOT NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END, [o].[OrderDate], [o].[OrderID] - 10000, CASE
WHEN [o].[OrderDate] IS NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END
FROM [Orders] AS [o]
WHERE [o].[OrderID] < 10300
ORDER BY [o].[OrderID]");
}

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

Expand Down

0 comments on commit 2fa06f4

Please sign in to comment.