diff --git a/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs b/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs index a08841312fc..6d9697aaf90 100644 --- a/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs +++ b/src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs @@ -433,11 +433,14 @@ static Expression RemoveConvert(Expression e) return result; } + var isBooleanExpression = binaryExpression.Type == typeof(bool) || binaryExpression.Type == typeof(bool?); var uncheckedNodeTypeVariant = binaryExpression.NodeType switch { ExpressionType.AddChecked => ExpressionType.Add, ExpressionType.SubtractChecked => ExpressionType.Subtract, ExpressionType.MultiplyChecked => ExpressionType.Multiply, + ExpressionType.And when isBooleanExpression => ExpressionType.AndAlso, + ExpressionType.Or when isBooleanExpression => ExpressionType.OrElse, _ => binaryExpression.NodeType }; diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs index 3c73c09d579..4b321787455 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindMiscellaneousQuerySqlServerTest.cs @@ -3121,10 +3121,7 @@ public override async Task Select_bitwise_or(bool async) AssertSql( """ SELECT [c].[CustomerID], CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) + WHEN [c].[CustomerID] IN (N'ALFKI', N'ANATR') THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END AS [Value] FROM [Customers] AS [c] @@ -3139,13 +3136,7 @@ public override async Task Select_bitwise_or_multiple(bool async) AssertSql( """ SELECT [c].[CustomerID], CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [c].[CustomerID] = N'ANTON' THEN CAST(1 AS bit) + WHEN [c].[CustomerID] IN (N'ALFKI', N'ANATR', N'ANTON') THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END AS [Value] FROM [Customers] AS [c] @@ -3159,13 +3150,7 @@ public override async Task Select_bitwise_and(bool async) AssertSql( """ -SELECT [c].[CustomerID], CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END & CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END AS [Value] +SELECT [c].[CustomerID], CAST(0 AS bit) AS [Value] FROM [Customers] AS [c] ORDER BY [c].[CustomerID] """); @@ -3177,13 +3162,7 @@ public override async Task Select_bitwise_and_or(bool async) AssertSql( """ -SELECT [c].[CustomerID], (CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END & CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END) | CASE +SELECT [c].[CustomerID], CASE WHEN [c].[CustomerID] = N'ANTON' THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END AS [Value] @@ -3200,13 +3179,7 @@ public override async Task Where_bitwise_or_with_logical_or(bool async) """ SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] -WHERE CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) OR [c].[CustomerID] = N'ANTON' +WHERE [c].[CustomerID] IN (N'ALFKI', N'ANATR', N'ANTON') """); } @@ -3218,13 +3191,7 @@ public override async Task Where_bitwise_and_with_logical_and(bool async) """ SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] -WHERE CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END & CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) AND [c].[CustomerID] = N'ANTON' +WHERE 0 = 1 """); } @@ -3236,13 +3203,7 @@ public override async Task Where_bitwise_or_with_logical_and(bool async) """ SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] -WHERE CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) AND [c].[Country] = N'Germany' +WHERE [c].[CustomerID] IN (N'ALFKI', N'ANATR') AND [c].[Country] = N'Germany' """); } @@ -3254,13 +3215,7 @@ public override async Task Where_bitwise_and_with_logical_or(bool async) """ SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] -WHERE CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END & CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) OR [c].[CustomerID] = N'ANTON' +WHERE [c].[CustomerID] = N'ANTON' """); } @@ -3309,13 +3264,7 @@ public override async Task Select_bitwise_or_with_logical_or(bool async) AssertSql( """ SELECT [c].[CustomerID], CASE - WHEN CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) - END | CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) - END = CAST(1 AS bit) OR [c].[CustomerID] = N'ANTON' THEN CAST(1 AS bit) + WHEN [c].[CustomerID] IN (N'ALFKI', N'ANATR', N'ANTON') THEN CAST(1 AS bit) ELSE CAST(0 AS bit) END AS [Value] FROM [Customers] AS [c] @@ -3329,16 +3278,7 @@ public override async Task Select_bitwise_and_with_logical_and(bool async) AssertSql( """ -SELECT [c].[CustomerID], CASE - WHEN CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) - END & CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) - END = CAST(1 AS bit) AND [c].[CustomerID] = N'ANTON' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END AS [Value] +SELECT [c].[CustomerID], CAST(0 AS bit) AS [Value] FROM [Customers] AS [c] ORDER BY [c].[CustomerID] """); diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs index 327a8a3d1e6..5b4c0ba2e7f 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NorthwindWhereQuerySqlServerTest.cs @@ -497,13 +497,7 @@ public override async Task Where_bitwise_or(bool async) """ SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] -WHERE CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) +WHERE [c].[CustomerID] IN (N'ALFKI', N'ANATR') """); } @@ -515,13 +509,7 @@ public override async Task Where_bitwise_and(bool async) """ SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region] FROM [Customers] AS [c] -WHERE CASE - WHEN [c].[CustomerID] = N'ALFKI' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END & CASE - WHEN [c].[CustomerID] = N'ANATR' THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) +WHERE 0 = 1 """); } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs index 24575897ce7..28d54ae44ad 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs @@ -3130,10 +3130,7 @@ FROM [Entities1] AS [e] """ SELECT [e].[Id], [e].[BoolA], [e].[BoolB], [e].[BoolC], [e].[IntA], [e].[IntB], [e].[IntC], [e].[NullableBoolA], [e].[NullableBoolB], [e].[NullableBoolC], [e].[NullableIntA], [e].[NullableIntB], [e].[NullableIntC], [e].[NullableStringA], [e].[NullableStringB], [e].[NullableStringC], [e].[StringA], [e].[StringB], [e].[StringC] FROM [Entities1] AS [e] -WHERE [e].[BoolB] | CASE - WHEN [e].[NullableBoolA] IS NOT NULL THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) +WHERE [e].[BoolB] = CAST(1 AS bit) OR [e].[NullableBoolA] IS NOT NULL """); } diff --git a/test/EFCore.SqlServer.FunctionalTests/Query/OperatorsQuerySqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Query/OperatorsQuerySqlServerTest.cs index f6287d64d4e..5aaa9b477df 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Query/OperatorsQuerySqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Query/OperatorsQuerySqlServerTest.cs @@ -48,13 +48,7 @@ CROSS JOIN [OperatorEntityString] AS [o0] CROSS JOIN [OperatorEntityString] AS [o1] CROSS JOIN [OperatorEntityString] AS [o2] CROSS JOIN [OperatorEntityInt] AS [o3] -WHERE CASE - WHEN [o].[Value] = N'A' AND [o].[Value] IS NOT NULL AND [o0].[Value] = N'A' AND [o0].[Value] IS NOT NULL THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END | CASE - WHEN [o1].[Value] = N'B' AND [o1].[Value] IS NOT NULL AND [o2].[Value] = N'B' AND [o2].[Value] IS NOT NULL THEN CAST(1 AS bit) - ELSE CAST(0 AS bit) -END = CAST(1 AS bit) AND [o3].[Value] = 2 +WHERE (([o].[Value] = N'A' AND [o0].[Value] = N'A') OR ([o1].[Value] = N'B' AND [o2].[Value] = N'B')) AND [o3].[Value] = 2 ORDER BY [o].[Id], [o0].[Id], [o1].[Id], [o2].[Id], [o3].[Id] """); } diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/NullSemanticsQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/NullSemanticsQuerySqliteTest.cs index 6036dbdc033..c0feb0490a8 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/NullSemanticsQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/NullSemanticsQuerySqliteTest.cs @@ -393,7 +393,7 @@ WHERE 0 """ SELECT "e"."Id", "e"."BoolA", "e"."BoolB", "e"."BoolC", "e"."IntA", "e"."IntB", "e"."IntC", "e"."NullableBoolA", "e"."NullableBoolB", "e"."NullableBoolC", "e"."NullableIntA", "e"."NullableIntB", "e"."NullableIntC", "e"."NullableStringA", "e"."NullableStringB", "e"."NullableStringC", "e"."StringA", "e"."StringB", "e"."StringC" FROM "Entities1" AS "e" -WHERE "e"."BoolB" | ("e"."NullableBoolA" IS NOT NULL) +WHERE "e"."BoolB" OR "e"."NullableBoolA" IS NOT NULL """); } diff --git a/test/EFCore.Sqlite.FunctionalTests/Query/OperatorsQuerySqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Query/OperatorsQuerySqliteTest.cs index 8f2e9821f94..e39a4c4f377 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Query/OperatorsQuerySqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Query/OperatorsQuerySqliteTest.cs @@ -46,7 +46,7 @@ CROSS JOIN "OperatorEntityString" AS "o0" CROSS JOIN "OperatorEntityString" AS "o1" CROSS JOIN "OperatorEntityString" AS "o2" CROSS JOIN "OperatorEntityInt" AS "o3" -WHERE ("o"."Value" = 'A' AND "o"."Value" IS NOT NULL AND "o0"."Value" = 'A' AND "o0"."Value" IS NOT NULL) | ("o1"."Value" = 'B' AND "o1"."Value" IS NOT NULL AND "o2"."Value" = 'B' AND "o2"."Value" IS NOT NULL) AND "o3"."Value" = 2 +WHERE (("o"."Value" = 'A' AND "o0"."Value" = 'A') OR ("o1"."Value" = 'B' AND "o2"."Value" = 'B')) AND "o3"."Value" = 2 ORDER BY "o"."Id", "o0"."Id", "o1"."Id", "o2"."Id", "o3"."Id" """); }