Commit 6a2ee9c8 authored by Alexander Barkov's avatar Alexander Barkov

MDEV-23032 FLOOR()/CEIL() incorrectly calculate the precision of a DECIMAL(M,D) column

The code in Item_func_int_val::fix_length_and_dec_int_or_decimal()
calculated badly the result data type for FLOOR()/CEIL(), so for example
the decimal(38,10) input created a decimal(28,0) result.
That was not correct, because one extra integer digit is needed.
   floor(-9.9) -> -10
   ceil(9.9)   ->  10

Rewritting the code in a more straightforward way.
Additional changes:
- FLOOR() now takes into account the presence of the UNSIGNED
flag of the argument: FLOOR(unsigned decimal) does not need an extra digits.
- FLOOR()/CEILING() now preserve the unsigned flag in the result
  data type is decimal.
These changes give nicer data types.
parent 706a7101
......@@ -194,12 +194,12 @@ show create table t1;
drop table t1;
select hex(concat(ceiling(0.5)));
create table t1 as select concat(ceiling(0.5)) as c1;
create table t1 as select ceiling(0.5) as c0, concat(ceiling(0.5)) as c1;
show create table t1;
drop table t1;
select hex(concat(floor(0.5)));
create table t1 as select concat(floor(0.5)) as c1;
create table t1 as select floor(0.5) as c0, concat(floor(0.5)) as c1;
show create table t1;
drop table t1;
......
......@@ -348,21 +348,23 @@ drop table t1;
select hex(concat(ceiling(0.5)));
hex(concat(ceiling(0.5)))
31
create table t1 as select concat(ceiling(0.5)) as c1;
create table t1 as select ceiling(0.5) as c0, concat(ceiling(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varbinary(4) DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varbinary(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(floor(0.5)));
hex(concat(floor(0.5)))
30
create table t1 as select concat(floor(0.5)) as c1;
create table t1 as select floor(0.5) as c0, concat(floor(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varbinary(4) DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varbinary(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(round(0.5)));
......
......@@ -757,21 +757,23 @@ drop table t1;
select hex(concat(ceiling(0.5)));
hex(concat(ceiling(0.5)))
31
create table t1 as select concat(ceiling(0.5)) as c1;
create table t1 as select ceiling(0.5) as c0, concat(ceiling(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) CHARACTER SET cp1251 DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) CHARACTER SET cp1251 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(floor(0.5)));
hex(concat(floor(0.5)))
30
create table t1 as select concat(floor(0.5)) as c1;
create table t1 as select floor(0.5) as c0, concat(floor(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) CHARACTER SET cp1251 DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) CHARACTER SET cp1251 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(round(0.5)));
......
......@@ -1066,21 +1066,23 @@ drop table t1;
select hex(concat(ceiling(0.5)));
hex(concat(ceiling(0.5)))
31
create table t1 as select concat(ceiling(0.5)) as c1;
create table t1 as select ceiling(0.5) as c0, concat(ceiling(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(floor(0.5)));
hex(concat(floor(0.5)))
30
create table t1 as select concat(floor(0.5)) as c1;
create table t1 as select floor(0.5) as c0, concat(floor(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(round(0.5)));
......
......@@ -1950,21 +1950,23 @@ drop table t1;
select hex(concat(ceiling(0.5)));
hex(concat(ceiling(0.5)))
0031
create table t1 as select concat(ceiling(0.5)) as c1;
create table t1 as select ceiling(0.5) as c0, concat(ceiling(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) CHARACTER SET ucs2 DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) CHARACTER SET ucs2 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(floor(0.5)));
hex(concat(floor(0.5)))
0030
create table t1 as select concat(floor(0.5)) as c1;
create table t1 as select floor(0.5) as c0, concat(floor(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) CHARACTER SET ucs2 DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) CHARACTER SET ucs2 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(round(0.5)));
......
......@@ -2817,21 +2817,23 @@ drop table t1;
select hex(concat(ceiling(0.5)));
hex(concat(ceiling(0.5)))
31
create table t1 as select concat(ceiling(0.5)) as c1;
create table t1 as select ceiling(0.5) as c0, concat(ceiling(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) CHARACTER SET utf8 DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(floor(0.5)));
hex(concat(floor(0.5)))
30
create table t1 as select concat(floor(0.5)) as c1;
create table t1 as select floor(0.5) as c0, concat(floor(0.5)) as c1;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`c1` varchar(4) CHARACTER SET utf8 DEFAULT NULL
`c0` int(3) NOT NULL,
`c1` varchar(3) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
select hex(concat(round(0.5)));
......
......@@ -1080,7 +1080,7 @@ Create Table CREATE TABLE `t2` (
`a` decimal(38,0) DEFAULT NULL,
`b` decimal(38,0) unsigned DEFAULT NULL,
`fa` decimal(38,0) DEFAULT NULL,
`fb` decimal(38,0) DEFAULT NULL
`fb` decimal(38,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999999999999999
b 99999999999999999999999999999999999999
......@@ -1090,8 +1090,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,1) DEFAULT NULL,
`b` decimal(38,1) unsigned DEFAULT NULL,
`fa` decimal(37,0) DEFAULT NULL,
`fb` decimal(37,0) DEFAULT NULL
`fa` decimal(38,0) DEFAULT NULL,
`fb` decimal(37,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999999999999999.9
b 9999999999999999999999999999999999999.9
......@@ -1101,8 +1101,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,2) DEFAULT NULL,
`b` decimal(38,2) unsigned DEFAULT NULL,
`fa` decimal(36,0) DEFAULT NULL,
`fb` decimal(36,0) DEFAULT NULL
`fa` decimal(37,0) DEFAULT NULL,
`fb` decimal(36,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999999999999999.99
b 999999999999999999999999999999999999.99
......@@ -1112,8 +1112,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,3) DEFAULT NULL,
`b` decimal(38,3) unsigned DEFAULT NULL,
`fa` decimal(35,0) DEFAULT NULL,
`fb` decimal(35,0) DEFAULT NULL
`fa` decimal(36,0) DEFAULT NULL,
`fb` decimal(35,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999999999999.999
b 99999999999999999999999999999999999.999
......@@ -1123,8 +1123,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,4) DEFAULT NULL,
`b` decimal(38,4) unsigned DEFAULT NULL,
`fa` decimal(34,0) DEFAULT NULL,
`fb` decimal(34,0) DEFAULT NULL
`fa` decimal(35,0) DEFAULT NULL,
`fb` decimal(34,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999999999999.9999
b 9999999999999999999999999999999999.9999
......@@ -1134,8 +1134,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,5) DEFAULT NULL,
`b` decimal(38,5) unsigned DEFAULT NULL,
`fa` decimal(33,0) DEFAULT NULL,
`fb` decimal(33,0) DEFAULT NULL
`fa` decimal(34,0) DEFAULT NULL,
`fb` decimal(33,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999999999999.99999
b 999999999999999999999999999999999.99999
......@@ -1145,8 +1145,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,6) DEFAULT NULL,
`b` decimal(38,6) unsigned DEFAULT NULL,
`fa` decimal(32,0) DEFAULT NULL,
`fb` decimal(32,0) DEFAULT NULL
`fa` decimal(33,0) DEFAULT NULL,
`fb` decimal(32,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999999999.999999
b 99999999999999999999999999999999.999999
......@@ -1156,8 +1156,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,7) DEFAULT NULL,
`b` decimal(38,7) unsigned DEFAULT NULL,
`fa` decimal(31,0) DEFAULT NULL,
`fb` decimal(31,0) DEFAULT NULL
`fa` decimal(32,0) DEFAULT NULL,
`fb` decimal(31,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999999999.9999999
b 9999999999999999999999999999999.9999999
......@@ -1167,8 +1167,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,8) DEFAULT NULL,
`b` decimal(38,8) unsigned DEFAULT NULL,
`fa` decimal(30,0) DEFAULT NULL,
`fb` decimal(30,0) DEFAULT NULL
`fa` decimal(31,0) DEFAULT NULL,
`fb` decimal(30,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999999999.99999999
b 999999999999999999999999999999.99999999
......@@ -1178,8 +1178,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,9) DEFAULT NULL,
`b` decimal(38,9) unsigned DEFAULT NULL,
`fa` decimal(29,0) DEFAULT NULL,
`fb` decimal(29,0) DEFAULT NULL
`fa` decimal(30,0) DEFAULT NULL,
`fb` decimal(29,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999999.999999999
b 99999999999999999999999999999.999999999
......@@ -1189,8 +1189,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,10) DEFAULT NULL,
`b` decimal(38,10) unsigned DEFAULT NULL,
`fa` decimal(28,0) DEFAULT NULL,
`fb` decimal(28,0) DEFAULT NULL
`fa` decimal(29,0) DEFAULT NULL,
`fb` decimal(28,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999999.9999999999
b 9999999999999999999999999999.9999999999
......@@ -1200,8 +1200,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,11) DEFAULT NULL,
`b` decimal(38,11) unsigned DEFAULT NULL,
`fa` decimal(27,0) DEFAULT NULL,
`fb` decimal(27,0) DEFAULT NULL
`fa` decimal(28,0) DEFAULT NULL,
`fb` decimal(27,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999999.99999999999
b 999999999999999999999999999.99999999999
......@@ -1211,8 +1211,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,12) DEFAULT NULL,
`b` decimal(38,12) unsigned DEFAULT NULL,
`fa` decimal(26,0) DEFAULT NULL,
`fb` decimal(26,0) DEFAULT NULL
`fa` decimal(27,0) DEFAULT NULL,
`fb` decimal(26,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999.999999999999
b 99999999999999999999999999.999999999999
......@@ -1222,8 +1222,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,13) DEFAULT NULL,
`b` decimal(38,13) unsigned DEFAULT NULL,
`fa` decimal(25,0) DEFAULT NULL,
`fb` decimal(25,0) DEFAULT NULL
`fa` decimal(26,0) DEFAULT NULL,
`fb` decimal(25,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999.9999999999999
b 9999999999999999999999999.9999999999999
......@@ -1233,8 +1233,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,14) DEFAULT NULL,
`b` decimal(38,14) unsigned DEFAULT NULL,
`fa` decimal(24,0) DEFAULT NULL,
`fb` decimal(24,0) DEFAULT NULL
`fa` decimal(25,0) DEFAULT NULL,
`fb` decimal(24,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999.99999999999999
b 999999999999999999999999.99999999999999
......@@ -1244,8 +1244,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,15) DEFAULT NULL,
`b` decimal(38,15) unsigned DEFAULT NULL,
`fa` decimal(23,0) DEFAULT NULL,
`fb` decimal(23,0) DEFAULT NULL
`fa` decimal(24,0) DEFAULT NULL,
`fb` decimal(23,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999.999999999999999
b 99999999999999999999999.999999999999999
......@@ -1255,8 +1255,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,16) DEFAULT NULL,
`b` decimal(38,16) unsigned DEFAULT NULL,
`fa` decimal(22,0) DEFAULT NULL,
`fb` decimal(22,0) DEFAULT NULL
`fa` decimal(23,0) DEFAULT NULL,
`fb` decimal(22,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999.9999999999999999
b 9999999999999999999999.9999999999999999
......@@ -1266,8 +1266,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,17) DEFAULT NULL,
`b` decimal(38,17) unsigned DEFAULT NULL,
`fa` decimal(21,0) DEFAULT NULL,
`fb` decimal(21,0) DEFAULT NULL
`fa` decimal(22,0) DEFAULT NULL,
`fb` decimal(21,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999.99999999999999999
b 999999999999999999999.99999999999999999
......@@ -1277,8 +1277,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,18) DEFAULT NULL,
`b` decimal(38,18) unsigned DEFAULT NULL,
`fa` decimal(20,0) DEFAULT NULL,
`fb` decimal(20,0) DEFAULT NULL
`fa` decimal(21,0) DEFAULT NULL,
`fb` decimal(20,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999.999999999999999999
b 99999999999999999999.999999999999999999
......@@ -1288,8 +1288,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,19) DEFAULT NULL,
`b` decimal(38,19) unsigned DEFAULT NULL,
`fa` decimal(19,0) DEFAULT NULL,
`fb` decimal(19,0) DEFAULT NULL
`fa` decimal(20,0) DEFAULT NULL,
`fb` decimal(19,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999.9999999999999999999
b 9999999999999999999.9999999999999999999
......@@ -1299,8 +1299,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,20) DEFAULT NULL,
`b` decimal(38,20) unsigned DEFAULT NULL,
`fa` decimal(18,0) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
`fa` decimal(19,0) DEFAULT NULL,
`fb` bigint(18) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999.99999999999999999999
b 999999999999999999.99999999999999999999
......@@ -1310,7 +1310,7 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,21) DEFAULT NULL,
`b` decimal(38,21) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fa` bigint(19) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999.999999999999999999999
......@@ -1321,8 +1321,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,22) DEFAULT NULL,
`b` decimal(38,22) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
`fa` bigint(18) DEFAULT NULL,
`fb` bigint(16) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999.9999999999999999999999
b 9999999999999999.9999999999999999999999
......@@ -1333,7 +1333,7 @@ Create Table CREATE TABLE `t2` (
`a` decimal(38,23) DEFAULT NULL,
`b` decimal(38,23) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
`fb` bigint(15) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999.99999999999999999999999
b 999999999999999.99999999999999999999999
......@@ -1343,8 +1343,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,24) DEFAULT NULL,
`b` decimal(38,24) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fb` bigint(16) unsigned DEFAULT NULL
`fa` bigint(16) DEFAULT NULL,
`fb` bigint(14) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999.999999999999999999999999
b 99999999999999.999999999999999999999999
......@@ -1354,8 +1354,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,25) DEFAULT NULL,
`b` decimal(38,25) unsigned DEFAULT NULL,
`fa` bigint(16) DEFAULT NULL,
`fb` bigint(15) unsigned DEFAULT NULL
`fa` bigint(15) DEFAULT NULL,
`fb` bigint(13) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999.9999999999999999999999999
b 9999999999999.9999999999999999999999999
......@@ -1365,8 +1365,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,26) DEFAULT NULL,
`b` decimal(38,26) unsigned DEFAULT NULL,
`fa` bigint(15) DEFAULT NULL,
`fb` bigint(14) unsigned DEFAULT NULL
`fa` bigint(14) DEFAULT NULL,
`fb` bigint(12) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999.99999999999999999999999999
b 999999999999.99999999999999999999999999
......@@ -1376,8 +1376,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,27) DEFAULT NULL,
`b` decimal(38,27) unsigned DEFAULT NULL,
`fa` bigint(14) DEFAULT NULL,
`fb` bigint(13) unsigned DEFAULT NULL
`fa` bigint(13) DEFAULT NULL,
`fb` bigint(11) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999.999999999999999999999999999
b 99999999999.999999999999999999999999999
......@@ -1387,8 +1387,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,28) DEFAULT NULL,
`b` decimal(38,28) unsigned DEFAULT NULL,
`fa` bigint(13) DEFAULT NULL,
`fb` bigint(12) unsigned DEFAULT NULL
`fa` bigint(12) DEFAULT NULL,
`fb` bigint(10) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999.9999999999999999999999999999
b 9999999999.9999999999999999999999999999
......@@ -1398,8 +1398,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,29) DEFAULT NULL,
`b` decimal(38,29) unsigned DEFAULT NULL,
`fa` bigint(12) DEFAULT NULL,
`fb` bigint(11) unsigned DEFAULT NULL
`fa` bigint(11) DEFAULT NULL,
`fb` int(9) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999.99999999999999999999999999999
b 999999999.99999999999999999999999999999
......@@ -1409,8 +1409,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,30) DEFAULT NULL,
`b` decimal(38,30) unsigned DEFAULT NULL,
`fa` bigint(11) DEFAULT NULL,
`fb` bigint(10) unsigned DEFAULT NULL
`fa` int(10) DEFAULT NULL,
`fb` int(8) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999.999999999999999999999999999999
b 99999999.999999999999999999999999999999
......@@ -1422,7 +1422,7 @@ Create Table CREATE TABLE `t2` (
`a` decimal(30,0) DEFAULT NULL,
`b` decimal(30,0) unsigned DEFAULT NULL,
`fa` decimal(30,0) DEFAULT NULL,
`fb` decimal(31,0) unsigned DEFAULT NULL
`fb` decimal(30,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999999999
b 999999999999999999999999999999
......@@ -1432,8 +1432,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,1) DEFAULT NULL,
`b` decimal(30,1) unsigned DEFAULT NULL,
`fa` decimal(29,0) DEFAULT NULL,
`fb` decimal(30,0) unsigned DEFAULT NULL
`fa` decimal(30,0) DEFAULT NULL,
`fb` decimal(29,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999999.9
b 99999999999999999999999999999.9
......@@ -1443,8 +1443,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,2) DEFAULT NULL,
`b` decimal(30,2) unsigned DEFAULT NULL,
`fa` decimal(28,0) DEFAULT NULL,
`fb` decimal(29,0) unsigned DEFAULT NULL
`fa` decimal(29,0) DEFAULT NULL,
`fb` decimal(28,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999999.99
b 9999999999999999999999999999.99
......@@ -1454,8 +1454,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,3) DEFAULT NULL,
`b` decimal(30,3) unsigned DEFAULT NULL,
`fa` decimal(27,0) DEFAULT NULL,
`fb` decimal(28,0) unsigned DEFAULT NULL
`fa` decimal(28,0) DEFAULT NULL,
`fb` decimal(27,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999999.999
b 999999999999999999999999999.999
......@@ -1465,8 +1465,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,4) DEFAULT NULL,
`b` decimal(30,4) unsigned DEFAULT NULL,
`fa` decimal(26,0) DEFAULT NULL,
`fb` decimal(27,0) unsigned DEFAULT NULL
`fa` decimal(27,0) DEFAULT NULL,
`fb` decimal(26,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999999.9999
b 99999999999999999999999999.9999
......@@ -1476,8 +1476,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,5) DEFAULT NULL,
`b` decimal(30,5) unsigned DEFAULT NULL,
`fa` decimal(25,0) DEFAULT NULL,
`fb` decimal(26,0) unsigned DEFAULT NULL
`fa` decimal(26,0) DEFAULT NULL,
`fb` decimal(25,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999999.99999
b 9999999999999999999999999.99999
......@@ -1487,8 +1487,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,6) DEFAULT NULL,
`b` decimal(30,6) unsigned DEFAULT NULL,
`fa` decimal(24,0) DEFAULT NULL,
`fb` decimal(25,0) unsigned DEFAULT NULL
`fa` decimal(25,0) DEFAULT NULL,
`fb` decimal(24,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999999.999999
b 999999999999999999999999.999999
......@@ -1498,8 +1498,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,7) DEFAULT NULL,
`b` decimal(30,7) unsigned DEFAULT NULL,
`fa` decimal(23,0) DEFAULT NULL,
`fb` decimal(24,0) unsigned DEFAULT NULL
`fa` decimal(24,0) DEFAULT NULL,
`fb` decimal(23,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999999.9999999
b 99999999999999999999999.9999999
......@@ -1509,8 +1509,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,8) DEFAULT NULL,
`b` decimal(30,8) unsigned DEFAULT NULL,
`fa` decimal(22,0) DEFAULT NULL,
`fb` decimal(23,0) unsigned DEFAULT NULL
`fa` decimal(23,0) DEFAULT NULL,
`fb` decimal(22,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999999.99999999
b 9999999999999999999999.99999999
......@@ -1520,8 +1520,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,9) DEFAULT NULL,
`b` decimal(30,9) unsigned DEFAULT NULL,
`fa` decimal(21,0) DEFAULT NULL,
`fb` decimal(22,0) unsigned DEFAULT NULL
`fa` decimal(22,0) DEFAULT NULL,
`fb` decimal(21,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999999.999999999
b 999999999999999999999.999999999
......@@ -1531,8 +1531,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,10) DEFAULT NULL,
`b` decimal(30,10) unsigned DEFAULT NULL,
`fa` decimal(20,0) DEFAULT NULL,
`fb` decimal(21,0) unsigned DEFAULT NULL
`fa` decimal(21,0) DEFAULT NULL,
`fb` decimal(20,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999999.9999999999
b 99999999999999999999.9999999999
......@@ -1542,8 +1542,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,11) DEFAULT NULL,
`b` decimal(30,11) unsigned DEFAULT NULL,
`fa` decimal(19,0) DEFAULT NULL,
`fb` decimal(20,0) unsigned DEFAULT NULL
`fa` decimal(20,0) DEFAULT NULL,
`fb` decimal(19,0) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999999.99999999999
b 9999999999999999999.99999999999
......@@ -1553,8 +1553,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,12) DEFAULT NULL,
`b` decimal(30,12) unsigned DEFAULT NULL,
`fa` decimal(18,0) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
`fa` decimal(19,0) DEFAULT NULL,
`fb` bigint(18) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999999.999999999999
b 999999999999999999.999999999999
......@@ -1564,7 +1564,7 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,13) DEFAULT NULL,
`b` decimal(30,13) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fa` bigint(19) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999999.9999999999999
......@@ -1575,8 +1575,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,14) DEFAULT NULL,
`b` decimal(30,14) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
`fa` bigint(18) DEFAULT NULL,
`fb` bigint(16) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999999.99999999999999
b 9999999999999999.99999999999999
......@@ -1587,7 +1587,7 @@ Create Table CREATE TABLE `t2` (
`a` decimal(30,15) DEFAULT NULL,
`b` decimal(30,15) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fb` bigint(17) unsigned DEFAULT NULL
`fb` bigint(15) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999999.999999999999999
b 999999999999999.999999999999999
......@@ -1597,8 +1597,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,16) DEFAULT NULL,
`b` decimal(30,16) unsigned DEFAULT NULL,
`fa` bigint(17) DEFAULT NULL,
`fb` bigint(16) unsigned DEFAULT NULL
`fa` bigint(16) DEFAULT NULL,
`fb` bigint(14) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999999.9999999999999999
b 99999999999999.9999999999999999
......@@ -1608,8 +1608,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,17) DEFAULT NULL,
`b` decimal(30,17) unsigned DEFAULT NULL,
`fa` bigint(16) DEFAULT NULL,
`fb` bigint(15) unsigned DEFAULT NULL
`fa` bigint(15) DEFAULT NULL,
`fb` bigint(13) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999999.99999999999999999
b 9999999999999.99999999999999999
......@@ -1619,8 +1619,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,18) DEFAULT NULL,
`b` decimal(30,18) unsigned DEFAULT NULL,
`fa` bigint(15) DEFAULT NULL,
`fb` bigint(14) unsigned DEFAULT NULL
`fa` bigint(14) DEFAULT NULL,
`fb` bigint(12) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999999.999999999999999999
b 999999999999.999999999999999999
......@@ -1630,8 +1630,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,19) DEFAULT NULL,
`b` decimal(30,19) unsigned DEFAULT NULL,
`fa` bigint(14) DEFAULT NULL,
`fb` bigint(13) unsigned DEFAULT NULL
`fa` bigint(13) DEFAULT NULL,
`fb` bigint(11) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999999.9999999999999999999
b 99999999999.9999999999999999999
......@@ -1641,8 +1641,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,20) DEFAULT NULL,
`b` decimal(30,20) unsigned DEFAULT NULL,
`fa` bigint(13) DEFAULT NULL,
`fb` bigint(12) unsigned DEFAULT NULL
`fa` bigint(12) DEFAULT NULL,
`fb` bigint(10) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999999.99999999999999999999
b 9999999999.99999999999999999999
......@@ -1652,8 +1652,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,21) DEFAULT NULL,
`b` decimal(30,21) unsigned DEFAULT NULL,
`fa` bigint(12) DEFAULT NULL,
`fb` bigint(11) unsigned DEFAULT NULL
`fa` bigint(11) DEFAULT NULL,
`fb` int(9) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999999.999999999999999999999
b 999999999.999999999999999999999
......@@ -1663,8 +1663,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,22) DEFAULT NULL,
`b` decimal(30,22) unsigned DEFAULT NULL,
`fa` bigint(11) DEFAULT NULL,
`fb` bigint(10) unsigned DEFAULT NULL
`fa` int(10) DEFAULT NULL,
`fb` int(8) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999999.9999999999999999999999
b 99999999.9999999999999999999999
......@@ -1674,8 +1674,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,23) DEFAULT NULL,
`b` decimal(30,23) unsigned DEFAULT NULL,
`fa` bigint(10) DEFAULT NULL,
`fb` int(9) unsigned DEFAULT NULL
`fa` int(9) DEFAULT NULL,
`fb` int(7) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999999.99999999999999999999999
b 9999999.99999999999999999999999
......@@ -1685,8 +1685,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,24) DEFAULT NULL,
`b` decimal(30,24) unsigned DEFAULT NULL,
`fa` int(9) DEFAULT NULL,
`fb` int(8) unsigned DEFAULT NULL
`fa` int(8) DEFAULT NULL,
`fb` int(6) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999999.999999999999999999999999
b 999999.999999999999999999999999
......@@ -1696,8 +1696,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,25) DEFAULT NULL,
`b` decimal(30,25) unsigned DEFAULT NULL,
`fa` int(8) DEFAULT NULL,
`fb` int(7) unsigned DEFAULT NULL
`fa` int(7) DEFAULT NULL,
`fb` int(5) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99999.9999999999999999999999999
b 99999.9999999999999999999999999
......@@ -1707,8 +1707,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,26) DEFAULT NULL,
`b` decimal(30,26) unsigned DEFAULT NULL,
`fa` int(7) DEFAULT NULL,
`fb` int(6) unsigned DEFAULT NULL
`fa` int(6) DEFAULT NULL,
`fb` int(4) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9999.99999999999999999999999999
b 9999.99999999999999999999999999
......@@ -1718,8 +1718,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,27) DEFAULT NULL,
`b` decimal(30,27) unsigned DEFAULT NULL,
`fa` int(6) DEFAULT NULL,
`fb` int(5) unsigned DEFAULT NULL
`fa` int(5) DEFAULT NULL,
`fb` int(3) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 999.999999999999999999999999999
b 999.999999999999999999999999999
......@@ -1729,8 +1729,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,28) DEFAULT NULL,
`b` decimal(30,28) unsigned DEFAULT NULL,
`fa` int(5) DEFAULT NULL,
`fb` int(4) unsigned DEFAULT NULL
`fa` int(4) DEFAULT NULL,
`fb` int(2) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 99.9999999999999999999999999999
b 99.9999999999999999999999999999
......@@ -1740,8 +1740,8 @@ Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(30,29) DEFAULT NULL,
`b` decimal(30,29) unsigned DEFAULT NULL,
`fa` int(4) DEFAULT NULL,
`fb` int(3) unsigned DEFAULT NULL
`fa` int(3) DEFAULT NULL,
`fb` int(1) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 9.99999999999999999999999999999
b 9.99999999999999999999999999999
......
......@@ -2411,3 +2411,292 @@ drop table t1;
#
# End of 10.2 tests
#
#
# Start of 10.4 tests
#
#
# MDEV-23032 FLOOR()/CEIL() incorrectly calculate the precision of a DECIMAL(M,D) column.
#
CREATE PROCEDURE p1(prec INT, scale INT, suffix VARCHAR(32))
BEGIN
EXECUTE IMMEDIATE CONCAT('CREATE TABLE t1 (a decimal(',prec,',',scale,')',suffix,')');
INSERT IGNORE INTO t1 VALUES (-1e100), (+1e100);
CREATE TABLE t2 AS SELECT
a,
FLOOR(a) AS fa,
CEILING(a) AS ca,
LENGTH(FLOOR(a)),
LENGTH(CEILING(a))
FROM t1 ORDER BY a;
SHOW CREATE TABLE t2;
SELECT * FROM t2;
DROP TABLE t2, t1;
END;
$$
CALL p1(38,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,10) DEFAULT NULL,
`fa` decimal(29,0) DEFAULT NULL,
`ca` decimal(29,0) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -9999999999999999999999999999.9999999999
fa -10000000000000000000000000000
ca -9999999999999999999999999999
LENGTH(FLOOR(a)) 30
LENGTH(CEILING(a)) 29
a 9999999999999999999999999999.9999999999
fa 9999999999999999999999999999
ca 10000000000000000000000000000
LENGTH(FLOOR(a)) 28
LENGTH(CEILING(a)) 29
CALL p1(28,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(28,10) DEFAULT NULL,
`fa` decimal(19,0) DEFAULT NULL,
`ca` decimal(19,0) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -999999999999999999.9999999999
fa -1000000000000000000
ca -999999999999999999
LENGTH(FLOOR(a)) 20
LENGTH(CEILING(a)) 19
a 999999999999999999.9999999999
fa 999999999999999999
ca 1000000000000000000
LENGTH(FLOOR(a)) 18
LENGTH(CEILING(a)) 19
CALL p1(27,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(27,10) DEFAULT NULL,
`fa` bigint(19) DEFAULT NULL,
`ca` bigint(19) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -99999999999999999.9999999999
fa -100000000000000000
ca -99999999999999999
LENGTH(FLOOR(a)) 19
LENGTH(CEILING(a)) 18
a 99999999999999999.9999999999
fa 99999999999999999
ca 100000000000000000
LENGTH(FLOOR(a)) 17
LENGTH(CEILING(a)) 18
CALL p1(20,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(20,10) DEFAULT NULL,
`fa` bigint(12) DEFAULT NULL,
`ca` bigint(12) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -9999999999.9999999999
fa -10000000000
ca -9999999999
LENGTH(FLOOR(a)) 12
LENGTH(CEILING(a)) 11
a 9999999999.9999999999
fa 9999999999
ca 10000000000
LENGTH(FLOOR(a)) 10
LENGTH(CEILING(a)) 11
CALL p1(19,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(19,10) DEFAULT NULL,
`fa` bigint(11) DEFAULT NULL,
`ca` bigint(11) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -999999999.9999999999
fa -1000000000
ca -999999999
LENGTH(FLOOR(a)) 11
LENGTH(CEILING(a)) 10
a 999999999.9999999999
fa 999999999
ca 1000000000
LENGTH(FLOOR(a)) 9
LENGTH(CEILING(a)) 10
CALL p1(18,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(18,10) DEFAULT NULL,
`fa` int(10) DEFAULT NULL,
`ca` int(10) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -99999999.9999999999
fa -100000000
ca -99999999
LENGTH(FLOOR(a)) 10
LENGTH(CEILING(a)) 9
a 99999999.9999999999
fa 99999999
ca 100000000
LENGTH(FLOOR(a)) 8
LENGTH(CEILING(a)) 9
CALL p1(10,10,'');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(10,10) DEFAULT NULL,
`fa` int(2) DEFAULT NULL,
`ca` int(2) DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a -0.9999999999
fa -1
ca 0
LENGTH(FLOOR(a)) 2
LENGTH(CEILING(a)) 1
a 0.9999999999
fa 0
ca 1
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
CALL p1(38,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(38,10) unsigned DEFAULT NULL,
`fa` decimal(28,0) unsigned DEFAULT NULL,
`ca` decimal(29,0) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 9999999999999999999999999999.9999999999
fa 9999999999999999999999999999
ca 10000000000000000000000000000
LENGTH(FLOOR(a)) 28
LENGTH(CEILING(a)) 29
CALL p1(28,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(28,10) unsigned DEFAULT NULL,
`fa` bigint(18) unsigned DEFAULT NULL,
`ca` decimal(19,0) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 999999999999999999.9999999999
fa 999999999999999999
ca 1000000000000000000
LENGTH(FLOOR(a)) 18
LENGTH(CEILING(a)) 19
CALL p1(27,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(27,10) unsigned DEFAULT NULL,
`fa` bigint(17) unsigned DEFAULT NULL,
`ca` bigint(18) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 99999999999999999.9999999999
fa 99999999999999999
ca 100000000000000000
LENGTH(FLOOR(a)) 17
LENGTH(CEILING(a)) 18
CALL p1(20,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(20,10) unsigned DEFAULT NULL,
`fa` bigint(10) unsigned DEFAULT NULL,
`ca` bigint(11) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 9999999999.9999999999
fa 9999999999
ca 10000000000
LENGTH(FLOOR(a)) 10
LENGTH(CEILING(a)) 11
CALL p1(19,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(19,10) unsigned DEFAULT NULL,
`fa` int(9) unsigned DEFAULT NULL,
`ca` bigint(10) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 999999999.9999999999
fa 999999999
ca 1000000000
LENGTH(FLOOR(a)) 9
LENGTH(CEILING(a)) 10
CALL p1(18,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(18,10) unsigned DEFAULT NULL,
`fa` int(8) unsigned DEFAULT NULL,
`ca` int(9) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 99999999.9999999999
fa 99999999
ca 100000000
LENGTH(FLOOR(a)) 8
LENGTH(CEILING(a)) 9
CALL p1(10,10,' UNSIGNED');
Table t2
Create Table CREATE TABLE `t2` (
`a` decimal(10,10) unsigned DEFAULT NULL,
`fa` int(1) unsigned DEFAULT NULL,
`ca` int(1) unsigned DEFAULT NULL,
`LENGTH(FLOOR(a))` int(10) DEFAULT NULL,
`LENGTH(CEILING(a))` int(10) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
a 0.0000000000
fa 0
ca 0
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
a 0.9999999999
fa 0
ca 1
LENGTH(FLOOR(a)) 1
LENGTH(CEILING(a)) 1
DROP PROCEDURE p1;
......@@ -1871,3 +1871,51 @@ drop table t1;
--echo #
--echo # End of 10.2 tests
--echo #
--echo #
--echo # Start of 10.4 tests
--echo #
--echo #
--echo # MDEV-23032 FLOOR()/CEIL() incorrectly calculate the precision of a DECIMAL(M,D) column.
--echo #
DELIMITER $$;
CREATE PROCEDURE p1(prec INT, scale INT, suffix VARCHAR(32))
BEGIN
EXECUTE IMMEDIATE CONCAT('CREATE TABLE t1 (a decimal(',prec,',',scale,')',suffix,')');
INSERT IGNORE INTO t1 VALUES (-1e100), (+1e100);
CREATE TABLE t2 AS SELECT
a,
FLOOR(a) AS fa,
CEILING(a) AS ca,
LENGTH(FLOOR(a)),
LENGTH(CEILING(a))
FROM t1 ORDER BY a;
SHOW CREATE TABLE t2;
SELECT * FROM t2;
DROP TABLE t2, t1;
END;
$$
DELIMITER ;$$
--vertical_results
CALL p1(38,10,'');
CALL p1(28,10,'');
CALL p1(27,10,'');
CALL p1(20,10,'');
CALL p1(19,10,'');
CALL p1(18,10,'');
CALL p1(10,10,'');
CALL p1(38,10,' UNSIGNED');
CALL p1(28,10,' UNSIGNED');
CALL p1(27,10,' UNSIGNED');
CALL p1(20,10,' UNSIGNED');
CALL p1(19,10,' UNSIGNED');
CALL p1(18,10,' UNSIGNED');
CALL p1(10,10,' UNSIGNED');
--horizontal_results
DROP PROCEDURE p1;
......@@ -2173,35 +2173,64 @@ longlong Item_func_bit_neg::val_int()
void Item_func_int_val::fix_length_and_dec_int_or_decimal()
{
DBUG_ASSERT(args[0]->cmp_type() == DECIMAL_RESULT);
DBUG_ASSERT(args[0]->max_length <= DECIMAL_MAX_STR_LENGTH);
/*
The INT branch of this code should be revised.
It creates too large data types, e.g.
CREATE OR REPLACE TABLE t2 AS SELECT FLOOR(9999999.999) AS fa;
results in a BININT(10) column, while INT(7) should probably be enough.
FLOOR() for negative numbers can increase length: floor(-9.9) -> -10
CEILING() for positive numbers can increase length: ceil(9.9) -> 10
*/
ulonglong tmp_max_length= (ulonglong ) args[0]->max_length -
(args[0]->decimals ? args[0]->decimals + 1 : 0) + 2;
max_length= tmp_max_length > (ulonglong) UINT_MAX32 ?
(uint32) UINT_MAX32 : (uint32) tmp_max_length;
uint tmp= float_length(decimals);
set_if_smaller(max_length,tmp);
decimals= 0;
decimal_round_mode mode= round_mode();
uint length_increase= args[0]->decimals > 0 &&
(mode == CEILING ||
(mode == FLOOR && !args[0]->unsigned_flag)) ? 1 : 0;
uint precision= args[0]->decimal_int_part() + length_increase;
set_if_bigger(precision, 1);
/*
-2 because in most high position can't be used any digit for longlong
and one position for increasing value during operation
The BIGINT data type can store:
UNSIGNED BIGINT: 0..18446744073709551615 - up to 19 digits
SIGNED BIGINT: -9223372036854775808..9223372036854775807 - up to 18 digits
The INT data type can store:
UNSIGNED INT: 0..4294967295 - up to 9 digits
SIGNED INT: -2147483648..2147483647 - up to 9 digits
*/
if (args[0]->max_length - args[0]->decimals >= DECIMAL_LONGLONG_DIGITS - 2)
if (precision > 18)
{
unsigned_flag= args[0]->unsigned_flag;
fix_char_length(
my_decimal_precision_to_length_no_truncation(
args[0]->decimal_int_part(), 0, false));
my_decimal_precision_to_length_no_truncation(precision, 0,
unsigned_flag));
set_handler(&type_handler_newdecimal);
}
else
{
unsigned_flag= args[0]->unsigned_flag;
set_handler(type_handler_long_or_longlong());
uint sign_length= (unsigned_flag= args[0]->unsigned_flag) ? 0 : 1;
fix_char_length(precision + sign_length);
if (precision > 9)
{
#if MYSQL_VERSION_ID > 100500
#error Remove the '#else' branch and the conditional compilation
if (unsigned_flag)
set_handler(&type_handler_ulonglong);
else
set_handler(&type_handler_slonglong);
#else
set_handler(&type_handler_longlong);
#endif
}
else
{
#if MYSQL_VERSION_ID > 100500
#error Remove the '#else' branch and the conditional compilation
if (unsigned_flag)
set_handler(&type_handler_ulong);
else
set_handler(&type_handler_slong);
#else
set_handler(&type_handler_long);
#endif
}
}
}
......
......@@ -1695,6 +1695,7 @@ class Item_func_int_val :public Item_func_hybrid_field_type
Item_func_int_val(THD *thd, Item *a): Item_func_hybrid_field_type(thd, a) {}
bool check_partition_func_processor(void *int_arg) { return FALSE; }
bool check_vcol_func_processor(void *arg) { return FALSE; }
virtual decimal_round_mode round_mode() const= 0;
void fix_length_and_dec_double();
void fix_length_and_dec_int_or_decimal();
void fix_length_and_dec_time()
......@@ -1723,6 +1724,7 @@ class Item_func_ceiling :public Item_func_int_val
public:
Item_func_ceiling(THD *thd, Item *a): Item_func_int_val(thd, a) {}
const char *func_name() const { return "ceiling"; }
decimal_round_mode round_mode() const { return CEILING; }
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
......@@ -1738,6 +1740,7 @@ class Item_func_floor :public Item_func_int_val
public:
Item_func_floor(THD *thd, Item *a): Item_func_int_val(thd, a) {}
const char *func_name() const { return "floor"; }
decimal_round_mode round_mode() const { return FLOOR; }
longlong int_op();
double real_op();
my_decimal *decimal_op(my_decimal *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment