Skip to content

Commit

Permalink
Remove IS NULL parentheses hack (#2534)
Browse files Browse the repository at this point in the history
(leaving the one on the EF side)

Closes #2105
  • Loading branch information
roji authored Oct 15, 2022
1 parent 922fa77 commit 95aa5c4
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 104 deletions.
15 changes: 0 additions & 15 deletions src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,21 +478,6 @@ protected override Expression VisitSqlUnary(SqlUnaryExpression sqlUnaryExpressio
Sql.Append("!!");
Visit(sqlUnaryExpression.Operand);
return sqlUnaryExpression;

// EF uses unary Equal and NotEqual to represent is-null checking.
// These need to be surrounded in parentheses in various cases (e.g. where TRUE = x IS NOT NULL),
// see
case ExpressionType.Equal:
Sql.Append("(");
Visit(sqlUnaryExpression.Operand);
Sql.Append(" IS NULL)");
return sqlUnaryExpression;

case ExpressionType.NotEqual:
Sql.Append("(");
Visit(sqlUnaryExpression.Operand);
Sql.Append(" IS NOT NULL)");
return sqlUnaryExpression;
}

return base.VisitSqlUnary(sqlUnaryExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ WHERE EXISTS (
SELECT 1
FROM "Posts" AS p0
LEFT JOIN "Blogs" AS b ON p0."BlogId" = b."Id"
WHERE ((b."Title" IS NOT NULL)) AND (b."Title" LIKE 'Arthur%') AND p0."Id" = p."Id")
WHERE (b."Title" IS NOT NULL) AND (b."Title" LIKE 'Arthur%') AND p0."Id" = p."Id")
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ DELETE FROM "Order Details" AS o
SELECT (
SELECT 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))
LIMIT 1)
FROM "Orders" AS o0
GROUP BY o0."CustomerID"
Expand All @@ -233,7 +233,7 @@ GROUP BY o1."CustomerID"
HAVING count(*)::int > 9 AND (
SELECT o2."OrderID"
FROM "Orders" AS o2
WHERE o1."CustomerID" = o2."CustomerID" OR (((o1."CustomerID" IS NULL)) AND ((o2."CustomerID" IS NULL)))
WHERE o1."CustomerID" = o2."CustomerID" OR ((o1."CustomerID" IS NULL) AND (o2."CustomerID" IS NULL))
LIMIT 1) = o0."OrderID")
""");
}
Expand Down Expand Up @@ -345,7 +345,7 @@ SELECT 1
FROM "Order Details" AS o0
INNER JOIN "Orders" AS o1 ON o0."OrderID" = o1."OrderID"
LEFT JOIN "Customers" AS c ON o1."CustomerID" = c."CustomerID"
WHERE ((c."CustomerID" IS NOT NULL)) AND (c."CustomerID" LIKE 'F%') AND o0."OrderID" = o."OrderID" AND o0."ProductID" = o."ProductID")
WHERE (c."CustomerID" IS NOT NULL) AND (c."CustomerID" LIKE 'F%') AND o0."OrderID" = o."OrderID" AND o0."ProductID" = o."ProductID")
""");
}

Expand Down Expand Up @@ -488,7 +488,7 @@ SELECT 1
FROM "Order Details" AS o0
INNER JOIN "Orders" AS o1 ON o0."OrderID" = o1."OrderID"
LEFT JOIN "Customers" AS c ON o1."CustomerID" = c."CustomerID"
WHERE ((c."City" IS NOT NULL)) AND (c."City" LIKE 'Se%') AND o0."OrderID" = o."OrderID" AND o0."ProductID" = o."ProductID")
WHERE (c."City" IS NOT NULL) AND (c."City" LIKE 'Se%') AND o0."OrderID" = o."OrderID" AND o0."ProductID" = o."ProductID")
""");
}

Expand Down Expand Up @@ -863,7 +863,7 @@ public override async Task Update_Where_GroupBy_First_set_constant(bool async)
SELECT (
SELECT 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))
LIMIT 1)
FROM "Orders" AS o
GROUP BY o."CustomerID"
Expand Down Expand Up @@ -895,7 +895,7 @@ HAVING count(*)::int > 11 AND (
SELECT 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)))
WHERE o."CustomerID" = o0."CustomerID" OR ((o."CustomerID" IS NULL) AND (o0."CustomerID" IS NULL))
LIMIT 1) = c."CustomerID")
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SELECT count(*)::int
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
""");
}

