Bug #109119 "CAST AS JSON" casts numeric values with big precision incorrectly
Submitted: 17 Nov 2022 10:44 Modified: 17 Nov 2022 12:55
Reporter: Damian Cholewa Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: JSON Severity:S3 (Non-critical)
Version:8.0.20 OS:Any
Assigned to: CPU Architecture:Any
Tags: cast, cast string, double, FLOAT, json, precision

[17 Nov 2022 10:44] Damian Cholewa
Description:
Hello,
if you run this query:

SELECT CAST('{"end": 13.588410104011881}' AS JSON) AS TEST;

then it results in JSON with incorrect "end" field value:

{
    "end": 13.58841010401188
}

Always, the last digit is changed. For example 13.588410104011887 is converted to 13.588410104011889.

How to repeat:
Could be easily reproduced by running this query:

SELECT CAST('{"end": 13.588410104011881}' AS JSON) AS TEST;
[17 Nov 2022 11:02] Damian Cholewa
I would like to add if I add JSON value directly to the database by insert (for example {"end": 13.588410104011881}) then it shows correct value. So, only CAST AS JSON is causing a problem.
[17 Nov 2022 12:55] MySQL Verification Team
Hi Mr. Cholewa,

Thank you for your bug report.

However, it is not a bug.

Simply, CAST has to do the best it can. In your case, it supposes that you want a floating point value for that number. Hence it also has to suppose that it is the value of DOUBLE which has 15 (fifteen) significant digits, while 16th (sixteenth) should be included as well. However, you have 17 (seventeen) digits.

Not a bug.