Bug #7857 Negative floating point values sometimes truncated when stored in short char
Submitted: 13 Jan 2005 1:01 Modified: 23 Jun 2005 19:00
Reporter: Jim Winstead Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.1 OS:
Assigned to: Jim Winstead CPU Architecture:Any

[13 Jan 2005 1:01] Jim Winstead
Description:
There are code paths for storing a floating-point number in a character value that do not end up 
going through Field_str::float, so sometimes real values get truncated when they do not need to 
be.

How to repeat:
mysql> CREATE TABLE t1 (c char(6));
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO t1 VALUES (-1e5);
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> CREATE TABLE t2 (f float);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO t2 VALUES (-1e5);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO t1 SELECT f AS c FROM t2;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t1;
+--------+
| c      |
+--------+
| -10000 |
| -1e+05 |
+--------+
2 rows in set (0.00 sec)

mysql> SELECT -1e5;
+---------+
| -1e5    |
+---------+
| -100000 |
+---------+
1 row in set (0.00 sec)

Suggested fix:
The patch below causes the right thing to happen, but introduces many test failures that would 
need to be evaluated.

--- 1.184/sql/item.cc   Tue Dec 14 01:36:16 2004
+++ edited/sql/item.cc  Wed Jan 12 05:15:23 2005
@@ -1773,9 +1773,7 @@
 int Item::save_in_field(Field *field, bool no_conversions)
 {
   int error;
-  if (result_type() == STRING_RESULT ||
-      result_type() == REAL_RESULT &&
-      field->result_type() == STRING_RESULT)
+  if (result_type() == STRING_RESULT)
   {
     String *result;
     CHARSET_INFO *cs= collation.collation;
[9 Jun 2005 10:12] Ingo Strüwing
The patch looks good and logical. It makes things clearer. However, there is a plethora of possible combinations of all the items and fields with all of their conversion functions. This patch may not address the last remaining uncleanliness.

On the other hand, this has probably been added for a reason. This might however have been wrong and should be reworked. I hope that there is a test in the test suite which addresses this reason. So my suggestion is to run the tests and check the results. If all differences are reasonable then commit everything and I and/or someone else looks over them again. If not, we should think about the new problems.
[23 Jun 2005 19:00] Jim Winstead
looking closer, the test failures show that this causes all sorts of other trouble, and is probably like tugging at that loose thread on a sweater. no thanks!