Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dotnet/efcore
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fa790d762d0e6c63ee16d619e899c1e770e98d45
Choose a base ref
..
head repository: dotnet/efcore
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b7c0e4aa95cad0150e3d7927df5a58512f211fba
Choose a head ref
35 changes: 31 additions & 4 deletions src/EFCore.Cosmos/Query/Internal/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -685,10 +685,37 @@ public virtual SqlExpression Condition(SqlExpression test, SqlExpression ifTrue,
{
var typeMapping = ExpressionExtensions.InferTypeMapping(ifTrue, ifFalse);

return new SqlConditionalExpression(
ApplyTypeMapping(test, _boolTypeMapping),
ApplyTypeMapping(ifTrue, typeMapping),
ApplyTypeMapping(ifFalse, typeMapping));
test = ApplyTypeMapping(test, _boolTypeMapping);
ifTrue = ApplyTypeMapping(ifTrue, typeMapping);
ifFalse = ApplyTypeMapping(ifFalse, typeMapping);

// Simplify:
// a == b ? b : a -> a
// a != b ? a : b -> a
if (test is SqlBinaryExpression
{
OperatorType: ExpressionType.Equal or ExpressionType.NotEqual,
Left: var left,
Right: var right
} binary)
{
// Reverse ifEqual/ifNotEqual for ExpressionType.NotEqual for easier reasoning below
var (ifEqual, ifNotEqual) = binary.OperatorType is ExpressionType.Equal ? (ifTrue, ifFalse) : (ifFalse, ifTrue);

// a == b ? b : a -> a
if (left.Equals(ifNotEqual) && right.Equals(ifEqual))
{
return left;
}

// b == a ? b : a -> a
if (right.Equals(ifNotEqual) && left.Equals(ifEqual))
{
return right;
}
}

return new SqlConditionalExpression(test, ifTrue, ifFalse);
}

/// <summary>
9 changes: 6 additions & 3 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -835,13 +835,16 @@ public virtual SqlExpression Case(
&& typeMappedWhenClauses is
[
{
Test: SqlBinaryExpression { OperatorType: ExpressionType.Equal or ExpressionType.NotEqual } binary,
Test: SqlBinaryExpression
{
OperatorType: ExpressionType.Equal or ExpressionType.NotEqual,
Left: var left,
Right: var right
} binary,
Result: var result
}
])
{
var (left, right) = (binary.Left, binary.Right);

// Reverse ifEqual/ifNotEqual for ExpressionType.NotEqual for easier reasoning below
var (ifEqual, ifNotEqual) = binary.OperatorType is ExpressionType.Equal
? (result, elseResult ?? Constant(null, result.Type, result.TypeMapping))
Original file line number Diff line number Diff line change
@@ -19,12 +19,11 @@ public override Task Conditional_simplifiable_equality(bool async)
{
await base.Conditional_simplifiable_equality(a);

// TODO: Simplify this away, as per #35327 for relational
AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE (((c["Int"] = 9) ? 9 : c["Int"]) > 1)
WHERE (c["Int"] > 1)
""");
});

@@ -34,12 +33,11 @@ public override Task Conditional_simplifiable_inequality(bool async)
{
await base.Conditional_simplifiable_inequality(a);

// TODO: Simplify this away, as per #35327 for relational
AssertSql(
"""
SELECT VALUE c
FROM root c
WHERE (((c["Int"] != 8) ? c["Int"] : 8) > 1)
WHERE (c["Int"] > 1)
""");
});