diff options
author | Alexander Barkov <bar@mariadb.org> | 2015-10-01 14:07:42 +0400 |
---|---|---|
committer | Alexander Barkov <bar@mariadb.org> | 2015-10-01 14:07:42 +0400 |
commit | b50c607056e1d4b25e274a8ab87a7e8e4c918c45 (patch) | |
tree | aa2d20dd3172f9b3e8aa93c095e5ef13ed7ea15c | |
parent | 3266216f2c8f90c866b371fbd4a8bf6b0c628996 (diff) | |
download | mariadb-git-b50c607056e1d4b25e274a8ab87a7e8e4c918c45.tar.gz |
MDEV-4848 Wrong metadata or column type for LEAST(1.0,'10')
MDEV-8873 Wrong field type or metadata for LEAST(int_column,string_column)
-rw-r--r-- | mysql-test/r/func_hybrid_type.result | 923 | ||||
-rw-r--r-- | mysql-test/t/func_hybrid_type.test | 44 | ||||
-rw-r--r-- | sql/item_func.cc | 87 | ||||
-rw-r--r-- | sql/item_func.h | 27 |
4 files changed, 1046 insertions, 35 deletions
diff --git a/mysql-test/r/func_hybrid_type.result b/mysql-test/r/func_hybrid_type.result index 0d2ac5aab7b..a8cd4ca4429 100644 --- a/mysql-test/r/func_hybrid_type.result +++ b/mysql-test/r/func_hybrid_type.result @@ -1122,10 +1122,10 @@ def if_______a_b 8 10 2 Y 32928 0 63 def if_______b_a 8 10 10 Y 32928 0 63 def ifnull___a_b 8 10 10 Y 32928 0 63 def ifnull___b_a 8 10 2 Y 32928 0 63 -def least____a_b 253 10 2 Y 32 0 8 -def least____b_a 253 10 2 Y 32 0 8 -def greatest_a_b 253 10 10 Y 32 0 8 -def greatest_b_a 253 10 10 Y 32 0 8 +def least____a_b 8 10 2 Y 32928 0 63 +def least____b_a 8 10 2 Y 32928 0 63 +def greatest_a_b 8 10 10 Y 32928 0 63 +def greatest_b_a 8 10 10 Y 32928 0 63 case_____a_b 4294967295 case_____b_a 50 coalesce_a_b 4294967295 @@ -1392,5 +1392,920 @@ t2 CREATE TABLE `t2` ( DROP TABLE t2; DROP TABLE t1; # +CREATE TABLE t1 (a FLOAT, b SMALLINT); +INSERT INTO t1 VALUES (1,-32678); +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def test t1 t1 a ___________a 4 12 1 Y 32768 31 63 +def case_______a 4 12 1 Y 32896 31 63 +def case_____a_a 4 12 1 Y 32896 31 63 +def coalesce___a 4 12 1 Y 32896 31 63 +def coalesce_a_a 4 12 1 Y 32896 31 63 +def if_______a_a 4 12 1 Y 32896 31 63 +def ifnull___a_a 4 12 1 Y 32896 31 63 +def least____a_a 5 23 1 Y 32896 31 63 +def greatest_a_a 5 23 1 Y 32896 31 63 +def test t1 t1 b ___________b 2 6 6 Y 32768 0 63 +def case_______b 2 6 6 Y 32896 0 63 +def case_____b_b 2 6 6 Y 32896 0 63 +def coalesce___b 2 6 6 Y 32896 0 63 +def coalesce_b_b 2 6 6 Y 32896 0 63 +def if_______b_b 2 6 6 Y 32896 0 63 +def ifnull___b_b 2 6 6 Y 32896 0 63 +def least____b_b 2 6 6 Y 32896 0 63 +def greatest_b_b 2 6 6 Y 32896 0 63 +___________a 1 +case_______a 1 +case_____a_a 1 +coalesce___a 1 +coalesce_a_a 1 +if_______a_a 1 +ifnull___a_a 1 +least____a_a 1 +greatest_a_a 1 +___________b -32678 +case_______b -32678 +case_____b_b -32678 +coalesce___b -32678 +coalesce_b_b -32678 +if_______b_b -32678 +ifnull___b_b -32678 +least____b_b -32678 +greatest_b_b -32678 +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def case_____a_b 4 12 1 Y 32896 31 63 +def case_____b_a 4 12 6 Y 32896 31 63 +def coalesce_a_b 4 12 1 Y 32896 31 63 +def coalesce_b_a 4 12 6 Y 32896 31 63 +def if_______a_b 4 12 6 Y 32896 31 63 +def if_______b_a 4 12 1 Y 32896 31 63 +def ifnull___a_b 4 12 1 Y 32896 31 63 +def ifnull___b_a 4 12 6 Y 32896 31 63 +def least____a_b 5 23 6 Y 32896 31 63 +def least____b_a 5 23 6 Y 32896 31 63 +def greatest_a_b 5 23 1 Y 32896 31 63 +def greatest_b_a 5 23 1 Y 32896 31 63 +case_____a_b 1 +case_____b_a -32678 +coalesce_a_b 1 +coalesce_b_a -32678 +if_______a_b -32678 +if_______b_a 1 +ifnull___a_b 1 +ifnull___b_a -32678 +least____a_b -32678 +least____b_a -32678 +greatest_a_b 1 +greatest_b_a 1 +CREATE TABLE t2 AS +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `___________a` float DEFAULT NULL, + `case_______a` double DEFAULT NULL, + `case_____a_a` double DEFAULT NULL, + `coalesce___a` double DEFAULT NULL, + `coalesce_a_a` double DEFAULT NULL, + `if_______a_a` double DEFAULT NULL, + `ifnull___a_a` float DEFAULT NULL, + `least____a_a` double DEFAULT NULL, + `greatest_a_a` double DEFAULT NULL, + `___________b` smallint(6) DEFAULT NULL, + `case_______b` int(6) DEFAULT NULL, + `case_____b_b` int(6) DEFAULT NULL, + `coalesce___b` int(6) DEFAULT NULL, + `coalesce_b_b` int(6) DEFAULT NULL, + `if_______b_b` int(6) DEFAULT NULL, + `ifnull___b_b` smallint(6) DEFAULT NULL, + `least____b_b` int(6) DEFAULT NULL, + `greatest_b_b` int(6) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +CREATE TABLE t2 AS +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `case_____a_b` double DEFAULT NULL, + `case_____b_a` double DEFAULT NULL, + `coalesce_a_b` double DEFAULT NULL, + `coalesce_b_a` double DEFAULT NULL, + `if_______a_b` double DEFAULT NULL, + `if_______b_a` double DEFAULT NULL, + `ifnull___a_b` float DEFAULT NULL, + `ifnull___b_a` float DEFAULT NULL, + `least____a_b` double DEFAULT NULL, + `least____b_a` double DEFAULT NULL, + `greatest_a_b` double DEFAULT NULL, + `greatest_b_a` double DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +DROP TABLE t1; +# +CREATE TABLE t1 (a VARCHAR(10), b ENUM('b')); +INSERT INTO t1 VALUES ('a','b'); +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def test t1 t1 a ___________a 253 10 1 Y 0 0 8 +def case_______a 253 10 1 Y 0 31 8 +def case_____a_a 253 10 1 Y 0 31 8 +def coalesce___a 253 10 1 Y 0 31 8 +def coalesce_a_a 253 10 1 Y 0 31 8 +def if_______a_a 253 10 1 Y 0 31 8 +def ifnull___a_a 253 10 1 Y 0 31 8 +def least____a_a 253 10 1 Y 0 31 8 +def greatest_a_a 253 10 1 Y 0 31 8 +def test t1 t1 b ___________b 254 1 1 Y 256 0 8 +def case_______b 254 1 1 Y 0 31 8 +def case_____b_b 254 1 1 Y 0 31 8 +def coalesce___b 254 1 1 Y 0 31 8 +def coalesce_b_b 254 1 1 Y 0 31 8 +def if_______b_b 254 1 1 Y 0 31 8 +def ifnull___b_b 254 1 1 Y 0 31 8 +def least____b_b 254 1 1 Y 0 0 8 +def greatest_b_b 254 1 1 Y 0 0 8 +___________a a +case_______a a +case_____a_a a +coalesce___a a +coalesce_a_a a +if_______a_a a +ifnull___a_a a +least____a_a a +greatest_a_a a +___________b b +case_______b b +case_____b_b b +coalesce___b b +coalesce_b_b b +if_______b_b b +ifnull___b_b b +least____b_b b +greatest_b_b b +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def case_____a_b 253 10 1 Y 0 31 8 +def case_____b_a 253 10 1 Y 0 31 8 +def coalesce_a_b 253 10 1 Y 0 31 8 +def coalesce_b_a 253 10 1 Y 0 31 8 +def if_______a_b 253 10 1 Y 0 31 8 +def if_______b_a 253 10 1 Y 0 31 8 +def ifnull___a_b 253 10 1 Y 0 31 8 +def ifnull___b_a 253 10 1 Y 0 31 8 +def least____a_b 253 10 1 Y 0 31 8 +def least____b_a 253 10 1 Y 0 31 8 +def greatest_a_b 253 10 1 Y 0 31 8 +def greatest_b_a 253 10 1 Y 0 31 8 +case_____a_b a +case_____b_a b +coalesce_a_b a +coalesce_b_a b +if_______a_b b +if_______b_a a +ifnull___a_b a +ifnull___b_a b +least____a_b a +least____b_a a +greatest_a_b b +greatest_b_a b +CREATE TABLE t2 AS +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `___________a` varchar(10) DEFAULT NULL, + `case_______a` varchar(10) DEFAULT NULL, + `case_____a_a` varchar(10) DEFAULT NULL, + `coalesce___a` varchar(10) DEFAULT NULL, + `coalesce_a_a` varchar(10) DEFAULT NULL, + `if_______a_a` varchar(10) DEFAULT NULL, + `ifnull___a_a` varchar(10) DEFAULT NULL, + `least____a_a` varchar(10) DEFAULT NULL, + `greatest_a_a` varchar(10) DEFAULT NULL, + `___________b` enum('b') DEFAULT NULL, + `case_______b` varchar(1) DEFAULT NULL, + `case_____b_b` varchar(1) DEFAULT NULL, + `coalesce___b` varchar(1) DEFAULT NULL, + `coalesce_b_b` varchar(1) DEFAULT NULL, + `if_______b_b` varchar(1) DEFAULT NULL, + `ifnull___b_b` varchar(1) DEFAULT NULL, + `least____b_b` varchar(1) DEFAULT NULL, + `greatest_b_b` varchar(1) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +CREATE TABLE t2 AS +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `case_____a_b` varchar(10) DEFAULT NULL, + `case_____b_a` varchar(10) DEFAULT NULL, + `coalesce_a_b` varchar(10) DEFAULT NULL, + `coalesce_b_a` varchar(10) DEFAULT NULL, + `if_______a_b` varchar(10) DEFAULT NULL, + `if_______b_a` varchar(10) DEFAULT NULL, + `ifnull___a_b` varchar(10) DEFAULT NULL, + `ifnull___b_a` varchar(10) DEFAULT NULL, + `least____a_b` varchar(10) DEFAULT NULL, + `least____b_a` varchar(10) DEFAULT NULL, + `greatest_a_b` varchar(10) DEFAULT NULL, + `greatest_b_a` varchar(10) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +DROP TABLE t1; +# +CREATE TABLE t1 (a INT, b YEAR); +INSERT INTO t1 VALUES (-2147483648,2015); +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def test t1 t1 a ___________a 3 11 11 Y 32768 0 63 +def case_______a 3 11 11 Y 32896 0 63 +def case_____a_a 3 11 11 Y 32896 0 63 +def coalesce___a 3 11 11 Y 32896 0 63 +def coalesce_a_a 3 11 11 Y 32896 0 63 +def if_______a_a 3 11 11 Y 32896 0 63 +def ifnull___a_a 3 11 11 Y 32896 0 63 +def least____a_a 3 11 11 Y 32896 0 63 +def greatest_a_a 3 11 11 Y 32896 0 63 +def test t1 t1 b ___________b 13 4 4 Y 32864 0 63 +def case_______b 13 4 4 Y 32928 0 63 +def case_____b_b 13 4 4 Y 32928 0 63 +def coalesce___b 13 4 4 Y 32928 0 63 +def coalesce_b_b 13 4 4 Y 32928 0 63 +def if_______b_b 13 4 4 Y 32928 0 63 +def ifnull___b_b 13 4 4 Y 32928 0 63 +def least____b_b 13 4 4 Y 32928 0 63 +def greatest_b_b 13 4 4 Y 32928 0 63 +___________a -2147483648 +case_______a -2147483648 +case_____a_a -2147483648 +coalesce___a -2147483648 +coalesce_a_a -2147483648 +if_______a_a -2147483648 +ifnull___a_a -2147483648 +least____a_a -2147483648 +greatest_a_a -2147483648 +___________b 2015 +case_______b 2015 +case_____b_b 2015 +coalesce___b 2015 +coalesce_b_b 2015 +if_______b_b 2015 +ifnull___b_b 2015 +least____b_b 2015 +greatest_b_b 2015 +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def case_____a_b 246 11 11 Y 32896 0 63 +def case_____b_a 246 11 4 Y 32896 0 63 +def coalesce_a_b 246 11 11 Y 32896 0 63 +def coalesce_b_a 246 11 4 Y 32896 0 63 +def if_______a_b 246 11 4 Y 32896 0 63 +def if_______b_a 246 11 11 Y 32896 0 63 +def ifnull___a_b 246 11 11 Y 32896 0 63 +def ifnull___b_a 246 11 4 Y 32896 0 63 +def least____a_b 246 11 11 Y 32896 0 63 +def least____b_a 246 11 11 Y 32896 0 63 +def greatest_a_b 246 11 4 Y 32896 0 63 +def greatest_b_a 246 11 4 Y 32896 0 63 +case_____a_b -2147483648 +case_____b_a 2015 +coalesce_a_b -2147483648 +coalesce_b_a 2015 +if_______a_b 2015 +if_______b_a -2147483648 +ifnull___a_b -2147483648 +ifnull___b_a 2015 +least____a_b -2147483648 +least____b_a -2147483648 +greatest_a_b 2015 +greatest_b_a 2015 +CREATE TABLE t2 AS +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `___________a` int(11) DEFAULT NULL, + `case_______a` int(11) DEFAULT NULL, + `case_____a_a` int(11) DEFAULT NULL, + `coalesce___a` int(11) DEFAULT NULL, + `coalesce_a_a` int(11) DEFAULT NULL, + `if_______a_a` int(11) DEFAULT NULL, + `ifnull___a_a` int(11) DEFAULT NULL, + `least____a_a` int(11) DEFAULT NULL, + `greatest_a_a` int(11) DEFAULT NULL, + `___________b` year(4) DEFAULT NULL, + `case_______b` int(4) unsigned DEFAULT NULL, + `case_____b_b` int(4) unsigned DEFAULT NULL, + `coalesce___b` int(4) unsigned DEFAULT NULL, + `coalesce_b_b` int(4) unsigned DEFAULT NULL, + `if_______b_b` int(4) unsigned DEFAULT NULL, + `ifnull___b_b` year(4) DEFAULT NULL, + `least____b_b` int(4) unsigned DEFAULT NULL, + `greatest_b_b` int(4) unsigned DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +CREATE TABLE t2 AS +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `case_____a_b` decimal(10,0) DEFAULT NULL, + `case_____b_a` decimal(10,0) DEFAULT NULL, + `coalesce_a_b` decimal(10,0) DEFAULT NULL, + `coalesce_b_a` decimal(10,0) DEFAULT NULL, + `if_______a_b` decimal(10,0) DEFAULT NULL, + `if_______b_a` decimal(10,0) DEFAULT NULL, + `ifnull___a_b` decimal(10,0) DEFAULT NULL, + `ifnull___b_a` decimal(10,0) DEFAULT NULL, + `least____a_b` decimal(10,0) DEFAULT NULL, + `least____b_a` decimal(10,0) DEFAULT NULL, + `greatest_a_b` decimal(10,0) DEFAULT NULL, + `greatest_b_a` decimal(10,0) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +DROP TABLE t1; +# +CREATE TABLE t1 (a INT UNSIGNED, b YEAR); +INSERT INTO t1 VALUES (4294967295,2015); +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def test t1 t1 a ___________a 3 10 10 Y 32800 0 63 +def case_______a 3 10 10 Y 32928 0 63 +def case_____a_a 3 10 10 Y 32928 0 63 +def coalesce___a 3 10 10 Y 32928 0 63 +def coalesce_a_a 3 10 10 Y 32928 0 63 +def if_______a_a 3 10 10 Y 32928 0 63 +def ifnull___a_a 3 10 10 Y 32928 0 63 +def least____a_a 3 10 10 Y 32928 0 63 +def greatest_a_a 3 10 10 Y 32928 0 63 +def test t1 t1 b ___________b 13 4 4 Y 32864 0 63 +def case_______b 13 4 4 Y 32928 0 63 +def case_____b_b 13 4 4 Y 32928 0 63 +def coalesce___b 13 4 4 Y 32928 0 63 +def coalesce_b_b 13 4 4 Y 32928 0 63 +def if_______b_b 13 4 4 Y 32928 0 63 +def ifnull___b_b 13 4 4 Y 32928 0 63 +def least____b_b 13 4 4 Y 32928 0 63 +def greatest_b_b 13 4 4 Y 32928 0 63 +___________a 4294967295 +case_______a 4294967295 +case_____a_a 4294967295 +coalesce___a 4294967295 +coalesce_a_a 4294967295 +if_______a_a 4294967295 +ifnull___a_a 4294967295 +least____a_a 4294967295 +greatest_a_a 4294967295 +___________b 2015 +case_______b 2015 +case_____b_b 2015 +coalesce___b 2015 +coalesce_b_b 2015 +if_______b_b 2015 +ifnull___b_b 2015 +least____b_b 2015 +greatest_b_b 2015 +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def case_____a_b 3 10 10 Y 32928 0 63 +def case_____b_a 3 10 4 Y 32928 0 63 +def coalesce_a_b 3 10 10 Y 32928 0 63 +def coalesce_b_a 3 10 4 Y 32928 0 63 +def if_______a_b 3 10 4 Y 32928 0 63 +def if_______b_a 3 10 10 Y 32928 0 63 +def ifnull___a_b 3 10 10 Y 32928 0 63 +def ifnull___b_a 3 10 4 Y 32928 0 63 +def least____a_b 3 10 4 Y 32928 0 63 +def least____b_a 3 10 4 Y 32928 0 63 +def greatest_a_b 3 10 10 Y 32928 0 63 +def greatest_b_a 3 10 10 Y 32928 0 63 +case_____a_b 4294967295 +case_____b_a 2015 +coalesce_a_b 4294967295 +coalesce_b_a 2015 +if_______a_b 2015 +if_______b_a 4294967295 +ifnull___a_b 4294967295 +ifnull___b_a 2015 +least____a_b 2015 +least____b_a 2015 +greatest_a_b 4294967295 +greatest_b_a 4294967295 +CREATE TABLE t2 AS +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `___________a` int(10) unsigned DEFAULT NULL, + `case_______a` int(10) unsigned DEFAULT NULL, + `case_____a_a` int(10) unsigned DEFAULT NULL, + `coalesce___a` int(10) unsigned DEFAULT NULL, + `coalesce_a_a` int(10) unsigned DEFAULT NULL, + `if_______a_a` int(10) unsigned DEFAULT NULL, + `ifnull___a_a` int(10) unsigned DEFAULT NULL, + `least____a_a` int(10) unsigned DEFAULT NULL, + `greatest_a_a` int(10) unsigned DEFAULT NULL, + `___________b` year(4) DEFAULT NULL, + `case_______b` int(4) unsigned DEFAULT NULL, + `case_____b_b` int(4) unsigned DEFAULT NULL, + `coalesce___b` int(4) unsigned DEFAULT NULL, + `coalesce_b_b` int(4) unsigned DEFAULT NULL, + `if_______b_b` int(4) unsigned DEFAULT NULL, + `ifnull___b_b` year(4) DEFAULT NULL, + `least____b_b` int(4) unsigned DEFAULT NULL, + `greatest_b_b` int(4) unsigned DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +CREATE TABLE t2 AS +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `case_____a_b` int(10) unsigned DEFAULT NULL, + `case_____b_a` int(10) unsigned DEFAULT NULL, + `coalesce_a_b` int(10) unsigned DEFAULT NULL, + `coalesce_b_a` int(10) unsigned DEFAULT NULL, + `if_______a_b` int(10) unsigned DEFAULT NULL, + `if_______b_a` int(10) unsigned DEFAULT NULL, + `ifnull___a_b` int(10) unsigned DEFAULT NULL, + `ifnull___b_a` int(10) unsigned DEFAULT NULL, + `least____a_b` int(10) unsigned DEFAULT NULL, + `least____b_a` int(10) unsigned DEFAULT NULL, + `greatest_a_b` int(10) unsigned DEFAULT NULL, + `greatest_b_a` int(10) unsigned DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +DROP TABLE t1; +# +# MDEV-8873 Wrong field type or metadata for LEAST(int_column,string_column) +# +CREATE TABLE t1 (a INT, b VARCHAR(10)); +INSERT INTO t1 VALUES (-2147483648,'100x'); +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def test t1 t1 a ___________a 3 11 11 Y 32768 0 63 +def case_______a 3 11 11 Y 32896 0 63 +def case_____a_a 3 11 11 Y 32896 0 63 +def coalesce___a 3 11 11 Y 32896 0 63 +def coalesce_a_a 3 11 11 Y 32896 0 63 +def if_______a_a 3 11 11 Y 32896 0 63 +def ifnull___a_a 3 11 11 Y 32896 0 63 +def least____a_a 3 11 11 Y 32896 0 63 +def greatest_a_a 3 11 11 Y 32896 0 63 +def test t1 t1 b ___________b 253 10 4 Y 0 0 8 +def case_______b 253 10 4 Y 0 31 8 +def case_____b_b 253 10 4 Y 0 31 8 +def coalesce___b 253 10 4 Y 0 31 8 +def coalesce_b_b 253 10 4 Y 0 31 8 +def if_______b_b 253 10 4 Y 0 31 8 +def ifnull___b_b 253 10 4 Y 0 31 8 +def least____b_b 253 10 4 Y 0 31 8 +def greatest_b_b 253 10 4 Y 0 31 8 +___________a -2147483648 +case_______a -2147483648 +case_____a_a -2147483648 +coalesce___a -2147483648 +coalesce_a_a -2147483648 +if_______a_a -2147483648 +ifnull___a_a -2147483648 +least____a_a -2147483648 +greatest_a_a -2147483648 +___________b 100x +case_______b 100x +case_____b_b 100x +coalesce___b 100x +coalesce_b_b 100x +if_______b_b 100x +ifnull___b_b 100x +least____b_b 100x +greatest_b_b 100x +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def case_____a_b 253 11 11 Y 0 31 8 +def case_____b_a 253 11 4 Y 0 31 8 +def coalesce_a_b 253 11 11 Y 0 31 8 +def coalesce_b_a 253 11 4 Y 0 31 8 +def if_______a_b 253 11 4 Y 0 31 8 +def if_______b_a 253 11 11 Y 0 31 8 +def ifnull___a_b 253 11 11 Y 0 31 8 +def ifnull___b_a 253 11 4 Y 0 31 8 +def least____a_b 5 23 11 Y 32896 31 63 +def least____b_a 5 23 11 Y 32896 31 63 +def greatest_a_b 5 23 3 Y 32896 31 63 +def greatest_b_a 5 23 3 Y 32896 31 63 +case_____a_b -2147483648 +case_____b_a 100x +coalesce_a_b -2147483648 +coalesce_b_a 100x +if_______a_b 100x +if_______b_a -2147483648 +ifnull___a_b -2147483648 +ifnull___b_a 100x +least____a_b -2147483648 +least____b_a -2147483648 +greatest_a_b 100 +greatest_b_a 100 +Warnings: +Level Warning +Code 1292 +Message Truncated incorrect DOUBLE value: '100x' +Level Warning +Code 1292 +Message Truncated incorrect DOUBLE value: '100x' +Level Warning +Code 1292 +Message Truncated incorrect DOUBLE value: '100x' +Level Warning +Code 1292 +Message Truncated incorrect DOUBLE value: '100x' +CREATE TABLE t2 AS +SELECT +a AS ___________a, +CASE WHEN a IS NOT NULL THEN a END AS case_______a, +CASE WHEN a IS NOT NULL THEN a ELSE a END AS case_____a_a, +COALESCE(a) AS coalesce___a, +COALESCE(a, a) AS coalesce_a_a, +IF(a IS NULL, a, a) AS if_______a_a, +IFNULL(a, a) AS ifnull___a_a, +LEAST(a, a) AS least____a_a, +GREATEST(a, a) AS greatest_a_a, +b AS ___________b, +CASE WHEN a IS NOT NULL THEN b END AS case_______b, +CASE WHEN a IS NOT NULL THEN b ELSE b END AS case_____b_b, +COALESCE(b) AS coalesce___b, +COALESCE(b, b) AS coalesce_b_b, +IF(a IS NULL, b, b) AS if_______b_b, +IFNULL(b, b) AS ifnull___b_b, +LEAST(b, b) AS least____b_b, +GREATEST(b, b) AS greatest_b_b +FROM t1; +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `___________a` int(11) DEFAULT NULL, + `case_______a` int(11) DEFAULT NULL, + `case_____a_a` int(11) DEFAULT NULL, + `coalesce___a` int(11) DEFAULT NULL, + `coalesce_a_a` int(11) DEFAULT NULL, + `if_______a_a` int(11) DEFAULT NULL, + `ifnull___a_a` int(11) DEFAULT NULL, + `least____a_a` int(11) DEFAULT NULL, + `greatest_a_a` int(11) DEFAULT NULL, + `___________b` varchar(10) DEFAULT NULL, + `case_______b` varchar(10) DEFAULT NULL, + `case_____b_b` varchar(10) DEFAULT NULL, + `coalesce___b` varchar(10) DEFAULT NULL, + `coalesce_b_b` varchar(10) DEFAULT NULL, + `if_______b_b` varchar(10) DEFAULT NULL, + `ifnull___b_b` varchar(10) DEFAULT NULL, + `least____b_b` varchar(10) DEFAULT NULL, + `greatest_b_b` varchar(10) DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +CREATE TABLE t2 AS +SELECT +CASE WHEN a IS NOT NULL THEN a ELSE b END AS case_____a_b, +CASE WHEN a IS NOT NULL THEN b ELSE a END AS case_____b_a, +COALESCE(a, b) AS coalesce_a_b, +COALESCE(b, a) AS coalesce_b_a, +IF(a IS NULL, a, b) AS if_______a_b, +IF(a IS NULL, b, a) AS if_______b_a, +IFNULL(a, b) AS ifnull___a_b, +IFNULL(b, a) AS ifnull___b_a, +LEAST(a, b) AS least____a_b, +LEAST(b, a) AS least____b_a, +GREATEST(a, b) AS greatest_a_b, +GREATEST(b, a) AS greatest_b_a +FROM t1; +Warnings: +Warning 1292 Truncated incorrect DOUBLE value: '100x' +Warning 1292 Truncated incorrect DOUBLE value: '100x' +Warning 1292 Truncated incorrect DOUBLE value: '100x' +Warning 1292 Truncated incorrect DOUBLE value: '100x' +SHOW CREATE TABLE t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `case_____a_b` varchar(11) DEFAULT NULL, + `case_____b_a` varchar(11) DEFAULT NULL, + `coalesce_a_b` varchar(11) DEFAULT NULL, + `coalesce_b_a` varchar(11) DEFAULT NULL, + `if_______a_b` varchar(11) DEFAULT NULL, + `if_______b_a` varchar(11) DEFAULT NULL, + `ifnull___a_b` varchar(11) DEFAULT NULL, + `ifnull___b_a` varchar(11) DEFAULT NULL, + `least____a_b` double DEFAULT NULL, + `least____b_a` double DEFAULT NULL, + `greatest_a_b` double DEFAULT NULL, + `greatest_b_a` double DEFAULT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t2; +DROP TABLE t1; +# +# MDEV-4848 Wrong metadata or column type for LEAST(1.0,'10') +# +SELECT LEAST(1.0,'10'); +Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr +def LEAST(1.0,'10') 5 23 1 N 32897 31 63 +LEAST(1.0,'10') +1 +CREATE TABLE t1 AS SELECT LEAST(1.0,'10'); +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `LEAST(1.0,'10')` double NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +DROP TABLE t1; +# # End of 10.1 tests # diff --git a/mysql-test/t/func_hybrid_type.test b/mysql-test/t/func_hybrid_type.test index 55bfead668c..5cd0c6ecade 100644 --- a/mysql-test/t/func_hybrid_type.test +++ b/mysql-test/t/func_hybrid_type.test @@ -57,6 +57,50 @@ INSERT INTO t1 VALUES (0x32,0x32); --source include/func_hybrid_type.inc DROP TABLE t1; +--echo # +CREATE TABLE t1 (a FLOAT, b SMALLINT); +INSERT INTO t1 VALUES (1,-32678); +--source include/func_hybrid_type.inc +DROP TABLE t1; + +--echo # +CREATE TABLE t1 (a VARCHAR(10), b ENUM('b')); +INSERT INTO t1 VALUES ('a','b'); +--source include/func_hybrid_type.inc +DROP TABLE t1; + +--echo # +CREATE TABLE t1 (a INT, b YEAR); +INSERT INTO t1 VALUES (-2147483648,2015); +--source include/func_hybrid_type.inc +DROP TABLE t1; + +--echo # +CREATE TABLE t1 (a INT UNSIGNED, b YEAR); +INSERT INTO t1 VALUES (4294967295,2015); +--source include/func_hybrid_type.inc +DROP TABLE t1; + + +--echo # +--echo # MDEV-8873 Wrong field type or metadata for LEAST(int_column,string_column) +--echo # +CREATE TABLE t1 (a INT, b VARCHAR(10)); +INSERT INTO t1 VALUES (-2147483648,'100x'); +--source include/func_hybrid_type.inc +DROP TABLE t1; + +--echo # +--echo # MDEV-4848 Wrong metadata or column type for LEAST(1.0,'10') +--echo # +--disable_ps_protocol +--enable_metadata +SELECT LEAST(1.0,'10'); +--disable_metadata +--enable_ps_protocol +CREATE TABLE t1 AS SELECT LEAST(1.0,'10'); +SHOW CREATE TABLE t1; +DROP TABLE t1; --echo # --echo # End of 10.1 tests diff --git a/sql/item_func.cc b/sql/item_func.cc index 7155f1d4ade..9e98b80bdad 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -2833,7 +2833,8 @@ void Item_func_min_max::fix_length_and_dec() maybe_null=0; thd= current_thd; compare_as_dates= find_date_time_item(args, arg_count, 0); - cmp_type=args[0]->result_type(); + Item_result tmp_cmp_type= args[0]->cmp_type(); + uint string_type_count= 0; for (uint i=0 ; i < arg_count ; i++) { @@ -2843,14 +2844,39 @@ void Item_func_min_max::fix_length_and_dec() unsigned_count+= args[i]->unsigned_flag; if (args[i]->maybe_null) maybe_null= 1; - cmp_type= item_cmp_type(cmp_type,args[i]->result_type()); + tmp_cmp_type= item_cmp_type(tmp_cmp_type, args[i]->cmp_type()); + string_type_count+= args[i]->cmp_type() == STRING_RESULT; } unsigned_flag= unsigned_count == arg_count; // if all args are unsigned - if (cmp_type == STRING_RESULT) + + switch (tmp_cmp_type) { + case TIME_RESULT: + // At least one temporal argument was found. + collation.set_numeric(); + set_handler_by_field_type(compare_as_dates->field_type()); + if (mysql_type_to_time_type(Item_func_min_max::field_type()) == + MYSQL_TIMESTAMP_DATE) + decimals= 0; + else + set_if_smaller(decimals, TIME_SECOND_PART_DIGITS); + break; + + case STRING_RESULT: + /* + All arguments are of string-alike types: + CHAR, VARCHAR, TEXT, BINARY, VARBINARY, BLOB, SET, ENUM + No numeric and no temporal types were found. + */ agg_arg_charsets_for_string_result_with_comparison(collation, args, arg_count); - else if (cmp_type == INT_RESULT) - { + set_handler_by_field_type(agg_field_type(args, arg_count, false)); + break; + + case INT_RESULT: + /* + All arguments have INT-alike types: + TINY, SHORT, LONG, LONGLONG, INT24, YEAR, BIT. + */ collation.set_numeric(); fix_char_length(my_decimal_precision_to_length_no_truncation(max_int_part + decimals, @@ -2862,32 +2888,45 @@ void Item_func_min_max::fix_length_and_dec() If all args are of INT-alike type, but have different unsigned_flag, then change type to DECIMAL. */ - cmp_type= DECIMAL_RESULT; - cached_field_type= MYSQL_TYPE_NEWDECIMAL; - return; + set_handler_by_field_type(MYSQL_TYPE_NEWDECIMAL); } - } - else if (cmp_type == DECIMAL_RESULT) - { + else + { + /* + There are only INT-alike arguments with equal unsigned_flag. + Aggregate types to get the best covering type. + Treat BIT as LONGLONG when aggregating to non-BIT types. + Possible final type: TINY, SHORT, LONG, LONGLONG, INT24, YEAR, BIT. + */ + set_handler_by_field_type(agg_field_type(args, arg_count, true)); + } + break; + + case DECIMAL_RESULT: + // All arguments are of DECIMAL type collation.set_numeric(); fix_char_length(my_decimal_precision_to_length_no_truncation(max_int_part + decimals, decimals, unsigned_flag)); - } - else if (cmp_type == REAL_RESULT) - fix_char_length(float_length(decimals)); + set_handler_by_field_type(MYSQL_TYPE_NEWDECIMAL); + break; - if (compare_as_dates) - { - cached_field_type= compare_as_dates->field_type(); - if (mysql_type_to_time_type(cached_field_type) == MYSQL_TIMESTAMP_DATE) - decimals= 0; - else - set_if_smaller(decimals, TIME_SECOND_PART_DIGITS); + case ROW_RESULT: + DBUG_ASSERT(0); + // Pass through + case REAL_RESULT: + collation.set_numeric(); + fix_char_length(float_length(decimals)); + /* + Set type to DOUBLE, as Item_func::tmp_table_field() does not + distinguish between DOUBLE and FLOAT and always creates Field_double. + Perhaps we should eventually change this to use agg_field_type() here, + and fix Item_func::tmp_table_field() to create Field_float when possible. + */ + set_handler_by_field_type(MYSQL_TYPE_DOUBLE); + break; } - else - cached_field_type= agg_field_type(args, arg_count, false); } @@ -2964,7 +3003,7 @@ String *Item_func_min_max::val_str(String *str) DBUG_ASSERT(fixed == 1); if (compare_as_dates) return val_string_from_date(str); - switch (cmp_type) { + switch (Item_func_min_max::result_type()) { case INT_RESULT: return val_string_from_int(str); case DECIMAL_RESULT: diff --git a/sql/item_func.h b/sql/item_func.h index 74a18c1edfe..eda1572d610 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -986,19 +986,28 @@ public: }; -class Item_func_min_max :public Item_func +/** + Item_func_min_max does not derive from Item_func_hybrid_field_type + because the way how its methods val_xxx() and get_date() work depend + not only by its arguments, but also on the context in which + LEAST() and GREATEST() appear. + For example, using Item_func_min_max in a CAST like this: + CAST(LEAST('11','2') AS SIGNED) + forces Item_func_min_max to compare the arguments as numbers rather + than strings. + Perhaps this should be changed eventually (see MDEV-5893). +*/ +class Item_func_min_max :public Item_func, + public Type_handler_hybrid_field_type { - Item_result cmp_type; String tmp_value; int cmp_sign; /* An item used for issuing warnings while string to DATETIME conversion. */ Item *compare_as_dates; THD *thd; -protected: - enum_field_types cached_field_type; public: Item_func_min_max(THD *thd, List<Item> &list, int cmp_sign_arg): - Item_func(thd, list), cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), + Item_func(thd, list), cmp_sign(cmp_sign_arg), compare_as_dates(0) {} double val_real(); longlong val_int(); @@ -1006,8 +1015,12 @@ public: my_decimal *val_decimal(my_decimal *); bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date); void fix_length_and_dec(); - enum Item_result result_type () const { return cmp_type; } - enum_field_types field_type() const { return cached_field_type; } + enum Item_result cmp_type() const + { return Type_handler_hybrid_field_type::cmp_type(); } + enum Item_result result_type() const + { return Type_handler_hybrid_field_type::result_type(); } + enum_field_types field_type() const + { return Type_handler_hybrid_field_type::field_type(); } }; class Item_func_min :public Item_func_min_max |