Skip to content

Translate BigInteger division to div() - #4144

Merged
roji merged 1 commit into
npgsql:mainfrom
Zentauer:bigint-division
Nov 26, 2026
Merged

roji merged 1 commit into
npgsql:mainfrom
Zentauer:bigint-division

Conversation

@Zentauer

Copy link
Copy Markdown
Contributor

Fixes #4143

BigInteger maps to numeric, so / between two BigInteger values was sent to PostgreSQL as numeric division. For 7 / 2 that returns 3.5, while .NET returns 3.

This translates / to div(a, b) when both operands are BigInteger (nullable included). div truncates towards zero, like BigInteger division in .NET. Division by zero fails in both cases. The change sits in NpgsqlSqlTranslatingExpressionVisitor.VisitBinary, next to the existing LocalDate subtraction case.

Before:

WHERE e."BigInteger" / 2 = 1

After:

WHERE div(e."BigInteger", 2) = 1

Tests added to BigIntegerTranslationsTest:

  • Divide: / 2 == 1 returns the rows with 2 and 3. Numeric division only returned 2.
  • Divide_truncates_towards_zero: / 2 == 0 returns 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?

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 roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@roji
roji enabled auto-merge (squash) November 26, 2026 00:27
@roji
roji merged commit 095be10 into npgsql:main Nov 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BigInteger division returns fractional results instead of integer division

2 participants