Skip to content

Commit

Permalink
Explain why the CASE transformation is not always preferred
Browse files Browse the repository at this point in the history
  • Loading branch information
ranma42 committed Jul 27, 2024
1 parent 144b7e0 commit 4c3a542
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,11 @@ private SqlExpression RewriteNullSemantics(
// doing a full null semantics rewrite - removing all nulls from truth table
nullable = false;

// When both operands are nullable, the CASE transformation is invalid.
// We also use the generic transformation when it is simplifies into
// a == b && some_column IS NOT NULL
// as we consider it simpler than the CASE expression; additionally it
// might take advantage of indexes on some_column, on a and/or on b.
if (leftNullable && rightNullable
|| leftIsNull is SqlUnaryExpression { Operand: ColumnExpression }
|| rightIsNull is SqlUnaryExpression { Operand: ColumnExpression })
Expand All @@ -1826,6 +1831,8 @@ private SqlExpression RewriteNullSemantics(
}
else
{
// When only one of the operands is nullable, we avoid duplicating
// complex expressions by performing the following transformation:
// a == b -> CASE WHEN a == b THEN TRUE ELSE FALSE END
body = _sqlExpressionFactory.Case(
[new(body, _sqlExpressionFactory.Constant(true, body.Type, body.TypeMapping))],
Expand Down

0 comments on commit 4c3a542

Please sign in to comment.