diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index 00436019f85..a6ad95570f8 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -512,3 +512,24 @@ id IFNULL(dsc, '-') 2 line number two 3 line number three drop table t1; +CREATE TABLE t1 ( +ID int(11) NOT NULL auto_increment, +x varchar(20) default NULL, +y decimal(10,0) default NULL, +PRIMARY KEY (ID), +KEY (y) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; +INSERT INTO t1 VALUES +(1,'ba','-1'), +(2,'ba','1150'), +(306,'ba','-1'), +(307,'ba','1150'), +(611,'ba','-1'), +(612,'ba','1150'); +select count(distinct x,y) from t1; +count(distinct x,y) +2 +select count(distinct concat(x,y)) from t1; +count(distinct concat(x,y)) +2 +drop table t1; diff --git a/mysql-test/t/distinct.test b/mysql-test/t/distinct.test index 6483284633f..45bd0c7a51c 100644 --- a/mysql-test/t/distinct.test +++ b/mysql-test/t/distinct.test @@ -358,3 +358,27 @@ select distinct id, IFNULL(dsc, '-') from t1; drop table t1; # End of 4.1 tests + + +# +# Bug #15745 ( COUNT(DISTINCT CONCAT(x,y)) returns wrong result) +# +CREATE TABLE t1 ( + ID int(11) NOT NULL auto_increment, + x varchar(20) default NULL, + y decimal(10,0) default NULL, + PRIMARY KEY (ID), + KEY (y) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +INSERT INTO t1 VALUES +(1,'ba','-1'), +(2,'ba','1150'), +(306,'ba','-1'), +(307,'ba','1150'), +(611,'ba','-1'), +(612,'ba','1150'); + +select count(distinct x,y) from t1; +select count(distinct concat(x,y)) from t1; +drop table t1; diff --git a/sql/item_sum.cc b/sql/item_sum.cc index a3a25ec8d6f..804adcd022d 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -2541,9 +2541,9 @@ bool Item_sum_count_distinct::setup(THD *thd) Field *f= *field; enum enum_field_types type= f->type(); tree_key_length+= f->pack_length(); - if (!f->binary() && (type == MYSQL_TYPE_STRING || - type == MYSQL_TYPE_VAR_STRING || - type == MYSQL_TYPE_VARCHAR)) + if ((type == MYSQL_TYPE_VARCHAR) || + !f->binary() && (type == MYSQL_TYPE_STRING || + type == MYSQL_TYPE_VAR_STRING)) { all_binary= FALSE; break; diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 1ed8887a878..49b0c70ff03 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -223,7 +223,7 @@ static int find_keyword(LEX *lex, uint len, bool function) SYNOPSIS is_keyword() - name checked name + name checked name (must not be empty) len length of checked name RETURN VALUES @@ -233,6 +233,7 @@ static int find_keyword(LEX *lex, uint len, bool function) bool is_keyword(const char *name, uint len) { + DBUG_ASSERT(len != 0); return get_hash_symbol(name,len,0)!=0; } diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 8920efa87ab..5c9d13cac3e 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -713,7 +713,8 @@ append_identifier(THD *thd, String *packet, const char *name, uint length) int get_quote_char_for_identifier(THD *thd, const char *name, uint length) { - if (!is_keyword(name,length) && + if (!length || + !is_keyword(name,length) && !require_quotes(name, length) && !(thd->options & OPTION_QUOTE_SHOW_CREATE)) return EOF;