From: Date: April 1 2007 10:40am Subject: bk commit into 4.1 tree (igor:1.2623) BUG#26788 List-Archive: http://lists.mysql.com/commits/23483 X-Bug: 26788 Message-Id: <20070401084037.BDD3C1CD1F2@olga.mysql.com> 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-01 01:40:33-07:00, igor@stripped +4 -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-01 01:40:31-07:00, igor@stripped +54 -0 Added a test case for bug #26788. mysql-test/r/type_float.result@stripped, 2007-04-01 01:40:31-07:00, igor@stripped +2 -2 Adjusted results after fix for bug #26788. mysql-test/t/insert.test@stripped, 2007-04-01 01:40:31-07:00, igor@stripped +33 -0 Added a test case for bug #26788. sql/field.cc@stripped, 2007-04-01 01:40:31-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. # 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-01 01:40:37 -07:00 +++ 1.236/sql/field.cc 2007-04-01 01:40:37 -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; + } + else + { + /* Calculate the number of significant digits in exponent */ + double b= 1.0e+10; + double lim= b; + double d= anr < 1.0 ? 1/anr : anr; + for (exp_len= 1 ; d >= lim && DBL_MAX/lim >= b ; exp_len++, lim*= b) ; + 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.23/mysql-test/r/insert.result 2007-04-01 01:40:37 -07:00 +++ 1.24/mysql-test/r/insert.result 2007-04-01 01:40:37 -07:00 @@ -325,3 +325,57 @@ 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,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.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-01 01:40:37 -07:00 +++ 1.39/mysql-test/r/type_float.result 2007-04-01 01:40:37 -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-01 01:40:37 -07:00 +++ 1.22/mysql-test/t/insert.test 2007-04-01 01:40:37 -07:00 @@ -155,4 +155,37 @@ 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,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