[CONCPP-96] PreparedStatement SetDouble Drops Significant Digits Created: 2022-02-20  Updated: 2022-05-09  Resolved: 2022-05-09

Status: Closed
Project: MariaDB Connector/C++
Component/s: General
Affects Version/s: 1.0.1, 1.1.0
Fix Version/s: 1.0.2, 1.1.2

Type: Bug Priority: Major
Reporter: Joe C Assignee: Lawrin Novitsky
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Tested with mariadbcpp versions 1.0.1 and 1.0.0 on ubuntu 20.04



 Description   

Table for testing:

CREATE TABLE `contracts` (
  `cost_as_dbl` decimal(15,4) NOT NULL DEFAULT 0.0000,
  `cost_as_str` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

C++ code for testing:

#include "mariadb/conncpp.hpp"
#include <memory>
 
int
main()
{
  sql::Driver* driver = sql::mariadb::get_driver_instance();
  sql::SQLString url("jdbc:mariadb://localhost:3306/*****");
  sql::Properties properties(
    { { "user", "******" }, { "password", "********" } });
 
   std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
 
  double cost(98.765432109);
 
  std::unique_ptr<sql::PreparedStatement> pstmnt(
    conn->prepareStatement("INSERT INTO contracts (cost_as_dbl, cost_as_str) VALUES (?, ?);"));
 
  do {
    pstmnt->setDouble(1, cost);
    pstmnt->setString(2, std::to_string(cost));
    pstmnt->executeQuery();
    cost *= 10.0;
  } while (cost < 1000000000);
}

Query Output:

MariaDB [test]> select * from contracts;
+----------------+------------------+
| cost_as_dbl    | cost_as_str      |
+----------------+------------------+
|        98.7654 | 98.765432        |
|       987.6540 | 987.654321       |
|      9876.5400 | 9876.543211      |
|     98765.4000 | 98765.432109     |
|    987654.0000 | 987654.321090    |
|   9876540.0000 | 9876543.210900   |
|  98765400.0000 | 98765432.109000  |
| 987654000.0000 | 987654321.090000 |
+----------------+------------------+
8 rows in set (0.000 sec)

pstmnt->setDouble only processes the first 6 significant digits and zeros the remaining.



 Comments   
Comment by Lawrin Novitsky [ 2022-02-22 ]

Thank you for your report. I could reproduce the issue. The picture is slightly better with useServerPrepStmts, but still some digits are dropped

Comment by Joe C [ 2022-02-22 ]

Thanks.
useServerPrepStmts will do as a temporary fix for me.

Comment by Lawrin Novitsky [ 2022-05-09 ]

The fix and the testcase have been pushed to mater(1.0) branch. Yet to be merged into develop(1.1 branch). The fix ensures that decimal digits are not lost during conversion to the string.

Generated at Thu Feb 08 03:08:18 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.