Skip to content

feat: read and write decimal, numeric and money values exactly as strings - #1839

Open
colinhacks wants to merge 3 commits into
tediousjs:masterfrom
colinhacks:decimal-as-string
Open

colinhacks wants to merge 3 commits into
tediousjs:masterfrom
colinhacks:decimal-as-string

Conversation

@colinhacks

Copy link
Copy Markdown

Adds a decimalAsString connection option for reading, and writes decimal strings exactly.

  • Reading. With decimalAsString: true, decimal, numeric, money and smallmoney values, sql_variant ones included, come back as strings that hold the exact value, with as many fractional digits as the type's scale: '1.50' for a decimal(10, 2), '1.5000' for a money. The default stays a number.
  • Writing. A Decimal, Numeric, Money or SmallMoney parameter given a decimal string such as '-12.50' is encoded from its digits instead of through parseFloat, rounding half away from zero to the scale as SQL Server does. Today '12345678901234567.123456789' bound as Decimal(38, 9) is stored as 12345678901234568.244756480.

Numbers keep the current path, and so do strings in exponent form: mssql's batch mode writes a string parameter into the batch as N'…', and SQL Server doesn't convert N'1e5' to decimal.

One visible change: validate on these four types returns a decimal string as a string (trimmed) instead of a number, which the tests in validations-test.ts now expect.

Tested on SQL Server 2019, 2022 and 2025 on Linux with the integration suite, and through mssql 12.7.14, which passes config.options to tedious as they are.

Closes #163
Refs #760, #1675, #678

Copilot AI balanced review requested due to automatic review settings November 16, 2026 21:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

done();
}
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this table-driven, with each SQL expression beside its expected value and a named test for each case? Something like it.each, using Mocha’s existing loop-based pattern here. That would be easier to follow than matching 15 selected columns to a separate expected array.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, done in 08b0877. The cases go through the file's testDataType loop now, one named test each.

@sumitmsft

Copy link
Copy Markdown

Hi @colinhacks

Thanks for working on this! We’re developing SQL Server support for Prisma 8 and need lossless Decimal/Numeric parameters and results. We’d prefer to coordinate here rather than submit an overlapping implementation.

Our intended usage is to validate decimal strings against the declared precision/scale before passing them to Tedious, and enable decimalAsString for results. That would let us reject values requiring rounding without changing Tedious’s existing compatibility behavior. Does this fit the intended public contract?

Two areas would be particularly helpful to confirm:

  1. Exact strings work through prepared execution and stored-procedure input/output parameters, including precision-38 values, fractional scales and nulls.

  2. Decimal/Numeric overflow is handled clearly. The current writer checks storage width rather than declared precision, for example, 1000 fits the buffer but not decimal(3,0). Is server-side rejection intentional? We can enforce this earlier in our integration.

One small testing suggestion: assert the expected row/output-event count at completion, so a missing event cannot leave the value assertions unexecuted.

Happy to help validate the relevant cases against SQL Server 2022/2025. Please let us know what would be useful.

@colinhacks

Copy link
Copy Markdown
Author

Yes, that's the intended contract. A decimal string is written exactly, decimalAsString reads results as strings, and numbers behave as before. Checking precision and scale on your side fits too, since a string with more fractional digits than the scale is rounded half away from zero (as SQL Server rounds), not refused.

Strings go through the same writer for execSql, prepared execution and stored procedures, and output parameters are read with the option the same way rows are. 6c7b4bf adds integration tests for prepared execution and for procedure input and output parameters, with precision 38, fractional scales and nulls.

The overflow check is the one already on master, by storage width, so 1000 goes out as a decimal(3, 0) and it's left to the server. This PR doesn't change that. Checking the declared precision would change it for numbers too, so I'd keep it as a separate change.

Also took the test suggestion, the helpers now assert the row and output-value counts at completion.

@sumitmsft

Copy link
Copy Markdown

Yes, that's the intended contract. A decimal string is written exactly, decimalAsString reads results as strings, and numbers behave as before. Checking precision and scale on your side fits too, since a string with more fractional digits than the scale is rounded half away from zero (as SQL Server rounds), not refused.

Strings go through the same writer for execSql, prepared execution and stored procedures, and output parameters are read with the option the same way rows are. 6c7b4bf adds integration tests for prepared execution and for procedure input and output parameters, with precision 38, fractional scales and nulls.

The overflow check is the one already on master, by storage width, so 1000 goes out as a decimal(3, 0) and it's left to the server. This PR doesn't change that. Checking the declared precision would change it for numbers too, so I'd keep it as a separate change.

Also took the test suggestion, the helpers now assert the row and output-value counts at completion.

Hi @colinhacks

Thanks for clarifying and adding the integration tests and completion-count assertions, that addresses my questions.

The contract works for our intended Prisma integration. We’ll pass exact decimal strings, enable decimalAsString for reads, and validate declared precision, scale and range before dispatch so values requiring rounding are rejected on our side. Preserving existing number behavior makes sense, and we agree that changing the existing precision-overflow validation is separate from this PR.

We’ll track this PR and validate the released implementation against our SQL Server cases before enabling support. Thanks for moving this forward!

@arthurschreiber

Copy link
Copy Markdown
Collaborator

Should we just make this a breaking change? Options like decimalAsString are annoying because they change how the driver behaves, and I'd rather just do the "correct" thing always, which is to never accept / return data that can be silently truncated / changed.

@sumitmsft

Copy link
Copy Markdown

Should we just make this a breaking change? Options like decimalAsString are annoying because they change how the driver behaves, and I'd rather just do the "correct" thing always, which is to never accept / return data that can be silently truncated / changed.

Hi @arthurschreiber

I agree that exact values by default would be cleaner and safer. Users shouldn’t need to discover an option to avoid losing digits. The main benefit of decimalAsString is that existing applications can keep working and migrate gradually. Always returning strings would give everyone one consistent behavior, but applications expecting numbers would need updating.

I’d support making this a breaking change with clear migration guidance. It would also help to clarify the write behavior separately, whether number inputs remain accepted and whether values with too many fractional digits are rounded or rejected.

Happy to help test the agreed behavior against SQL Server. Thanks for considering this!

@colinhacks

Copy link
Copy Markdown
Author

Agreed it should be the default. I'd still ship it as an option first though, so it can go out in a minor instead of waiting on the next major, then make it the default and drop the option in that major. Happy to do that PR as well.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String support for Decimal and Numeric values

5 participants