Repository navigation
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Bug Description
After a recent upgrade from v4 to v6 we started seeing some out of range errors when updating numeric values. e.g.
The value of "value" is out of range. It must be >= 0n and < 2n ** 64n. Received 319_585_080_000_000_032_768n
A specific value that causes the problem: 31.958508000000002
We tracked the issue down to the getScale method incorrectly identifying this number as having a scale of 19, when it should be 15. When tedious later tries to format the query parameter it fails because the scale results in a number that's too big.
sequelize/packages/core/src/dialects/mssql/query.js
Lines 17 to 28 in 62a59b0
| function getScale(aNum) { | |
| if (!Number.isFinite(aNum)) { | |
| return 0; | |
| } | |
| let e = 1; | |
| while (Math.round(aNum * e) / e !== aNum) { | |
| e *= 10; | |
| } | |
| return Math.log10(e); | |
| } |
Reproducible Example
You can run this in the browser console (copied from lib source). It returns a scale of 19.
const getScale = (aNum) => {
if (!Number.isFinite(aNum)) return 0;
let e = 1;
while (Math.round(aNum * e) / e !== aNum) e *= 10;
return Math.log10(e);
}
getScale(31.958508000000002);
What do you expect to happen?
getScale() calculates scale of 15.
What is actually happening?
getScale() calculates scale of 19.
Environment
- Sequelize version: [email protected]
- Node.js version: v18.17.1
- If TypeScript related: TypeScript version:
- Database & Version: MSSQL
- Connector library & Version: tedious 16.2.0
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
Activity
WikiRik commentedon Sep 6, 2023
Thanks for this report. Can you make a PR that improves this function? I quickly checked and I think adding some new test cases to the following file should be enough;
sequelize/packages/core/test/unit/dialects/mssql/query.test.js
Lines 44 to 75 in 62f0419
Please make a PR towards the
mainbranch first and once that has been approved we can open another PR to backport it to the v6 branchmlev commentedon Sep 9, 2023
The main problem is I'm not sure how best to improve the function - I can't find a "standard" way that work for all numbers.
It looks like the current implementation was based on the SO answer here: https://stackoverflow.com/a/27865285 - but as pointed out in the comments this doesn't work for all cases. The string based way seems more consistent - except when Number.toString() outputs in scientific notation. I guess you could start with string based approach and if that returns scientific notation then use the calculated method?
WikiRik commentedon Sep 9, 2023
I think that would be a good approach