pingcap / pingcap/tidb

planner: some case's apply usage is redundant

Open
#34,219 3 comments 0 reactions 1 assignee Claimed by @AilinKid View on GitHub
planner/cascades severity/moderate sig/planner type/enhancement
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!

### 1. Minimal reproduce step (Required)
```
CREATE TABLE t (
a int NOT NULL,
d int NOT NULL,
e int NOT NULL,
i int NOT NULL,
PRIMARY KEY (a) /*T![clustered_index] CLUSTERED */
) ;

explain SELECT t.i+1 AS ii, (SELECT U0.e FROM t U0 WHERE U0.i+1 =
t.i+1 ORDER BY U0.e DESC LIMIT 1) AS top_e, SUM(t.d) AS total_d FROM t GROUP BY t.i+1, top_e ORDER BY 1;
```

### 2. What did you expect to see? (Required)
As you see, crdb has eliminated apply as left outer join, while TiDB doesn't, even has two redundant apply OP.

### 3. What did you see instead (Required)
crdb
```
demo@127.0.0.1:26257/movr> explain SELECT t.i+1 AS ii, (SELECT U0.e FROM t U0 WHERE U0.i+1 =
t.i+1 ORDER BY U0.e DESC LIMIT 1) AS top_e, SUM(t.d) AS total_d FROM t GROUP BY t.i+1, top_e ORDER BY 1;
info
------------------------------------------------------------------------------------------------------------
distribution: local
vectorized: true

• sort
│ estimated row count: 1
│ order: +column14

└── • group (hash)
│ estimated row count: 1
│ group by: column14, top_e

└── • render
│ estimated row count: 1

└── • distinct
│ estimated row count: 1
│ distinct on: a

└── • sort
│ estimated row count: 1
│ order: -e

└── • hash join (left outer)
│ estimated row count: 1
│ equality: (column16) = (column17)

├── • render
│ │ estimated row count: 1
│ │
│ └── • scan
│ estimated row count: 1 (100% of the table; stats collected 2 minutes ago)
│ table: t@t_pkey
│ spans: FULL SCAN

└── • render
│ estimated row count: 1

└── • scan
estimated row count: 1 (100% of the table; stats collected 2 minutes ago)
table: t@t_pkey
spans: FULL SCAN
(41 rows)
```
tidb
```
mysql> explain SELECT t.i+1 AS ii, (SELECT U0.e FROM t U0 WHERE U0.i+1 = t.i+1 ORDER BY U0.e DESC LIMIT 1) AS top_e, SUM(t.d) AS total_d FROM t GROUP BY t.i+1, top_e ORDER BY 1;
+--------------------------------------------+----------+-----------+---------------+-------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+--------------------------------------------+----------+-----------+---------------+-------------------------------------------------------------------------------------------------------+
| Sort_22 | 10000.00 | root | | Column#14 |
| └─Projection_24 | 10000.00 | root | | plus(test2.t.i, 1)->Column#14, test2.t.e, Column#13 |
| └─Apply_26 | 10000.00 | root | | CARTESIAN left outer join |
| ├─HashAgg_27(Build) | 10000.00 | root | | group by:Column#22, Column#23, funcs:sum(Column#20)->Column#13, funcs:firstrow(Column#21)->test2.t.i |
| │ └─Projection_50 | 10000.00 | root | | cast(test2.t.d, decimal(10,0) BINARY)->Column#20, test2.t.i, plus(test2.t.i, 1)->Column#22, test2.t.e |
| │ └─Apply_29 | 10000.00 | root | | CARTESIAN left outer join |
| │ ├─TableReader_31(Build) | 10000.00 | root | | data:TableFullScan_30 |
| │ │ └─TableFullScan_30 | 10000.00 | cop[tikv] | table:t | keep order:false, stats:pseudo |
| │ └─TopN_32(Probe) | 1.00 | root | | test2.t.e:desc, offset:0, count:1 |
| │ └─TableReader_40 | 1.00 | root | | data:TopN_39 |
| │ └─TopN_39 | 1.00 | cop[tikv] | | test2.t.e:desc, offset:0, count:1 |
| │ └─Selection_38 | 8000.00 | cop[tikv] | | eq(plus(test2.t.i, 1), plus(test2.t.i, 1)) |
| │ └─TableFullScan_37 | 10000.00 | cop[tikv] | table:U0 | keep order:false, stats:pseudo |
| └─TopN_41(Probe) | 1.00 | root | | test2.t.e:desc, offset:0, count:1 |
| └─TableReader_49 | 1.00 | root | | data:TopN_48 |
| └─TopN_48 | 1.00 | cop[tikv] | | test2.t.e:desc, offset:0, count:1 |
| └─Selection_47 | 8000.00 | cop[tikv] | | eq(plus(test2.t.i, 1), plus(test2.t.i, 1)) |
| └─TableFullScan_46 | 10000.00 | cop[tikv] | table:U0 | keep order:false, stats:pseudo |
+--------------------------------------------+----------+-----------+---------------+-------------------------------------------------------------------------------------------------------+
18 rows in set (0.01 sec)
```

### 4. What is your TiDB version? (Required)

master

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.