Repository navigation
Translate BigInteger division to div() - #4144
Conversation
BigInteger maps to numeric, so `/` between two BigInteger values ran as numeric division in PostgreSQL and returned 3.5 for 7 / 2. .NET truncates towards zero and returns 3. Translate the division to div(a, b), which truncates the same way. Fixes npgsql#4143
roji
left a comment
There was a problem hiding this comment.
Problem: PostgreSQL maps BigInteger to numeric, so 7 / 2 with SQL / yields 3.5 rather than the .NET BigInteger result 3. The reported predicate r.Big / r.Big2 == 3 consequently misses the row.
Expected approach: Special-case BigInteger division and translate to PostgreSQL div(a, b), which truncates toward zero. Keep the change limited to the operator; translating the separate BigInteger.Divide method is not needed for this issue.
Assessment: The PR follows that approach in VisitBinary, preserves operand/result type mapping and null propagation, and adds result/SQL coverage for positive division, negative truncation, and a parameter divisor. The change is focused and I found no material correctness gap.
Manual validation: I ran a standalone Npgsql EF query against PostgreSQL using 10.0.2 and 10.0.3 with a connection-local temporary table: .NET reported the 7 / 2 == 3 condition as true, while both provider versions generated / and returned zero rows. I also ran the target PostgreSQL operation directly; div(7, 2) returned 3 and div(-1, 2) returned 0.
Posted by copilot
Fixes #4143
BigIntegermaps tonumeric, so/between twoBigIntegervalues was sent to PostgreSQL as numeric division. For 7 / 2 that returns 3.5, while .NET returns 3.This translates
/todiv(a, b)when both operands areBigInteger(nullable included).divtruncates towards zero, likeBigIntegerdivision in .NET. Division by zero fails in both cases. The change sits inNpgsqlSqlTranslatingExpressionVisitor.VisitBinary, next to the existingLocalDatesubtraction case.Before:
After:
Tests added to
BigIntegerTranslationsTest:Divide:/ 2 == 1returns the rows with 2 and 3. Numeric division only returned 2.Divide_truncates_towards_zero:/ 2 == 0returns the rows with 0, 1 and -1, so negative values are covered.Divide_with_parameter: the divisor is a parameter.BigInteger.Divide(a, b)still has no translation. Should I add it to this PR as well, or keep that separate?