WL#13195: TABLE WITH JSON SCHEMA VALIDATION CONSTRAINT SHOULD RETURN ERROR FOR CONCRETE ROW

Affects: Server-8.0   —   Status: Complete

When check constraint is used for the JSON schema validation, if      
user tries to insert document which violates the check constraint then
constraint violation error "ER_CHECK_CONSTRAINT_VIOLATED - Check      
constraint 'constraint_name' is violated." is reported. But the       
reported error or diagnostic area does not contain information about 
the document and a issue.                                             
                                                                      
Goal of this WL is to report a SQL condition with information about   
the document and a issue on CHECK constraint (used for the JSON       
schema validation) violation.  

Current behavior on constraint violation:                             
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                             
  CREATE TABLE t1 (                                                   
  geometry JSON,                                                      
  CHECK(JSON_SCHEMA_VALID('{                                          
        "type": "object",                                             
        "properties": {                                               
          "latitude": {"type": "number", "minimum": -90,  "maximum": 90},
          "longitude": {"type": "number", "minimum": -180, "maximum": 180}
        }                                                             
      }', geometry)                                                   
  )                                                                   
  );                                                                  
                                                                      
  INSERT INTO t1 VALUES ('{"latitude": 181, "longitude": 0}');        
  ERROR HY000: Check constraint 't1_chk_1' is violated.               
                                                                      
  SHOW WARNINGS;                                                      
  Level Code    Message                                               
  Error 3819    Check constraint 't1_chk_1' is violated.

New behavior on constraint violation:                                 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                              
  INSERT INTO t1 VALUES ('{"latitude": 181, "longitude": 0}');          
  ERROR HY000: Check constraint 't1_chk_1' is violated.        
         
  SHOW WARNINGS;                                                        
  Level   Code    Message                                               
  Error   XXXX    The JSON document location '#/latitude'
                  failed requirement 'maximum' at JSON Schema location 
                  '#/properties/latitude'.
  Error   3819    Check constraint 't1_chk_1' is violated.

This WL will add a SQL condition in Diagnostics area with error code.
XXXX. XXXX will be a new error code introduced by this WL for detailed
information about the JSON schema validation error. 
         
FR1 : On CHECK constraint to validate JSON schema violation failure,
      error(SQL condition) with document information must be written
      to Diagnostics area.

FR2 : Error should be written in following format,

      The JSON document location '' failed requirement
      '' at JSON Schema location ''.
When JSON schema validation is realized using CHECK constraint in
following way:

  CREATE TABLE dbschema.collection1(
       doc JSON,
       _json_schema JSON GENERATED ALWAYS AS '{"type":"object"}',
       CONSTRAINT check_table_name_strict
         CHECK (JSON_SCHEMA_VALID(_json_schema, doc)) ENFORCED
    );

Currently if user tries to insert a document which is not valid
according to the schema placed in '_json_schema' column then user is
going to receive a generic error:

  ER_CHECK_CONSTRAINT_VIOLATED - "Check constraint 'check_table_name_strict'
                                  is violated."

Error or diagnostics area does not contain any information about the
document and issue. Same issue is observed with single or multiple
documents update and multiple document insert operations. For multiple
document insert or update operations user would need to go through all
inserted or updated document to check if it is valid with the schema.

As part of this WL, on CHECK constraint violation detailed information
about the failed JSON schema validation is pushed to diagnostics area
in the format

  The JSON document location '' failed requirement
  '' at JSON Schema location ''.

On check constraint violation error the Diagnostics area would look
like

mysql> insert into foobar VALUES ('{"latitude": 93}', default), ('{"latitude":
10}', default), ('{"latitude": 100}', default);
ERROR 3819 (HY000): Check constraint 'constrX' is violated.
mysql> show warnings;
+-------+------+----------------------------------------------------------------
-------------------------------------------------------+
| Level | Code | Message
|
+-------+------+----------------------------------------------------------------
-------------------------------------------------------+
| Error | XXXX | The JSON document location '#/latitude' failed requirement
'minimum' at JSON Schema location '#/properties/latitude'. |
| Error | 3819 | Check constraint 'constrX' is violated.
|
+-------+------+----------------------------------------------------------------
-------------------------------------------------------+
2 rows in set (0,00 sec)

Here XXXX is a new error code introduced for the detailed information
of JSON schema validation error.

GET DIAGNOSTICS statement can be used to query the document information
and reason.

Replication:
---------------
  Replication upgrade from OLD to NEW server is unaffected.

Upgrade:
-----------
  No changes to data-dictionary and INFORMATION_SCHEMA. Upgrade is
  unaffected.
To achieve this following changes are done,

1. New error code is introduced in errmsg-utf8.txt:

   ER_JSON_SCHEMA_VALIDATION_ERROR_WITH_DETAILED_REPORT
     eng "%s."

2. Item class of JSON_SCHEMA_VALIDATE is modified to emit error
   with detail report on error.

   2.1 New members is added to Item class to indicate check
       constraint execution context.

       class Item {
         ...
         bool m_in_check_constraint_exec_ctx{false};
       };


   2.2 Item_func_json_schema_valid::val_bool() is modified to emit
       an error with the validation report if check constraint execution
       context is set.

3. invoke_table_check_constraint is modified to set check constraint
   execution context(i.e Item::m_in_check_constraint_exec_ctx) in the
   expression item tree before evaluating.