Bug #122067 Strict mode: a JSON string with an impossible date is stored in a DATE column as NULL without error or warning

Submitted: 5 Dec 2:42
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: JSON Severity:S2 (Serious)
Version:26.10.0.1 (trunk 4573da3722a), 9.7.4 OS:Any
Assigned to: CPU Architecture:Any

[5 Dec 2:42] Ke Han
Description:
In the default strict sql_mode, a JSON string that has a valid date syntax
but an impossible date ("2023-02-29", "2024-04-31", "2024-06-31T08:00:00",
"0000-00-00", "2024-00-10"), assigned to a nullable DATE column through a
JSON expression, is stored as NULL. The statement succeeds with no error and
no warning, and the date is gone. MySQL 8.4.13 rejects each of these
statements with ERROR 3156 (Invalid JSON value for CAST to
DATE/TIME/DATETIME/TIMESTAMP), and the same values through their text form
(doc->>'$.due') are rejected with ERROR 1292 in 26.10 as well.

Affected (each stores NULL with no warning in 26.10.0 and 9.7.4):
- INSERT ... SELECT doc->'$.due' and JSON_EXTRACT(doc, '$.due')
- INSERT ... SELECT CAST(doc->'$.due' AS DATE)
- UPDATE ... SET due = doc->'$.due' (an existing date is overwritten with NULL)
- STORED generated columns, and indexed VIRTUAL generated columns,
  due DATE AS (doc->'$.due')
- a DATE variable in a stored procedure set from (SELECT doc->'$.due' ...)
- a trigger doing SET NEW.due = NEW.doc->'$.due'
A NOT NULL DATE column gets ERROR 1048 "Column 'due' cannot be null" instead
of the conversion error, which shows that the value has already become NULL.

The manual's JSON conversion rules ("Converting between JSON and non-JSON
values") say that CAST(JSON AS other type) "returns NULL and produces a
warning" when the scalar cannot be cast to the target type. 26.10 produces no
warning, so strict mode cannot turn it into an error, and INSERT ... SELECT or
UPDATE silently replaces the dates with NULL. Typical sources are JSON
documents from systems with a calendar bug or a 0000-00-00 placeholder,
loaded with INSERT ... SELECT into typed tables that strict mode should
protect.

The change comes from the fix for Bug#38998827 ("Assert in
Date_val::day_number", commit 46227d2ee07c, February 2026, in 9.7.0), see
Suggested fix.

How to repeat:
SELECT @@sql_mode;
CREATE DATABASE r226;
USE r226;
CREATE TABLE staging (id INT PRIMARY KEY, doc JSON);
INSERT INTO staging VALUES
  (1, '{"order": "A1", "due": "2024-02-29"}'),
  (2, '{"order": "A2", "due": "2023-02-29"}'),
  (3, '{"order": "A3", "due": "2024-04-31"}'),
  (4, '{"order": "A4", "due": "2024-06-31T08:00:00"}'),
  (5, '{"order": "A5", "due": "0000-00-00"}'),
  (6, '{"order": "A6", "due": "2024-00-10"}');
CREATE TABLE orders (id INT PRIMARY KEY, order_no VARCHAR(10), due DATE);

-- 1. INSERT ... SELECT of the JSON value
INSERT INTO orders SELECT id, doc->>'$.order', doc->'$.due' FROM staging;
SHOW WARNINGS;
SELECT o.id, o.order_no, s.doc->>'$.due' AS due_in_json, o.due FROM orders o JOIN staging s USING (id) ORDER BY o.id;

-- 2. UPDATE: overwrites an existing date with NULL
DELETE FROM orders;
INSERT INTO orders SELECT id, doc->>'$.order', '2000-01-01' FROM staging;
UPDATE orders o JOIN staging s USING (id) SET o.due = s.doc->'$.due';
SHOW WARNINGS;
SELECT COUNT(*), COUNT(due) FROM orders;

-- 3. CAST(... AS DATE) and a STORED generated column
DELETE FROM orders;
INSERT INTO orders SELECT id, doc->>'$.order', CAST(doc->'$.due' AS DATE) FROM staging;
SELECT COUNT(*), COUNT(due) FROM orders;
CREATE TABLE orders_gc (id INT PRIMARY KEY, doc JSON, due DATE AS (doc->'$.due') STORED);
INSERT INTO orders_gc (id, doc) SELECT id, doc FROM staging;
SELECT COUNT(*), COUNT(due) FROM orders_gc;

