Skip to content

MSSQL: Query getScale calculates invalid scale #16463

Description

@mlev

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.

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

  1. added
    pending-approval
    type: bug
    on Sep 6, 2023
  2. WikiRik commented on Sep 6, 2023

    @WikiRik
    Member

    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;

    describe('getSQLTypeFromJsType', () => {
    const TYPES = tedious.TYPES;
    it('should return correct parameter type', () => {
    expect(query.getSQLTypeFromJsType(2_147_483_647, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {}, value: 2_147_483_647 });
    expect(query.getSQLTypeFromJsType(-2_147_483_648, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {}, value: -2_147_483_648 });
    expect(query.getSQLTypeFromJsType(2_147_483_648, TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {}, value: 2_147_483_648 });
    expect(query.getSQLTypeFromJsType(-2_147_483_649, TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {}, value: -2_147_483_649 });
    expect(query.getSQLTypeFromJsType(2_147_483_647n, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {}, value: 2_147_483_647 });
    expect(query.getSQLTypeFromJsType(-2_147_483_648n, TYPES)).to.eql({ type: TYPES.Int, typeOptions: {}, value: -2_147_483_648 });
    expect(query.getSQLTypeFromJsType(BigInt(Number.MAX_SAFE_INTEGER), TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {}, value: Number.MAX_SAFE_INTEGER });
    expect(query.getSQLTypeFromJsType(BigInt(Number.MIN_SAFE_INTEGER), TYPES)).to.eql({ type: TYPES.BigInt, typeOptions: {}, value: Number.MIN_SAFE_INTEGER });
    const overMaxSafe = BigInt(Number.MAX_SAFE_INTEGER) + 1n;
    expect(query.getSQLTypeFromJsType(overMaxSafe, TYPES)).to.eql({ type: TYPES.VarChar, typeOptions: {}, value: overMaxSafe.toString() });
    const underMinSafe = BigInt(Number.MIN_SAFE_INTEGER) - 1n;
    expect(query.getSQLTypeFromJsType(underMinSafe, TYPES)).to.eql({ type: TYPES.VarChar, typeOptions: {}, value: underMinSafe.toString() });
    const buffer = Buffer.from('abc');
    expect(query.getSQLTypeFromJsType(buffer, TYPES)).to.eql({ type: TYPES.VarBinary, typeOptions: {}, value: buffer });
    });
    it('should return parameter type correct scale for float', () => {
    expect(query.getSQLTypeFromJsType(1.23, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 2 }, value: 1.23 });
    expect(query.getSQLTypeFromJsType(0.300_000_000_000_000_04, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 17 }, value: 0.300_000_000_000_000_04 });
    expect(query.getSQLTypeFromJsType(2.5e-15, TYPES)).to.eql({ type: TYPES.Numeric, typeOptions: { precision: 30, scale: 16 }, value: 2.5e-15 });
    });
    });
    });

    Please make a PR towards the main branch first and once that has been approved we can open another PR to backport it to the v6 branch

  3. mlev commented on Sep 9, 2023

    @mlev
    Author

    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?

  4. WikiRik commented on Sep 9, 2023

    @WikiRik
    Member

    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?

    I think that would be a good approach

  5. removed
    pending-approval
    on Sep 9, 2023
  6. added theissue type on Mar 9, 2025
  7. removed
    type: bug
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions