[GODRIVER-1922] Arithmetic functions with Decimal128 Created: 16/Mar/21 Updated: 27/Oct/23 Resolved: 25/Nov/21 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | BSON |
| Affects Version/s: | 1.5.0 |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Swee Tat Lim | Assignee: | Benji Rewis (Inactive) |
| Resolution: | Gone away | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Epic Link: | GODRIVER-2148 | ||||||||
| Quarter: | FY22Q4 | ||||||||
| Case: | (copied to CRM) | ||||||||
| Description |
|
I want to store and use decimals in mongo to represent monetary data. Looking at the implementation of decimal128 across the other drivers such as python and csharp, it is odd that the maturity of the golang driver implementation lacks the following:
a. Add, Sub, Mul, Div b. IsEqual
Is there any roadmap for these functions? |
| Comments |
| Comment by PM Bot [ 25/Nov/21 ] |
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information. |
| Comment by Benji Rewis (Inactive) [ 10/Nov/21 ] |
|
Hello again sweetat@withevo.com, and thanks again for your feature request. Unfortunately, we cannot implement arithmetic functions for the Decimal128 type (Add, Sub, Mul, Div). Our specifications for the Decimal128 type (to which all drivers must adhere) explicitly forbid implementing math operations on the type. No official MongoDB driver, including C# and Python, has arithmetic methods for their BSON Decimal128 types. Implementing, maintaining and guaranteeing correctness of IEEE decimal support is far from trivial, and a mistake could be disastrous given the importance of arbitrary-precision-decimal-floating-point-number arithmetic. We’d rather not implement IsEqual, either, as the definition of equality between instances of primitive.Decimal128 is not obvious. For example, two {{primitive.Decimal128}}s may have mathematically equivalent values, but differing high and low Int64 encodings (e.g. “1.2” and “1.200”). Those values’ equality could be ambiguous, and one user’s definition may not match another’s. As chuntao.lu@coinbase.com mentioned, I understand that converting between primitive.Decimal128 and other popular decimal libraries can add undesired overhead to perform arithmetic functions. I gather that the main pain point there is having to check errors for conversions to and from primitive.Decimal128. I can suggest an alternative of registering a custom BSON codec for your preferred decimal type that converts to and from primitive.Decimal128. Doing so would mean that conversion errors only appear in Marshaling and Unmarshaling, and you could insert/query with your preferred decimal type rather than primitive.Decimal128. That way, from your application’s point of view, you’d never even touch primitive.Decimal128. Decimal values would only be stored in your collections as primitive.Decimal128. I’ve attached an example of registering a custom codec for shopspring.Decimal, a popular decimal type with full arithmetic support. I believe this method would make working with decimals more seamless. Let me know what you think. |
| Comment by Chuntao Lu [ 04/Aug/21 ] |
|
I was redirected to this ticket by our support engineer from Mongo, our use case is exactly about the lack of arithmetic methods on Decimal128 type. The decimal type is great, but without the arithmetic support, it is less useful and adds a lot of overhead in our application: we have to convert mongo's decimal type to a decimal type from another third party lib that supports arithmetics, perform the arithmetic operations and finally convert the result back to mongo's decimal type for storage. I am surprised that the arithmetic methods are present in other drivers but not in golang driver, would love to see this feature request gets higher priority. |
| Comment by Swee Tat Lim [ 25/Mar/21 ] |
|
I'm keen to know which kinds of arithmetic functions will be provided, and if it will achieve feature parity with the other language drivers. |
| Comment by Kevin Albertson [ 22/Mar/21 ] |
|
Hi sweetat@withevo.com, thank you for the feature request! We do not have immediate plans to implement additional arithmetic methods on Decimal128, but we are opening this for future consideration. |