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

Query: Fix double negation in SQL generation #26734

Merged
1 commit merged into from
Nov 21, 2021
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
43 changes: 43 additions & 0 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,20 +676,56 @@ protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpressio
case ExpressionType.Not:
{
_relationalCommandBuilder.Append("~");

var requiresBrackets = RequiresParentheses(sqlUnaryExpression, sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append("(");
}

Visit(sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append(")");
}

break;
}

case ExpressionType.Equal:
{

var requiresBrackets = RequiresParentheses(sqlUnaryExpression, sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append("(");
}

Visit(sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append(")");
}

_relationalCommandBuilder.Append(" IS NULL");
break;
}

case ExpressionType.NotEqual:
{

var requiresBrackets = RequiresParentheses(sqlUnaryExpression, sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append("(");
}

Visit(sqlUnaryExpression.Operand);
if (requiresBrackets)
{
_relationalCommandBuilder.Append(")");
}
AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved

_relationalCommandBuilder.Append(" IS NOT NULL");
break;
}
Expand Down Expand Up @@ -799,6 +835,13 @@ protected virtual bool RequiresParentheses(SqlExpression outerExpression, SqlExp
return true;
}

if (sqlUnaryExpression.OperatorType == ExpressionType.Negate
&& outerExpression is SqlUnaryExpression { OperatorType: ExpressionType.Negate })
{
// double negative sign is interpreted as a comment in SQL, so we need to enclose it in brackets
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public virtual Task Negate_on_column(bool async)
async,
ss => ss.Set<Squad>().Where(s => s.Id == -s.Id));

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Double_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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ FROM [Squads] AS [s]
WHERE [s].[Id] = -[s].[Id]");
}

public override async Task Double_negate_on_column(bool async)
{
await base.Double_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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ FROM [Squads] AS [s]
WHERE [s].[Id] = -[s].[Id]");
}

public override async Task Double_negate_on_column(bool async)
{
await base.Double_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);
Expand Down