Skip to content

Conversation

@jahnvi480
Copy link
Contributor

@jahnvi480 jahnvi480 commented Aug 22, 2025

Work Item / Issue Reference

AB#34928


Summary

This pull request adds support for retrieving table and index statistics via the ODBC SQLStatistics API in the mssql_python package. The main changes include implementing the statistics method 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:

  • Added the statistics method to the Cursor class in mssql_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.
  • Defined new constants (SQL_INDEX_UNIQUE, SQL_INDEX_ALL, SQL_QUICK, SQL_ENSURE) in mssql_python/constants.py to support the statistics method options.

C++ binding and integration:

  • Added the SQLStatisticsFunc type and related function pointer to mssql_python/pybind/ddbc_bindings.h and initialized it in mssql_python/pybind/ddbc_bindings.cpp.
  • Implemented the SQLStatistics_wrap function and exposed it to Python via the DDBCSQLStatistics binding in the pybind module.

Testing:

  • Added a comprehensive suite of tests in tests/test_004_cursor.py for the new statistics method, including tests for setup, basic functionality, unique index filtering, empty tables, non-existent tables, result structure, catalog filtering, the quick parameter, and cleanup.

@github-actions github-actions bot added the pr-size: medium Moderate update size label Aug 22, 2025
Copy link
Contributor

@sumitmsft sumitmsft left a 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

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Sep 12, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Sep 12, 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]>
@github-actions github-actions bot added pr-size: large Substantial code update and removed pr-size: medium Moderate update size labels Sep 15, 2025
@jahnvi480 jahnvi480 merged commit f37687b into jahnvi/cursor_specialcol Sep 15, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: large Substantial code update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants