From 47f8a473bd1179c7622deab59b491f5a9d91b211 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 11 Oct 2004 18:38:48 +0500 Subject: [PATCH] A fix (bug #5615 type of aggregate function column wrong when using group by). mysql-test/r/func_group.result: test case (bug #5615 type of aggregate function column wrong when using group by) mysql-test/t/func_group.test: test case (bug #5615 type of aggregate function column wrong when using group by) sql/sql_select.cc: A fix (bug #5615 type of aggregate function column wrong when using group by): should create a temporary field of the proper type in case of MIN|MAX(field). --- mysql-test/r/func_group.result | 9 +++++++++ mysql-test/t/func_group.test | 10 ++++++++++ sql/sql_select.cc | 9 +++++++++ 3 files changed, 28 insertions(+) diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result index c25f89d4df3..ecf6422261f 100644 --- a/mysql-test/r/func_group.result +++ b/mysql-test/r/func_group.result @@ -719,3 +719,12 @@ one 2 two 2 three 1 drop table t1; +create table t1(a int, b datetime); +insert into t1 values (1, NOW()), (2, NOW()); +create table t2 select MAX(b) from t1 group by a; +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `MAX(b)` datetime default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t1, t2; diff --git a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test index 3e001961f90..ecd3bf97f0b 100644 --- a/mysql-test/t/func_group.test +++ b/mysql-test/t/func_group.test @@ -458,3 +458,13 @@ INSERT INTO t1 VALUES select val, count(*) from t1 group by val; drop table t1; + +# +# Bug #5615: type of aggregate function column wrong when using group by +# + +create table t1(a int, b datetime); +insert into t1 values (1, NOW()), (2, NOW()); +create table t2 select MAX(b) from t1 group by a; +show create table t2; +drop table t1, t2; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 638ed229a70..7ffef151457 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -4744,6 +4744,15 @@ Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, item->name,table,item_sum->decimals); case Item_sum::UNIQUE_USERS_FUNC: return new Field_long(9,maybe_null,item->name,table,1); + case Item_sum::MIN_FUNC: + case Item_sum::MAX_FUNC: + if (item_sum->args[0]->type() == Item::FIELD_ITEM) + { + *from_field= ((Item_field*) item_sum->args[0])->field; + return create_tmp_field_from_field(thd, *from_field, item, table, + modify_item, convert_blob_length); + } + /* fall through */ default: switch (item_sum->result_type()) { case REAL_RESULT: