Skip to content

Commit 9840bb2

Browse files
committed
MDEV-23366 ROUND(18446744073709551615,rand()*0) returns a wrong result
Changing that in case of *INT and hex hybrid input: - ROUND(x,NULL) creates a column with the same type as x. The old code created a DOUBLE column, which was not relevant at all. This change simplifies the code a lot. - ROUND(x,non_constant) creates a column of the INT, BIGINT or DECIMAL data type (depending on the exact type of x). The old code created a column of the DOUBLE data type, which lead to precision loss. Hence MDEV-23366. - ROUND(bigint_30,negative_constant) creates a column of the DECIMAL(30,0) data type. The old code created DECIMAL(29,0), which looked strange: the data type promoted to a higher one, but max length reduced. Now the length attribute is preserved.
1 parent 97f7bfc commit 9840bb2

File tree

8 files changed

+300
-55
lines changed

8 files changed

+300
-55
lines changed

mysql-test/main/func_math.result

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ truncate(4, cast(-2 as unsigned)) truncate(4, 18446744073709551614) truncate(4,
348348
4 4 0
349349
Warnings:
350350
Note 1105 Cast to unsigned converted negative integer to it's positive complement
351-
Note 1105 Cast to unsigned converted negative integer to it's positive complement
352351
select round(10000000000000000000, -19), truncate(10000000000000000000, -19);
353352
round(10000000000000000000, -19) truncate(10000000000000000000, -19)
354353
10000000000000000000 10000000000000000000
@@ -1784,7 +1783,7 @@ ROUND(10e0,NULL) AS c3;
17841783
SHOW CREATE TABLE t1;
17851784
Table Create Table
17861785
t1 CREATE TABLE `t1` (
1787-
`c1` double DEFAULT NULL,
1786+
`c1` int(2) DEFAULT NULL,
17881787
`c2` double DEFAULT NULL,
17891788
`c3` double DEFAULT NULL
17901789
) ENGINE=MyISAM DEFAULT CHARSET=latin1

mysql-test/main/type_hex_hybrid.result

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,42 @@ ROUND(0xFFFFFFFFFFFFFFFF,-10) ROUND(0xFFFFFFFFFFFFFFFF,-11)
195195
18446744070000000000 18446744100000000000
196196
DROP TABLE t1;
197197
#
198+
# MDEV-23366 ROUND(18446744073709551615,rand()*0) returns a wrong result
199+
#
200+
SELECT
201+
ROUND(0xFFFFFFFFFFFFFFFF,NULL) AS c1,
202+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0) AS c2,
203+
ROUND(0xFFFFFFFFFFFFFFFF,-1) AS c3,
204+
ROUND(0xFFFFFFFFFFFFFFFF,-19) AS c4,
205+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0-19) AS c5;
206+
c1 NULL
207+
c2 18446744073709551615
208+
c3 18446744073709551620
209+
c4 20000000000000000000
210+
c5 20000000000000000000
211+
CREATE OR REPLACE TABLE t1 AS
212+
SELECT
213+
ROUND(0xFFFFFFFFFFFFFFFF,NULL) AS c1,
214+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0) AS c2,
215+
ROUND(0xFFFFFFFFFFFFFFFF,-1) AS c3,
216+
ROUND(0xFFFFFFFFFFFFFFFF,-19) AS c4,
217+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0-19) AS c5;
218+
SELECT * FROM t1;
219+
c1 NULL
220+
c2 18446744073709551615
221+
c3 18446744073709551620
222+
c4 20000000000000000000
223+
c5 20000000000000000000
224+
SHOW CREATE TABLE t1;
225+
Table t1
226+
Create Table CREATE TABLE `t1` (
227+
`c1` bigint(20) unsigned DEFAULT NULL,
228+
`c2` decimal(21,0) unsigned NOT NULL,
229+
`c3` decimal(21,0) unsigned NOT NULL,
230+
`c4` decimal(21,0) unsigned NOT NULL,
231+
`c5` decimal(21,0) unsigned NOT NULL
232+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
233+
DROP TABLE t1;
234+
#
198235
# End of 10.4 tests
199236
#

mysql-test/main/type_hex_hybrid.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ SELECT * FROM t1;
5757
DROP TABLE t1;
5858

5959

60+
--echo #
61+
--echo # MDEV-23366 ROUND(18446744073709551615,rand()*0) returns a wrong result
62+
--echo #
63+
64+
--vertical_results
65+
SELECT
66+
ROUND(0xFFFFFFFFFFFFFFFF,NULL) AS c1,
67+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0) AS c2,
68+
ROUND(0xFFFFFFFFFFFFFFFF,-1) AS c3,
69+
ROUND(0xFFFFFFFFFFFFFFFF,-19) AS c4,
70+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0-19) AS c5;
71+
72+
CREATE OR REPLACE TABLE t1 AS
73+
SELECT
74+
ROUND(0xFFFFFFFFFFFFFFFF,NULL) AS c1,
75+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0) AS c2,
76+
ROUND(0xFFFFFFFFFFFFFFFF,-1) AS c3,
77+
ROUND(0xFFFFFFFFFFFFFFFF,-19) AS c4,
78+
ROUND(0xFFFFFFFFFFFFFFFF,rand()*0-19) AS c5;
79+
80+
SELECT * FROM t1;
81+
SHOW CREATE TABLE t1;
82+
DROP TABLE t1;
83+
--horizontal_results
84+
6085
--echo #
6186
--echo # End of 10.4 tests
6287
--echo #

