Skip to content

FIX: Setinputsizes() SQL_DECIMAL crash#519

Open
jahnvi480 wants to merge 6 commits intomainfrom
jahnvi/setinputsize_with_decimal
Open

FIX: Setinputsizes() SQL_DECIMAL crash#519
jahnvi480 wants to merge 6 commits intomainfrom
jahnvi/setinputsize_with_decimal

Conversation

@jahnvi480
Copy link
Copy Markdown
Contributor

Work Item / Issue Reference

AB#53990

GitHub Issue: #503


Summary

This pull request improves the handling of Python Decimal values when binding to SQL DECIMAL and NUMERIC types, especially when using setinputsizes and executemany. It fixes a runtime error by ensuring Decimal objects are converted to strings for proper binding, and adds comprehensive tests to verify this behavior.

Decimal binding and conversion improvements:

  • Changed the mapping for SQL DECIMAL and NUMERIC types to use SQL_C_CHAR instead of SQL_C_NUMERIC in _get_c_type_for_sql_type, enabling string-based binding for decimals.
  • Updated parameter handling in _create_parameter_types_list and executemany to convert Python Decimal objects to strings when binding to SQL DECIMAL or NUMERIC columns.

Testing enhancements:

  • Added new tests in test_004_cursor.py to verify that setinputsizes with SQL_DECIMAL and SQL_NUMERIC accepts Python Decimal values, works with both executemany and execute, and correctly handles NULL values.

Copilot AI review requested due to automatic review settings April 14, 2026 07:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR fixes a crash when binding Python Decimal values to SQL DECIMAL/NUMERIC using setinputsizes() (notably with executemany) by switching to string-based binding and adding regression tests.

Changes:

  • Map SQL DECIMAL/NUMERIC to SQL_C_CHAR for C-type binding and convert Python Decimal values to strings during parameter processing.
  • Update executemany preprocessing to convert Decimal values when SQL type is DECIMAL/NUMERIC.
  • Add new cursor tests covering setinputsizes() + SQL_DECIMAL/SQL_NUMERIC with executemany, execute, and NULL.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
mssql_python/cursor.py Switches DECIMAL/NUMERIC binding to SQL_C_CHAR and adds Decimal-to-string conversions in parameter handling paths.
tests/test_004_cursor.py Adds regression tests validating Decimal binding via setinputsizes() for execute/executemany and NULL handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mssql_python/cursor.py
Comment thread mssql_python/cursor.py
Comment thread tests/test_004_cursor.py Outdated
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 14, 2026

📊 Code Coverage Report

🔥 Diff Coverage

77%


🎯 Overall Coverage

79%


📈 Total Lines Covered: 6663 out of 8408
📁 Project: mssql-python


Diff Coverage

Diff: main...HEAD, staged and unstaged changes

  • mssql_python/cursor.py (77.8%): Missing lines 2327-2328

Summary

  • Total: 9 lines
  • Missing: 2 lines
  • Coverage: 77%

mssql_python/cursor.py

Lines 2323-2332

  2323                         processed_row[i] = format(val, "f")
  2324                     else:
  2325                         try:
  2326                             processed_row[i] = format(decimal.Decimal(str(val)), "f")
! 2327                         except Exception as e:  # pylint: disable=broad-exception-caught
! 2328                             raise ValueError(
  2329                                 f"Failed to convert parameter at row {row}, column {i} to Decimal: {e}"
  2330                             ) from e
  2331             processed_parameters.append(processed_row)


📋 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.ddbc_bindings.cpp: 74.5%
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%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-size: medium Moderate update size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants