List:Commits« Previous MessageNext Message »
From:igor Date:April 2 2007 8:42am
Subject:bk commit into 4.1 tree (igor:1.2623) BUG#26788
View as plain text  
Below is the list of changes that have just been committed into a local
4.1 repository of igor. When igor does a push these changes will
be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2007-04-02 01:42:14-07:00, igor@stripped +7 -0
  Fixed bug #26788: an assertion abort the function  Field_str::store
  that converts double values into strings for a character field.
  It chooses the most precise and most compact the representation
  for any double value. If however the the field is not long enough
  to hold any meaningful representation the function truncates it
  and issues a warning message.
  In some cases the string representations were incorrect. This is
  fixed by the patch.

  mysql-test/r/insert.result@stripped, 2007-04-02 01:42:12-07:00, igor@stripped +62 -0
    Added a test case for bug #26788.

  mysql-test/r/type_float.result@stripped, 2007-04-02 01:42:13-07:00, igor@stripped +2 -2
    Adjusted results after fix for bug #26788.

  mysql-test/t/insert.test@stripped, 2007-04-02 01:42:13-07:00, igor@stripped +37 -0
    Added a test case for bug #26788.

  sql/field.cc@stripped, 2007-04-02 01:42:13-07:00, igor@stripped +70 -19
    Fixed bug #26788: an assertion abort the function  Field_str::store
    that converts double values into strings for a character field.
    It chooses the most precise and the most compact representation
    for any double value. If however the the field is not long enough
    to hold any meaningful representation the function truncates the
    result of conversion and issues a warning message.
    In some cases the string representations were incorrect. This is
    fixed by the patch.

  sql/init.cc@stripped, 2007-04-02 01:42:13-07:00, igor@stripped +9 -0
    Added global array log_10_log_10 containing values of 10^(10^n).

  sql/mysql_priv.h@stripped, 2007-04-02 01:42:13-07:00, igor@stripped +1 -0
    Added global array log_10_log_10 containing values of 10^(10^n).

  sql/mysqld.cc@stripped, 2007-04-02 01:42:13-07:00, igor@stripped +1 -0
    Added global array log_10_log_10 containing values of 10^(10^n).

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	igor
# Host:	olga.mysql.com
# Root:	/home/igor/dev-opt/mysql-4.1-opt-bug26788

--- 1.235/sql/field.cc	2007-04-02 01:42:19 -07:00
+++ 1.236/sql/field.cc	2007-04-02 01:42:19 -07:00
@@ -4999,33 +4999,84 @@
 {
   char buff[DOUBLE_TO_STRING_CONVERSION_BUFFER_SIZE];
   uint length;
-  bool use_scientific_notation= TRUE;
+  uint exp_len;
+  int no_scientific_notation= 0;
   uint char_length= field_length / charset()->mbmaxlen;
+  uint precision= char_length;
   /*
     Check fabs(nr) against longest value that can be stored in field,
-    which depends on whether the value is < 1 or not, and negative or not
+    which depends on whether the value is < 1 or not, and negative or not.
   */
   double anr= fabs(nr);
-  int neg= (nr < 0.0) ? 1 : 0;
-  if (char_length > 4 && char_length < 32 &&
-      (anr < 1.0 ? anr > 1/(log_10[max(0,(int) char_length-neg-2)]) /* -2 for "0." */
-                 : anr < log_10[char_length-neg]-1))
-    use_scientific_notation= FALSE;
+  if (nr < 0.0)
+    precision--;  /* '-' always takes one character */
+  /* 
+    The scientific notation is always used when the exponent of the value is
+    less than -4 or greater than or equal to the precision argument.
+  */
+  if (precision > 0 && precision < array_elements(log_10) && 
+      anr >= 1/log_10[min(4,precision-1)] && anr < log_10[precision])
+  {
+    uint i;
+
+    if (anr < log_10[precision-1])
+      --precision; /* take into account possible '.' */
+
+    /* Take into account possible zeros at the beginning (4 at most) */
+    for (i= 0 ; i < precision && anr < 1/log_10[i] ; i++) ;
 
-  length= (uint) my_sprintf(buff, (buff, "%-.*g",
-                                   (use_scientific_notation ?
-                                    max(0, (int)char_length-neg-5) :
-                                    char_length),
-                                   nr));
+    if (precision > i)
+    {
+      precision-= i;
+      no_scientific_notation= 1;
+    }
+    else
+      no_scientific_notation= 2;  /* force %f format */
+  }
+  else
+  { 
+    /* Calculate the number of significant digits in exponent */
+    double d= anr < 1.0 ? 1/anr : anr;
+    for (exp_len= 1 ; 
+         exp_len < array_elements(log_10_log_10) && d >= log_10_log_10[exp_len] ;
+         exp_len++) ;
+    if (precision > exp_len+3)
+      precision-= exp_len+3;
+    else if (anr < 1/log_10[precision-1])
+      no_scientific_notation= 2;  /* force %f format */
+    else
+      precision= 0;
+  }
+ 
+  length= (uint) my_sprintf(buff, 
+                            (buff,
+			     (no_scientific_notation == 2 ? "%-.*f" :  "%-.*g"),
+                             precision, nr));
+
+  if (!no_scientific_notation)
+  {
+    /* 
+      Remove insignificant '0' in the exponent.
+      This will make the representation of the exponent platform independent. 
+    */  
+    char *end= buff+length-exp_len;
+    char *ptr= end;
+    for ( ; *(ptr-1) == '0' ; --ptr) ;
+    if (ptr != end)
+    {
+      memmove(ptr, end, exp_len); 
+      length-= end - ptr;
+    }
+  }    
+    
   /*
-    +1 below is because "precision" in %g above means the
-    max. number of significant digits, not the output width.
-    Thus the width can be larger than number of significant digits by 1
-    (for decimal point)
-    the test for char_length < 5 is for extreme cases,
-    like inserting 500.0 in char(1)
+    Note that length may be greater than char_length.
+    If this is a case a warning about data truncation will be
+    issued by the following call of the store method.
   */
-  DBUG_ASSERT(char_length < 5 || length <= char_length+1);
+  DBUG_ASSERT(length <= char_length || 
+              char_length - test(nr < 0.0) <= 4); 
+
   return store((const char *) buff, length, charset());
 }
 

--- 1.16/sql/init.cc	2007-04-02 01:42:19 -07:00
+++ 1.17/sql/init.cc	2007-04-02 01:42:19 -07:00
@@ -51,6 +51,15 @@
     log_01[i]= nr;
     nr*= 0.1;
   }
+  /* Make a tab of super powers of 10: 10^(10^n) */
+  log_10_log_10[0]= 10.0;
+  for (i= 1 ; i < array_elements(log_10_log_10) ; i++)
+  { 
+    uint ii;
+    for (ii=0, nr= 1.0 ; ii < 10 ; ii++)
+      nr*= log_10_log_10[i-1];
+    log_10_log_10[i]= nr;
+  } 
   specialflag|=options;			/* Set options from argv */
   DBUG_VOID_RETURN;
 }

--- 1.388/sql/mysql_priv.h	2007-04-02 01:42:19 -07:00
+++ 1.389/sql/mysql_priv.h	2007-04-02 01:42:19 -07:00
@@ -910,6 +910,7 @@
 extern char log_error_file[FN_REFLEN];
 extern double log_10[32];
 extern double log_01[32];
+extern double log_10_log_10[3];
 extern ulonglong log_10_int[20];
 extern ulonglong keybuff_size;
 extern ulong refresh_version,flush_version, thread_id,query_id,opened_tables;

--- 1.628/sql/mysqld.cc	2007-04-02 01:42:19 -07:00
+++ 1.629/sql/mysqld.cc	2007-04-02 01:42:19 -07:00
@@ -356,6 +356,7 @@
 
 double log_10[32];			/* 10 potences */
 double log_01[32];
+double log_10_log_10[3];                /* 10^(10^n)   */
 time_t start_time;
 
 char mysql_home[FN_REFLEN], pidfile_name[FN_REFLEN], system_time_zone[30];

--- 1.23/mysql-test/r/insert.result	2007-04-02 01:42:19 -07:00
+++ 1.24/mysql-test/r/insert.result	2007-04-02 01:42:19 -07:00
@@ -325,3 +325,65 @@
 f_double_15_1_u	0.0
 f_float_3_1_u	0.0
 drop table t1;
