Commit c4e647e
authored
FIX: Align date/time type code mappings with ODBC 18 driver source (microsoft#352) (microsoft#355)
### Work Item / Issue Reference
<!--
IMPORTANT: Please follow the PR template guidelines below.
For mssql-python maintainers: Insert your ADO Work Item ID below
For external contributors: Insert Github Issue number below
Only one reference is required - either GitHub issue OR ADO Work Item.
-->
<!-- External contributors: GitHub Issue -->
> GitHub Issue: microsoft#352
-------------------------------------------------------------------
### Summary
Fixes `polars.read_database()` `ComputeError` on DATE columns by
aligning `cursor.py` type code mappings with the ODBC 18 driver's actual
reported SQL type codes.
**Root Cause:**
The ODBC 18 driver reports ODBC 3.x type codes (`SQL_TYPE_DATE=91`,
`SQL_TYPE_TIMESTAMP=93`, `SQL_SS_TIME2=-154`) but `_map_data_type` only
had ODBC 2.x constants (`SQL_DATE=9`, `SQL_TIME=10`,
`SQL_TIMESTAMP=11`). DATE columns fell through to the `str` default,
causing a schema mismatch when polars expected `pl.Date`.
**Fix (verified against ODBC 18 driver C++ source):**
- `_map_data_type`: Rewritten with driver-verified ODBC 3.x type codes
organized by category (string, integer, float, decimal, date/time,
boolean, binary, UUID, XML). Removed phantom ODBC 2.x entries that the
driver never reports.
- `_get_c_type_for_sql_type`: Updated C-type bindings from ODBC 2.x to
3.x codes for all date/time types.
- `constants.py`: Added `SQL_SS_TIME2`, `SQL_SS_XML`, `SQL_C_SS_TIME2`.
**Zero breaking changes.** `cursor.description[i][1]` continues to
return Python type objects (`datetime.date`, `datetime.datetime`, etc.)
— now mapped from the correct SQL type codes.
## Changes
| File | Change |
|------|--------|
| `mssql_python/constants.py` | +3 constants (`SQL_SS_TIME2`,
`SQL_SS_XML`, `SQL_C_SS_TIME2`) |
| `mssql_python/cursor.py` | Rewritten `_map_data_type` and
`_get_c_type_for_sql_type` with ODBC 3.x codes |
| `tests/test_018_polars_pandas_integration.py` | **NEW** — 14
integration tests |
## Testing
14 new tests across 3 test classes:
- **`TestCursorDescriptionTypeCodes`** (7 tests): Verifies all 6
date/time SQL types return correct Python types with `isclass()` checks
- **`TestPolarsIntegration`** (4 tests): `polars.read_database()` with
DATE, all datetime types, mixed types, NULLs
- **`TestPandasIntegration`** (3 tests): `pandas.read_sql()` with DATE,
all datetime types, mixed types
All 14 tests pass. Existing test suite (448 tests) unaffected.1 parent 5a2187d commit c4e647e
File tree
3 files changed
+440
-12
lines changed- mssql_python
- tests
3 files changed
+440
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| 118 | + | |
| 119 | + | |
118 | 120 | | |
119 | 121 | | |
120 | 122 | | |
| |||
365 | 367 | | |
366 | 368 | | |
367 | 369 | | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
368 | 376 | | |
369 | 377 | | |
370 | 378 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
844 | 844 | | |
845 | 845 | | |
846 | 846 | | |
847 | | - | |
| 847 | + | |
848 | 848 | | |
849 | 849 | | |
850 | 850 | | |
| |||
865 | 865 | | |
866 | 866 | | |
867 | 867 | | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
868 | 875 | | |
869 | 876 | | |
870 | 877 | | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
871 | 881 | | |
872 | 882 | | |
873 | 883 | | |
| |||
1026 | 1036 | | |
1027 | 1037 | | |
1028 | 1038 | | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
1029 | 1058 | | |
1030 | | - | |
| 1059 | + | |
1031 | 1060 | | |
1032 | 1061 | | |
1033 | 1062 | | |
1034 | 1063 | | |
1035 | 1064 | | |
1036 | | - | |
1037 | | - | |
1038 | | - | |
| 1065 | + | |
1039 | 1066 | | |
| 1067 | + | |
| 1068 | + | |
1040 | 1069 | | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
1041 | 1078 | | |
1042 | 1079 | | |
| 1080 | + | |
| 1081 | + | |
1043 | 1082 | | |
1044 | 1083 | | |
1045 | | - | |
1046 | | - | |
1047 | | - | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
1048 | 1095 | | |
1049 | | - | |
1050 | | - | |
1051 | | - | |
| 1096 | + | |
1052 | 1097 | | |
1053 | 1098 | | |
1054 | 1099 | | |
| 1100 | + | |
1055 | 1101 | | |
1056 | | - | |
| 1102 | + | |
| 1103 | + | |
1057 | 1104 | | |
1058 | 1105 | | |
1059 | 1106 | | |
| |||
0 commit comments