mysql-test/main/type_int.result

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,75 @@ ROUND(a,-2) 9223372036854775800
11631163
ROUND(a,-19) 10000000000000000000
11641164
ROUND(a,-20) 0
11651165
ROUND(a,-30) 0
1166+
CALL p1('bigint(22)');
1167+
bigint(22)
1168+
Table t2
1169+
Create Table CREATE TABLE `t2` (
1170+
`a` bigint(22) DEFAULT NULL,
1171+
`ROUND(a,-1)` decimal(22,0) DEFAULT NULL,
1172+
`ROUND(a,-2)` decimal(22,0) DEFAULT NULL,
1173+
`ROUND(a,-19)` decimal(22,0) DEFAULT NULL,
1174+
`ROUND(a,-20)` decimal(22,0) DEFAULT NULL,
1175+
`ROUND(a,-30)` decimal(22,0) DEFAULT NULL
1176+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1177+
a -9223372036854775808
1178+
ROUND(a,-1) -9223372036854775810
1179+
ROUND(a,-2) -9223372036854775800
1180+
ROUND(a,-19) -10000000000000000000
1181+
ROUND(a,-20) 0
1182+
ROUND(a,-30) 0
1183+
a 9223372036854775807
1184+
ROUND(a,-1) 9223372036854775810
1185+
ROUND(a,-2) 9223372036854775800
1186+
ROUND(a,-19) 10000000000000000000
1187+
ROUND(a,-20) 0
1188+
ROUND(a,-30) 0
1189+
CALL p1('bigint(23)');
1190+
bigint(23)
1191+
Table t2
1192+
Create Table CREATE TABLE `t2` (
1193+
`a` bigint(23) DEFAULT NULL,
1194+
`ROUND(a,-1)` decimal(23,0) DEFAULT NULL,
1195+
`ROUND(a,-2)` decimal(23,0) DEFAULT NULL,
1196+
`ROUND(a,-19)` decimal(23,0) DEFAULT NULL,
1197+
`ROUND(a,-20)` decimal(23,0) DEFAULT NULL,
1198+
`ROUND(a,-30)` decimal(23,0) DEFAULT NULL
1199+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1200+
a -9223372036854775808
1201+
ROUND(a,-1) -9223372036854775810
1202+
ROUND(a,-2) -9223372036854775800
1203+
ROUND(a,-19) -10000000000000000000
1204+
ROUND(a,-20) 0
1205+
ROUND(a,-30) 0
1206+
a 9223372036854775807
1207+
ROUND(a,-1) 9223372036854775810
1208+
ROUND(a,-2) 9223372036854775800
1209+
ROUND(a,-19) 10000000000000000000
1210+
ROUND(a,-20) 0
1211+
ROUND(a,-30) 0
1212+
CALL p1('bigint(30)');
1213+
bigint(30)
1214+
Table t2
1215+
Create Table CREATE TABLE `t2` (
1216+
`a` bigint(30) DEFAULT NULL,
1217+
`ROUND(a,-1)` decimal(30,0) DEFAULT NULL,
1218+
`ROUND(a,-2)` decimal(30,0) DEFAULT NULL,
1219+
`ROUND(a,-19)` decimal(30,0) DEFAULT NULL,
1220+
`ROUND(a,-20)` decimal(30,0) DEFAULT NULL,
1221+
`ROUND(a,-30)` decimal(30,0) DEFAULT NULL
1222+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1223+
a -9223372036854775808
1224+
ROUND(a,-1) -9223372036854775810
1225+
ROUND(a,-2) -9223372036854775800
1226+
ROUND(a,-19) -10000000000000000000
1227+
ROUND(a,-20) 0
1228+
ROUND(a,-30) 0
1229+
a 9223372036854775807
1230+
ROUND(a,-1) 9223372036854775810
1231+
ROUND(a,-2) 9223372036854775800
1232+
ROUND(a,-19) 10000000000000000000
1233+
ROUND(a,-20) 0
1234+
ROUND(a,-30) 0
11661235
CALL p1('tinyint unsigned');
11671236
tinyint unsigned
11681237
Table t2
@@ -1324,7 +1393,103 @@ ROUND(a,-2) 18446744073709551600
13241393
ROUND(a,-19) 20000000000000000000
13251394
ROUND(a,-20) 0
13261395
ROUND(a,-30) 0
1396+
CALL p1('bigint(22) unsigned');
1397+
bigint(22) unsigned
1398+
Table t2
1399+
Create Table CREATE TABLE `t2` (
1400+
`a` bigint(22) unsigned DEFAULT NULL,
1401+
`ROUND(a,-1)` decimal(23,0) unsigned DEFAULT NULL,
1402+
`ROUND(a,-2)` decimal(23,0) unsigned DEFAULT NULL,
1403+
`ROUND(a,-19)` decimal(23,0) unsigned DEFAULT NULL,
1404+
`ROUND(a,-20)` decimal(23,0) unsigned DEFAULT NULL,
1405+
`ROUND(a,-30)` decimal(23,0) unsigned DEFAULT NULL
1406+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1407+
a 0
1408+
ROUND(a,-1) 0
1409+
ROUND(a,-2) 0
1410+
ROUND(a,-19) 0
1411+
ROUND(a,-20) 0
1412+
ROUND(a,-30) 0
1413+
a 18446744073709551615
1414+
ROUND(a,-1) 18446744073709551620
1415+
ROUND(a,-2) 18446744073709551600
1416+
ROUND(a,-19) 20000000000000000000
1417+
ROUND(a,-20) 0
1418+
ROUND(a,-30) 0
1419+
CALL p1('bigint(23) unsigned');
1420+
bigint(23) unsigned
1421+
Table t2
1422+
Create Table CREATE TABLE `t2` (
1423+
`a` bigint(23) unsigned DEFAULT NULL,
1424+
`ROUND(a,-1)` decimal(24,0) unsigned DEFAULT NULL,
1425+
`ROUND(a,-2)` decimal(24,0) unsigned DEFAULT NULL,
1426+
`ROUND(a,-19)` decimal(24,0) unsigned DEFAULT NULL,
1427+
`ROUND(a,-20)` decimal(24,0) unsigned DEFAULT NULL,
1428+
`ROUND(a,-30)` decimal(24,0) unsigned DEFAULT NULL
1429+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1430+
a 0
1431+
ROUND(a,-1) 0
1432+
ROUND(a,-2) 0
1433+
ROUND(a,-19) 0
1434+
ROUND(a,-20) 0
1435+
ROUND(a,-30) 0
1436+
a 18446744073709551615
1437+
ROUND(a,-1) 18446744073709551620
1438+
ROUND(a,-2) 18446744073709551600
1439+
ROUND(a,-19) 20000000000000000000
1440+
ROUND(a,-20) 0
1441+
ROUND(a,-30) 0
1442+
CALL p1('bigint(30) unsigned');
1443+
bigint(30) unsigned
1444+
Table t2
1445+
Create Table CREATE TABLE `t2` (
1446+
`a` bigint(30) unsigned DEFAULT NULL,
1447+
`ROUND(a,-1)` decimal(31,0) unsigned DEFAULT NULL,
1448+
`ROUND(a,-2)` decimal(31,0) unsigned DEFAULT NULL,
1449+
`ROUND(a,-19)` decimal(31,0) unsigned DEFAULT NULL,
1450+
`ROUND(a,-20)` decimal(31,0) unsigned DEFAULT NULL,
1451+
`ROUND(a,-30)` decimal(31,0) unsigned DEFAULT NULL
1452+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1453+
a 0
1454+
ROUND(a,-1) 0
1455+
ROUND(a,-2) 0
1456+
ROUND(a,-19) 0
1457+
ROUND(a,-20) 0
1458+
ROUND(a,-30) 0
1459+
a 18446744073709551615
1460+
ROUND(a,-1) 18446744073709551620
1461+
ROUND(a,-2) 18446744073709551600
1462+
ROUND(a,-19) 20000000000000000000
1463+
ROUND(a,-20) 0
1464+
ROUND(a,-30) 0
13271465
DROP PROCEDURE p1;
13281466
#
1467+
# MDEV-23366 ROUND(18446744073709551615,rand()*0) returns a wrong result
1468+
#
1469+
SELECT
1470+
ROUND(18446744073709551615,NULL) AS c1,
1471+
ROUND(18446744073709551615,rand()*0) AS c2,
1472+
ROUND(18446744073709551615,rand()*0-19) AS c3;
1473+
c1 NULL
1474+
c2 18446744073709551615
1475+
c3 20000000000000000000
1476+
CREATE OR REPLACE TABLE t1 AS
1477+
SELECT
1478+
ROUND(18446744073709551615,NULL) AS c1,
1479+
ROUND(18446744073709551615,rand()*0) AS c2,
1480+
ROUND(18446744073709551615,rand()*0-19) AS c3;
1481+
SELECT * FROM t1;
1482+
c1 NULL
1483+
c2 18446744073709551615
1484+
c3 20000000000000000000
1485+
SHOW CREATE TABLE t1;
1486+
Table t1
1487+
Create Table CREATE TABLE `t1` (
1488+
`c1` bigint(20) unsigned DEFAULT NULL,
1489+
`c2` decimal(21,0) unsigned NOT NULL,
1490+
`c3` decimal(21,0) unsigned NOT NULL
1491+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1492+
DROP TABLE t1;
1493+
#
13291494
# End of 10.4 tests
13301495
#

mysql-test/main/type_int.test

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ CALL p1('int');
394394
CALL p1('bigint');
395395
CALL p1('bigint(20)');
396396
CALL p1('bigint(21)');
397-
#CALL p1('bigint(22)');
398-
#CALL p1('bigint(23)');
399-
#CALL p1('bigint(30)');
397+
CALL p1('bigint(22)');
398+
CALL p1('bigint(23)');
399+
CALL p1('bigint(30)');
400400

401401
CALL p1('tinyint unsigned');
402402
CALL p1('smallint unsigned');
@@ -405,14 +405,36 @@ CALL p1('int unsigned');
405405
CALL p1('bigint unsigned');
406406
CALL p1('bigint(20) unsigned');
407407
CALL p1('bigint(21) unsigned');
408-
#CALL p1('bigint(22) unsigned');
409-
#CALL p1('bigint(23) unsigned');
410-
#CALL p1('bigint(30) unsigned');
408+
CALL p1('bigint(22) unsigned');
409+
CALL p1('bigint(23) unsigned');
410+
CALL p1('bigint(30) unsigned');
411411
--horizontal_results
412412

413413
DROP PROCEDURE p1;
414414

415415

416+
--echo #
417+
--echo # MDEV-23366 ROUND(18446744073709551615,rand()*0) returns a wrong result
418+
--echo #
419+
420+
--vertical_results
421+
SELECT
422+
ROUND(18446744073709551615,NULL) AS c1,
423+
ROUND(18446744073709551615,rand()*0) AS c2,
424+
ROUND(18446744073709551615,rand()*0-19) AS c3;
425+
426+
CREATE OR REPLACE TABLE t1 AS
427+
SELECT
428+
ROUND(18446744073709551615,NULL) AS c1,
429+
ROUND(18446744073709551615,rand()*0) AS c2,
430+
ROUND(18446744073709551615,rand()*0-19) AS c3;
431+
432+
SELECT * FROM t1;
433+
SHOW CREATE TABLE t1;
434+
DROP TABLE t1;
435+
--horizontal_results
436+
437+
416438
--echo #
417439
--echo # End of 10.4 tests
418440
--echo #

0 commit comments

Comments
 (0)