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

[SMALL] Fix to #18725 - Optimize out null checks when translating Enum flags #18750

Merged
merged 1 commit into from
Nov 5, 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 @@ -241,13 +241,11 @@ private SqlExpression SimplifyNullNotNullExpression(
// for coalesce:
// (a ?? b) == null -> a == null && b == null
// (a ?? b) != null -> a != null || b != null
// for AndAlso, OrElse, And, Or we can't do this optimization
// for AndAlso, OrElse we can't do this optimization
// we could do something like this, but it seems too complicated:
// (a && b) == null -> a == null && b != 0 || a != 0 && b == null
if (sqlBinaryOperand.OperatorType != ExpressionType.AndAlso
&& sqlBinaryOperand.OperatorType != ExpressionType.OrElse
&& sqlBinaryOperand.OperatorType != ExpressionType.And
&& sqlBinaryOperand.OperatorType != ExpressionType.Or)
&& sqlBinaryOperand.OperatorType != ExpressionType.OrElse)
{
var newLeft = SimplifyNullNotNullExpression(operatorType, sqlBinaryOperand.Left, typeof(bool), typeMapping);
var newRight = SimplifyNullNotNullExpression(operatorType, sqlBinaryOperand.Right, typeof(bool), typeMapping);
Expand Down
70 changes: 66 additions & 4 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7513,25 +7513,21 @@ public virtual Task OrderBy_StartsWith_with_null_parameter_as_argument(bool isAs
public virtual async Task Where_with_enum_flags_parameter(bool isAsync)
{
MilitaryRank? rank = MilitaryRank.Private;

await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.Rank & rank) == rank));

rank = null;

await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.Rank & rank) == rank));

rank = MilitaryRank.Corporal;

await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.Rank | rank) != rank));

rank = null;

await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.Rank | rank) != rank));
Expand All @@ -7547,6 +7543,72 @@ public virtual Task FirstOrDefault_navigation_access_entity_equality_in_where_pr
.Where(f => f.Capital == ss.Set<Gear>().OrderBy(s => s.Nickname).FirstOrDefault().CityOfBirth));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Bitwise_operation_with_non_null_parameter_optimizes_null_checks(bool isAsync)
{
var ranks = MilitaryRank.Corporal | MilitaryRank.Sergeant | MilitaryRank.General;

await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.Rank & ranks) != 0));

await AssertQueryScalar(
isAsync,
ss => ss.Set<Gear>().Select(g => (g.Rank | ranks) == ranks));

await AssertQueryScalar(
isAsync,
ss => ss.Set<Gear>().Select(g => (g.Rank | (g.Rank | (ranks | (g.Rank | ranks)))) == ranks));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Bitwise_operation_with_null_arguments(bool isAsync)
{
await AssertQuery(
isAsync,
ss => ss.Set<Weapon>().Where(w => (w.AmmunitionType & AmmunitionType.Cartridge) == null));

await AssertQuery(
isAsync,
ss => ss.Set<Weapon>().Where(w => (w.AmmunitionType | AmmunitionType.Shell) == null));

AmmunitionType? prm = null;
await AssertQuery(
isAsync,
ss => ss.Set<Weapon>().Where(w => (w.AmmunitionType | AmmunitionType.Shell) == prm));

await AssertQuery(
isAsync,
ss => ss.Set<Weapon>().Where(w => (w.AmmunitionType & prm) == prm));

prm = AmmunitionType.Shell;
await AssertQuery(
isAsync,
ss => ss.Set<Weapon>().Where(w => (w.AmmunitionType & prm) != 0));

prm = AmmunitionType.Cartridge;
await AssertQuery(
isAsync,
ss => ss.Set<Weapon>().Where(w => (w.AmmunitionType & prm) == prm));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Logical_operation_with_non_null_parameter_optimizes_null_checks(bool isAsync)
{
var prm = true;
await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.HasSoulPatch && prm) != prm));

prm = false;
await AssertQuery(
isAsync,
ss => ss.Set<Gear>().Where(g => (g.HasSoulPatch || prm) != prm));
}

protected async Task AssertTranslationFailed(Func<Task> testCode)
{
Assert.Contains(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ namespace Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel
[Flags]
public enum MilitaryRank
{
Private = 0,
Corporal = 1,
Sergeant = 2,
Lieutenant = 4,
Captain = 8,
Major = 16,
Colonel = 32,
General = 64
None = 0,
Private = 1,
Corporal = 2,
Sergeant = 4,
Lieutenant = 8,
Captain = 16,
Major = 32,
Colonel = 64,
General = 128
}
}
Loading