Skip to content

[WIP] Concating null with not null string resulting in null #14865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ var structuralComparisonExpression
? Expression.AndAlso(left, right)
: null;

case ExpressionType.OrElse:
case ExpressionType.Add:
return ProcessAddExpression(expression);

case ExpressionType.OrElse:
case ExpressionType.Subtract:
case ExpressionType.Multiply:
case ExpressionType.Divide:
Expand Down Expand Up @@ -575,6 +577,38 @@ var nullExpression
: null;
}

private Expression ProcessAddExpression(BinaryExpression addExpression)
{
var leftExpression = Visit(addExpression.Left);
var rightExpression = Visit(addExpression.Right);

if (leftExpression != null && rightExpression != null)
{
if (leftExpression.Type == typeof(string)
&& leftExpression.RemoveConvert() is ColumnExpression leftColumnExpression
&& leftColumnExpression.Property.IsNullable)
{
leftExpression = Expression.Coalesce(leftExpression, Expression.Constant(string.Empty));
}

if (rightExpression.Type == typeof(string)
&& rightExpression.RemoveConvert() is ColumnExpression rightColumnExpression
&& rightColumnExpression.Property.IsNullable)
{
rightExpression = Expression.Coalesce(rightExpression, Expression.Constant(string.Empty));
}

return Expression.MakeBinary(
addExpression.NodeType,
leftExpression,
rightExpression,
addExpression.IsLiftedToNull,
addExpression.Method);
}

return null;
}

private static Expression TransformNullComparison(
Expression left, Expression right, ExpressionType expressionType)
{
Expand Down
11 changes: 11 additions & 0 deletions test/EFCore.Specification.Tests/Query/SimpleQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3743,6 +3743,17 @@ public virtual Task Environment_newline_is_funcletized(bool isAsync)
cs => cs.Where(c => c.CustomerID.Contains(Environment.NewLine)));
}

[ConditionalFact]
public virtual void String_concat_with_null_produces_string()
{
using (var context = CreateContext())
{
var query = context.Customers.Select(c => new { c.City, c.Region, Concat = c.City + " " + c.Region }).ToList();

Assert.All(query, t => Assert.Equal(t.City + " " + t.Region, t.Concat));
}
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task String_concat_with_navigation1(bool isAsync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2809,6 +2809,15 @@ public override void Random_next_is_not_funcletized_6()
FROM [Orders] AS [o]");
}

public override void String_concat_with_null_produces_string()
{
base.String_concat_with_null_produces_string();

AssertSql(
@"SELECT [c].[City], [c].[Region], (COALESCE([c].[City], N'') + N' ') + COALESCE([c].[Region], N'') AS [Concat]
FROM [Customers] AS [c]");
}

public override async Task Environment_newline_is_funcletized(bool isAsync)
{
await base.Environment_newline_is_funcletized(isAsync);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ public override async Task String_Contains_MethodCall(bool isAsync)
WHERE instr(""c"".""ContactName"", 'M') > 0");
}

public override void String_concat_with_null_produces_string()
{
base.String_concat_with_null_produces_string();

AssertSql(
@"SELECT ""c"".""City"", ""c"".""Region"", (COALESCE(""c"".""City"", '') || ' ') || COALESCE(""c"".""Region"", '') AS ""Concat""
FROM ""Customers"" AS ""c""");
}

public override async Task IsNullOrWhiteSpace_in_predicate(bool isAsync)
{
await base.IsNullOrWhiteSpace_in_predicate(isAsync);
Expand Down