Planner: AGG can be eliminated if the join key is unique and the aggregate function operates on the UK.
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
AGG can be eliminated if the join key is unique and the aggregate function operates on the UK.
### 1. Minimal reproduce step (Required)
```
CREATE TABLE `t1` (
`id1` int NOT NULL,
`id2` int NOT NULL,
`id3` int NOT NULL,
`id4` int NOT NULL,
UNIQUE KEY `UK_id1_id2` (`id1`,`id2`)
);
create table t2 like t1;
explain select t1.id1,t1.id2,sum(t1.id3) from t1,t2 where t1.id1=t2.id1 and t1.id2=t2.id2 group by t1.id1,t1.id2;
explain select t1.id1,t1.id2,sum(t1.id3) from t1 left join t2 on t1.id1=t2.id1 and t1.id2=t2.id2 group by t1.id1,t1.id2;
```
### 2. What did you expect to see? (Required)
There are no aggregate operators.
### 3. What did you see instead (Required)
```
mysql> explain select t1.id1,t1.id2,sum(t1.id3) from t1,t2 where t1.id1=t2.id1 and t1.id2=t2.id2 group by t1.id1,t1.id2;
+------------------------------------+-------------+-----------+--------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------------+-------------+-----------+--------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection_8 | 7984.01 | root | | test1.t1.id1, test1.t1.id2, Column#11 |
| └─HashAgg_9 | 7984.01 | root | | group by:Column#19, Column#20, funcs:sum(Column#18)->Column#11, funcs:firstrow(Column#19)->test1.t1.id1, funcs:firstrow(Column#20)->test1.t1.id2 |
| └─Projection_69 | 99600599.60 | root | | cast(test1.t1.id3, decimal(10,0) BINARY)->Column#18, test1.t1.id1->Column#19, test1.t1.id2->Column#20 |
| └─HashJoin_37 | 99600599.60 | root | | inner join, equal:[eq(test1.t1.id1, test1.t2.id1) eq(test1.t1.id2, test1.t2.id2)] |
| ├─IndexReader_56(Build) | 9980.01 | root | | index:Selection_55 |
| │ └─Selection_55 | 9980.01 | cop[tikv] | | not(isnull(test1.t2.id2)) |
| │ └─IndexFullScan_54 | 9990.00 | cop[tikv] | table:t2, index:UK_id1_id2(id1, id2) | keep order:false, stats:pseudo |
| └─TableReader_49(Probe) | 9980.01 | root | | data:Selection_48 |
| └─Selection_48 | 9980.01 | cop[tikv] | | not(isnull(test1.t1.id1)), not(isnull(test1.t1.id2)) |
| └─TableFullScan_47 | 10000.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
+------------------------------------+-------------+-----------+--------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
mysql> explain select t1.id1,t1.id2,sum(t1.id3) from t1 left join t2 on t1.id1=t2.id1 and t1.id2=t2.id2 group by t1.id1,t1.id2;
+------------------------------------+-------------+-----------+--------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------------+-------------+-----------+--------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection_7 | 1.00 | root | | test1.t1.id1, test1.t1.id2, Column#11 |
| └─HashAgg_8 | 1.00 | root | | group by:Column#15, Column#16, funcs:sum(Column#14)->Column#11, funcs:firstrow(Column#15)->test1.t1.id1, funcs:firstrow(Column#16)->test1.t1.id2 |
| └─Projection_48 | 99800100.00 | root | | cast(test1.t1.id3, decimal(10,0) BINARY)->Column#14, test1.t1.id1->Column#15, test1.t1.id2->Column#16 |
| └─HashJoin_22 | 99800100.00 | root | | left outer join, left side:TableReader_32, equal:[eq(test1.t1.id1, test1.t2.id1) eq(test1.t1.id2, test1.t2.id2)] |
| ├─IndexReader_35(Build) | 9980.01 | root | | index:Selection_34 |
| │ └─Selection_34 | 9980.01 | cop[tikv] | | not(isnull(test1.t2.id2)) |
| │ └─IndexFullScan_33 | 9990.00 | cop[tikv] | table:t2, index:UK_id1_id2(id1, id2) | keep order:false, stats:pseudo |
| └─TableReader_32(Probe) | 10000.00 | root | | data:TableFullScan_31 |
| └─TableFullScan_31 | 10000.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
+------------------------------------+-------------+-----------+--------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set (0.00 sec)
```
### 4. What is your TiDB version? (Required)
TiDB-v9.0.0-beta.1.pre-432-gc508e4b8ac
Contributor guide
Assessment
This issue has not been assessed yet.