tedious icon indicating copy to clipboard operation
tedious copied to clipboard

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

Open Sachin-gupta14800 opened this issue 7 months ago • 3 comments

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'
}

Sachin-gupta14800 avatar Apr 28 '26 18:04 Sachin-gupta14800

We are also encountering the same issue while using the mssql package (which internally uses Tedious) on Node.js.

In our case, the failure occurs during UPDATE operations involving high-precision DECIMAL/NUMERIC columns (for example decimal(38,18)), where the scaled value exceeds the UInt64 limit during parameter serialization.

The exception originates from tedious/lib/data-types/numeric.js inside generateParameterData. Since this happens inside the stream transform pipeline, the resulting RangeError [ERR_OUT_OF_RANGE] is not catchable at the application level and crashes the entire Node.js process.

It also seems that the driver is not safely handling/rounding values internally before attempting UInt64 serialization. Ideally, values exceeding supported serialization limits should either:

  • be safely rounded/truncated according to the target precision/scale, or
  • return a normal catchable validation error instead of crashing the process.

The proper long-term fix would still be implementing correct multi-word integer serialization for high-precision NUMERIC/DECIMAL values according to the TDS protocol, rather than assuming all scaled values fit within UInt64.

drillyme avatar May 27 '26 05:05 drillyme

can I ask anyone to review https://github.com/tediousjs/tedious/pull/1749

AnnaRainier avatar Jul 27 '26 09:07 AnnaRainier

:tada: This issue has been resolved in version 20.0.6 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Aug 29 '26 12:08 github-actions[bot]