fix(core): prevent rounding overflow from being ignored during decimal divisions#6598
fix(core): prevent rounding overflow from being ignored during decimal divisions#6598bluestreak01 merged 1 commit intomasterfrom
Conversation
…low test for divide operation
WalkthroughModified 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
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. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
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:
- The expected result
"720575940379.279360"is hard-coded without verification. Consider validating against BigDecimal to ensure mathematical correctness.- Add a comment explaining why these specific input values (particularly
-1and 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
📒 Files selected for processing (2)
core/src/main/java/io/questdb/std/DecimalKnuthDivider.javacore/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 positionnwould be silently lost, leading to incorrect results.
[PR Coverage check]😍 pass : 2 / 2 (100.00%) file detail
|
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:
DecimalKnuthDivider.javato use the actual length of theqarray, preventing possible misses when incrementing quotient digits during rounding.Testing improvements:
testDivideIncrementOverflowinDecimal128Test.javato verify correct handling of increment overflow during division and rounding.