diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result index e6c323276ea..f5c1c5cfe39 100644 --- a/mysql-test/r/func_str.result +++ b/mysql-test/r/func_str.result @@ -2273,4 +2273,27 @@ abcxx select lpad('abc', cast(5 as unsigned integer), 'x'); lpad('abc', cast(5 as unsigned integer), 'x') xxabc +DROP TABLE IF EXISTS t1; +CREATE TABLE `t1` ( +`id` varchar(20) NOT NULL, +`tire` tinyint(3) unsigned NOT NULL, +PRIMARY KEY (`id`) +); +INSERT INTO `t1` (`id`, `tire`) VALUES ('A', 0), ('B', 1),('C', 2); +SELECT REPEAT( '#', tire ) AS A, +REPEAT( '#', tire % 999 ) AS B, tire FROM `t1`; +A B tire + 0 +# # 1 +## ## 2 +SELECT REPEAT('0', CAST(0 AS UNSIGNED)); +REPEAT('0', CAST(0 AS UNSIGNED)) + +SELECT REPEAT('0', -2); +REPEAT('0', -2) + +SELECT REPEAT('0', 2); +REPEAT('0', 2) +00 +DROP TABLE t1; End of 5.0 tests diff --git a/mysql-test/t/func_str.test b/mysql-test/t/func_str.test index 535af6907ad..3314a229d57 100644 --- a/mysql-test/t/func_str.test +++ b/mysql-test/t/func_str.test @@ -1115,4 +1115,30 @@ select repeat('a', cast(2 as unsigned int)); select rpad('abc', cast(5 as unsigned integer), 'x'); select lpad('abc', cast(5 as unsigned integer), 'x'); + +# +# Bug #25197 :repeat function returns null when using table field directly as count +# + +--disable_warnings +DROP TABLE IF EXISTS t1; +--enable_warnings + +CREATE TABLE `t1` ( + `id` varchar(20) NOT NULL, + `tire` tinyint(3) unsigned NOT NULL, + PRIMARY KEY (`id`) +); + +INSERT INTO `t1` (`id`, `tire`) VALUES ('A', 0), ('B', 1),('C', 2); + +SELECT REPEAT( '#', tire ) AS A, + REPEAT( '#', tire % 999 ) AS B, tire FROM `t1`; + +SELECT REPEAT('0', CAST(0 AS UNSIGNED)); +SELECT REPEAT('0', -2); +SELECT REPEAT('0', 2); + +DROP TABLE t1; + --echo End of 5.0 tests diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index faea5380a66..f92c328fc39 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -2274,8 +2274,10 @@ String *Item_func_repeat::val_str(String *str) if (args[0]->null_value || args[1]->null_value) goto err; // string and/or delim are null null_value= 0; - if ((count <= 0) && !args[1]->unsigned_flag) // For nicer SQL code + + if (count <= 0 && (count == 0 || !args[1]->unsigned_flag)) return &my_empty_string; + /* Assumes that the maximum length of a String is < INT_MAX32. */ /* Bounds check on count: If this is triggered, we will error. */ if ((ulonglong) count > INT_MAX32)