Commit bb5337c
committed
FIX: Fix precision loss when binding large Decimal values to SQL_NUMERIC_STRUCT (#287)
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below (e.g.
AB#37452)
For external contributors: Insert Github Issue number below (e.g. #149)
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- mssql-python maintainers: ADO Work Item -->
>
[AB#38111](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/38111)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #<ISSUE_NUMBER>
-------------------------------------------------------------------
<!-- Insert your summary of changes below. Minimum 10 characters
required. -->
This pull request significantly improves the handling of SQL Server
NUMERIC/DECIMAL values in both the Python and C++ layers, addressing
precision, scale, and binary representation for high-precision decimals.
It also introduces a comprehensive suite of tests to validate numeric
roundtrip, edge cases, and boundary conditions. The changes ensure
compliance with SQL Server's maximum precision (38 digits), robust
conversion between Python decimals and SQL binary formats, and better
test coverage for numeric types.
* The `_get_numeric_data` method in `cursor.py` now correctly calculates
the binary representation of decimal values, supporting up to 38 digits
of precision, and constructs the byte array for SQL Server
compatibility. The restriction on precision is raised from 15 to 38
digits.
[[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L198-R199)
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L218-R223)
[[3]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L232-R251)
* The C++ `NumericData` struct now stores the value as a binary string
(16 bytes) instead of a 64-bit integer, allowing support for
high-precision numerics. Related memory handling is updated for
parameter binding.
[[1]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1R24)
[[2]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1L59-R65)
[[3]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1L560-R564)
[[4]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1L2055-R2065)
[[5]](diffhunk://#diff-dde2297345718ec449a14e7dff91b7bb2342b008ecc071f562233646d71144a1L3797-R3801)
* Old numeric tests were removed and replaced with a new, thorough set
of tests covering roundtrip for basic, high-precision, negative, zero,
small, boundary, NULL, fetchmany, and executemany scenarios for numeric
values. This ensures that all critical cases are validated.
[[1]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L1643-L1658)
[[2]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L1724-L1765)
[[3]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69R11348-R11564)
---
These changes collectively make the library more robust and compliant
with SQL Server's numeric type requirements, and the expanded tests will
help catch future regressions.
<!--
> For feature requests
FEAT: (short-description)
> For non-feature requests like test case updates, config updates ,
dependency updates etc
CHORE: (short-description)
> For Fix requests
FIX: (short-description)
> For doc update requests
DOC: (short-description)
> For Formatting, indentation, or styling update
STYLE: (short-description)
> For Refactor, without any feature changes
REFACTOR: (short-description)
> For release related changes, without any feature changes
RELEASE: #<RELEASE_VERSION> (short-description)
External contributors:
- Create a GitHub issue first:
https://github.com/microsoft/mssql-python/issues/new
- Link the GitHub issue in the "GitHub Issue" section above
- Follow the PR title format and provide a meaningful summary
mssql-python maintainers:
- Create an ADO Work Item following internal processes
- Link the ADO Work Item in the "ADO Work Item" section above
- Follow the PR title format and provide a meaningful summary
-->1 parent 47ffca1 commit bb5337c
1 file changed
+381
-58
lines changed
0 commit comments