Bug #88033 Generated INT column with value from JSON document
Submitted: 9 Oct 2017 18:38 Modified: 10 Oct 2017 6:57
Reporter: Geert Vanderkelen Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: JSON Severity:S3 (Non-critical)
Version:8.0.3, 5.7.19 OS:Any
Assigned to: CPU Architecture:Any

[9 Oct 2017 18:38] Geert Vanderkelen
Description:
I'm trying to generate a column based on a value found in JSON. This value is an integer in the document, or it can be null.

Whenever the JSON column has an integer, no problem. When it is null, I get the 

ERROR 3156 (22018): Invalid JSON value for CAST to INTEGER from column json_extract

However, the generate column is set to allow NULL. Why a CAST is needed, and how would that cast be done? If documented, where? Or simply a bug thus :)

How to repeat:
DROP TABLE IF EXISTS t1;

CREATE TABLE t1 (
  id BIGINT PRIMARY KEY,
  c1 BIGINT AS (c2->>"$.c1") STORED NULL,
  c2 JSON, INDEX(c1));

mysql> INSERT INTO t2 (id, c2) VALUES (1, '{"c1": null}');
ERROR 3156 (22018): Invalid JSON value for CAST to INTEGER from column json_extract at row 1

mysql> INSERT INTO t2 (id, c2) VALUES (1, '{"c1": 1}');
Query OK, 1 row affected (0.01 sec)
[9 Oct 2017 18:46] Geert Vanderkelen
The insert statements were using the incorrect table (was using a few to try), but no matter. A more scaled down example:

CREATE TABLE t1 (
  id INT PRIMARY KEY,
  c1 INT AS (c2->>"$.c1") STORED NULL,
  c2 JSON);

mysql> INSERT INTO t1 (id, c2) VALUES (1, '{"c1": null}');
ERROR 1366 (HY000): Incorrect integer value: 'null' for column 'c1' at row 1

mysql> INSERT INTO t1 (id, c2) VALUES (1, '{"c1": 1}');
Query OK, 1 row affected (0.00 sec)
[9 Oct 2017 19:13] Geert Vanderkelen
Workaround is to omit the entry in JSON document when saving:

INSERT INTO t1 (id, c2) VALUES (1, '{}');

That will insert a NULL value since $c2->".c1" is not available.
Though that is application side; would be great if JSON null is NULL.
[10 Oct 2017 6:57] Umesh Shastry
Hello Geert,

Thank you for the report and test case.
This is even observed in 5.7.19.

Regards,
Umesh
[7 Jan 3:15] JJ Ward
Using version 8.0.13 this is still an issue, and I'd say raise the severity to S2 at least.

I am pre-extracting the fields/paths from the JSON document before dynamically generating a view off of JSON value found in base JSON table.

Numerics fail across the board for NULL values when trying to INSERT, but when running the view itself the fields will resolve as expected - why is CAST needed here, and what purposes do/should the ON ERROR/ON EMPTY clauses for JSON_TABLE( ..., COLUMNS() ) serve if not this exact scenario?