Skip to content

fix(core): prevent rounding overflow from being ignored during decimal divisions#6598

Merged
bluestreak01 merged 1 commit intomasterfrom
rd_fix_decimal_div
Jan 5, 2026
Merged

fix(core): prevent rounding overflow from being ignored during decimal divisions#6598
bluestreak01 merged 1 commit intomasterfrom
rd_fix_decimal_div

Conversation

@RaphDal
Copy link
Copy Markdown
Contributor

@RaphDal RaphDal commented Jan 5, 2026

This pull request addresses an overflow issue when incrementing quotient digits during decimal division and adds a corresponding unit test to ensure correct behavior. The main changes are focused on improving the reliability and correctness of the decimal division logic.

Bug fix in decimal division logic:

  • Updated the increment loop in DecimalKnuthDivider.java to use the actual length of the q array, preventing possible misses when incrementing quotient digits during rounding.

Testing improvements:

  • Added a new unit test testDivideIncrementOverflow in Decimal128Test.java to verify correct handling of increment overflow during division and rounding.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 5, 2026

Walkthrough

Modified the rounding logic in decimal division to iterate over the entire quotient array (length 8) instead of only the first n elements, enabling carry propagation across the full quotient vector. Added a corresponding test case to verify overflow behavior during division.

Changes

Cohort / File(s) Summary
Decimal division rounding logic
core/src/main/java/io/questdb/std/DecimalKnuthDivider.java
Changed rounding loop to iterate over entire quotient array (length 8) instead of first n elements, affecting carry propagation behavior in quotient increments
Decimal division test coverage
core/src/test/java/io/questdb/test/std/Decimal128Test.java
Added new test method testDivideIncrementOverflow() to verify overflow behavior during decimal division with rounding

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • bluestreak01

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The description is directly related to the changeset, explaining both the bug fix in decimal division logic and the new test case added, which aligns with the actual modifications.
Title check ✅ Passed The title 'fix(core): prevent rounding overflow from being ignored during decimal divisions' is fully related to the main change: fixing an overflow issue in decimal division rounding by correcting the increment loop in DecimalKnuthDivider.java.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@RaphDal
Copy link
Copy Markdown
Contributor Author

RaphDal commented Jan 5, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 5, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
core/src/test/java/io/questdb/test/std/Decimal128Test.java (1)

331-339: Add verification against BigDecimal oracle and explanatory comments.

The test validates the overflow fix but could be more robust:

  1. The expected result "720575940379.279360" is hard-coded without verification. Consider validating against BigDecimal to ensure mathematical correctness.
  2. Add a comment explaining why these specific input values (particularly -1 and the chosen scales) trigger the carry propagation scenario that the fix addresses.
🔎 Suggested improvements
 @Test
 public void testDivideIncrementOverflow() {
+    // Test case that triggers carry propagation beyond the divisor's significant digits
+    // during rounding. The dividend uses max unsigned long (2^64-1) with large scale,
+    // and rounding causes carries that must propagate through the full quotient array.
     Decimal128 a = new Decimal128(0, -1, 19);
     Decimal128 b = new Decimal128(0, 256, 14);

     a.divide(b, 6, RoundingMode.HALF_UP);

+    // Verify against BigDecimal oracle
+    BigDecimal bdA = new BigDecimal("18446744073709551615").movePointLeft(19);
+    BigDecimal bdB = new BigDecimal("256").movePointLeft(14);
+    BigDecimal expected = bdA.divide(bdB, 6, RoundingMode.HALF_UP);
+    Assert.assertEquals(expected, a.toBigDecimal());
+    
+    // Also verify string representation
     Assert.assertEquals("720575940379.279360", a.toString());
 }
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf87730 and 2c81e53.

📒 Files selected for processing (2)
  • core/src/main/java/io/questdb/std/DecimalKnuthDivider.java
  • core/src/test/java/io/questdb/test/std/Decimal128Test.java
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (36)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-other)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-pgwire)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-cairo-sub)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-cairo-root)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-fuzz2)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-fuzz1)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-griffin-sub)
  • GitHub Check: New pull request (SelfHosted Running tests with cover on linux-griffin-root)
  • GitHub Check: New pull request (Trigger Enterprise CI Trigger Enterprise Pipeline)
  • GitHub Check: New pull request (SelfHosted Other tests on linux-x64-zfs)
  • GitHub Check: New pull request (Rust Test and Lint on linux-jdk17)
  • GitHub Check: New pull request (SelfHosted Other tests on linux-arm64)
  • GitHub Check: New pull request (SelfHosted Other tests on linux-x86-graal)
  • GitHub Check: New pull request (Hosted Running tests on windows-other-2)
  • GitHub Check: New pull request (Hosted Running tests on windows-other-1)
  • GitHub Check: New pull request (Hosted Running tests on windows-pgwire)
  • GitHub Check: New pull request (Hosted Running tests on windows-cairo-2)
  • GitHub Check: New pull request (Hosted Running tests on windows-cairo-1)
  • GitHub Check: New pull request (Hosted Running tests on windows-fuzz2)
  • GitHub Check: New pull request (Hosted Running tests on windows-fuzz1)
  • GitHub Check: New pull request (Hosted Running tests on windows-griffin-sub)
  • GitHub Check: New pull request (Hosted Running tests on windows-griffin-base)
  • GitHub Check: New pull request (Hosted Running tests on mac-other)
  • GitHub Check: New pull request (Hosted Running tests on mac-pgwire)
  • GitHub Check: New pull request (Hosted Running tests on mac-cairo-fuzz)
  • GitHub Check: New pull request (Hosted Running tests on mac-cairo)
  • GitHub Check: New pull request (SelfHosted Cairo tests on linux-x64-zfs)
  • GitHub Check: New pull request (SelfHosted Griffin tests on linux-x64-zfs)
  • GitHub Check: New pull request (SelfHosted Griffin tests on linux-arm64)
  • GitHub Check: New pull request (SelfHosted Griffin tests on linux-x86-graal)
  • GitHub Check: New pull request (Hosted Running tests on mac-griffin)
  • GitHub Check: New pull request (SelfHosted Cairo tests on linux-x86-graal)
  • GitHub Check: New pull request (SelfHosted Cairo tests on linux-arm64)
  • GitHub Check: New pull request (Check Changes Check changes)
  • GitHub Check: build
  • GitHub Check: build
🔇 Additional comments (1)
core/src/main/java/io/questdb/std/DecimalKnuthDivider.java (1)

416-422: LGTM! Critical correctness fix.

The change correctly propagates carry during rounding across the entire quotient array rather than stopping at position n. Previously, if the quotient had more significant digits than the divisor, rounding overflow beyond position n would be silently lost, leading to incorrect results.

@RaphDal RaphDal changed the title chore(core): prevent rounding overflow from being ignored during decimal divisions fix(core): prevent rounding overflow from being ignored during decimal divisions Jan 5, 2026
@glasstiger
Copy link
Copy Markdown
Contributor

[PR Coverage check]

😍 pass : 2 / 2 (100.00%)

file detail

path covered line new line coverage
🔵 io/questdb/std/Os.java 1 1 100.00%
🔵 io/questdb/std/DecimalKnuthDivider.java 1 1 100.00%

@bluestreak01 bluestreak01 merged commit 4058c01 into master Jan 5, 2026
44 checks passed
@bluestreak01 bluestreak01 deleted the rd_fix_decimal_div branch January 5, 2026 17:44
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.

3 participants