parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Add an overide for the increment function to allow user to specify siginificant digits

Open switch201 opened this issue 4 years ago • 5 comments

New Feature / Enhancement Checklist

Current Limitation

When using the parseObj.increment function, there is no way to account for floating point arithmatic errors. For example, if in the database I have a Quantity Column, and it has a value of 6.5 and I want to call `someObj.increment("Quantity", -4.1) instead of 2.4 being saved to that column 2.4000000000000004 is saved instead. From what I can tell there is no way to use the increment function and have it account for this. you have to do the math yourself and use set, which is not an option for us in thie case

Feature / Enhancement Description

make an override to the increment function that takes an additional parameter specifying the number of significant digits

Example Use Case

if in the database I have a Quantity Column, and it has a value of 6.5 and I want to call `someObj.increment("Quantity", -4.1) instead of 2.4 being saved to that column 2.4000000000000004 is saved instead. From what I can tell there is no way to use the increment function and have it account for this. you have to do the math yourself and use set, which is not an option for us in thie case

Alternatives / Workarounds

Using Set and the save would work, but given this column sees alot of traffic at once, it raises concerns of a race condition happening which can lead to an inacurate count which is why we use the increment function rather than set and save.

3rd Party References

switch201 avatar Dec 06 '21 22:12 switch201

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!

parse-github-assistant avatar Dec 06 '21 22:12 parse-github-assistant

2.4000000000000004 is saved

I am not sure that this is something to be addressed in Parse Server. The increment operator is executed by the database engine and therefore also the calculation is done there. At least for MongoDB, I don't think the $inc op supports a "significant digit" argument - and why should it since it's not a multiplication (or division).

In any case, I suspect a different issue in your case, because a +- op should result in exactly 2.4 in your example, it's a simple binary subtraction. I suspect either the original value was not exactly 6.5 or the increment argument was not exactly -4.1.

mtrezza avatar Dec 06 '21 23:12 mtrezza

@mtrezza hit F12 and go to your console and type 6.5-4.1 and you will see. image

At the base level its a JavaScript problem. and when I look into the implementation increment, it appears to call the IncrementOp function and going further down that simply does the addition/subtraction without rounding.

image

image

image

the issue is where I circled in the last screen shot. if running node js application, when this._amount = 6.5 and value is -4.1 then it will result with 2.400000000000004 being saved.

When using set and save we do our own rounding to avoid this, but in this case we need to use increment which is why we are stuck.

switch201 avatar Dec 07 '21 00:12 switch201

I should point out thats not the only number combination that results in erroneous remainder. again this is 100% a limitation of javascript, but because of that limitation there should be an option to allow the user to round to a specified number.

switch201 avatar Dec 07 '21 00:12 switch201

make an override to the increment function that takes an additional parameter specifying the number of significant digits

A user-defined rounding parameter comes with potential issues. The priority needs to be data consistency between the Parse Object and the DB value. Whatever floating point precision is used for atomic operations on the DB side should be mirrored on the Parse Object side. If the value calculated by the DB engine and stored in the DB is 2.4000000000000004, then the same value should be set for the Parse Object, because the DB value is the single point of truth, apparent when you fetch the object from the DB.

This may depend on the DB engine (MongoDB, Postgres, etc) and even the engine version. From that it seems that it should be a database adapter config, or if necessary on the client side a Parse client SDK initialization parameter. Since the calculation occurs on the client side (Parse JS SDK), we will probably have to dig a bit deeper into this for a solution. I guess the first step would be to research the used floating point precision for increment operations in MongoDB and Postgres.

See the MongoDB docs for modeling monetary data as an example.

mtrezza avatar Dec 07 '21 10:12 mtrezza