From 7f3e2dc48d1f40a68b45218642677738a6607370 Mon Sep 17 00:00:00 2001 From: "hf@deer.(none)" <> Date: Tue, 27 Sep 2005 15:11:39 +0500 Subject: [PATCH 1/3] Fix for bug #13372 (decimal union) --- mysql-test/r/type_decimal.result | 17 ++++++++++++++++- mysql-test/r/type_float.result | 15 +++++++++++++++ mysql-test/t/type_decimal.test | 14 +++++++++++++- mysql-test/t/type_float.test | 13 +++++++++++++ sql/item.cc | 24 ++++++++++++++++++++++-- 5 files changed, 79 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/type_decimal.result b/mysql-test/r/type_decimal.result index c3b2d5090ef..de8610f6514 100644 --- a/mysql-test/r/type_decimal.result +++ b/mysql-test/r/type_decimal.result @@ -1,4 +1,4 @@ -DROP TABLE IF EXISTS t1; +DROP TABLE IF EXISTS t1, t2, t3; SET SQL_WARNINGS=1; CREATE TABLE t1 ( id int(11) NOT NULL auto_increment, @@ -655,3 +655,18 @@ select * from t1; a b 123.12345 123.1 drop table t1; +create table t1 (d decimal(10,1)); +create table t2 (d decimal(10,9)); +insert into t1 values ("100000000.0"); +insert into t2 values ("1.23456780"); +create table t3 select * from t2 union select * from t1; +select * from t3; +d +1.234567800 +100000000.000000000 +show create table t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `d` decimal(18,9) default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t2, t3; diff --git a/mysql-test/r/type_float.result b/mysql-test/r/type_float.result index 319a957498b..2e6642c3fcf 100644 --- a/mysql-test/r/type_float.result +++ b/mysql-test/r/type_float.result @@ -235,3 +235,18 @@ select * from t1 where reckey=1.09E2; reckey recdesc 109 Has 109 as key drop table t1; +create table t1 (d double(10,1)); +create table t2 (d double(10,9)); +insert into t1 values ("100000000.0"); +insert into t2 values ("1.23456780"); +create table t3 select * from t2 union select * from t1; +select * from t3; +d +1.234567800 +100000000.000000000 +show create table t3; +Table Create Table +t3 CREATE TABLE `t3` ( + `d` double(61,9) default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t2, t3; diff --git a/mysql-test/t/type_decimal.test b/mysql-test/t/type_decimal.test index 1f133666910..cc5e9278b12 100644 --- a/mysql-test/t/type_decimal.test +++ b/mysql-test/t/type_decimal.test @@ -1,7 +1,7 @@ # bug in decimal() with negative numbers by kaido@tradenet.ee --disable_warnings -DROP TABLE IF EXISTS t1; +DROP TABLE IF EXISTS t1, t2, t3; --enable_warnings SET SQL_WARNINGS=1; @@ -276,4 +276,16 @@ update t1 set b=a; select * from t1; drop table t1; +# +# Bug #13372 (decimal union) +# +create table t1 (d decimal(10,1)); +create table t2 (d decimal(10,9)); +insert into t1 values ("100000000.0"); +insert into t2 values ("1.23456780"); +create table t3 select * from t2 union select * from t1; +select * from t3; +show create table t3; +drop table t1, t2, t3; + # End of 4.1 tests diff --git a/mysql-test/t/type_float.test b/mysql-test/t/type_float.test index 2d4a90911a1..abaf72ea2ed 100644 --- a/mysql-test/t/type_float.test +++ b/mysql-test/t/type_float.test @@ -149,4 +149,17 @@ select * from t1 where reckey=109; select * from t1 where reckey=1.09E2; drop table t1; +# +# Bug #13372 (decimal union) +# +create table t1 (d double(10,1)); +create table t2 (d double(10,9)); +insert into t1 values ("100000000.0"); +insert into t2 values ("1.23456780"); +create table t3 select * from t2 union select * from t1; +select * from t3; +show create table t3; +drop table t1, t2, t3; + + # End of 4.1 tests diff --git a/sql/item.cc b/sql/item.cc index 010189c321c..ec83cc1f511 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3205,9 +3205,14 @@ enum_field_types Item_type_holder::get_real_type(Item *item) bool Item_type_holder::join_types(THD *thd, Item *item) { + uint max_length_orig= max_length; + uint decimals_orig= decimals; max_length= max(max_length, display_length(item)); + decimals= max(decimals, item->decimals); fld_type= Field::field_type_merge(fld_type, get_real_type(item)); - if (Field::result_merge_type(fld_type) == STRING_RESULT) + switch (Field::result_merge_type(fld_type)) + { + case STRING_RESULT: { const char *old_cs, *old_derivation; old_cs= collation.collation->name; @@ -3221,8 +3226,23 @@ bool Item_type_holder::join_types(THD *thd, Item *item) "UNION"); return TRUE; } + break; } - decimals= max(decimals, item->decimals); + case REAL_RESULT: + { + decimals= max(decimals, item->decimals); + if (decimals != NOT_FIXED_DEC) + { + int delta1= max_length_orig - decimals_orig; + int delta2= item->max_length - item->decimals; + max_length= max(delta1, delta2) + decimals; + } + else + max_length= (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7; + break; + } + default:; + }; maybe_null|= item->maybe_null; get_full_info(item); return FALSE; From 9b88d252470984beb62bf5fd6c1c1da745f22456 Mon Sep 17 00:00:00 2001 From: "hf@deer.(none)" <> Date: Tue, 27 Sep 2005 15:31:38 +0500 Subject: [PATCH 2/3] additional fix to the bug #13372 (decimal union) --- mysql-test/r/type_float.result | 2 +- sql/item.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/type_float.result b/mysql-test/r/type_float.result index 2e6642c3fcf..6e381192270 100644 --- a/mysql-test/r/type_float.result +++ b/mysql-test/r/type_float.result @@ -247,6 +247,6 @@ d show create table t3; Table Create Table t3 CREATE TABLE `t3` ( - `d` double(61,9) default NULL + `d` double(22,9) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1, t2, t3; diff --git a/sql/item.cc b/sql/item.cc index ec83cc1f511..b3c0e64dec1 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3230,12 +3230,12 @@ bool Item_type_holder::join_types(THD *thd, Item *item) } case REAL_RESULT: { - decimals= max(decimals, item->decimals); if (decimals != NOT_FIXED_DEC) { int delta1= max_length_orig - decimals_orig; int delta2= item->max_length - item->decimals; - max_length= max(delta1, delta2) + decimals; + max_length= min(max(delta1, delta2) + decimals, + (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7); } else max_length= (fld_type == MYSQL_TYPE_FLOAT) ? FLT_DIG+6 : DBL_DIG+7; From f0dfa1954856a6e49e843f4725101d05eff2c1a5 Mon Sep 17 00:00:00 2001 From: "hf@deer.(none)" <> Date: Tue, 27 Sep 2005 16:23:37 +0500 Subject: [PATCH 3/3] additional fix for bug #13372 (decimal union) --- mysql-test/r/union.result | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result index a3dd2c5c291..318bfa2cda8 100644 --- a/mysql-test/r/union.result +++ b/mysql-test/r/union.result @@ -565,7 +565,7 @@ a show create table t1; Table Create Table t1 CREATE TABLE `t1` ( - `a` double(53,1) NOT NULL default '0.0' + `a` double(21,1) NOT NULL default '0.0' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; create table t2 (it1 tinyint, it2 tinyint not null, i int not null, ib bigint, f float, d double, y year, da date, dt datetime, sc char(10), sv varchar(10), b blob, tx text);