Skip to content
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

Query: Don't simplify Case block when ElseResult exists #28870

Merged
merged 1 commit into from
Aug 24, 2022
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 @@ -231,7 +231,8 @@ private Expression SimplifySqlBinary(SqlBinaryExpression sqlBinaryExpression)
&& sqlConstantComponent != null
&& sqlConstantComponent.Value != null
&& caseComponent != null
&& caseComponent.Operand == null)
&& caseComponent.Operand == null
&& caseComponent.ElseResult == null)
{
var matchingCaseBlock = caseComponent.WhenClauses.FirstOrDefault(wc => sqlConstantComponent.Equals(wc.Result));
if (matchingCaseBlock != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,16 @@ FROM root c
WHERE (c[""Discriminator""] = ""Customer"")");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (((c[""Region""] = null) ? ""OR"" : c[""Region""]) = ""OR""))");
}

public override async Task Where_compare_null_with_cast_to_object(bool async)
{
await base.Where_compare_null_with_cast_to_object(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2433,4 +2433,14 @@ public virtual Task GetType_on_non_hierarchy4(bool async)
async,
ss => ss.Set<Customer>().Where(c => c.GetType() != typeof(Order)),
entryCount: 91);

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Case_block_simplification_works_correctly(bool async)
#pragma warning disable IDE0029 // Use coalesce expression
=> AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => (c.Region == null ? "OR" : c.Region) == "OR"),
entryCount: 64);
#pragma warning restore IDE0029 // Use coalesce expression
}
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,19 @@ public override async Task GetType_on_non_hierarchy4(bool async)
FROM [Customers] AS [c]");
}

public override async Task Case_block_simplification_works_correctly(bool async)
{
await base.Case_block_simplification_works_correctly(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 CASE
WHEN [c].[Region] IS NULL THEN N'OR'
ELSE [c].[Region]
END = N'OR'");
}

public override async Task Where_poco_closure(bool async)
{
await base.Where_poco_closure(async);
Expand Down