Bug #116852 Difference between JSONPath "$" vs "$[*]"
Submitted: 3 Dec 2024 3:33 Modified: 3 Dec 2024 6:15
Reporter: Seunguck Lee Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: JSON Severity:S3 (Non-critical)
Version:8.0.34, 8.0.40 OS:Any
Assigned to: CPU Architecture:Any

[3 Dec 2024 3:33] Seunguck Lee
Description:
Using MultiValued Index on json array field, Json functional part(JSONPath) "$" and "$[*]" makes different result. (See "How to repeat" section)

And I have found JSONPath "$" and "$[*]" yield different results on MySQL server as following example.

mysql> select json_extract(json_array(), '$'), json_extract(json_array(), '$[*]');
+---------------------------------+------------------------------------+
| json_extract(json_array(), '$') | json_extract(json_array(), '$[*]') |
+---------------------------------+------------------------------------+
| []                              | NULL                               |
+---------------------------------+------------------------------------+

I have read the manual which explains about "Nullability and multi-valued indexes".
https://dev.mysql.com/doc/refman/8.4/en/create-index.html#create-index-multi-valued:~:text...

What I am wondering is that following is working as designed? I want to use JSONPath "json->"$[*]" in Multi-valued Index. But result of JSONPath for empty-array is not explained in Manual. Just wondering if these behavior can be changed in future version
1) JSONPath "$[*]" for empty-array yeild NULL
1) JSONPath "$" for empty-array yeild "[]"

How to repeat:
sql> create table mvi (
       id int primary key, 
       t int, 
       j json, 
       index index1 (t, (cast(j->'$' as signed array))), 
       index index2 (t, (cast(j->'$[*]' as signed array)))
     );
sql> insert into mvi values (1,1,'[1,2,3]'), (2,1,'[]'), (3,1,NULL);

sql> select * from mvi force index(index1) where t=1;
+----+------+-----------+
| id | t    | j         |
+----+------+-----------+
|  3 |    1 | NULL      |
|  1 |    1 | [1, 2, 3] |
+----+------+-----------+
2 rows in set (0.04 sec)

sql> select * from mvi force index(index2) where t=1;
+----+------+-----------+
| id | t    | j         |
+----+------+-----------+
|  2 |    1 | []        |
|  3 |    1 | NULL      |
|  1 |    1 | [1, 2, 3] |
+----+------+-----------+
3 rows in set (0.04 sec)
[3 Dec 2024 6:15] MySQL Verification Team
Hello Seunguck Lee,

Thank you for the report and test case.

regards,
Umesh