Skip to content

Commit

Permalink
Simplify COALESCE
Browse files Browse the repository at this point in the history
Drop all of the arguments after the first non-nullable sub-expressions.
Simplify unary `COALESCE` to its argument.
  • Loading branch information
ranma42 committed Jun 27, 2024
1 parent 50da67b commit 38d9f0f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,12 +1422,25 @@ protected virtual SqlExpression VisitSqlFunction(
foreach (var argument in sqlFunctionExpression.Arguments)
{
coalesceArguments.Add(Visit(argument, out var argumentNullable));
coalesceNullable = coalesceNullable && argumentNullable;
if (!argumentNullable)
{
coalesceNullable = false;
break;
}
}

nullable = coalesceNullable;

return sqlFunctionExpression.Update(sqlFunctionExpression.Instance, coalesceArguments);
return coalesceArguments switch
{
[var singleArgument] => singleArgument,

_ => sqlFunctionExpression.Update(
sqlFunctionExpression.Instance,
coalesceArguments,
argumentsPropagateNullability: coalesceArguments.Select(_ => false).ToArray()
),
};
}

var useNullabilityPropagation = sqlFunctionExpression is { InstancePropagatesNullability: true };
Expand Down

0 comments on commit 38d9f0f

Please sign in to comment.