From 1fb773583cc8b15bddfefe52b2f32656368b6d66 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Fri, 15 Jan 2021 11:25:43 +0100 Subject: [PATCH] Generate parentheses for PostgresBinaryExpression Fixes #1652 --- .../Query/Internal/NpgsqlQuerySqlGenerator.cs | 2 +- .../FullTextSearchDbFunctionsNpgsqlTest.cs | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs index 670df2ee1..4bfe19e5e 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs @@ -674,6 +674,6 @@ public virtual Expression VisitPgFunction([NotNull] PostgresFunctionExpression e } static bool RequiresBrackets(SqlExpression expression) - => expression is SqlBinaryExpression || expression is LikeExpression; + => expression is SqlBinaryExpression || expression is LikeExpression || expression is PostgresBinaryExpression; } } diff --git a/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs index b68224076..b05df0640 100644 --- a/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/FullTextSearchDbFunctionsNpgsqlTest.cs @@ -19,7 +19,7 @@ public FullTextSearchDbFunctionsNpgsqlTest(NorthwindQueryNpgsqlFixture EF.Functions.Unaccent(x.ContactName)) .FirstOrDefault(); @@ -877,7 +877,7 @@ public void Unaccent() public void Unaccent_with_constant_regdictionary() { using var context = CreateContext(); - var _ = context.Customers + _ = context.Customers .Select(x => EF.Functions.Unaccent("unaccent", x.ContactName)) .FirstOrDefault(); @@ -891,7 +891,7 @@ public void Unaccent_with_parameter_regdictionary() { using var context = CreateContext(); var regDictionary = "unaccent"; - var _ = context.Customers + _ = context.Customers .Select(x => EF.Functions.Unaccent(regDictionary, x.ContactName)) .FirstOrDefault(); @@ -903,6 +903,20 @@ SELECT unaccent(@__regDictionary_1::regdictionary, c.""ContactName"") LIMIT 1"); } + [Fact] // #1652 + public void Match_and_boolean_operator_precedence() + { + using var context = CreateContext(); + _ = context.Customers + .Count(c => EF.Functions.ToTsVector(c.ContactTitle) + .Matches(EF.Functions.ToTsQuery("owner").Or(EF.Functions.ToTsQuery("foo")))); + + AssertSql( + @"SELECT COUNT(*)::INT +FROM ""Customers"" AS c +WHERE to_tsvector(c.""ContactTitle"") @@ (to_tsquery('owner') || to_tsquery('foo'))"); + } + void AssertSql(params string[] expected) => Fixture.TestSqlLoggerFactory.AssertBaseline(expected);