pingcap / pingcap/tidb

planner: EXPLAIN FORMAT='hint' may skip outer LEADING when a join group contains a derived table alias

Open
#68,564 4 comments 0 reactions 0 assignees View on GitHub
contribution
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)

The original report came from a customer query, but the following de-identified repro captures the same bug without exposing customer data.

```sql
use test;
drop table if exists t1, t2, t3;
create table t1(a int, b int, key(a));
create table t2(a int, b int, key(a));
create table t3(a int, b int, key(a, b));

explain format='hint'
select *
from t1
join t2 on t1.a = t2.a
join (
select a, b, row_number() over(partition by a order by b desc) as rn
from t3
) dt on t2.a = dt.a
where dt.rn = 1;
```

Representative de-identified plan shape from the failing query:

```text
HashJoin(outer join group)
├─TableReader(t1)
├─TableReader(t2)
└─Selection
└─Window(row_number partition by a order by b desc)
└─TableReader(t3) -- exposed to the outer query as derived alias dt
```

The key point is that the outer join group contains a derived-table alias (`dt`), not only base tables.

### 2. What did you expect to see? (Required)

`EXPLAIN FORMAT='hint'` should export a replayable outer-block `LEADING` hint that uses the visible derived-table alias, for example:

```sql
leading(`test`.`t1`, `test`.`t2`, `test`.`dt`)
```

The exported hints should remain replayable through hint injection / binding workflows and preserve the intended outer join order.

### 3. What did you see instead (Required)

Before the fix, `EXPLAIN FORMAT='hint'` did not emit a stable outer-block `LEADING` for the join group containing the derived-table alias.

As a result:
- the exported hints did not fully constrain the outer join order;
- replay / binding-from-history could not reliably preserve the intended outer join order;
- related mixed-query-block cases could also surface invalid or unstable exported `LEADING` hints.

From local investigation, the issue seems to happen when hint generation cannot stably map one join node in the outer join group back to a visible hint table name, so the whole outer `LEADING` is skipped.

### 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.