Skip to content

Commit

Permalink
Implement local simplification for Coalesce
Browse files Browse the repository at this point in the history
  • Loading branch information
ranma42 committed Jun 16, 2024
1 parent 1234a12 commit eb25eff
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/EFCore.Relational/Query/SqlExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,25 @@ public virtual SqlExpression Coalesce(SqlExpression left, SqlExpression right, R
?? ExpressionExtensions.InferTypeMapping(left, right)
?? _typeMappingSource.FindMapping(resultType, Dependencies.Model);

var typeMappedArguments = new List<SqlExpression>
left = ApplyTypeMapping(left, inferredTypeMapping);
right = ApplyTypeMapping(right, inferredTypeMapping);

return left switch
{
ApplyTypeMapping(left, inferredTypeMapping), ApplyTypeMapping(right, inferredTypeMapping)
SqlConstantExpression { Value: null } => right,

SqlConstantExpression { Value: not null } or
ColumnExpression { IsNullable: false } => left,

_ => new SqlFunctionExpression(
"COALESCE",
[left, right],
nullable: true,
// COALESCE is handled separately since it's only nullable if *all* arguments are null
argumentsPropagateNullability: [false, false],
resultType,
inferredTypeMapping)
};

return new SqlFunctionExpression(
"COALESCE",
typeMappedArguments,
nullable: true,
// COALESCE is handled separately since it's only nullable if *all* arguments are null
argumentsPropagateNullability: [false, false],
resultType,
inferredTypeMapping);
}

/// <inheritdoc />
Expand Down

0 comments on commit eb25eff

Please sign in to comment.