Skip to content

Commit aafb888

Browse files
MDEV-26013 distinct not work properly in some cases for spider tables
The bug is caused by the following reasons: * spider_group_by_handler::init_scan() generates a query for a data node. * The function adds DISTINCT if and only if spider_group_by_handler::query::distinct is TRUE. * spider_group_by_handler::query::distinct is set to the value of JOIN::select_distinct in JOIN::make_aggr_tables_info(). * In the test case, DISTINCT is not added because JOIN::select_distinct is FALSE at the call of JOIN::make_aggr_tables_info(). Why JOIN::select_distinct is set to FALSE? That is because the function JOIN::optimize_stage2() convert DISTINCT into GROUP BY and then optimizes away GROUP BY.
1 parent 7ffa801 commit aafb888

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

sql/sql_select.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,9 +3274,17 @@ bool JOIN::make_aggr_tables_info()
32743274

32753275
if (ht && ht->create_group_by)
32763276
{
3277-
/* Check if the storage engine can intercept the query */
3278-
Query query= {&all_fields, select_distinct, tables_list, conds,
3279-
group_list, order ? order : group_list, having};
3277+
/*
3278+
Check if the storage engine can intercept the query.
3279+
3280+
JOIN::optimize_stage2() might convert DISTINCT into GROUP BY and then
3281+
optimize away GROUP BY (group_list). In such a case, we need to notify
3282+
a storage engine supporting a group by handler of the existence of the
3283+
original DISTINCT. Thus, we set select_distinct || group_optimized_away
3284+
to Query::distinct.
3285+
*/
3286+
Query query= {&all_fields, select_distinct || group_optimized_away, tables_list,
3287+
conds, group_list, order ? order : group_list, having};
32803288
group_by_handler *gbh= ht->create_group_by(thd, &query);
32813289

32823290
if (gbh)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
for master_1
2+
for child2
3+
child2_1
4+
child2_2
5+
child2_3
6+
for child3
7+
8+
MDEV-26013 distinct not work properly in some cases for spider tables
9+
10+
connection child2_1;
11+
CREATE DATABASE auto_test_remote;
12+
USE auto_test_remote;
13+
CREATE TABLE tbl_a (
14+
`a`int,
15+
`b`int,
16+
PRIMARY KEY (`a`)
17+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
18+
insert into `tbl_a` VALUES (1,999), (2,999);
19+
connection master_1;
20+
CREATE DATABASE auto_test_remote;
21+
USE auto_test_remote;
22+
CREATE TABLE tbl_a (
23+
`a`int,
24+
`b`int,
25+
PRIMARY KEY (`a`)
26+
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='table "tbl_a"' PARTITION BY LIST COLUMNS(`a`) (
27+
PARTITION `pt1` DEFAULT COMMENT = 'srv "s_2_1"'
28+
);
29+
connection master_1;
30+
SELECT distinct b FROM tbl_a WHERE b=999;
31+
b
32+
999
33+
connection master_1;
34+
DROP DATABASE IF EXISTS auto_test_remote;
35+
connection child2_1;
36+
DROP DATABASE IF EXISTS auto_test_remote;
37+
for master_1
38+
for child2
39+
child2_1
40+
child2_2
41+
child2_3
42+
for child3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!include include/default_mysqld.cnf
2+
!include ../my_1_1.cnf
3+
!include ../my_2_1.cnf
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--disable_warnings
2+
--disable_query_log
3+
--disable_result_log
4+
--source ../../t/test_init.inc
5+
--enable_result_log
6+
--enable_query_log
7+
--enable_warnings
8+
9+
--echo
10+
--echo MDEV-26013 distinct not work properly in some cases for spider tables
11+
--echo
12+
13+
--connection child2_1
14+
CREATE DATABASE auto_test_remote;
15+
USE auto_test_remote;
16+
17+
eval CREATE TABLE tbl_a (
18+
`a`int,
19+
`b`int,
20+
PRIMARY KEY (`a`)
21+
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET;
22+
23+
insert into `tbl_a` VALUES (1,999), (2,999);
24+
25+
--connection master_1
26+
CREATE DATABASE auto_test_remote;
27+
USE auto_test_remote;
28+
29+
eval CREATE TABLE tbl_a (
30+
`a`int,
31+
`b`int,
32+
PRIMARY KEY (`a`)
33+
) $MASTER_1_ENGINE $MASTER_1_CHARSET COMMENT='table "tbl_a"' PARTITION BY LIST COLUMNS(`a`) (
34+
PARTITION `pt1` DEFAULT COMMENT = 'srv "s_2_1"'
35+
);
36+
37+
--connection master_1
38+
SELECT distinct b FROM tbl_a WHERE b=999;
39+
40+
--connection master_1
41+
DROP DATABASE IF EXISTS auto_test_remote;
42+
--connection child2_1
43+
DROP DATABASE IF EXISTS auto_test_remote;
44+
45+
--disable_warnings
46+
--disable_query_log
47+
--disable_result_log
48+
--source ../../t/test_deinit.inc
49+
--enable_result_log
50+
--enable_query_log
51+
--enable_warnings

0 commit comments

Comments
 (0)