Expand Down Expand Up @@ -150,7 +150,7 @@ SELECT count(*)::int
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
""");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ SELECT count(*)::int
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
""");
}

Expand Down
18 changes: 9 additions & 9 deletions test/EFCore.PG.FunctionalTests/Query/ArrayArrayQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override async Task Nullable_value_array_index_compare_to_null(bool async
"""
SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE (s."NullableIntArray"[3] IS NULL)
WHERE s."NullableIntArray"[3] IS NULL
""");
}

Expand All @@ -81,7 +81,7 @@ public override async Task Nullable_reference_array_index_compare_to_null(bool a
"""
SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE (s."NullableStringArray"[3] IS NULL)
WHERE s."NullableStringArray"[3] IS NULL
""");
}

Expand Down Expand Up @@ -232,7 +232,7 @@ public override async Task Array_column_Contains_null_constant(bool async)
"""
SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE (array_position(s."NullableStringArray", NULL) IS NOT NULL)
WHERE array_position(s."NullableStringArray", NULL) IS NOT NULL
""");
}

Expand Down Expand Up @@ -298,7 +298,7 @@ await AssertQuery(

SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE s."NullableText" = ANY (@__array_0) OR (((s."NullableText" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableText" = ANY (@__array_0) OR ((s."NullableText" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand Down Expand Up @@ -353,7 +353,7 @@ public override void Array_param_with_null_Contains_non_nullable_not_found_negat

SELECT count(*)::int
FROM "SomeEntities" AS s
WHERE NOT (s."NonNullableText" = ANY (@__array_0) AND ((s."NonNullableText" = ANY (@__array_0) IS NOT NULL)))
WHERE NOT (s."NonNullableText" = ANY (@__array_0) AND (s."NonNullableText" = ANY (@__array_0) IS NOT NULL))
""");
}

Expand All @@ -371,7 +371,7 @@ public override void Array_param_with_null_Contains_nullable_not_found()

SELECT count(*)::int
FROM "SomeEntities" AS s
WHERE s."NullableText" = ANY (@__array_0) OR (((s."NullableText" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableText" = ANY (@__array_0) OR ((s."NullableText" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand All @@ -389,7 +389,7 @@ public override void Array_param_with_null_Contains_nullable_not_found_negated()

SELECT count(*)::int
FROM "SomeEntities" AS s
WHERE NOT (s."NullableText" = ANY (@__array_0) AND ((s."NullableText" = ANY (@__array_0) IS NOT NULL))) AND (((s."NullableText" IS NOT NULL)) OR ((array_position(@__array_0, NULL) IS NULL)))
WHERE NOT (s."NullableText" = ANY (@__array_0) AND (s."NullableText" = ANY (@__array_0) IS NOT NULL)) AND ((s."NullableText" IS NOT NULL) OR (array_position(@__array_0, NULL) IS NULL))
""");
}

Expand Down Expand Up @@ -485,7 +485,7 @@ await AssertQuery(

SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE s."NullableEnumConvertedToString" = ANY (@__array_0) OR (((s."NullableEnumConvertedToString" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableEnumConvertedToString" = ANY (@__array_0) OR ((s."NullableEnumConvertedToString" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand All @@ -504,7 +504,7 @@ await AssertQuery(

SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE s."NullableEnumConvertedToStringWithNonNullableLambda" = ANY (@__array_0) OR (((s."NullableEnumConvertedToStringWithNonNullableLambda" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableEnumConvertedToStringWithNonNullableLambda" = ANY (@__array_0) OR ((s."NullableEnumConvertedToStringWithNonNullableLambda" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand Down
18 changes: 9 additions & 9 deletions test/EFCore.PG.FunctionalTests/Query/ArrayListQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override async Task Nullable_value_array_index_compare_to_null(bool async
"""
SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE (s."NullableIntList"[3] IS NULL)
WHERE s."NullableIntList"[3] IS NULL
""");
}

Expand All @@ -81,7 +81,7 @@ public override async Task Nullable_reference_array_index_compare_to_null(bool a
"""
SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE (s."NullableStringList"[3] IS NULL)
WHERE s."NullableStringList"[3] IS NULL
""");
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public override async Task Array_column_Contains_null_constant(bool async)
"""
SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE (array_position(s."NullableStringList", NULL) IS NOT NULL)
WHERE array_position(s."NullableStringList", NULL) IS NOT NULL
""");
}

Expand Down Expand Up @@ -281,7 +281,7 @@ await AssertQuery(

SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE s."NullableText" = ANY (@__array_0) OR (((s."NullableText" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableText" = ANY (@__array_0) OR ((s."NullableText" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");

}
Expand Down Expand Up @@ -347,7 +347,7 @@ public override void Array_param_with_null_Contains_non_nullable_not_found_negat

SELECT count(*)::int
FROM "SomeEntities" AS s
WHERE NOT (s."NonNullableText" = ANY (@__array_0) AND ((s."NonNullableText" = ANY (@__array_0) IS NOT NULL)))
WHERE NOT (s."NonNullableText" = ANY (@__array_0) AND (s."NonNullableText" = ANY (@__array_0) IS NOT NULL))
""");
}

Expand All @@ -370,7 +370,7 @@ public override void Array_param_with_null_Contains_nullable_not_found()

SELECT count(*)::int
FROM "SomeEntities" AS s
WHERE s."NullableText" = ANY (@__array_0) OR (((s."NullableText" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableText" = ANY (@__array_0) OR ((s."NullableText" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand All @@ -393,7 +393,7 @@ public override void Array_param_with_null_Contains_nullable_not_found_negated()

SELECT count(*)::int
FROM "SomeEntities" AS s
WHERE NOT (s."NullableText" = ANY (@__array_0) AND ((s."NullableText" = ANY (@__array_0) IS NOT NULL))) AND (((s."NullableText" IS NOT NULL)) OR ((array_position(@__array_0, NULL) IS NULL)))
WHERE NOT (s."NullableText" = ANY (@__array_0) AND (s."NullableText" = ANY (@__array_0) IS NOT NULL)) AND ((s."NullableText" IS NOT NULL) OR (array_position(@__array_0, NULL) IS NULL))
""");
}

Expand Down Expand Up @@ -488,7 +488,7 @@ await AssertQuery(

SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE s."NullableEnumConvertedToString" = ANY (@__array_0) OR (((s."NullableEnumConvertedToString" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableEnumConvertedToString" = ANY (@__array_0) OR ((s."NullableEnumConvertedToString" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand All @@ -507,7 +507,7 @@ await AssertQuery(

SELECT s."Id", s."ArrayContainerEntityId", s."Byte", s."ByteArray", s."Bytea", s."EnumConvertedToInt", s."EnumConvertedToString", s."IntArray", s."IntList", s."NonNullableText", s."NullableEnumConvertedToString", s."NullableEnumConvertedToStringWithNonNullableLambda", s."NullableIntArray", s."NullableIntList", s."NullableStringArray", s."NullableStringList", s."NullableText", s."StringArray", s."StringList", s."ValueConvertedArray", s."ValueConvertedList", s."Varchar10", s."Varchar15"
FROM "SomeEntities" AS s
WHERE s."NullableEnumConvertedToStringWithNonNullableLambda" = ANY (@__array_0) OR (((s."NullableEnumConvertedToStringWithNonNullableLambda" IS NULL)) AND ((array_position(@__array_0, NULL) IS NOT NULL)))
WHERE s."NullableEnumConvertedToStringWithNonNullableLambda" = ANY (@__array_0) OR ((s."NullableEnumConvertedToStringWithNonNullableLambda" IS NULL) AND (array_position(@__array_0, NULL) IS NOT NULL))
""");
}

Expand Down
Loading

0 comments on commit 95aa5c4

Please sign in to comment.