Wired casting when left join a table with JSON_EXTRACT column
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
**a_table**:
|id |
|---|
|00000e94-b6ff-4380-97f3-3cc2178eb099 |
**b_table**:
|id | |
|---|---|
|1 |{"userId": "00000e94-b6ff-4380-97f3-3cc2178eb099"}|
### 1. Minimal reproduce step (Required)
```sql
select a_table.id
from
a_table
left join
(select
JSON_EXTRACT(b_table.json_data, '$.userId') AS user_id
from
b_table
where xxxxx
) as b
on b.user_id = a_table.id;
```
### 2. What did you expect to see? (Required)
table a and b will be joined on `VARCHAR` type id.
### 3. What did you see instead (Required)
```
Invalid JSON text: The document root must not be followed by other values.
```
The plan seems wired, it will try to build the following column to left join `JSON_EXTRACT(b_table.json_data, '$.userId') AS user_id`
```
cast(a_table.id, json BINARY)->Column#31
```
I think that is why will throw the error.
When I modify it to:
```sql
select a_table.id
from
a_table
left join
(select
JSON_UNQUOTE(JSON_EXTRACT(b_table.json_data, '$.userId') AS user_id)
from
b_table
where xxxxx
) as b
on b.user_id = a_table.id;
```
It will be right.
### 4. What is your TiDB version? (Required)
TIDB-6.5
Contributor guide
Assessment
This issue has not been assessed yet.