Multi-valued index should implicitly cast the json type
- 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
CREATE TABLE t ( id INT PRIMARY KEY, j JSON, INDEX idx_a ((CAST(j->'$.a' AS DATE ARRAY))) );
INSERT INTO t VALUES (1, JSON_OBJECT('a', JSON_ARRAY('2024-05-13', '2020-01-01')));
```
### 2. What did you expect to see? (Required)
```sql
mysql> CREATE TABLE t ( id INT PRIMARY KEY, j JSON, INDEX idx_a ((CAST(j->'$.a' AS DATE ARRAY))) );
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO t VALUES (1, JSON_OBJECT('a', JSON_ARRAY('2024-05-13', '2020-01-01')));
Query OK, 1 row affected (0.01 sec)
```
### 3. What did you see instead (Required)
```sql
mysql> CREATE TABLE t ( id INT PRIMARY KEY, j JSON, INDEX idx_a ((CAST(j->'$.a' AS DATE ARRAY))) );
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO t VALUES (1, JSON_OBJECT('a', JSON_ARRAY('2024-05-13', '2020-01-01')));
ERROR 3903 (HY000): Invalid JSON value for CAST for expression index 'idx_a'
```
A workaround is manually casting the type to date:
```sql
mysql> INSERT INTO t VALUES (1, JSON_OBJECT('a', JSON_ARRAY(cast('2024-05-13' as date))));
Query OK, 1 row affected (0.01 sec)
```
### 4. What is your TiDB version? (Required)
```
mysql> select tidb_version();
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v8.0.0
Edition: Community
Git Commit Hash: 8ba1fa452b1ccdbfb85879ea94b9254aabba2916
Git Branch: HEAD
UTC Build Time: 2024-03-28 14:22:15
GoVersion: go1.21.4
Race Enabled: false
Check Table Before Drop: false
Store: tikv |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.