Commit 9f17c10
authored
FIX: Binary data padding + tests (#218)
### Work Item / Issue Reference
<!--
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#38480](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/38480)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #<ISSUE_NUMBER>
-------------------------------------------------------------------
### Summary
This pull request improves how binary data (Python `bytes` and
`bytearray`) is handled in the MSSQL Python driver, especially for edge
cases like empty values, mixed types, and large binaries. It also
significantly expands the test suite to cover these scenarios and
documents current driver limitations regarding parameter and fetch
buffer sizes.
**Binary Data Handling Improvements**
* Updated `_map_sql_type` in `cursor.py` to always use `VARBINARY` for
Python `bytes`/`bytearray`, avoiding storage waste and ensuring correct
handling of variable-length data. This removes previous logic that
sometimes used fixed-length `BINARY` and simplifies type mapping.
* Improved sample value selection in `_select_best_sample_value` to
correctly handle columns that contain only binary types (`bytes` or
`bytearray`).
**Test Suite Enhancements for Binary Data**
* Fixed and clarified the `test_longvarbinary` test to expect the
correct number of rows and removed assumptions about zero-padding in
returned binary data.
* Added new tests covering edge cases:
- Executemany with binary data and empty byte arrays, ensuring correct
insertion and retrieval of empty, regular, and null binary values.
- Binary data over 8000 bytes, documenting driver limitations (8192
bytes for parameters, 4096 bytes for fetch buffer) and verifying correct
error handling and data retrieval for supported sizes.
- All empty binaries: verifies that multiple empty binary rows are
handled and returned as zero-length `bytes`.
- Mixing `bytes` and `bytearray` types in the same column, confirming
consistent storage and retrieval as `bytes`.
- Binary columns with mostly small/empty values and one large value,
ensuring the large value is handled within driver limits.
- Table with only NULL and empty binary values, verifying clear
distinction between NULL and empty values and correct query results.
<!--
### PR Title Guide
> 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)
### Contribution Guidelines
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 95a1f18 commit 9f17c10
2 files changed
+338
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
| 383 | + | |
| 384 | + | |
391 | 385 | | |
392 | | - | |
| 386 | + | |
393 | 387 | | |
394 | 388 | | |
395 | 389 | | |
396 | 390 | | |
397 | 391 | | |
398 | 392 | | |
399 | 393 | | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
| 394 | + | |
| 395 | + | |
408 | 396 | | |
409 | | - | |
| 397 | + | |
410 | 398 | | |
411 | 399 | | |
412 | 400 | | |
| |||
848 | 836 | | |
849 | 837 | | |
850 | 838 | | |
| 839 | + | |
| 840 | + | |
851 | 841 | | |
852 | 842 | | |
853 | 843 | | |
| |||
0 commit comments