summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.com>2020-05-14 11:41:27 +0400
committerAlexander Barkov <bar@mariadb.com>2020-05-14 11:41:27 +0400
commit31f34b20f3295db7e99877dcfe61b5798a6cfe95 (patch)
tree3391f3e65825ec0bb928b8477bee5104f8bdb5b2
parent910c31928ee62646f06e1584e43071df34738afb (diff)
downloadmariadb-git-31f34b20f3295db7e99877dcfe61b5798a6cfe95.tar.gz
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.
-rw-r--r--mysql-test/r/func_math.result18
-rw-r--r--mysql-test/t/func_math.test12
-rw-r--r--sql/item_func.cc2
3 files changed, 32 insertions, 0 deletions
diff --git a/mysql-test/r/func_math.result b/mysql-test/r/func_math.result
index 23815bcfed4..fcb2e73e623 100644
--- a/mysql-test/r/func_math.result
+++ b/mysql-test/r/func_math.result
@@ -994,6 +994,24 @@ SELECT -9223372036854775808 MOD -9223372036854775808;
-9223372036854775808 MOD -9223372036854775808
0
#
+# MDEV-22502 MDB crashes in CREATE TABLE AS SELECT when the precision of returning type = 0
+#
+CREATE TABLE t1 (d decimal(5,5));
+INSERT INTO t1 VALUES (0.55555);
+SELECT TRUNCATE(d,0) FROM t1;
+TRUNCATE(d,0)
+0
+CREATE TABLE t2 AS SELECT TRUNCATE(d,0) FROM t1;
+SELECT * FROM t2;
+TRUNCATE(d,0)
+0
+SHOW CREATE TABLE t2;
+Table Create Table
+t2 CREATE TABLE `t2` (
+ `TRUNCATE(d,0)` decimal(1,0) DEFAULT NULL
+) ENGINE=MyISAM DEFAULT CHARSET=latin1
+DROP TABLE t1, t2;
+#
# MDEV-22503 MDB limits DECIMAL column precision to 16 doing CTAS with floor/ceil over DECIMAL(X,Y) where X > 16
#
CREATE TABLE t44 (d1 decimal(38,0) DEFAULT NULL);
diff --git a/mysql-test/t/func_math.test b/mysql-test/t/func_math.test
index 6edc342e9df..56eb7640f65 100644
--- a/mysql-test/t/func_math.test
+++ b/mysql-test/t/func_math.test
@@ -719,6 +719,18 @@ SELECT 9223372036854775808 MOD -9223372036854775808;
SELECT -9223372036854775808 MOD 9223372036854775808;
SELECT -9223372036854775808 MOD -9223372036854775808;
+--echo #
+--echo # MDEV-22502 MDB crashes in CREATE TABLE AS SELECT when the precision of returning type = 0
+--echo #
+
+CREATE TABLE t1 (d decimal(5,5));
+INSERT INTO t1 VALUES (0.55555);
+SELECT TRUNCATE(d,0) FROM t1;
+CREATE TABLE t2 AS SELECT TRUNCATE(d,0) FROM t1;
+SELECT * FROM t2;
+SHOW CREATE TABLE t2;
+DROP TABLE t1, t2;
+
--echo #
--echo # MDEV-22503 MDB limits DECIMAL column precision to 16 doing CTAS with floor/ceil over DECIMAL(X,Y) where X > 16
diff --git a/sql/item_func.cc b/sql/item_func.cc
index f76f6515365..c41d7809bf9 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -2523,6 +2523,8 @@ void Item_func_round::fix_length_and_dec()
precision-= decimals_delta - length_increase;
decimals= MY_MIN(decimals_to_set, DECIMAL_MAX_SCALE);
+ if (!precision)
+ precision= 1; // DECIMAL(0,0) -> DECIMAL(1,0)
max_length= my_decimal_precision_to_length_no_truncation(precision,
decimals,
unsigned_flag);