From aa6785d7a8a8c70ff9335bb4c875d3ae9db9db6a Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 19 Nov 2004 19:35:36 +0400 Subject: [PATCH] Bug #6658 MAX(column) returns incorrect coercibility Also, Item_sum_hybrid->charset was removed as redundant, and switched to use collation.collation instead. mysql-test/r/func_group.result: Bug #6658 MAX(column) returns incorrect coercibility mysql-test/r/func_str.result: Bug #6658 MAX(column) returns incorrect coercibility mysql-test/t/func_group.test: Bug #6658 MAX(column) returns incorrect coercibility sql/item_func.cc: Bug #6658 MAX(column) returns incorrect coercibility sql/item_sum.cc: Bug #6658 MAX(column) returns incorrect coercibility sql/item_sum.h: Bug #6658 MAX(column) returns incorrect coercibility --- mysql-test/r/func_group.result | 16 +++++++++++++++- mysql-test/r/func_str.result | 14 ++++++++++++++ mysql-test/t/func_group.test | 11 +++++++++++ sql/item_func.cc | 1 + sql/item_sum.cc | 9 +++------ sql/item_sum.h | 9 ++++----- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result index c25f89d4df3..4bb79a1cb41 100644 --- a/mysql-test/r/func_group.result +++ b/mysql-test/r/func_group.result @@ -637,8 +637,22 @@ create table t1 (a char(10)); insert into t1 values ('a'),('b'),('c'); select coercibility(max(a)) from t1; coercibility(max(a)) -3 +2 drop table t1; +create table t1 (a char character set latin2); +insert into t1 values ('a'),('b'); +select charset(max(a)), coercibility(max(a)), +charset(min(a)), coercibility(min(a)) from t1; +charset(max(a)) coercibility(max(a)) charset(min(a)) coercibility(min(a)) +latin2 2 latin2 2 +create table t2 select max(a),min(a) from t1; +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `max(a)` char(1) character set latin2 default NULL, + `min(a)` char(1) character set latin2 default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t2,t1; create table t1 (a int); insert into t1 values (1); select max(a) as b from t1 having b=1; diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result index 8d49d55be39..083f85ee6fa 100644 --- a/mysql-test/r/func_str.result +++ b/mysql-test/r/func_str.result @@ -614,6 +614,20 @@ t1 CREATE TABLE `t1` ( `encode('abcd','ab')` binary(4) NOT NULL default '' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 drop table t1; +create table t1 (a char character set latin2); +insert into t1 values ('a'),('b'); +select charset(max(a)), coercibility(max(a)), +charset(min(a)), coercibility(min(a)) from t1; +charset(max(a)) coercibility(max(a)) charset(min(a)) coercibility(min(a)) +latin2 2 latin2 2 +create table t2 select max(a),min(a) from t1; +show create table t2; +Table Create Table +t2 CREATE TABLE `t2` ( + `max(a)` char(1) character set latin2 default NULL, + `min(a)` char(1) character set latin2 default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1 +drop table t2,t1; select SUBSTR('abcdefg',3,2); SUBSTR('abcdefg',3,2) cd diff --git a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test index 8d8779e9d1b..79d6112e6de 100644 --- a/mysql-test/t/func_group.test +++ b/mysql-test/t/func_group.test @@ -383,6 +383,17 @@ insert into t1 values ('a'),('b'),('c'); select coercibility(max(a)) from t1; drop table t1; +# +# Bug #6658 MAX(column) returns incorrect coercibility +# +create table t1 (a char character set latin2); +insert into t1 values ('a'),('b'); +select charset(max(a)), coercibility(max(a)), + charset(min(a)), coercibility(min(a)) from t1; +create table t2 select max(a),min(a) from t1; +show create table t2; +drop table t2,t1; + # # aggregate functions on static tables # diff --git a/sql/item_func.cc b/sql/item_func.cc index c07e9f23ea2..3fb5bcd01c6 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -350,6 +350,7 @@ void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array, { uint el= fields.elements; Item *new_item= new Item_ref(ref_pointer_array + el, 0, item->name); + new_item->collation.set(item->collation); fields.push_front(item); ref_pointer_array[el]= item; thd->change_item_tree(arg, new_item); diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 3b3a6083725..c43a7d87f8f 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -218,16 +218,13 @@ Item_sum_hybrid::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) hybrid_type= item->result_type(); if (hybrid_type == INT_RESULT) { - cmp_charset= &my_charset_bin; max_length=20; } else if (hybrid_type == REAL_RESULT) { - cmp_charset= &my_charset_bin; max_length=float_length(decimals); }else { - cmp_charset= item->collation.collation; max_length=item->max_length; } decimals=item->decimals; @@ -557,7 +554,7 @@ bool Item_sum_min::add() { String *result=args[0]->val_str(&tmp_value); if (!args[0]->null_value && - (null_value || sortcmp(&value,result,cmp_charset) > 0)) + (null_value || sortcmp(&value,result,collation.collation) > 0)) { value.copy(*result); null_value=0; @@ -610,7 +607,7 @@ bool Item_sum_max::add() { String *result=args[0]->val_str(&tmp_value); if (!args[0]->null_value && - (null_value || sortcmp(&value,result,cmp_charset) < 0)) + (null_value || sortcmp(&value,result,collation.collation) < 0)) { value.copy(*result); null_value=0; @@ -921,7 +918,7 @@ Item_sum_hybrid::min_max_update_str_field() result_field->val_str(&tmp_value); if (result_field->is_null() || - (cmp_sign * sortcmp(res_str,&tmp_value,cmp_charset)) < 0) + (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0) result_field->store(res_str->ptr(),res_str->length(),res_str->charset()); result_field->set_notnull(); } diff --git a/sql/item_sum.h b/sql/item_sum.h index 5aa0d37190b..f9c48304795 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -402,20 +402,19 @@ class Item_sum_hybrid :public Item_sum enum_field_types hybrid_field_type; int cmp_sign; table_map used_table_cache; - CHARSET_INFO *cmp_charset; public: Item_sum_hybrid(Item *item_par,int sign) :Item_sum(item_par), sum(0.0), sum_int(0), hybrid_type(INT_RESULT), hybrid_field_type(FIELD_TYPE_LONGLONG), - cmp_sign(sign), used_table_cache(~(table_map) 0), - cmp_charset(&my_charset_bin) - {} + cmp_sign(sign), used_table_cache(~(table_map) 0) + { collation.set(&my_charset_bin); } Item_sum_hybrid(THD *thd, Item_sum_hybrid *item): Item_sum(thd, item), value(item->value), sum(item->sum), sum_int(item->sum_int), hybrid_type(item->hybrid_type), hybrid_field_type(item->hybrid_field_type),cmp_sign(item->cmp_sign), - used_table_cache(item->used_table_cache), cmp_charset(item->cmp_charset) {} + used_table_cache(item->used_table_cache) + { collation.set(item->collation); } bool fix_fields(THD *, TABLE_LIST *, Item **); table_map used_tables() const { return used_table_cache; } bool const_item() const { return !used_table_cache; }