diff --git a/src/EFCore.PG/Query/Internal/NpgsqlSqlTranslatingExpressionVisitor.cs b/src/EFCore.PG/Query/Internal/NpgsqlSqlTranslatingExpressionVisitor.cs index 70dc2494a..6c35a7f0e 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlSqlTranslatingExpressionVisitor.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlSqlTranslatingExpressionVisitor.cs @@ -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; @@ -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. diff --git a/test/EFCore.PG.FunctionalTests/Query/Translations/BigIntegerTranslationsTest.cs b/test/EFCore.PG.FunctionalTests/Query/Translations/BigIntegerTranslationsTest.cs index e3e9899ff..9c312140f 100644 --- a/test/EFCore.PG.FunctionalTests/Query/Translations/BigIntegerTranslationsTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/Translations/BigIntegerTranslationsTest.cs @@ -124,6 +124,58 @@ await AssertQuery( """); } + [ConditionalTheory] + [MemberData(nameof(IsAsyncData))] + public async Task Divide(bool async) + { + await AssertQuery( + async, + ss => ss.Set().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().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().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);