Skip to content

Commit

Permalink
[automated] Merge branch 'release/9.0' => 'main' (#35577)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Feb 10, 2025
2 parents 028980e + 1177a3b commit 3b5648d
Show file tree
Hide file tree
Showing 11 changed files with 1,806 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ variables:
is1ESPipeline: true

${{ each parameter in parameters }}:
${{ parameter.key }}: ${{ parameter.value }}
${{ parameter.key }}: ${{ parameter.value }}
24 changes: 17 additions & 7 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,20 +660,30 @@ private SqlExpression Not(SqlExpression operand, SqlExpression? existingExpressi
SqlBinaryExpression { OperatorType: ExpressionType.OrElse } binary
=> AndAlso(Not(binary.Left), Not(binary.Right)),

// use equality where possible
// !(a == true) -> a == false
// !(a == false) -> a == true
SqlBinaryExpression { OperatorType: ExpressionType.Equal, Right: SqlConstantExpression { Value: bool } } binary
SqlBinaryExpression
{
OperatorType: ExpressionType.Equal,
Right: SqlConstantExpression { Value: bool },
Left: SqlConstantExpression { Value: bool }
or SqlParameterExpression { IsNullable: false }
or ColumnExpression { IsNullable: false }
} binary
=> Equal(binary.Left, Not(binary.Right)),

// !(true == a) -> false == a
// !(false == a) -> true == a
SqlBinaryExpression { OperatorType: ExpressionType.Equal, Left: SqlConstantExpression { Value: bool } } binary
SqlBinaryExpression
{
OperatorType: ExpressionType.Equal,
Left: SqlConstantExpression { Value: bool },
Right: SqlConstantExpression { Value: bool }
or SqlParameterExpression { IsNullable: false }
or ColumnExpression { IsNullable: false }
} binary
=> Equal(Not(binary.Left), binary.Right),

// !(a == b) -> a != b
SqlBinaryExpression { OperatorType: ExpressionType.Equal } sqlBinaryOperand => NotEqual(
sqlBinaryOperand.Left, sqlBinaryOperand.Right),

// !(a != b) -> a == b
SqlBinaryExpression { OperatorType: ExpressionType.NotEqual } sqlBinaryOperand => Equal(
sqlBinaryOperand.Left, sqlBinaryOperand.Right),
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ protected virtual SqlExpression VisitSqlBinary(
// we assume that NullSemantics rewrite is only needed (on the current level)
// if the optimization didn't make any changes.
// Reason is that optimization can/will change the nullability of the resulting expression
// and that inforation is not tracked/stored anywhere
// and that information is not tracked/stored anywhere
// so we can no longer rely on nullabilities that we computed earlier (leftNullable, rightNullable)
// when performing null semantics rewrite.
// It should be fine because current optimizations *radically* change the expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,26 @@ public virtual async Task Rewrite_compare_int_with_int(bool async)
public virtual async Task Rewrite_compare_bool_with_bool(bool async)
{
var bools = new[] { false, true };
var onetwothree = new[] { 1, 2, 3 };

foreach (var neq in bools)
{
foreach (var negated in bools)
{
foreach (var negateB in bools)
{
foreach (var nullableA in bools)
foreach (var nullableA in onetwothree)
{
foreach (var negateA in bools)
{
foreach (var nullableB in bools)
foreach (var nullableB in onetwothree)
{
// filter out tests comparing two constants
if (nullableA == 3 && nullableB == 3) continue;

var queryBuilder = (ISetSource ss) =>
{
var data = nullableA
var data = nullableA == 2
? ss.Set<NullSemanticsEntity1>().Select(
e => new
{
Expand All @@ -116,30 +120,47 @@ public virtual async Task Rewrite_compare_bool_with_bool(bool async)
e.BoolB,
e.NullableBoolB
})
: ss.Set<NullSemanticsEntity1>().Select(
e => new
{
e.Id,
A = (bool?)e.BoolA,
e.BoolB,
e.NullableBoolB
});

var query = nullableB
: nullableA == 1
? ss.Set<NullSemanticsEntity1>().Select(
e => new
{
e.Id,
A = (bool?)e.BoolA,
e.BoolB,
e.NullableBoolB
})
: ss.Set<NullSemanticsEntity1>().Select(
e => new
{
e.Id,
A = (bool?)true,
e.BoolB,
e.NullableBoolB
});

var query = nullableB == 2
? data.Select(
e => new
{
e.Id,
e.A,
B = e.NullableBoolB
})
: data.Select(
e => new
{
e.Id,
e.A,
B = (bool?)e.BoolB
});
: nullableB == 1
? data.Select(
e => new
{
e.Id,
e.A,
B = (bool?)e.BoolB
})
: data.Select(
e => new
{
e.Id,
e.A,
B = (bool?)true
});

query = negateA
? query.Select(
Expand Down Expand Up @@ -2467,6 +2488,18 @@ await AssertQueryScalar(
ss => ss.Set<NullSemanticsEntity1>().Where(e => true).Select(e => e.Id));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Compare_constant_true_to_expression_which_evaluates_to_null(bool async)
{
var prm = default(bool?);

await AssertQueryScalar(
async,
ss => ss.Set<NullSemanticsEntity1>().Where(x => x.NullableBoolA != null
&& !object.Equals(true, x.NullableBoolA == null ? null : prm)).Select(x => x.Id));
}

// We can't client-evaluate Like (for the expected results).
// However, since the test data has no LIKE wildcards, it effectively functions like equality - except that 'null like null' returns
// false instead of true. So we have this "lite" implementation which doesn't support wildcards.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4467,7 +4467,7 @@ INNER JOIN (
FROM [Factions] AS [f]
WHERE [f].[Name] = N'Swarm'
) AS [f0] ON [l].[Name] = [f0].[CommanderName]
WHERE [f0].[Eradicated] = CAST(0 AS bit) OR [f0].[Eradicated] IS NULL
WHERE [f0].[Eradicated] <> CAST(1 AS bit) OR [f0].[Eradicated] IS NULL
""");
}

Expand All @@ -4484,7 +4484,7 @@ LEFT JOIN (
FROM [Factions] AS [f]
WHERE [f].[Name] = N'Swarm'
) AS [f0] ON [l].[Name] = [f0].[CommanderName]
WHERE [f0].[Eradicated] = CAST(0 AS bit) OR [f0].[Eradicated] IS NULL
WHERE [f0].[Eradicated] <> CAST(1 AS bit) OR [f0].[Eradicated] IS NULL
""");
}

Expand Down Expand Up @@ -6993,7 +6993,7 @@ FROM [LocustLeaders] AS [l]
INNER JOIN [Factions] AS [f] ON [l].[Name] = [f].[CommanderName]
WHERE CASE
WHEN [f].[Name] = N'Locust' THEN CAST(1 AS bit)
END = CAST(0 AS bit) OR CASE
END <> CAST(1 AS bit) OR CASE
WHEN [f].[Name] = N'Locust' THEN CAST(1 AS bit)
END IS NULL
""");
Expand Down
Loading

0 comments on commit 3b5648d

Please sign in to comment.