Skip to content

Commit

Permalink
Add parentheses around SQL IS NULL
Browse files Browse the repository at this point in the history
Fixes #29222
  • Loading branch information
roji committed Sep 28, 2022
1 parent 754d193 commit b53cde3
Show file tree
Hide file tree
Showing 72 changed files with 3,557 additions and 3,559 deletions.
6 changes: 2 additions & 4 deletions src/EFCore.Relational/Query/QuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,8 @@ protected virtual bool RequiresParentheses(SqlExpression outerExpression, SqlExp

case SqlUnaryExpression sqlUnaryExpression:
{
// Wrap IS (NOT) NULL operation when applied on bool column.
if ((sqlUnaryExpression.OperatorType == ExpressionType.Equal
|| sqlUnaryExpression.OperatorType == ExpressionType.NotEqual)
&& sqlUnaryExpression.Operand.Type == typeof(bool))
// Wrap IS (NOT) NULL operation
if (sqlUnaryExpression.OperatorType is ExpressionType.Equal or ExpressionType.NotEqual)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override async Task Delete_predicate_based_on_optional_navigation(bool as
@"DELETE FROM [p]
FROM [Posts] AS [p]
LEFT JOIN [Blogs] AS [b] ON [p].[BlogId] = [b].[Id]
WHERE [b].[Title] IS NOT NULL AND ([b].[Title] LIKE N'Arthur%')");
WHERE ([b].[Title] IS NOT NULL) AND ([b].[Title] LIKE N'Arthur%')");
}

private void AssertSql(params string[] expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ FROM [Order Details] AS [o]
SELECT TOP(1) (
SELECT TOP(1) [o1].[OrderID]
FROM [Orders] AS [o1]
WHERE [o0].[CustomerID] = [o1].[CustomerID] OR ([o0].[CustomerID] IS NULL AND [o1].[CustomerID] IS NULL))
WHERE [o0].[CustomerID] = [o1].[CustomerID] OR (([o0].[CustomerID] IS NULL) AND ([o1].[CustomerID] IS NULL)))
FROM [Orders] AS [o0]
GROUP BY [o0].[CustomerID]
HAVING COUNT(*) > 11)");
Expand All @@ -217,7 +217,7 @@ GROUP BY [o1].[CustomerID]
HAVING COUNT(*) > 9 AND (
SELECT TOP(1) [o2].[OrderID]
FROM [Orders] AS [o2]
WHERE [o1].[CustomerID] = [o2].[CustomerID] OR ([o1].[CustomerID] IS NULL AND [o2].[CustomerID] IS NULL)) = [o0].[OrderID])");
WHERE [o1].[CustomerID] = [o2].[CustomerID] OR (([o1].[CustomerID] IS NULL) AND ([o2].[CustomerID] IS NULL))) = [o0].[OrderID])");
}

public override async Task Delete_GroupBy_Where_Select(bool async)
Expand Down Expand Up @@ -321,7 +321,7 @@ public override async Task Delete_Where_using_navigation_2(bool async)
FROM [Order Details] AS [o]
INNER JOIN [Orders] AS [o0] ON [o].[OrderID] = [o0].[OrderID]
LEFT JOIN [Customers] AS [c] ON [o0].[CustomerID] = [c].[CustomerID]
WHERE [c].[CustomerID] IS NOT NULL AND ([c].[CustomerID] LIKE N'F%')");
WHERE ([c].[CustomerID] IS NOT NULL) AND ([c].[CustomerID] LIKE N'F%')");
}

public override async Task Delete_Union(bool async)
Expand Down Expand Up @@ -455,7 +455,7 @@ public override async Task Delete_Where_optional_navigation_predicate(bool async
FROM [Order Details] AS [o]
INNER JOIN [Orders] AS [o0] ON [o].[OrderID] = [o0].[OrderID]
LEFT JOIN [Customers] AS [c] ON [o0].[CustomerID] = [c].[CustomerID]
WHERE [c].[City] IS NOT NULL AND ([c].[City] LIKE N'Se%')");
WHERE ([c].[City] IS NOT NULL) AND ([c].[City] LIKE N'Se%')");
}