+CREATE TABLE t1 (
+a char(20) NOT NULL,
+b char(7) DEFAULT NULL,
+c char(4) DEFAULT NULL
+);
+INSERT INTO t1(a,b,c) VALUES (9.999999e+0, 9.999999e+0, 9.999e+0);
+INSERT INTO t1(a,b,c) VALUES (1.225e-05, 1.225e-05, 1.225e-05);
+Warnings:
+Warning	1265	Data truncated for column 'c' at row 1
+INSERT INTO t1(a,b) VALUES (1.225e-04, 1.225e-04);
+INSERT INTO t1(a,b) VALUES (1.225e-01, 1.225e-01);
+INSERT INTO t1(a,b) VALUES (1.225877e-01, 1.225877e-01);
+INSERT INTO t1(a,b) VALUES (1.225e+01, 1.225e+01);
+INSERT INTO t1(a,b,c) VALUES (1.225e+01, 1.225e+01, 1.225e+01);
+INSERT INTO t1(a,b) VALUES (1.225e+05, 1.225e+05);
+INSERT INTO t1(a,b) VALUES (1.225e+10, 1.225e+10);
+INSERT INTO t1(a,b) VALUES (1.225e+15, 1.225e+15);
+INSERT INTO t1(a,b) VALUES (5000000e+0, 5000000e+0);
+INSERT INTO t1(a,b) VALUES (1.25e+78, 1.25e+78);
+INSERT INTO t1(a,b) VALUES (1.25e-94, 1.25e-94);
+INSERT INTO t1(a,b) VALUES (1.25e+203, 1.25e+203);
+INSERT INTO t1(a,b) VALUES (1.25e-175, 1.25e-175);
+INSERT INTO t1(a,c) VALUES (1.225e+0, 1.225e+0);
+INSERT INTO t1(a,c) VALUES (1.37e+0, 1.37e+0);
+INSERT INTO t1(a,c) VALUES (-1.37e+0, -1.37e+0);
+Warnings:
+Warning	1265	Data truncated for column 'c' at row 1
+INSERT INTO t1(a,c) VALUES (1.87e-3, 1.87e-3);
+Warnings:
+Warning	1265	Data truncated for column 'c' at row 1
+INSERT INTO t1(a,c) VALUES (-1.87e-2, -1.87e-2);
+Warnings:
+Warning	1265	Data truncated for column 'c' at row 1
+INSERT INTO t1(a,c) VALUES (5000e+0, 5000e+0);
+INSERT INTO t1(a,c) VALUES (-5000e+0, -5000e+0);
+Warnings:
+Warning	1265	Data truncated for column 'c' at row 1
+SELECT * FROM t1;
+a	b	c
+9.999999000000000748	10	10
+1.225e-5	1.22e-5	0.00
+0.0001225	0.00012	NULL
+0.122499999999999998	0.1225	NULL
+0.122587699999999994	0.12259	NULL
+12.25	12.25	NULL
+12.25	12.25	12.2
+122500	122500	NULL
+12250000000	1.2e+10	NULL
+1225000000000000	1.2e+15	NULL
+5000000	5000000	NULL
+1.25e+78	1.3e+78	NULL
+1.25e-94	1.3e-94	NULL
+1.25e+203	1e+203	NULL
+1.25e-175	1e-175	NULL
+1.225000000000000089	NULL	1.23
+1.370000000000000107	NULL	1.37
+-1.37	NULL	-1.3
+0.00187	NULL	0.00
+-0.0187	NULL	-0.0
+5000	NULL	5000
+-5000	NULL	-500
+DROP TABLE t1;

--- 1.38/mysql-test/r/type_float.result	2007-04-02 01:42:19 -07:00
+++ 1.39/mysql-test/r/type_float.result	2007-04-02 01:42:19 -07:00
@@ -211,9 +211,9 @@
 select * from t1;
 c
 200000
-2e+06
+2e+6
 0.0002
-2e-05
+2e-5
 drop table t1;
 CREATE TABLE t1 (
 reckey int unsigned NOT NULL,

--- 1.21/mysql-test/t/insert.test	2007-04-02 01:42:19 -07:00
+++ 1.22/mysql-test/t/insert.test	2007-04-02 01:42:19 -07:00
@@ -155,4 +155,41 @@
 drop table t1;
 --enable_ps_protocol
 
+#
+# Bug #26788: insert a double literal into a char field
+#
+
+CREATE TABLE t1 (
+  a char(20) NOT NULL,
+  b char(7) DEFAULT NULL,
+  c char(4) DEFAULT NULL
+);
+
+INSERT INTO t1(a,b,c) VALUES (9.999999e+0, 9.999999e+0, 9.999e+0);
+INSERT INTO t1(a,b,c) VALUES (1.225e-05, 1.225e-05, 1.225e-05);
+INSERT INTO t1(a,b) VALUES (1.225e-04, 1.225e-04);
+INSERT INTO t1(a,b) VALUES (1.225e-01, 1.225e-01);
+INSERT INTO t1(a,b) VALUES (1.225877e-01, 1.225877e-01);
+INSERT INTO t1(a,b) VALUES (1.225e+01, 1.225e+01);
+INSERT INTO t1(a,b,c) VALUES (1.225e+01, 1.225e+01, 1.225e+01);
+INSERT INTO t1(a,b) VALUES (1.225e+05, 1.225e+05);
+INSERT INTO t1(a,b) VALUES (1.225e+10, 1.225e+10);
+INSERT INTO t1(a,b) VALUES (1.225e+15, 1.225e+15);
+INSERT INTO t1(a,b) VALUES (5000000e+0, 5000000e+0);
+INSERT INTO t1(a,b) VALUES (1.25e+78, 1.25e+78);
+INSERT INTO t1(a,b) VALUES (1.25e-94, 1.25e-94);
+INSERT INTO t1(a,b) VALUES (1.25e+203, 1.25e+203);
+INSERT INTO t1(a,b) VALUES (1.25e-175, 1.25e-175);
+INSERT INTO t1(a,c) VALUES (1.225e+0, 1.225e+0);
+INSERT INTO t1(a,c) VALUES (1.37e+0, 1.37e+0);
+INSERT INTO t1(a,c) VALUES (-1.37e+0, -1.37e+0);
+INSERT INTO t1(a,c) VALUES (1.87e-3, 1.87e-3);
+INSERT INTO t1(a,c) VALUES (-1.87e-2, -1.87e-2);
+INSERT INTO t1(a,c) VALUES (5000e+0, 5000e+0);
+INSERT INTO t1(a,c) VALUES (-5000e+0, -5000e+0);
+
+SELECT * FROM t1;
+
+DROP TABLE t1;
+
 # End of 4.1 tests
Thread
bk commit into 4.1 tree (igor:1.2623) BUG#26788igor2 Apr