Skip to content

Getting RangeError [ERR_OUT_OF_RANGE] for "decimal" type column while using bulk insert #1733

@Sachin-gupta14800

Description

@Sachin-gupta14800

Software versions

  • Tedious: 18.2.4
  • SQL Server: Azure SQL (production)
  • Node.js: 22.11.0

Additional Libraries Used and Versions
mssql (via tedious bulk-load API)

Table schema
Target table contains DECIMAL/NUMERIC columns with high precision and scale (e.g., decimal(38,18), decimal(30,15)).

Problem description
During bulk-load insert operations (execBulkLoad), when a decimal/numeric column's scaled value exceeds the unsigned 64-bit integer limit (2^64), generateParameterData in decimal.js throws a RangeError inside RowTransform._transform. Because this error occurs inside a Node.js stream transform, it is not catchable by the application — it crashes the entire Node.js process.

The root cause is on this line:

const value = Math.round(Math.abs(parameter.value * Math.pow(10, parameter.scale)));


Expected behavior Tedious should either:

Validate the scaled value before writing to the buffer and return a catchable error (not crash the process), OR
Properly implement multi-word integer serialization for precision > 19 by splitting the value across the UInt64 + UInt32 fields instead of hardcoding the upper bytes to zero
Option 2 is the correct long-term fix  SQL Server's TDS protocol uses a multi-part integer representation for high-precision decimals, and tedious should implement this properly.

Actual behavior Node.js process crashes with uncatchable RangeError:

Error message/stack trace

RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= 0n and < 2n ** 64n. Received 100_000_000_000_000_000_000n
    at checkInt (node:internal/buffer:74:11)
    at writeBigU_Int64LE (node:internal/buffer:581:3)
    at Buffer.writeBigUInt64LE (node:internal/buffer:603:10)
    at WritableTrackingBuffer.writeBigUInt64LE (/node_modules/tedious/lib/tracking-buffer/writable-tracking-buffer.js:105:17)
    at WritableTrackingBuffer.writeUInt64LE (/node_modules/tedious/lib/tracking-buffer/writable-tracking-buffer.js:100:10)
    at Object.generateParameterData (/node_modules/tedious/lib/data-types/decimal.js:88:14)
    at generateParameterData.next (<anonymous>)
    at RowTransform._transform (/node_modules/tedious/lib/bulk-load.js:125:18)
    at Transform._write (node:internal/streams/transform:171:8)
    at writeOrBuffer (node:internal/streams/writable:572:12) {
  code: 'ERR_OUT_OF_RANGE'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions