Skip to content
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

Fix Issue 13919: Translation error using negative #21389

Merged
merged 5 commits into from
Jun 30, 2020
Merged
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
10 changes: 10 additions & 0 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,17 @@ when sqlUnaryExpression.Type.UnwrapNullableType() == typeof(bool):
case ExpressionType.Negate:
{
_relationalCommandBuilder.Append("-");
var requiresBrackets = RequiresBrackets(sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append("(");
}

Visit(sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append(")");
}
break;
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ protected GearsOfWarQueryTestBase(TFixture fixture)
{
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Negate_on_binary_expression(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Squad>().Where(s => s.Id == -(s.Id + s.Id)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Negate_on_column(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Squad>().Where(s => s.Id == -s.Id));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Negate_on_like_expression(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Squad>().Where(s => !s.Name.StartsWith("us")));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Entity_equality_empty(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@ public GearsOfWarQuerySqlServerTest(GearsOfWarQuerySqlServerFixture fixture, ITe

protected override bool CanExecuteQueryString => true;

public override async Task Negate_on_binary_expression(bool async)
{
await base.Negate_on_binary_expression(async);

AssertSql(
@"SELECT [s].[Id], [s].[Banner], [s].[Banner5], [s].[InternalNumber], [s].[Name]
FROM [Squads] AS [s]
WHERE [s].[Id] = -([s].[Id] + [s].[Id])");
}

public override async Task Negate_on_column(bool async)
{
await base.Negate_on_column(async);

AssertSql(
@"SELECT [s].[Id], [s].[Banner], [s].[Banner5], [s].[InternalNumber], [s].[Name]
FROM [Squads] AS [s]
WHERE [s].[Id] = -[s].[Id]");
}

public override async Task Negate_on_like_expression(bool async)
{
await base.Negate_on_like_expression(async);

AssertSql(
@"SELECT [s].[Id], [s].[Banner], [s].[Banner5], [s].[InternalNumber], [s].[Name]
FROM [Squads] AS [s]
WHERE [s].[Name] IS NOT NULL AND NOT ([s].[Name] LIKE N'us%')");
}

public override async Task Entity_equality_empty(bool async)
{
await base.Entity_equality_empty(async);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ public override Task DateTimeOffset_Date_returns_datetime(bool async)

public override Task Subquery_projecting_non_nullable_scalar_contains_non_nullable_value_doesnt_need_null_expansion_negated(bool async) => null;

public override async Task Negate_on_binary_expression(bool async)
{
await base.Negate_on_binary_expression(async);

AssertSql(
@"SELECT ""s"".""Id"", ""s"".""Banner"", ""s"".""Banner5"", ""s"".""InternalNumber"", ""s"".""Name""
FROM ""Squads"" AS ""s""
WHERE ""s"".""Id"" = -(""s"".""Id"" + ""s"".""Id"")");
}

public override async Task Negate_on_column(bool async)
{
await base.Negate_on_column(async);

AssertSql(
@"SELECT ""s"".""Id"", ""s"".""Banner"", ""s"".""Banner5"", ""s"".""InternalNumber"", ""s"".""Name""
FROM ""Squads"" AS ""s""
WHERE ""s"".""Id"" = -""s"".""Id""");
}

public override async Task Negate_on_like_expression(bool async)
{
await base.Negate_on_like_expression(async);

AssertSql(
@"SELECT ""s"".""Id"", ""s"".""Banner"", ""s"".""Banner5"", ""s"".""InternalNumber"", ""s"".""Name""
FROM ""Squads"" AS ""s""
WHERE ""s"".""Name"" IS NOT NULL AND NOT (""s"".""Name"" LIKE 'us%')");
}

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