pingcap / pingcap/tidb

`json_contains` function not compatible with MySQL

Open
#58,291 3 comments 0 reactions 1 assignee Claimed by @YangKeao View on GitHub
compatibility-mysql80 component/json severity/moderate sig/sql-infra type/bug
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)

```mysql
create table t1 (id int not null key auto_increment, f1 json);
create index i1 on t1((cast(f1->"$[*]" as unsigned array)));
insert into t1(f1) values
(cast('[1,3]' as json)), (cast(2 as json)), (cast('[3,3,4,4,7]' as json)),
(cast('[5,7]' as json)),
(cast('[8,4,3,5]' as json)), (cast('[5,6,7]' as json)),
(cast('[9,2,7]' as json)), (cast('[1,3]' as json)),
(cast('[3,3,4,4,7]' as json)), (cast(4 as json)), (cast('[8,4,3,5]' as json)),
(cast('[9,2,7]' as json)), (cast('[9,2,7]' as json)),
(cast('[1,3]' as json)),
(cast('[3,3,4,4,7]' as json)), (cast(4 as json)),
(cast(7 as json)), (cast('[8,4,3,5]' as json)), (cast('[9,2,7]' as json)),
('[98,99]');
explain select * from t1 force index(i1) where json_contains(f1->"$[*]", "[4,");
```

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

```mysql
mysql> explain select * from t1 force index(i1) where json_contains(f1->"$[*]", "[4,");
ERROR 3141 (22032): Invalid JSON text in argument 2 to function json_contains: "Invalid value." at position 3.
```

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

```mysql
mysql> explain select * from t1 force index(i1) where json_contains(f1->"$[*]", "[4,");
+-------------------------+---------+-----------+---------------+---------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------+---------+-----------+---------------+---------------------------------------------------------------------------+
| TableReader_7 | 16.00 | root | | data:Selection_6 |
| └─Selection_6 | 16.00 | cop[tikv] | | json_contains(json_extract(test.t1.f1, "$[*]"), cast("[4,", json BINARY)) |
| └─TableFullScan_5 | 20.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
+-------------------------+---------+-----------+---------------+---------------------------------------------------------------------------+
3 rows in set (0.00 sec)
```

### 4. What is your TiDB version? (Required)

```mysql
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v8.5.0-alpha-343-g59dff48ea2
Edition: Community
Git Commit Hash: 59dff48ea2ea17f823f47ece9ef3a15209645bb4
Git Branch: master
UTC Build Time: 2024-12-16 03:41:31
GoVersion: go1.23.4
Race Enabled: false
Check Table Before Drop: false
Store: unistore |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

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.