Skip to content

Commit b973022

Browse files
MDEV-26345 SELECT MIN on Spider table returns more rows than expected
The Spider storage engine ignored the implicit grouping when aggregation was converted to constant by the query optimizer. As a result, the Spider SE returned rows more than expected. To fix the problem, we notify the Spider SE of the existence of the implicit grouping via Query::distinct.
1 parent 5a6de6f commit b973022

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

sql/sql_select.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,13 +3278,12 @@ bool JOIN::make_aggr_tables_info()
32783278
/*
32793279
Check if the storage engine can intercept the query.
32803280

3281-
JOIN::optimize_stage2() might convert DISTINCT into GROUP BY and then
3282-
optimize away GROUP BY (group_list). In such a case, we need to notify
3283-
a storage engine supporting a group by handler of the existence of the
3284-
original DISTINCT. Thus, we set select_distinct || group_optimized_away
3285-
to Query::distinct.
3281+
The query optimizer might optimize away aggregation functins or DISTINCT.
3282+
In such a cage, we need to notify a storage engine supporting a group by
3283+
handler of the existence of the original explicit or implicit grouping.
3284+
Thus, we set select_distinct || implicit_grouping to Query::distinct.
32863285
*/
3287-
Query query= {&all_fields, select_distinct || group_optimized_away, tables_list,
3286+
Query query= {&all_fields, select_distinct || implicit_grouping, tables_list,
32883287
conds, group_list, order ? order : group_list, having};
32893288
group_by_handler *gbh= ht->create_group_by(thd, &query);
32903289

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-26345 SELECT MIN on Spider table returns more rows than expected
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`, `b`)
17+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
18+
insert into `tbl_a` VALUES (1,1), (1,2), (2,11);
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`, `b`)
26+
) ENGINE=Spider DEFAULT CHARSET=utf8 COMMENT='table "tbl_a"' PARTITION BY LIST COLUMNS(`b`) (
27+
PARTITION `pt1` DEFAULT COMMENT = 'srv "s_2_1"'
28+
);
29+
connection master_1;
30+
SELECT MIN(b), a FROM tbl_a WHERE a=1;
31+
MIN(b) a
32+
1 1
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-26345 SELECT MIN on Spider table returns more rows than expected
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`, `b`)
21+
) $CHILD2_1_ENGINE $CHILD2_1_CHARSET;
22+
23+
insert into `tbl_a` VALUES (1,1), (1,2), (2,11);
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`, `b`)
33+
) $MASTER_1_ENGINE $MASTER_1_CHARSET COMMENT='table "tbl_a"' PARTITION BY LIST COLUMNS(`b`) (
34+
PARTITION `pt1` DEFAULT COMMENT = 'srv "s_2_1"'
35+
);
36+
37+
--connection master_1
38+
SELECT MIN(b), a FROM tbl_a WHERE a=1;
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

storage/spider/mysql-test/spider/r/direct_join.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ connection child2_1;
167167
SELECT argument FROM mysql.general_log WHERE argument LIKE '%select %';
168168
argument
169169
select `id`,`hr_status`,`region_code`,`region` from `auto_test_remote`.`tbl_person` where `id` = '24FC3F0A5119432BAE13DD65AABAA39C' and `region` = 510411
170-
select count(0) `count(0)` from `auto_test_remote`.`tbl_ncd_cm_person` t0 where ((t0.`person_id` = '24FC3F0A5119432BAE13DD65AABAA39C') and (t0.`diseaseKind_id` = '52A0328740914BCE86ED10A4D2521816'))
170+
select distinct count(0) `count(0)` from `auto_test_remote`.`tbl_ncd_cm_person` t0 where ((t0.`person_id` = '24FC3F0A5119432BAE13DD65AABAA39C') and (t0.`diseaseKind_id` = '52A0328740914BCE86ED10A4D2521816'))
171171
SELECT argument FROM mysql.general_log WHERE argument LIKE '%select %'
172172
SELECT * FROM tbl_person;
173173
id hr_status region_code region

0 commit comments

Comments
 (0)