Skip to content

Commit

Permalink
Always add parentheses around PgUnknownBinaryExpression (#3073)
Browse files Browse the repository at this point in the history
Fixes #3072
  • Loading branch information
roji authored Jan 25, 2024
1 parent e6d545a commit 35f0fd5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -1416,6 +1416,9 @@ protected override bool RequiresParentheses(SqlExpression outerExpression, SqlEx
return true;
}

case PgUnknownBinaryExpression:
return true;

default:
return base.RequiresParentheses(outerExpression, innerExpression);
}
Expand Down
48 changes: 32 additions & 16 deletions test/EFCore.PG.FunctionalTests/Query/TrigramsQueryNpgsqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -175,18 +175,18 @@ public void TrigramsStrictWordSimilarityDistance()
public void TrigramsStrictWordSimilarityDistanceInverted()
{
using var context = CreateContext();
var _ = context.TrigramsTestEntities
_ = context.TrigramsTestEntities
.Select(x => EF.Functions.TrigramsStrictWordSimilarityDistanceInverted(x.Text, "target"))
.ToArray();

AssertContainsSql(@"t.""Text"" <->>> 'target'");
}

[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();

Expand All @@ -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
Expand Down

0 comments on commit 35f0fd5

Please sign in to comment.