Skip to content

Commit 31f34b2

Browse files
committed
MDEV-22502 MDB crashes in CREATE TABLE AS SELECT when the precision of returning type = 0.
TRUNCATE(decimal_5_5) erroneously tried to create a DECIMAL(0,0) column. Creating a DECIMAL(1,0) column instead.
1 parent 910c319 commit 31f34b2

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

mysql-test/r/func_math.result

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,24 @@ SELECT -9223372036854775808 MOD -9223372036854775808;
994994
-9223372036854775808 MOD -9223372036854775808
995995
0
996996
#
997+
# MDEV-22502 MDB crashes in CREATE TABLE AS SELECT when the precision of returning type = 0
998+
#
999+
CREATE TABLE t1 (d decimal(5,5));
1000+
INSERT INTO t1 VALUES (0.55555);
1001+
SELECT TRUNCATE(d,0) FROM t1;
1002+
TRUNCATE(d,0)
1003+
0
1004+
CREATE TABLE t2 AS SELECT TRUNCATE(d,0) FROM t1;
1005+
SELECT * FROM t2;
1006+
TRUNCATE(d,0)
1007+
0
1008+
SHOW CREATE TABLE t2;
1009+
Table Create Table
1010+
t2 CREATE TABLE `t2` (
1011+
`TRUNCATE(d,0)` decimal(1,0) DEFAULT NULL
1012+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1013+
DROP TABLE t1, t2;
1014+
#
9971015
# MDEV-22503 MDB limits DECIMAL column precision to 16 doing CTAS with floor/ceil over DECIMAL(X,Y) where X > 16
9981016
#
9991017
CREATE TABLE t44 (d1 decimal(38,0) DEFAULT NULL);

mysql-test/t/func_math.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,18 @@ SELECT 9223372036854775808 MOD -9223372036854775808;
719719
SELECT -9223372036854775808 MOD 9223372036854775808;
720720
SELECT -9223372036854775808 MOD -9223372036854775808;
721721

722+
--echo #
723+
--echo # MDEV-22502 MDB crashes in CREATE TABLE AS SELECT when the precision of returning type = 0
724+
--echo #
725+
726+
CREATE TABLE t1 (d decimal(5,5));
727+
INSERT INTO t1 VALUES (0.55555);
728+
SELECT TRUNCATE(d,0) FROM t1;
729+
CREATE TABLE t2 AS SELECT TRUNCATE(d,0) FROM t1;
730+
SELECT * FROM t2;
731+
SHOW CREATE TABLE t2;
732+
DROP TABLE t1, t2;
733+
722734

723735
--echo #
724736
--echo # MDEV-22503 MDB limits DECIMAL column precision to 16 doing CTAS with floor/ceil over DECIMAL(X,Y) where X > 16

sql/item_func.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,8 @@ void Item_func_round::fix_length_and_dec()
25232523

25242524
precision-= decimals_delta - length_increase;
25252525
decimals= MY_MIN(decimals_to_set, DECIMAL_MAX_SCALE);
2526+
if (!precision)
2527+
precision= 1; // DECIMAL(0,0) -> DECIMAL(1,0)
25262528
max_length= my_decimal_precision_to_length_no_truncation(precision,
25272529
decimals,
25282530
unsigned_flag);

0 commit comments

Comments
 (0)