Skip to content

Commit

Permalink
Use XOR to translate some NOT expressions
Browse files Browse the repository at this point in the history
When neither the parent expression nor the inner one is a predicate, translate to:
```sql
x ^ CAST(1 AS bit)
```

instead of

```sql
CASE
    WHEN x = CAST(0 AS bit) THEN CAST(1 AS bit)
    ELSE CAST(0 AS bit)
END
```

Contributes to dotnet#34001 for simple cases (NOT of BIT expressions).
  • Loading branch information
ranma42 committed Jun 27, 2024
1 parent a1f13d3 commit 4cf9c9d
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpressio
case ExpressionType.Not
when sqlUnaryExpression.Type == typeof(bool):
{
// when possible, avoid converting to/from predicate form
if (!_isSearchCondition && sqlUnaryExpression.Operand is not (ExistsExpression or InExpression or LikeExpression))
{
var negatedOperand = (SqlExpression)Visit(sqlUnaryExpression.Operand);
return _sqlExpressionFactory.MakeBinary(
ExpressionType.ExclusiveOr,
negatedOperand,
_sqlExpressionFactory.Constant(true, negatedOperand.TypeMapping),
negatedOperand.TypeMapping
)!;
}

_isSearchCondition = true;
resultCondition = true;
break;
Expand Down

0 comments on commit 4cf9c9d

Please sign in to comment.