Skip to content

Commit 1d4a417

Browse files
committed
Merged PR 5320: Enhance Data Handling and Precision in DDBC Bindings
This PR introduces several improvements and fixes to the DDBC bindings, focusing on data handling, precision, and proper rounding for various SQL data types. The key changes include: 1. **Handling SQL_FLOAT and SQL_REAL with Proper Rounding:** 2. **Handling SQL_FLOAT and SQL_DOUBLE:** - Ensured that `SQL_DOUBLE` values are fetched and appended to the row with the correct precision. **Testing:** - Updated the test script `testing_ddbc_bindings.py` to include test cases for the newly handled data types and conversions. - Verified that the data is fetched and converted correctly for various SQL data types, including numeric, time, and binary types. **Impact:** - These changes enhance the robustness and accuracy of data handling in the DDBC bindings, ensuring that various SQL data types are correctly fetched, converted, and appended to the result rows. - The improvements also make the codebase more maintainable and easier to understand. **Reviewer Notes:** - Please review the changes to the handling of `SQL_FLOAT`, `SQL_REAL`, and `SQL_DOUBLE` with proper rounding. Related work items: #33831
1 parent 3ef7fc4 commit 1d4a417

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct ColumnBuffers {
105105
std::vector<std::vector<SQLWCHAR>> wcharBuffers;
106106
std::vector<std::vector<SQLINTEGER>> intBuffers;
107107
std::vector<std::vector<SQLSMALLINT>> smallIntBuffers;
108-
std::vector<std::vector<SQLFLOAT>> floatBuffers;
108+
std::vector<std::vector<SQLREAL>> realBuffers;
109109
std::vector<std::vector<SQLDOUBLE>> doubleBuffers;
110110
std::vector<std::vector<SQL_NUMERIC_STRUCT>> numericBuffers;
111111
std::vector<std::vector<SQL_TIMESTAMP_STRUCT>> timestampBuffers;
@@ -120,7 +120,7 @@ struct ColumnBuffers {
120120
wcharBuffers(numCols),
121121
intBuffers(numCols),
122122
smallIntBuffers(numCols),
123-
floatBuffers(numCols),
123+
realBuffers(numCols),
124124
doubleBuffers(numCols),
125125
numericBuffers(numCols),
126126
timestampBuffers(numCols),
@@ -906,12 +906,11 @@ SQLRETURN SQLGetData_wrap(intptr_t StatementHandle, SQLUSMALLINT colCount, py::l
906906
}
907907
break;
908908
}
909-
case SQL_FLOAT:
910909
case SQL_REAL: {
911-
SQLREAL floatValue;
912-
ret = SQLGetData_ptr(hStmt, i, SQL_C_FLOAT, &floatValue, 0, NULL);
910+
SQLREAL realValue;
911+
ret = SQLGetData_ptr(hStmt, i, SQL_REAL, &realValue, 0, NULL);
913912
if (SQL_SUCCEEDED(ret)) {
914-
row.append(static_cast<float>(floatValue));
913+
row.append(realValue);
915914
} else {
916915
row.append(py::none());
917916
}
@@ -933,11 +932,12 @@ SQLRETURN SQLGetData_wrap(intptr_t StatementHandle, SQLUSMALLINT colCount, py::l
933932
}
934933
break;
935934
}
936-
case SQL_DOUBLE: {
935+
case SQL_DOUBLE:
936+
case SQL_FLOAT: {
937937
SQLDOUBLE doubleValue;
938938
ret = SQLGetData_ptr(hStmt, i, SQL_C_DOUBLE, &doubleValue, 0, NULL);
939939
if (SQL_SUCCEEDED(ret)) {
940-
row.append(static_cast<double>(doubleValue));
940+
row.append(doubleValue);
941941
} else {
942942
row.append(py::none());
943943
}
@@ -1154,9 +1154,8 @@ SQLRETURN SQLBindColums(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& column
11541154
sizeof(SQLCHAR), buffers.indicators[col - 1].data());
11551155
break;
11561156
case SQL_REAL:
1157-
case SQL_FLOAT:
1158-
buffers.floatBuffers[col - 1].resize(fetchSize);
1159-
ret = SQLBindCol_ptr(hStmt, col, SQL_C_FLOAT, buffers.floatBuffers[col - 1].data(),
1157+
buffers.realBuffers[col - 1].resize(fetchSize);
1158+
ret = SQLBindCol_ptr(hStmt, col, SQL_REAL, buffers.realBuffers[col - 1].data(),
11601159
sizeof(SQLREAL), buffers.indicators[col - 1].data());
11611160
break;
11621161
case SQL_DECIMAL:
@@ -1167,6 +1166,7 @@ SQLRETURN SQLBindColums(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& column
11671166
sizeof(SQL_NUMERIC_STRUCT), buffers.indicators[col - 1].data());
11681167
break;
11691168
case SQL_DOUBLE:
1169+
case SQL_FLOAT:
11701170
buffers.doubleBuffers[col - 1].resize(fetchSize);
11711171
ret =
11721172
SQLBindCol_ptr(hStmt, col, SQL_C_DOUBLE, buffers.doubleBuffers[col - 1].data(),
@@ -1305,8 +1305,7 @@ SQLRETURN FetchBatchData(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& colum
13051305
row.append(buffers.charBuffers[col - 1][i]);
13061306
break;
13071307
case SQL_REAL:
1308-
case SQL_FLOAT:
1309-
row.append(static_cast<float>(buffers.floatBuffers[col - 1][i]));
1308+
row.append(buffers.realBuffers[col - 1][i]);
13101309
break;
13111310
case SQL_DECIMAL:
13121311
case SQL_NUMERIC:
@@ -1320,7 +1319,8 @@ SQLRETURN FetchBatchData(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& colum
13201319
.to_double());
13211320
break;
13221321
case SQL_DOUBLE:
1323-
row.append(static_cast<double>(buffers.doubleBuffers[col - 1][i]));
1322+
case SQL_FLOAT:
1323+
row.append(buffers.doubleBuffers[col - 1][i]);
13241324
break;
13251325
case SQL_TIMESTAMP:
13261326
case SQL_TYPE_TIMESTAMP:

0 commit comments

Comments
 (0)