public override async Task Delete_with_join(bool async)
Expand Down Expand Up @@ -826,7 +826,7 @@ FROM [Customers] AS [c]
SELECT TOP(1) (
SELECT TOP(1) [o0].[CustomerID]
FROM [Orders] AS [o0]
WHERE [o].[CustomerID] = [o0].[CustomerID] OR ([o].[CustomerID] IS NULL AND [o0].[CustomerID] IS NULL))
WHERE [o].[CustomerID] = [o0].[CustomerID] OR (([o].[CustomerID] IS NULL) AND ([o0].[CustomerID] IS NULL)))
FROM [Orders] AS [o]
GROUP BY [o].[CustomerID]
HAVING COUNT(*) > 11)");
Expand Down Expand Up @@ -855,7 +855,7 @@ HAVING COUNT(*) > 11 AND (
SELECT TOP(1) [c0].[CustomerID]
FROM [Orders] AS [o0]
LEFT JOIN [Customers] AS [c0] ON [o0].[CustomerID] = [c0].[CustomerID]
WHERE [o].[CustomerID] = [o0].[CustomerID] OR ([o].[CustomerID] IS NULL AND [o0].[CustomerID] IS NULL)) = [c].[CustomerID])");
WHERE [o].[CustomerID] = [o0].[CustomerID] OR (([o].[CustomerID] IS NULL) AND ([o0].[CustomerID] IS NULL))) = [c].[CustomerID])");
}

public override async Task Update_Where_Distinct_set_constant(bool async)
Expand Down Expand Up @@ -1179,7 +1179,7 @@ FROM [Customers] AS [c]
CROSS JOIN (
SELECT [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region]
FROM [Customers] AS [c0]
WHERE [c0].[City] IS NOT NULL AND ([c0].[City] LIKE N'S%')
WHERE ([c0].[City] IS NOT NULL) AND ([c0].[City] LIKE N'S%')
) AS [t]
LEFT JOIN (
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
Expand All @@ -1200,7 +1200,7 @@ FROM [Customers] AS [c]
CROSS JOIN (
SELECT [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region]
FROM [Customers] AS [c0]
WHERE [c0].[City] IS NOT NULL AND ([c0].[City] LIKE N'S%')
WHERE ([c0].[City] IS NOT NULL) AND ([c0].[City] LIKE N'S%')
) AS [t]
CROSS APPLY (
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
Expand All @@ -1221,7 +1221,7 @@ FROM [Customers] AS [c]
CROSS JOIN (
SELECT [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region]
FROM [Customers] AS [c0]
WHERE [c0].[City] IS NOT NULL AND ([c0].[City] LIKE N'S%')
WHERE ([c0].[City] IS NOT NULL) AND ([c0].[City] LIKE N'S%')
) AS [t]
OUTER APPLY (
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Id] = [b].[Id]
LEFT JOIN [Eagle] AS [e] ON [a].[Id] = [e].[Id]
LEFT JOIN [Kiwi] AS [k] ON [a].[Id] = [k].[Id]
WHERE [a].[CountryId] = 1 AND [c].[Id] = [a].[CountryId] AND [k].[Id] IS NOT NULL AND [a].[CountryId] > 0) > 0");
WHERE [a].[CountryId] = 1 AND [c].[Id] = [a].[CountryId] AND ([k].[Id] IS NOT NULL) AND [a].[CountryId] > 0) > 0");
}

public override async Task Delete_where_keyless_entity_mapped_to_sql_query(bool async)
Expand Down Expand Up @@ -149,7 +149,7 @@ FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Id] = [b].[Id]
LEFT JOIN [Eagle] AS [e] ON [a].[Id] = [e].[Id]
LEFT JOIN [Kiwi] AS [k] ON [a].[Id] = [k].[Id]
WHERE [a].[CountryId] = 1 AND [c].[Id] = [a].[CountryId] AND [k].[Id] IS NOT NULL AND [a].[CountryId] > 0) > 0");
WHERE [a].[CountryId] = 1 AND [c].[Id] = [a].[CountryId] AND ([k].[Id] IS NOT NULL) AND [a].[CountryId] > 0) > 0");
}

public override async Task Update_where_keyless_entity_mapped_to_sql_query(bool async)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ FROM [Animals] AS [a]
LEFT JOIN [Birds] AS [b] ON [a].[Id] = [b].[Id]
LEFT JOIN [Eagle] AS [e] ON [a].[Id] = [e].[Id]
LEFT JOIN [Kiwi] AS [k] ON [a].[Id] = [k].[Id]
WHERE [c].[Id] = [a].[CountryId] AND [k].[Id] IS NOT NULL AND [a].[CountryId] > 0) > 0");
WHERE [c].[Id] = [a].[CountryId] AND ([k].[Id] IS NOT NULL) AND [a].[CountryId] > 0) > 0");
}

public override async Task Update_where_keyless_entity_mapped_to_sql_query(bool async)
Expand Down
Loading

0 comments on commit b53cde3

Please sign in to comment.