Uh oh!
There was an error while loading. Please reload this page.
FIX: Stored datetime.time values have the microseconds attribute set to zero - #479
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes microsecond loss when round-tripping SQL Server TIME/TIME2 values by switching Python datetime.time binding to a text C-type with microsecond precision and updating the C++ fetch/batch-fetch paths to parse TIME2 from text back into datetime.time.
Changes:
- Bind Python
datetime.timeparameters as text (SQL_C_CHAR/SQL_C_WCHAR) usingisoformat(timespec="microseconds"). - Fetch
SQL_SS_TIME2as text in bothSQLGetData_wrapand batch fetch, converting todatetime.timevia a new parser. - Add a regression test asserting
TIME(6)preserves microseconds on insert/select.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/test_004_cursor.py | Adds a regression test for TIME microsecond round-trip; minor formatting updates to SQL strings. |
| mssql_python/pybind/ddbc_bindings.cpp | Adds a TIME text parser and changes SQL_SS_TIME2 retrieval/binding to use SQL_C_CHAR buffers. |
| mssql_python/cursor.py | Changes TIME parameter mapping and normalizes TIME values to microsecond ISO text in execute/executemany binding. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql_python/pybind/ddbc_bindings.cppLines 3496-3507 3496 row.append(PythonObjectCache::get_time_class()(
3497 t2.hour, t2.minute, t2.second, t2.fraction / 1000)); // ns to µs3498 } else {
3499if (!SQL_SUCCEEDED(ret)) {
! 3500LOG("SQLGetData: Error retrieving SQL_SS_TIME2 for column "
! 3501"%d - SQLRETURN=%d",
! 3502 i, ret);
! 3503 }
3504 row.append(py::none());
3505 }
3506break;
3507 }Lines 3674-3682 3674 #endif
3675default:
3676 std::ostringstream errorString;
3677 errorString << "Unsupported data type for column - " << columnName << ", Type - "
! 3678 << effectiveDataType << ", column ID - " << i;
3679LOG("SQLGetData: %s", errorString.str().c_str());
3680ThrowStdException(errorString.str());
3681break;
3682 }📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 67.8%
mssql_python.row.py: 70.5%
mssql_python.pybind.logger_bridge.hpp: 70.8%
mssql_python.pybind.connection.connection.cpp: 75.3%
mssql_python.__init__.py: 77.3%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.connection.py: 85.2%
mssql_python.cursor.py: 86.1%🔗 Quick Links
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Work Item / Issue Reference
Summary
This pull request introduces significant improvements to how SQL TIME/TIME2 values are handled in the MSSQL Python driver, transitioning from native C-type bindings to text-based representations. The changes ensure correct parsing, binding, and conversion between SQL TIME values and Python
datetime.timeobjects, addressing edge cases and improving compatibility.SQL TIME/TIME2 Handling Improvements
mssql_python/cursor.pyto useSQL_TYPE_TIMEand text C-types (SQL_C_CHAR), and normalized Pythondatetime.timevalues to ISO text format with microseconds.ddbc_bindings.cppto bind and fetch TIME/TIME2 columns as text buffers instead of native structs, and introduced a robust parser (ParseSqlTimeTextToPythonObject) for converting SQL time text to Python objects.SQL_TIME_TEXT_MAX_LEN).Testing and Utilities
Miscellaneous
drop_table_if_existsfor reliability.