Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using Npgsql.EntityFrameworkCore.PostgreSQL.Extensions.Internal;
Expand Down Expand Up @@ -192,6 +193,32 @@ protected override Expression VisitBinary(BinaryExpression binaryExpression)
// Note: many other date/time arithmetic operators are fully supported as-is by PostgreSQL - see NpgsqlSqlExpressionFactory
}

// PostgreSQL division over numeric returns a fractional result, but .NET BigInteger division truncates towards zero.
// div() performs integer division with the same truncation semantics.
if (binaryExpression.NodeType is ExpressionType.Divide
&& binaryExpression.Left.Type.UnwrapNullableType() == typeof(BigInteger)
&& binaryExpression.Right.Type.UnwrapNullableType() == typeof(BigInteger))
{
if (TranslationFailed(binaryExpression.Left, Visit(binaryExpression.Left), out var sqlLeft)
|| TranslationFailed(binaryExpression.Right, Visit(binaryExpression.Right), out var sqlRight))
{
return QueryCompilationContext.NotTranslatedExpression;
}

var typeMapping = ExpressionExtensions.InferTypeMapping(sqlLeft!, sqlRight!);

return _sqlExpressionFactory.Function(
"div",
[
_sqlExpressionFactory.ApplyTypeMapping(sqlLeft!, typeMapping),
_sqlExpressionFactory.ApplyTypeMapping(sqlRight!, typeMapping)
],
nullable: true,
argumentsPropagateNullability: TrueArrays[2],
binaryExpression.Type,
typeMapping);
}

return base.VisitBinary(binaryExpression) switch
{
// Optimize (x - c) - (y - c) to x - y.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,58 @@ await AssertQuery(
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Divide(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Entity>().Where(e => e.BigInteger / 2 == 1));

AssertSql(
"""
SELECT e."Id", e."BigInteger"
FROM "Entities" AS e
WHERE div(e."BigInteger", 2) = 1
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Divide_truncates_towards_zero(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Entity>().Where(e => e.BigInteger / 2 == 0));

AssertSql(
"""
SELECT e."Id", e."BigInteger"
FROM "Entities" AS e
WHERE div(e."BigInteger", 2) = 0
""");
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public async Task Divide_with_parameter(bool async)
{
var divisor = new BigInteger(2);

await AssertQuery(
async,
ss => ss.Set<Entity>().Where(e => e.BigInteger / divisor == 1));

AssertSql(
"""
@divisor='2' (DbType = Decimal)

SELECT e."Id", e."BigInteger"
FROM "Entities" AS e
WHERE div(e."BigInteger", @divisor) = 1
""");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down