tediousjs / tedious

Node TDS module for connecting to SQL Server databases.

Home Page:http://tediousjs.github.io/tedious/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting RangeError and Nodejs throws error for "bigint" type column if float values are provided in bulk load while using bulk insert

shivamrai45 opened this issue · comments

Software versions

  • Tedious: 17.0.0
  • SQL Server: MsSQL Server 2017 - 14.0.3381.3
  • Node.js: 18.12.1

Additional Libraries Used and Versions
NA

Table schema
Columns - MyBigInt (bigint 8)

Connection configuration
{ authentication: { type: 'default', options: { userName: '***', password: '***' } }, server: 'ms-sql-sql-server-2017.***.amazonaws.com', options: { database: 'MsSQL_Restart', encrypt: true, port: 1433, requestTimeout: 100000, trustServerCertificate: true } }

Problem description
I am trying to bulk insert data in MsSQL using bulkLoad. The destination column contains bigint datatype column with size 8, If we provide it with a float/decimal value it crases the server while inserting in buffer here
I've attached a sample script to replicate the behaviour
tediousTest.txt

Expected behavior
Ideally we should validate the data and return a error if invalid datatype is present in data or in case of float, round off to greatest minimum integer as we do in case of query run's for mssql.

Actual behavior
Node internals throw error, thus crashing the server

Error message/stack trace
RangeError: The number 0.5 cannot be converted to a BigInt because it is not an integer
at BigInt ()
at WritableTrackingBuffer.writeInt64LE (node_modules/tedious/lib/tracking-buffer/writable-tracking-buffer.js:103:26)
at Object.generateParameterData (node_modules/tedious/lib/data-types/bigint.js:33:12)
at generateParameterData.next ()
at RowTransform._transform (node_modules/tedious/lib/bulk-load.js:126:18)
at Transform._write (node:internal/streams/transform:175:8)
at doWrite (node:internal/streams/writable:411:12)
at clearBuffer (node:internal/streams/writable:572:7)
at onwrite (node:internal/streams/writable:464:7)
at node:internal/streams/transform:190:7
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

Any other details that can be helpful

Hi @shivamrai45 , we did some digging and instead of report this error within generateParameterData, we can catch it within the validate function and process it there. @arthurschreiber , is there any concern to add a line within the validate function BigInt(value) which will trigger this error, so we can catch error then either throw it or round the input float typed value to the nearest int?

Sure @MichaelSun90 , We can handle it in same way as it is being done for normal int data type, i.e. by using

return value | 0;
.
This way it acts similar to Math.trunc() and only considers the integer part of value.
https://github.com/tediousjs/tedious/compare/master...shivamrai45:BigIntNodeError?expand=1

I guess it needs to be return value | 0n to keep the bigint type (not sure about that), but yeah, I agree that those should be handled the same way.

We made a fix in # 1620. Will close this one for now. Feel free to reopen this if anything is needed related to this issue.