Skip to content

Commit

Permalink
Fix bool comparison on SqlServer
Browse files Browse the repository at this point in the history
The provider for SqlServer is currently unable to translate `bool?` expressions
when they are on Boolean values instead of `BIT`s.

Work around this by performing the usual translation.
  • Loading branch information
ranma42 committed Jun 22, 2024
1 parent adb20b7 commit d924e3d
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected override SqlExpression RewriteNullSemantics(
var result = base.RewriteNullSemantics(sqlBinaryExpression, left, right, leftNullable, rightNullable, optimize, out nullable);

if (_useIs
&& IsSupportedSubExpression(left, leftNullable) && IsSupportedSubExpression(right, rightNullable)
&& sqlBinaryExpression.OperatorType is ExpressionType.Equal or ExpressionType.NotEqual
&& result is SqlBinaryExpression { OperatorType: ExpressionType.AndAlso or ExpressionType.OrElse })
{
Expand All @@ -131,6 +132,14 @@ protected override SqlExpression RewriteNullSemantics(
}

return result;

// The provider for SqlServer is unable to translate `bool?` expressions
// when they are on Boolean values instead of `BIT`s. See #34001
static bool IsSupportedSubExpression(SqlExpression sqlExpression, bool nullable) =>
!nullable
|| sqlExpression.Type != typeof(bool)
|| (sqlExpression is not SqlUnaryExpression { OperatorType: ExpressionType.Not } or
SqlBinaryExpression { OperatorType: ExpressionType.AndAlso or ExpressionType.OrElse });
}

/// <summary>
Expand Down

0 comments on commit d924e3d

Please sign in to comment.