-
Notifications
You must be signed in to change notification settings - Fork 31
FEAT: Adding statistics API in cursor class #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sumitmsft
requested changes
Sep 11, 2025
Contributor
sumitmsft
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a few changes
sumitmsft
approved these changes
Sep 12, 2025
bewithgaurav
approved these changes
Sep 15, 2025
### 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#34927](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34927) ------------------------------------------------------------------- ### Summary This pull request adds support for retrieving column metadata from SQL Server using the ODBC `SQLColumns` API, and exposes this functionality to Python through the `columns()` method on the `cursor` class. The implementation includes both backend C++ bindings and a high-level Python interface, as well as careful handling of cursor state and result set mapping. The most important changes are: **Python interface:** * Added a new `columns()` method to the `cursor` class in `mssql_python/cursor.py` to retrieve detailed column metadata for tables using the ODBC `SQLColumns` function. This method includes extensive documentation, result mapping, and warnings about potentially expensive queries. It also wraps and overrides the fetch methods to provide specialized column mappings for the result set. * Updated the `execute()` method in `cursor.py` to restore the original fetch methods if they were previously overridden, ensuring correct behavior after using `columns()`. **C++ bindings and backend:** * Added the `SQLColumnsFunc` typedef, function pointer, and symbol loading for the `SQLColumnsW` ODBC API in `ddbc_bindings.h` and `ddbc_bindings.cpp`. This ensures the backend can call the ODBC function for column metadata. * Implemented the `SQLColumns_wrap` C++ function in `ddbc_bindings.cpp` to call the ODBC `SQLColumns` API, handling both Windows and Unix platforms, and converting Python objects to wide strings as needed. * Exposed a new `DDBCSQLColumns` method in the Python extension module, allowing Python code to call the new backend functionality. --------- Co-authored-by: Jahnvi Thakkar <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request adds support for retrieving table and index statistics via the ODBC
SQLStatisticsAPI in themssql_pythonpackage. The main changes include implementing thestatisticsmethod on the Python cursor, exposing the required C++ bindings, defining new constants, and introducing comprehensive tests to ensure the new functionality works as expected.New statistics feature:
statisticsmethod to theCursorclass inmssql_python/cursor.py, allowing users to retrieve index and table statistics for a given table, with options to filter by uniqueness, catalog, schema, and quickness of statistics retrieval.SQL_INDEX_UNIQUE,SQL_INDEX_ALL,SQL_QUICK,SQL_ENSURE) inmssql_python/constants.pyto support the statistics method options.C++ binding and integration:
SQLStatisticsFunctype and related function pointer tomssql_python/pybind/ddbc_bindings.hand initialized it inmssql_python/pybind/ddbc_bindings.cpp.SQLStatistics_wrapfunction and exposed it to Python via theDDBCSQLStatisticsbinding in the pybind module.Testing:
tests/test_004_cursor.pyfor the newstatisticsmethod, including tests for setup, basic functionality, unique index filtering, empty tables, non-existent tables, result structure, catalog filtering, thequickparameter, and cleanup.