diff --git a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs index 42e9684c3..0c3c4494c 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs @@ -1401,7 +1401,7 @@ protected override bool RequiresParentheses(SqlExpression outerExpression, SqlEx // If both operators have the same precedence, add parentheses unless they're the same operator, and // that operator is associative (e.g. a + b + c) - 0 => outerExpression is not PgBinaryExpression outerBinary + _ => outerExpression is not PgBinaryExpression outerBinary || outerBinary.OperatorType != innerBinary.OperatorType || !isOuterAssociative // Arithmetic operators on floating points aren't associative, because of rounding errors. @@ -1416,6 +1416,9 @@ protected override bool RequiresParentheses(SqlExpression outerExpression, SqlEx return true; } + case PgUnknownBinaryExpression: + return true; + default: return base.RequiresParentheses(outerExpression, innerExpression); } diff --git a/test/EFCore.PG.FunctionalTests/Query/TrigramsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/TrigramsQueryNpgsqlTest.cs index e9b5e1d85..d42779741 100644 --- a/test/EFCore.PG.FunctionalTests/Query/TrigramsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/TrigramsQueryNpgsqlTest.cs @@ -27,7 +27,7 @@ public TrigramsQueryNpgsqlTest(TrigramsQueryNpgsqlFixture fixture, ITestOutputHe public void TrigramsShow() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsShow(x.Text)) .ToArray(); @@ -38,7 +38,7 @@ public void TrigramsShow() public void TrigramsSimilarity() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsSimilarity(x.Text, "target")) .ToArray(); @@ -49,7 +49,7 @@ public void TrigramsSimilarity() public void TrigramsWordSimilarity() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsWordSimilarity(x.Text, "target")) .ToArray(); @@ -61,7 +61,7 @@ public void TrigramsWordSimilarity() public void TrigramsStrictWordSimilarity() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsStrictWordSimilarity(x.Text, "target")) .ToArray(); @@ -72,7 +72,7 @@ public void TrigramsStrictWordSimilarity() public void TrigramsAreSimilar() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsAreSimilar(x.Text, "target")) .ToArray(); @@ -83,7 +83,7 @@ public void TrigramsAreSimilar() public void TrigramsAreWordSimilar() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsAreWordSimilar(x.Text, "target")) .ToArray(); @@ -94,7 +94,7 @@ public void TrigramsAreWordSimilar() public void TrigramsAreNotWordSimilar() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsAreNotWordSimilar(x.Text, "target")) .ToArray(); @@ -106,7 +106,7 @@ public void TrigramsAreNotWordSimilar() public void TrigramsAreStrictWordSimilar() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsAreStrictWordSimilar(x.Text, "target")) .ToArray(); @@ -118,7 +118,7 @@ public void TrigramsAreStrictWordSimilar() public void TrigramsAreNotStrictWordSimilar() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsAreNotStrictWordSimilar(x.Text, "target")) .ToArray(); @@ -129,7 +129,7 @@ public void TrigramsAreNotStrictWordSimilar() public void TrigramsSimilarityDistance() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsSimilarityDistance(x.Text, "target")) .ToArray(); @@ -140,7 +140,7 @@ public void TrigramsSimilarityDistance() public void TrigramsWordSimilarityDistance() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsWordSimilarityDistance(x.Text, "target")) .ToArray(); @@ -151,7 +151,7 @@ public void TrigramsWordSimilarityDistance() public void TrigramsWordSimilarityDistanceInverted() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsWordSimilarityDistanceInverted(x.Text, "target")) .ToArray(); @@ -163,7 +163,7 @@ public void TrigramsWordSimilarityDistanceInverted() public void TrigramsStrictWordSimilarityDistance() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsStrictWordSimilarityDistance(x.Text, "target")) .ToArray(); @@ -175,7 +175,7 @@ public void TrigramsStrictWordSimilarityDistance() public void TrigramsStrictWordSimilarityDistanceInverted() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Select(x => EF.Functions.TrigramsStrictWordSimilarityDistanceInverted(x.Text, "target")) .ToArray(); @@ -183,10 +183,10 @@ public void TrigramsStrictWordSimilarityDistanceInverted() } [Fact] // #1659 - public void Operator_precedence() + public void Concatenation_operator_precedence() { using var context = CreateContext(); - var _ = context.TrigramsTestEntities + _ = context.TrigramsTestEntities .Where(e => EF.Functions.TrigramsAreSimilar(e.Text + " " + e.Text, "query")) .ToArray(); @@ -198,6 +198,22 @@ public void Operator_precedence() """); } + [Fact] // #3072 + public void PgUnknownBinary_operator_precedence() + { + using var context = CreateContext(); + _ = context.TrigramsTestEntities + .Where(e => 1 - EF.Functions.TrigramsSimilarityDistance(e.Text, "query") > 8) + .ToArray(); + + AssertSql( + """ +SELECT t."Id", t."Text" +FROM "TrigramsTestEntities" AS t +WHERE 1 - (t."Text" <-> 'query') > 8 +"""); + } + #endregion #region Fixtures