planner: sort should not be after projection in some cases
- 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)
```sql
SELECT a.biz_acct_id, a.tenant_id, a.ranked AS show_rank FROM (
SELECT @rank:=0, @tenant_id:=null, @biz_acct_id:=null ) rk,
( select p1.*, @rank:=CASE WHEN @tenant_id = p1.tenant_id AND @biz_acct_id = p1.biz_acct_id THEN @rank + 1 ELSE 1 END AS ranked, @tenant_id:=p1.tenant_id, @biz_acct_id:=p1.biz_acct_id
from ( select 1 as tenant_id, 2 as biz_acct_id union ALL select 3 as tenant_id, 4 as biz_acct_id
union ALL select 1 as tenant_id, 2 as biz_acct_id
union ALL select 5 as tenant_id, 6 as biz_acct_id
union ALL select 1 as tenant_id, 2 as biz_acct_id ) p1 order by p1.tenant_id, p1.biz_acct_id ) a;
```
### 2. What did you expect to see? (Required)
MySQL has the following result:
```
+-------------+-----------+-----------+
| biz_acct_id | tenant_id | show_rank |
+-------------+-----------+-----------+
| 2 | 1 | 1 |
| 2 | 1 | 2 |
| 2 | 1 | 3 |
| 4 | 3 | 1 |
| 6 | 5 | 1 |
+-------------+-----------+-----------+
5 rows in set (0.70 sec)
```
### 3. What did you see instead (Required)
```
+-------------+-----------+-----------+
| biz_acct_id | tenant_id | show_rank |
+-------------+-----------+-----------+
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 4 | 3 | 1 |
| 6 | 5 | 1 |
+-------------+-----------+-----------+
5 rows in set (0.01 sec)
```
### 4. What is your TiDB version? (Required)
| tidb_version() |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v6.6.0-alpha-371-gddaf0d781-dirty
Edition: Community
Git Commit Hash: ddaf0d7814fe5fe5b1aca0695ec34b3bfa9c6650
Git Branch: local/fix_sort_after_projection
UTC Build Time: 2023-02-06 07:28:42
GoVersion: go1.19.4
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: unistore |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Contributor guide
Research direction
Start by running the provided SQL reproduction against the reported TiDB version and compare its output with the MySQL result. Trace the planner behavior around projection and sort ordering; done means the query produces ranks 1, 2, 3 for the repeated tenant and account rows while preserving the other rows.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100