Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible issue related to JSON functions #13710

Closed
js00070 opened this issue Nov 24, 2019 · 11 comments · Fixed by #21903
Closed

Incompatible issue related to JSON functions #13710

js00070 opened this issue Nov 24, 2019 · 11 comments · Fixed by #21903
Assignees

Comments

@js00070
Copy link
Contributor

js00070 commented Nov 24, 2019

Bug Report

Seems there's an incompatible issue related to JSON functions

  1. What did you do?
select json_extract("\"asd\"","$") in ("asd","abcd");
  1. What did you expect to see?

in MySQL:5.7

mysql> select json_extract("\"asd\"","$") in ("asd","abcd");
+----------------------------------------------+
| json_extract("\"asd\"","$") in ("asd","abc") |
+----------------------------------------------+
|                                            0 |
+----------------------------------------------+
1 row in set, 1 warning (0.00 sec)

in MySQL:8

mysql> select json_extract("\"asd\"","$") in ("asd","abcd");
+-----------------------------------------------+
| json_extract("\"asd\"","$") in ("asd","abcd") |
+-----------------------------------------------+
|                                             1 |
+-----------------------------------------------+
1 row in set (0.00 sec)
  1. What did you see instead?
MySQL [(none)]> select json_extract("\"asd\"","$") in ("asd","abcd");
ERROR 3140 (22032): Invalid JSON text: The document root must not be followed by other values.
MySQL [(none)]>
  1. What version of TiDB are you using (tidb-server -V or run select tidb_version(); on TiDB)?
Release Version: v4.0.0-alpha-940-g92e774913
Git Commit Hash: 92e774913a2f6166b104d3d8eaed5513733f1830
Git Branch: master
UTC Build Time: 2019-11-24 03:10:38
GoVersion: go version go1.13.1 linux/amd64
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
@js00070 js00070 added the type/bug The issue is confirmed as a bug. label Nov 24, 2019
@wjhuang2016
Copy link
Member

Thanks for your report, could you fire a PR to solve it?

@js00070
Copy link
Contributor Author

js00070 commented Nov 25, 2019

Thanks for your report, could you fire a PR to solve it?

ok, I will take a try.

@Deardrops
Copy link
Contributor

Deardrops commented Dec 3, 2019

related issue #11883

@js00070
Copy link
Contributor Author

js00070 commented Dec 11, 2019

the behaviour of MySQL:8 is a little strange.
in MySQL:8, we will see

mysql> select json_extract('"asd"',"$"),json_extract('"asd"',"$") in ('"asd"','"abcd"');
+---------------------------+-------------------------------------------------+
| json_extract('"asd"',"$") | json_extract('"asd"',"$") in ('"asd"','"abcd"') |
+---------------------------+-------------------------------------------------+
| "asd"                     |                                               0 |
+---------------------------+-------------------------------------------------+
1 row in set (0.00 sec)

in TiDB master branch, we have:

MySQL [(none)]> select json_extract('"asd"',"$"),json_extract('"asd"',"$") in ('"asd"','"abcd"');
+---------------------------+-------------------------------------------------+
| json_extract('"asd"',"$") | json_extract('"asd"',"$") in ('"asd"','"abcd"') |
+---------------------------+-------------------------------------------------+
| "asd"                     |                                               1 |
+---------------------------+-------------------------------------------------+
1 row in set (0.00 sec)

I think the TiDB's result of this case is more reasonable. How should we handle this condition?
@wjhuang2016

@zz-jason
Copy link
Member

zz-jason commented Dec 12, 2019

@js00070 We can file a bug issue to the MySQL Team through https://bugs.mysql.com/

@wjhuang2016
Copy link
Member

@zz-jason , No, it's our bug, currently json_extract always return json, and there are some bugs/issues related to this.
image

@js00070
Copy link
Contributor Author

js00070 commented Dec 12, 2019

more incompatible cases

in MySQL:8

mysql> select json_extract(1, "$");
ERROR 3146 (22032): Invalid data type for JSON data in argument 1 to function json_extract; a JSON string or JSON type is required.

in TiDB master

MySQL [(none)]> select json_extract(1, "$");
+----------------------+
| json_extract(1, "$") |
+----------------------+
| 1                    |
+----------------------+
1 row in set (0.00 sec)

in MySQL:8

mysql> select json_extract('{"a": 1}', "$") in ('{"a": 1}',"abcd");
+------------------------------------------------------+
| json_extract('{"a": 1}', "$") in ('{"a": 1}',"abcd") |
+------------------------------------------------------+
|                                                    0 |
+------------------------------------------------------+
1 row in set (0.00 sec)

in TiDB master

MySQL [(none)]> select json_extract('{"a": 1}', "$") in ('{"a": 1}',"abcd");
ERROR 3140 (22032): Invalid JSON text: The document root must not be followed by other values.

in MySQL:8

mysql> select json_extract('{"a": 1}', "$") in ('{"a": 1}','"abcd"');
+--------------------------------------------------------+
| json_extract('{"a": 1}', "$") in ('{"a": 1}','"abcd"') |
+--------------------------------------------------------+
|                                                      0 |
+--------------------------------------------------------+
1 row in set (0.00 sec)

in TiDB master

MySQL [(none)]> select json_extract('{"a": 1}', "$") in ('{"a": 1}','"abcd"');
+--------------------------------------------------------+
| json_extract('{"a": 1}', "$") in ('{"a": 1}','"abcd"') |
+--------------------------------------------------------+
|                                                      1 |
+--------------------------------------------------------+
1 row in set (0.00 sec)

@js00070
Copy link
Contributor Author

js00070 commented Dec 12, 2019

MySQL 8.0 document (https://dev.mysql.com/doc/refman/8.0/en/json.html#json-comparison)
says that IN function is not yet supported with JSON values.

and mysql uses an workaround

A workaround for the comparison operators and functions just listed is to cast JSON values to a native MySQL numeric or string data type so they have a consistent non-JSON scalar type. 

Should we implement this workaround in TiDB? @zz-jason @wjhuang2016

@ichn-hu
Copy link
Contributor

ichn-hu commented Dec 21, 2020

/assign @ichn-hu

@ichn-hu
Copy link
Contributor

ichn-hu commented Dec 21, 2020

This bug can also be fixed by #21903

@ti-srebot
Copy link
Contributor

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: #20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

6. Fixed versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants