Skip to content

Conversation

@gargsaumya
Copy link
Contributor

@gargsaumya gargsaumya commented Sep 19, 2025

Work Item / Issue Reference

AB#34945

GitHub Issue: #<ISSUE_NUMBER>


Summary

This pull request adds full support for handling Python uuid.UUID objects with SQL Server's UNIQUEIDENTIFIER type in the mssql_python driver. It introduces robust conversion between Python UUIDs and SQL GUIDs for both single and batch operations, ensures correct retrieval as Python uuid.UUID objects, and adds comprehensive tests for these scenarios.

UUID/GUID Support Improvements:

  • Added mapping for Python uuid.UUID to SQL GUID types in _map_sql_type, enabling seamless parameter binding for UUIDs.
  • Implemented correct binding and conversion logic for UUIDs in both single (BindParameters) and batch (BindParameterArray) parameter bindings, including validation of binary length and error handling. [1] [2]
  • Enhanced data retrieval to return UUID columns as Python uuid.UUID objects instead of strings or raw bytes, in both single-row (SQLGetData_wrap) and batch (FetchBatchData) fetches. [1] [2]

Testing Enhancements:

  • Added comprehensive tests for inserting, selecting, and batch operations involving UUIDs, including cases with None (NULL) values and mixed UUID/NULL batches.
  • Updated test imports to include the uuid module for use in new test cases.

@gargsaumya gargsaumya changed the title FEAT : uniqueidentifier support in executemany() FEAT: uniqueidentifier support in executemany() Sep 22, 2025
@github-actions github-actions bot added the pr-size: small Minimal code update label Sep 22, 2025
@gargsaumya gargsaumya changed the base branch from saumya/uuid to main September 24, 2025 05:15
@github-actions github-actions bot added pr-size: small Minimal code update and removed pr-size: small Minimal code update labels Sep 24, 2025
Copilot AI review requested due to automatic review settings September 24, 2025 11:36
@gargsaumya gargsaumya force-pushed the saumya/uuid-executemany branch from f05ee81 to 763f32f Compare September 24, 2025 11:36
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: small Minimal code update labels Sep 24, 2025
Copy link
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

This pull request adds comprehensive support for handling Python uuid.UUID objects with SQL Server's UNIQUEIDENTIFIER type in the mssql_python driver. It enables seamless conversion between Python UUIDs and SQL GUIDs for both single and batch operations.

  • Implements UUID parameter binding for executemany() operations with proper type conversion and validation
  • Enhances data retrieval to return UUID columns as Python uuid.UUID objects instead of raw data
  • Adds comprehensive test coverage for UUID operations including mixed UUID/NULL scenarios

Reviewed Changes

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

File Description
mssql_python/pybind/ddbc_bindings.cpp Adds UUID parameter binding logic for batch operations and improves UUID data retrieval with NULL handling
mssql_python/cursor.py Removes unnecessary whitespace in executemany method
tests/test_004_cursor.py Adds comprehensive test cases for UUID operations in both single and batch scenarios

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Sep 24, 2025
@github-actions github-actions bot added pr-size: medium Moderate update size and removed pr-size: medium Moderate update size labels Sep 24, 2025
@bewithgaurav
Copy link
Collaborator

merging main branch with this to test out code coverage reports and comments

@github-actions
Copy link

github-actions bot commented Sep 26, 2025

📊 Code Coverage Report

🔥 Diff Coverage

72%


🎯 Overall Coverage

73%


📈 Total Lines Covered: 4023 out of 5457
📁 Project: mssql-python


Diff Coverage

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

  • mssql_python/pybind/ddbc_bindings.cpp (72.3%): Missing lines 1981-1986,1991-1993,2008,3143-3145

Summary

  • Total: 47 lines
  • Missing: 13 lines
  • Coverage: 72%

mssql_python/pybind/ddbc_bindings.cpp

Lines 1977-1997

  1977                             strLenOrIndArray[i] = SQL_NULL_DATA;
  1978                             continue;
  1979                         }
  1980                         else if (py::isinstance<py::bytes>(element)) {
! 1981                             py::bytes b = element.cast<py::bytes>();
! 1982                             if (PyBytes_GET_SIZE(b.ptr()) != 16) {
! 1983                                 ThrowStdException("UUID binary data must be exactly 16 bytes long.");
! 1984                             }
! 1985                             std::memcpy(uuid_bytes.data(), PyBytes_AS_STRING(b.ptr()), 16);
! 1986                         }
  1987                         else if (py::isinstance(element, uuid_class)) {
  1988                             py::bytes b = element.attr("bytes_le").cast<py::bytes>();
  1989                             std::memcpy(uuid_bytes.data(), PyBytes_AS_STRING(b.ptr()), 16);
  1990                         }
! 1991                         else {
! 1992                             ThrowStdException(MakeParamMismatchErrorStr(info.paramCType, paramIndex));
! 1993                         }
  1994                         guidArray[i].Data1 = (static_cast<uint32_t>(uuid_bytes[3]) << 24) |
  1995                                             (static_cast<uint32_t>(uuid_bytes[2]) << 16) |
  1996                                             (static_cast<uint32_t>(uuid_bytes[1]) << 8)  |
  1997                                             (static_cast<uint32_t>(uuid_bytes[0]));

Lines 2004-2012

  2004                     }
  2005                     dataPtr = guidArray;
  2006                     bufferLength = sizeof(SQLGUID);
  2007                     break;
! 2008                 }
  2009                 default: {
  2010                     ThrowStdException("BindParameterArray: Unsupported C type: " + std::to_string(info.paramCType));
  2011                 }
  2012             }

Lines 3139-3149

  3139                 }
  3140                 case SQL_GUID: {
  3141                     SQLLEN indicator = buffers.indicators[col - 1][i];
  3142                     if (indicator == SQL_NULL_DATA) {
! 3143                         row.append(py::none());
! 3144                         break;
! 3145                     }
  3146                     SQLGUID* guidValue = &buffers.guidBuffers[col - 1][i];
  3147                     uint8_t reordered[16];
  3148                     reordered[0] = ((char*)&guidValue->Data1)[3];
  3149                     reordered[1] = ((char*)&guidValue->Data1)[2];


📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
mssql_python.pybind.ddbc_bindings.cpp: 67.7%
mssql_python.pybind.connection.connection.cpp: 68.3%
mssql_python.ddbc_bindings.py: 68.5%
mssql_python.cursor.py: 78.8%
mssql_python.pybind.connection.connection_pool.cpp: 78.9%
mssql_python.connection.py: 81.7%
mssql_python.helpers.py: 84.7%
mssql_python.auth.py: 85.3%
mssql_python.type.py: 86.8%
mssql_python.pooling.py: 88.8%

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

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 comment for test cases

@gargsaumya gargsaumya force-pushed the saumya/uuid-executemany branch from 2bcee36 to 14b9b4f Compare September 29, 2025 13:29
@gargsaumya gargsaumya force-pushed the saumya/uuid-executemany branch from 14b9b4f to eebd6de Compare September 29, 2025 13:30
@gargsaumya gargsaumya merged commit 7598b35 into main Sep 29, 2025
20 of 21 checks passed
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.

coercion of UNIQUEIDENTIFIER / uuid values from binary to string only works with fetchone(), not fetchall() or fetchmany()

4 participants