-- 4. the same values as text are rejected
DELETE FROM orders;
INSERT INTO orders SELECT id, doc->>'$.order', doc->>'$.due' FROM staging;

Result on 26.10.0.1 (9.7.4 prints the same):

INSERT INTO orders SELECT id, doc->>'$.order', doc->'$.due' FROM staging;
  Query OK, 6 rows affected
  Records: 6  Duplicates: 0  Warnings: 0
SHOW WARNINGS;
  Empty set
+----+----------+---------------------+------------+
| id | order_no | due_in_json         | due        |
+----+----------+---------------------+------------+
|  1 | A1       | 2024-02-29          | 2024-02-29 |
|  2 | A2       | 2023-02-29          | NULL       |
|  3 | A3       | 2024-04-31          | NULL       |
|  4 | A4       | 2024-06-31T08:00:00 | NULL       |
|  5 | A5       | 0000-00-00          | NULL       |
|  6 | A6       | 2024-00-10          | NULL       |
+----+----------+---------------------+------------+

UPDATE orders o JOIN staging s USING (id) SET o.due = s.doc->'$.due';
  Query OK, 6 rows affected
  Rows matched: 6  Changed: 6  Warnings: 0
SHOW WARNINGS;  -> Empty set
SELECT COUNT(*), COUNT(due) FROM orders;  -> 6, 1   (the five 2000-01-01 dates are now NULL)

INSERT ... CAST(doc->'$.due' AS DATE):     Query OK, 6 rows, Warnings: 0;  COUNT(*), COUNT(due) -> 6, 1
INSERT INTO orders_gc (generated column):   Query OK, 6 rows, Warnings: 0;  COUNT(*), COUNT(due) -> 6, 1

INSERT INTO orders SELECT id, doc->>'$.order', doc->>'$.due' FROM staging;
  ERROR 1292 (22007): Incorrect date value: '2023-02-29' for column 'due' at row 2

Result on 8.4.13.1 (mysql --force): the first INSERT, the UPDATE, the CAST
INSERT and the INSERT INTO orders_gc each fail with
  ERROR 3156 (22018): Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column json_extract at row 2
(so after the failed UPDATE, SELECT COUNT(*), COUNT(due) FROM orders returns
6, 6: the existing dates are kept), and the text-form INSERT fails with
ERROR 1292 as above.

Expected: the 8.4 behaviour - ERROR 3156 in strict mode (and NULL with a
warning where strict mode does not apply), as the conversion rules describe.

Suggested fix:
Json_wrapper::coerce_date() parses a J_STRING with no date flags and checks
the date afterwards; a failed check only returns true, without the
error_handler call that the parse failure makes:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql-co...
  (L3124: str_to_datetime(..., &mtime, 0, &status);
   L3137: error_handler("DATE", ER_INVALID_JSON_VALUE_FOR_CAST);
   L3140: return date->check_date(flags) != 0;)

Date_val::check_date() returns only a warning code:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/mysys/...

get_date_from_json() passes the failure up, and Item::save_in_field_inner()
turns it into NULL for a JSON item assigned to a DATE field
(Item_typecast_date::val_date() returns NULL the same way for an explicit
CAST):
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...

Up to 9.6 the string was parsed with the caller's flags, so an impossible date
failed inside str_to_datetime() and reached error_handler (8.4.13):
https://github.com/mysql/mysql-server/blob/8a180268d832300c09a375115dfa0ea77d83f1af/sql-co...

The parse flags became 0 and the final "return date->check_date(flags) != 0"
was added by the fix for Bug#38998827 (screening J_DATE/J_DATETIME/
J_TIMESTAMP values too):
https://github.com/mysql/mysql-server/commit/46227d2ee07c3a1415f9a9ab4f276403381d1705

Reporting a failed check_date() through error_handler("DATE",
ER_INVALID_JSON_VALUE_FOR_CAST), at least for J_STRING values, would restore
the 8.4 behaviour (error in strict mode, NULL with a warning otherwise).

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.040 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.