diff --git a/sql/sql_select.cc b/sql/sql_select.cc index d8116b20cbe..8a251ae339e 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -3274,9 +3274,17 @@ bool JOIN::make_aggr_tables_info() if (ht && ht->create_group_by) { - /* Check if the storage engine can intercept the query */ - Query query= {&all_fields, select_distinct, tables_list, conds, - group_list, order ? order : group_list, having}; + /* + Check if the storage engine can intercept the query. + + JOIN::optimize_stage2() might convert DISTINCT into GROUP BY and then + optimize away GROUP BY (group_list). In such a case, we need to notify + a storage engine supporting a group by handler of the existence of the + original DISTINCT. Thus, we set select_distinct || group_optimized_away + to Query::distinct. + */ + Query query= {&all_fields, select_distinct || group_optimized_away, tables_list, + conds, group_list, order ? order : group_list, having}; group_by_handler *gbh= ht->create_group_by(thd, &query); if (gbh) diff --git a/storage/spider/mysql-test/spider/bugfix/r/mdev_26013.result b/storage/spider/mysql-test/spider/bugfix/r/mdev_26013.result new file mode 100644 index 00000000000..3af1f7df0a7 --- /dev/null +++ b/storage/spider/mysql-test/spider/bugfix/r/mdev_26013.result @@ -0,0 +1,42 @@ +for master_1 +for child2 +child2_1 +child2_2 +child2_3 +for child3 + +MDEV-26013 distinct not work properly in some cases for spider tables + +connection child2_1; +CREATE DATABASE auto_test_remote; +USE auto_test_remote; +CREATE TABLE tbl_a ( +`a`int, +`b`int, +PRIMARY KEY (`a`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +insert into `tbl_a` VALUES (1,999), (2,999); +connection master_1; +CREATE DATABASE auto_test_remote; +USE auto_test_remote; +CREATE TABLE tbl_a ( +`a`int, +`b`int, +PRIMARY KEY (`a`) +) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='table "tbl_a"' PARTITION BY LIST COLUMNS(`a`) ( +PARTITION `pt1` DEFAULT COMMENT = 'srv "s_2_1"' +); +connection master_1; +SELECT distinct b FROM tbl_a WHERE b=999; +b +999 +connection master_1; +DROP DATABASE IF EXISTS auto_test_remote; +connection child2_1; +DROP DATABASE IF EXISTS auto_test_remote; +for master_1 +for child2 +child2_1 +child2_2 +child2_3 +for child3 diff --git a/storage/spider/mysql-test/spider/bugfix/t/mdev_26013.cnf b/storage/spider/mysql-test/spider/bugfix/t/mdev_26013.cnf new file mode 100644 index 00000000000..05dfd8a0bce --- /dev/null +++ b/storage/spider/mysql-test/spider/bugfix/t/mdev_26013.cnf @@ -0,0 +1,3 @@ +!include include/default_mysqld.cnf +!include ../my_1_1.cnf +!include ../my_2_1.cnf diff --git a/storage/spider/mysql-test/spider/bugfix/t/mdev_26013.test b/storage/spider/mysql-test/spider/bugfix/t/mdev_26013.test new file mode 100644 index 00000000000..e31041c3bf1 --- /dev/null +++ b/storage/spider/mysql-test/spider/bugfix/t/mdev_26013.test @@ -0,0 +1,51 @@ +--disable_warnings +--disable_query_log +--disable_result_log +--source ../../t/test_init.inc +--enable_result_log +--enable_query_log +--enable_warnings + +--echo +--echo MDEV-26013 distinct not work properly in some cases for spider tables +--echo + +--connection child2_1 +CREATE DATABASE auto_test_remote; +USE auto_test_remote; + +eval CREATE TABLE tbl_a ( + `a`int, + `b`int, + PRIMARY KEY (`a`) +) $CHILD2_1_ENGINE $CHILD2_1_CHARSET; + +insert into `tbl_a` VALUES (1,999), (2,999); + +--connection master_1 +CREATE DATABASE auto_test_remote; +USE auto_test_remote; + +eval CREATE TABLE tbl_a ( + `a`int, + `b`int, + PRIMARY KEY (`a`) +) $MASTER_1_ENGINE $MASTER_1_CHARSET COMMENT='table "tbl_a"' PARTITION BY LIST COLUMNS(`a`) ( + PARTITION `pt1` DEFAULT COMMENT = 'srv "s_2_1"' +); + +--connection master_1 +SELECT distinct b FROM tbl_a WHERE b=999; + +--connection master_1 +DROP DATABASE IF EXISTS auto_test_remote; +--connection child2_1 +DROP DATABASE IF EXISTS auto_test_remote; + +--disable_warnings +--disable_query_log +--disable_result_log +--source ../../t/test_deinit.inc +--enable_result_log +--enable_query_log +--enable_warnings