fix: fix precision loss for large decimal values#4135
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4135 +/- ##
==========================================
+ Coverage 90.23% 90.28% +0.05%
==========================================
Files 86 86
Lines 13740 13757 +17
Branches 1664 1668 +4
==========================================
+ Hits 12398 12421 +23
+ Misses 1342 1336 -6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
d971e2f to
0cee994
Compare
Add length-based bailout to parseFloat() to fix accumulated rounding errors from repeated *10 operations. For numbers longer than 17 characters, delegate to Number.parseFloat() which handles precision correctly. This fixes two critical issues: - DECIMAL(36,18) precision loss where 50000.000...0 parsed as 49999.999 - MAX_VALUE doubles corruption where last digits were incorrect The threshold of 17 is based on IEEE 754 double precision limits (~15-17 significant digits). Testing shows this affects only ~1% of typical MySQL data while preserving the fast path for 98%+ of cases. Add comprehensive test suite with 54 test cases covering both issues, edge cases, and regression tests. Closes #3690 Closes #2928
0cee994 to
5f16d62
Compare
Add tests exercising parseFloat bailout paths to improve coverage: - DECIMAL(36,18) with many fractional digits (>17 chars) - DOUBLE with scientific notation values These integration tests ensure the bailout conditions are covered in real database query scenarios.
1934664 to
4202e21
Compare
wellwelwel
approved these changes
Feb 28, 2026
This was referenced Mar 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix precision loss for large decimal values
Fixes #3690
Fixes #2928
Background
MySQL2 uses manual parsing in
Packet.parseFloat()to avoid expensiveBuffer.toString()conversions. This optimization provides ~3x performance improvement (see comment in packet.js L27-36) by parsing numbers directly from buffer bytes.However, the current implementation accumulates floating-point rounding errors with repeated
*10operations, causing precision loss for numbers with many digits.Issues Fixed
Issue #3690: DECIMAL(36,18) Precision Loss
Problem: Values like
50000.000000000000000000(DECIMAL(36,18) with 18 fractional digits) were parsed incorrectly:Issue #2928: MAX_VALUE Doubles Corruption
Problem: Very large DOUBLE values lost precision in the last significant digits:
Solution
Add a length-based bailout to
parseFloat()for numbers >17 characters:Why 17? IEEE 754 double precision supports ~15-17 significant digits. Testing shows this threshold:
Performance Impact
Tested with 50,000 realistic MySQL values:
The fast path remains for:
Testing
Added comprehensive test suite with 54 test cases:
See:
test/unit/packets/test-packet-parseFloat-precision.test.mtsAlternative Approaches Explored
WebAssembly Implementation
Explored two WASM approaches as potential solutions:
Benchmark Results (50,000 test cases):
Outcome: JavaScript is 40-50% faster than WASM due to:
Conclusion: WASM not beneficial for this use case. The minimal JavaScript fix is optimal.
References
lib/packets/packet.js#L659-L697