Commit 82119f7
authored
FIX: Improvement to parameter type inference and handling (#215)
### 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#38383](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/38383)
<!-- External contributors: GitHub Issue -->
> GitHub Issue: #<ISSUE_NUMBER>
-------------------------------------------------------------------
### Summary
<!-- Insert your summary of changes below. Minimum 10 characters
required. -->
This pull request refactors the logic for inferring SQL types from
Python parameters in the `mssql_python/cursor.py` module, especially for
batch operations using `executemany`. The main improvements are more
accurate type detection for integer columns by considering the minimum
and maximum values in the data, and a cleaner separation of concerns in
the codebase.
**Improvements to type inference and parameter handling:**
* Refactored the type mapping logic in `_map_sql_type` to use `min_val`
and `max_val` for integer columns, allowing for more accurate type
selection based on the actual range of values in the data.
* Updated `_create_parameter_types_list` to accept and forward `min_val`
and `max_val`, supporting the improved type inference for batch
operations.
* Replaced the static method `_select_best_sample_value` with a new
`_compute_column_type` method, which determines a representative sample
value and computes min/max for integer columns, enhancing how types are
inferred for each parameter column in `executemany`.
[[1]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280L827-L855)
[[2]](diffhunk://#diff-deceea46ae01082ce8400e14fa02f4b7585afb7b5ed9885338b66494f5f38280R848-R873)
* Modified the `executemany` method to use `_compute_column_type` for
each parameter column, passing the computed min/max values to
`_create_parameter_types_list` for better type assignment.
**Code cleanup:**
* Removed the now-unnecessary `_select_best_sample_value` static method,
consolidating logic and reducing code duplication.
<!--
### 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 7d06c96 commit 82119f7
1 file changed
+49
-49
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
| 225 | + | |
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
250 | 254 | | |
251 | 255 | | |
252 | 256 | | |
253 | 257 | | |
254 | 258 | | |
255 | 259 | | |
256 | 260 | | |
257 | | - | |
| 261 | + | |
258 | 262 | | |
259 | 263 | | |
260 | 264 | | |
261 | 265 | | |
262 | 266 | | |
263 | 267 | | |
264 | 268 | | |
265 | | - | |
| 269 | + | |
266 | 270 | | |
267 | 271 | | |
268 | 272 | | |
| |||
514 | 518 | | |
515 | 519 | | |
516 | 520 | | |
517 | | - | |
518 | | - | |
| 521 | + | |
| 522 | + | |
519 | 523 | | |
520 | 524 | | |
521 | | - | |
522 | 525 | | |
523 | 526 | | |
524 | | - | |
525 | 527 | | |
526 | 528 | | |
527 | 529 | | |
528 | 530 | | |
529 | 531 | | |
530 | | - | |
| 532 | + | |
531 | 533 | | |
532 | 534 | | |
533 | 535 | | |
| |||
833 | 835 | | |
834 | 836 | | |
835 | 837 | | |
836 | | - | |
837 | | - | |
838 | | - | |
839 | | - | |
840 | | - | |
841 | | - | |
842 | | - | |
843 | | - | |
844 | | - | |
845 | | - | |
846 | | - | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
865 | | - | |
866 | | - | |
867 | 838 | | |
868 | 839 | | |
869 | 840 | | |
| |||
882 | 853 | | |
883 | 854 | | |
884 | 855 | | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
885 | 882 | | |
886 | 883 | | |
887 | 884 | | |
| |||
896 | 893 | | |
897 | 894 | | |
898 | 895 | | |
899 | | - | |
| 896 | + | |
900 | 897 | | |
901 | 898 | | |
902 | | - | |
| 899 | + | |
903 | 900 | | |
904 | 901 | | |
905 | 902 | | |
| |||
910 | 907 | | |
911 | 908 | | |
912 | 909 | | |
913 | | - | |
914 | | - | |
915 | | - | |
916 | | - | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
917 | 916 | | |
| 917 | + | |
918 | 918 | | |
919 | 919 | | |
920 | | - | |
| 920 | + | |
921 | 921 | | |
922 | 922 | | |
923 | 923 | | |
| |||
934 | 934 | | |
935 | 935 | | |
936 | 936 | | |
937 | | - | |
| 937 | + | |
938 | 938 | | |
939 | 939 | | |
940 | 940 | | |
941 | | - | |
| 941 | + | |
942 | 942 | | |
943 | 943 | | |
944 | 944 | | |
| |||
0 commit comments