JOIN ON condition with EXISTS subquery is rejected with ERROR 1105
- 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 a small table:
```sql
DROP TABLE IF EXISTS t_join_exists;
CREATE TABLE t_join_exists (
id INT PRIMARY KEY,
v INT
);
INSERT INTO t_join_exists VALUES (1, 10), (2, 20);
```
Run a `JOIN` query whose `ON` condition contains an `EXISTS` subquery:
```sql
SELECT
a.id AS a_id,
b.id AS b_id
FROM t_join_exists AS a
JOIN t_join_exists AS b
ON EXISTS (
SELECT 1
FROM t_join_exists AS c
WHERE c.id = a.id
);
```
The same error also shows up in larger nested join queries where the `ON` expression contains `EXISTS` predicates. A representative shape is:
```sql
SELECT ...
FROM table0 AS ref_3
INNER JOIN table0 AS ref_4
ON (false AND true)
RIGHT JOIN table0 AS ref_5
ON (
(false OR EXISTS (
SELECT (SELECT v4 FROM table0) AS c0,
ref_3.v2 AS c1,
ref_4.v4 AS c2
FROM table0 AS ref_6
WHERE EXISTS (
SELECT ref_6.v3 AS c0, ref_4.v1 AS c1
FROM table0 AS ref_7
WHERE false
)
))
AND false
);
```
The full query is syntactically accepted by TiDB's parser, but planning fails when the subquery appears inside the `JOIN ... ON` condition.
### 2. What did you expect to see? (Required)
TiDB should either support subqueries in `JOIN ... ON` conditions where SQL semantics allow them, or return a documented and specific unsupported-feature error.
For the minimal query above, a MySQL-compatible behavior would be to evaluate the `EXISTS` predicate as part of the join condition and return rows according to SQL semantics.
### 3. What did you see instead (Required)
TiDB returned the following error:
```text
ERROR 1105 (HY000): ON condition doesn't support subqueries yet
```
The error happens at query planning time. The cluster remains healthy after the failed query; the problem is not a server crash, but a SQL planner / compatibility limitation.
The part that triggers the error is the subquery inside the `ON` predicate:
```sql
JOIN t_join_exists AS b
ON EXISTS (
SELECT 1
FROM t_join_exists AS c
WHERE c.id = a.id
)
```
For larger generated SQL, the same class appears with nested joins and nested `EXISTS` expressions inside `ON`:
```sql
RIGHT JOIN table0 AS ref_5
ON ((false OR EXISTS (...)) AND false)
```
The query is not rejected as a syntax error. It reaches the planner and then fails with `ERROR 1105`.
### 4. What is your TiDB version? (Required)
```text
SELECT tidb_version();
8.0.11-TiDB-v8.5.6
```
Contributor guide
Research direction
Start by running the minimal JOIN ... ON EXISTS query against TiDB 8.0.11 and inspect the SQL planner path that emits “ON condition doesn't support subqueries yet.” Compare the planner behavior with the expected SQL semantics and the larger nested-join example. Done means supported valid subqueries produce the expected rows, or the limitation has a documented, specific error.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100