Commit aa6785d7 authored by unknown's avatar unknown

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
parent 77c163c3
...@@ -637,8 +637,22 @@ create table t1 (a char(10)); ...@@ -637,8 +637,22 @@ create table t1 (a char(10));
insert into t1 values ('a'),('b'),('c'); insert into t1 values ('a'),('b'),('c');
select coercibility(max(a)) from t1; select coercibility(max(a)) from t1;
coercibility(max(a)) coercibility(max(a))
3 2
drop table t1; 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); create table t1 (a int);
insert into t1 values (1); insert into t1 values (1);
select max(a) as b from t1 having b=1; select max(a) as b from t1 having b=1;
......
...@@ -614,6 +614,20 @@ t1 CREATE TABLE `t1` ( ...@@ -614,6 +614,20 @@ t1 CREATE TABLE `t1` (
`encode('abcd','ab')` binary(4) NOT NULL default '' `encode('abcd','ab')` binary(4) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1; 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); select SUBSTR('abcdefg',3,2);
SUBSTR('abcdefg',3,2) SUBSTR('abcdefg',3,2)
cd cd
......
...@@ -383,6 +383,17 @@ insert into t1 values ('a'),('b'),('c'); ...@@ -383,6 +383,17 @@ insert into t1 values ('a'),('b'),('c');
select coercibility(max(a)) from t1; select coercibility(max(a)) from t1;
drop table 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 # aggregate functions on static tables
# #
......
...@@ -350,6 +350,7 @@ void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array, ...@@ -350,6 +350,7 @@ void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
{ {
uint el= fields.elements; uint el= fields.elements;
Item *new_item= new Item_ref(ref_pointer_array + el, 0, item->name); Item *new_item= new Item_ref(ref_pointer_array + el, 0, item->name);
new_item->collation.set(item->collation);
fields.push_front(item); fields.push_front(item);
ref_pointer_array[el]= item; ref_pointer_array[el]= item;
thd->change_item_tree(arg, new_item); thd->change_item_tree(arg, new_item);
......
...@@ -218,16 +218,13 @@ Item_sum_hybrid::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) ...@@ -218,16 +218,13 @@ Item_sum_hybrid::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref)
hybrid_type= item->result_type(); hybrid_type= item->result_type();
if (hybrid_type == INT_RESULT) if (hybrid_type == INT_RESULT)
{ {
cmp_charset= &my_charset_bin;
max_length=20; max_length=20;
} }
else if (hybrid_type == REAL_RESULT) else if (hybrid_type == REAL_RESULT)
{ {
cmp_charset= &my_charset_bin;
max_length=float_length(decimals); max_length=float_length(decimals);
}else }else
{ {
cmp_charset= item->collation.collation;
max_length=item->max_length; max_length=item->max_length;
} }
decimals=item->decimals; decimals=item->decimals;
...@@ -557,7 +554,7 @@ bool Item_sum_min::add() ...@@ -557,7 +554,7 @@ bool Item_sum_min::add()
{ {
String *result=args[0]->val_str(&tmp_value); String *result=args[0]->val_str(&tmp_value);
if (!args[0]->null_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); value.copy(*result);
null_value=0; null_value=0;
...@@ -610,7 +607,7 @@ bool Item_sum_max::add() ...@@ -610,7 +607,7 @@ bool Item_sum_max::add()
{ {
String *result=args[0]->val_str(&tmp_value); String *result=args[0]->val_str(&tmp_value);
if (!args[0]->null_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); value.copy(*result);
null_value=0; null_value=0;
...@@ -921,7 +918,7 @@ Item_sum_hybrid::min_max_update_str_field() ...@@ -921,7 +918,7 @@ Item_sum_hybrid::min_max_update_str_field()
result_field->val_str(&tmp_value); result_field->val_str(&tmp_value);
if (result_field->is_null() || 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->store(res_str->ptr(),res_str->length(),res_str->charset());
result_field->set_notnull(); result_field->set_notnull();
} }
......
...@@ -402,20 +402,19 @@ class Item_sum_hybrid :public Item_sum ...@@ -402,20 +402,19 @@ class Item_sum_hybrid :public Item_sum
enum_field_types hybrid_field_type; enum_field_types hybrid_field_type;
int cmp_sign; int cmp_sign;
table_map used_table_cache; table_map used_table_cache;
CHARSET_INFO *cmp_charset;
public: public:
Item_sum_hybrid(Item *item_par,int sign) Item_sum_hybrid(Item *item_par,int sign)
:Item_sum(item_par), sum(0.0), sum_int(0), :Item_sum(item_par), sum(0.0), sum_int(0),
hybrid_type(INT_RESULT), hybrid_field_type(FIELD_TYPE_LONGLONG), hybrid_type(INT_RESULT), hybrid_field_type(FIELD_TYPE_LONGLONG),
cmp_sign(sign), used_table_cache(~(table_map) 0), cmp_sign(sign), used_table_cache(~(table_map) 0)
cmp_charset(&my_charset_bin) { collation.set(&my_charset_bin); }
{}
Item_sum_hybrid(THD *thd, Item_sum_hybrid *item): Item_sum_hybrid(THD *thd, Item_sum_hybrid *item):
Item_sum(thd, item), value(item->value), Item_sum(thd, item), value(item->value),
sum(item->sum), sum_int(item->sum_int), hybrid_type(item->hybrid_type), 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), 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 **); bool fix_fields(THD *, TABLE_LIST *, Item **);
table_map used_tables() const { return used_table_cache; } table_map used_tables() const { return used_table_cache; }
bool const_item() const { return !used_table_cache; } bool const_item() const { return !used_table_cache; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment