diff --git a/mysql-test/r/func_math.result b/mysql-test/r/func_math.result index e4889289c18da615bcfd5a96f4dfa57ad785b054..c7674c57c8d113899afcf76ca88fe7b20e4f9cbc 100644 --- a/mysql-test/r/func_math.result +++ b/mysql-test/r/func_math.result @@ -143,3 +143,6 @@ select format(col2,6) from t1 where col1=7; format(col2,6) 1,234,567,890,123,456.123450 drop table t1; +select round(150, 2); +round(150, 2) +150.00 diff --git a/mysql-test/r/type_newdecimal.result b/mysql-test/r/type_newdecimal.result index c0693d1585cb6a1ee87833129955e222a5d0249f..b406dbab82fbabbc5d1f465aca3b04ed290efff7 100644 --- a/mysql-test/r/type_newdecimal.result +++ b/mysql-test/r/type_newdecimal.result @@ -896,6 +896,14 @@ create table t1( d1 decimal(18) unsigned, d2 decimal(20) unsigned, d3 decimal (2 insert into t1 values(1,-1,-1); ERROR 22003: Out of range value adjusted for column 'd2' at row 1 drop table t1; +create table t1 (col1 decimal(5,2), col2 numeric(5,2)); +insert into t1 values (999.999,999.999); +ERROR 22003: Out of range value adjusted for column 'col1' at row 1 +insert into t1 values (-999.999,-999.999); +ERROR 22003: Out of range value adjusted for column 'col1' at row 1 +select * from t1; +col1 col2 +drop table t1; set sql_mode=''; set @sav_dpi= @@div_precision_increment; set @@div_precision_increment=15; diff --git a/mysql-test/t/func_math.test b/mysql-test/t/func_math.test index b21f38052b636b5c64188a60033595cfcfd1c09c..03057af69111842baedf31e39dee2633d9ae0d9a 100644 --- a/mysql-test/t/func_math.test +++ b/mysql-test/t/func_math.test @@ -79,3 +79,8 @@ insert into t1 values(7,1234567890123456.12345); select format(col2,6) from t1 where col1=7; drop table t1; + +# +# Bug #10083 (round doesn't increase decimals) +# +select round(150, 2); diff --git a/mysql-test/t/type_newdecimal.test b/mysql-test/t/type_newdecimal.test index d1d595285a204039be29411dadf2e91cded0ffe6..6199bd34fa9a700eadb9f3c385e8489e5d9e906f 100644 --- a/mysql-test/t/type_newdecimal.test +++ b/mysql-test/t/type_newdecimal.test @@ -934,6 +934,13 @@ create table t1( d1 decimal(18) unsigned, d2 decimal(20) unsigned, d3 decimal (2 --error 1264 insert into t1 values(1,-1,-1); drop table t1; +create table t1 (col1 decimal(5,2), col2 numeric(5,2)); +--error 1264 +insert into t1 values (999.999,999.999); +--error 1264 +insert into t1 values (-999.999,-999.999); +select * from t1; +drop table t1; set sql_mode=''; # diff --git a/sql/item_func.cc b/sql/item_func.cc index 68292859504423837f8db811ea04f4838fbd5112..c3fc537f04eada19df224a456866102a0a53f36b 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -1876,7 +1876,8 @@ void Item_func_round::fix_length_and_dec() max_length= float_length(decimals); break; case INT_RESULT: - if (truncate || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS)) + if ((decimals_to_set==0) && + (truncate || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))) { /* Here we can keep INT_RESULT */ hybrid_type= INT_RESULT; @@ -1890,18 +1891,12 @@ void Item_func_round::fix_length_and_dec() hybrid_type= DECIMAL_RESULT; int decimals_delta= args[0]->decimals - decimals_to_set; int precision= args[0]->decimal_precision(); - if (decimals_delta > 0) - { - int length_increase= truncate ? 0:1; - precision-= decimals_delta - length_increase; - decimals= decimals_to_set; - } - else - /* Decimals to set is bigger that the original scale */ - /* we keep original decimals value */ - decimals= args[0]->decimals; + int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1; + + precision-= decimals_delta - length_increase; + decimals= decimals_to_set; max_length= my_decimal_precision_to_length(precision, decimals, - unsigned_flag); + unsigned_flag); break; } default: diff --git a/strings/decimal.c b/strings/decimal.c index 787d00dcb46be096cff59dd2093e51193bb687d5..4a487f3c9b0890644e697eaebe4abd79569b8bee 100644 --- a/strings/decimal.c +++ b/strings/decimal.c @@ -1546,6 +1546,14 @@ decimal_round(decimal_t *from, decimal_t *to, int scale, *buf1=1; to->intg++; } + /* Here we check 999.9 -> 1000 case when we need to increase intg */ + else + { + int first_dig= to->intg % DIG_PER_DEC1; + /* first_dig==0 should be handled above in the 'if' */ + if (first_dig && (*buf1 >= powers10[first_dig])) + to->intg++; + } } else {