Bug #122073 JSON duality view UPDATE from JSON text deletes and re-inserts every child keyed by DATETIME/DATE/TIME/TIMESTAMP

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

[5 Dec 2:49] Ke Han
Description:
A duality view has a nested child array (JSON_ARRAYAGG) over a table whose
primary key is temporal (DATETIME, DATE, TIME or TIMESTAMP, any precision).
A client reads a document, changes only a parent field, and writes the
document back as JSON text with its ETAG: the usual read/modify/write round
trip. The update reports "5 rows affected" for one changed row. It deletes
every child row and inserts it again from the projected columns only:
- the child table's columns that the view does not project (audit columns,
  counters, flags) are reset to their defaults;
- every row of another table that references a child with ON DELETE CASCADE
  is deleted, ON DELETE SET NULL references are cleared, and DELETE
  triggers on the child table fire.
There is no error or warning. The manual says: "If an object is modified
but sub-object columns are not modified, then only the object's base table
is updated". With INT, DECIMAL or ENUM child keys, and with the same change
made by JSON_SET(data, ...) on the server, the update reports "1 row
affected" and leaves the children alone.

Cause, in sql/json_duality_view/dml.cc at trunk 06bcb0eb0ad:
jdv_handle_update() (line 2497) matches the children of the current
document (built from the base tables: temporal columns are JSON
DATETIME/DATE/TIME values) with those of the new document (parsed from JSON
text: the same values are JSON strings). It sorts them with
compare_bindings() (line 1147), which orders the bindings of one table by
compare_doms(apk, bpk) (line 1175), i.e. Json_wrapper::compare()
(sql-common/json_dom.cc line 2620). merge_bindings_for_update() (line 1396)
then pairs the equal ones into updates. Json_wrapper::compare() compares the
JSON types first (type_comparison), so a JSON DATETIME and the string of the
same value are never equal. The existing child stays an unpaired delete and
the incoming one an unpaired insert. The deletes run first (DELETE FROM slot
WHERE start_at = _utf8mb4 '2026-11-02 09:00:00.000000', which matches), and
make_insert() inserts the row again with only the projected columns.
Numbers are not affected because Json_wrapper::compare() compares INT,
DECIMAL and DOUBLE by value.

How to repeat:
The user variable stands for the client: it holds the document as text,
exactly as a client sends it back.

CREATE DATABASE m233;
USE m233;
CREATE TABLE schedule (id INT PRIMARY KEY, title VARCHAR(30));
CREATE TABLE slot (start_at DATETIME PRIMARY KEY, schedule_id INT, room VARCHAR(10),
                   created_by VARCHAR(20) NOT NULL DEFAULT 'unknown', capacity INT NOT NULL DEFAULT 0);
CREATE TABLE booking (id INT PRIMARY KEY, slot DATETIME, person VARCHAR(20),
                      FOREIGN KEY (slot) REFERENCES slot (start_at) ON DELETE CASCADE);
CREATE JSON RELATIONAL DUALITY VIEW schedule_dv AS
  SELECT JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE)
           '_id': id, 'title': title,
           'slots': (SELECT JSON_ARRAYAGG(JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE)
                              'start': start_at, 'room': room))
                     FROM slot WHERE slot.schedule_id = schedule.id))
  FROM schedule;
INSERT INTO schedule VALUES (1, 'Dentist');
INSERT INTO slot VALUES ('2026-11-02 09:00:00', 1, 'A', 'alice', 3), ('2026-11-02 14:00:00', 1, 'B', 'bob', 5);
INSERT INTO booking VALUES (1, '2026-11-02 09:00:00', 'carol'), (2, '2026-11-02 14:00:00', 'dave');

SET @doc = (SELECT JSON_SET(data, '$.title', 'Dentist (Dr. Lee)') FROM schedule_dv WHERE data->'$._id' = 1);
SELECT @doc;
UPDATE schedule_dv SET data = @doc WHERE data->'$._id' = 1;
SELECT * FROM schedule;
SELECT * FROM slot;
SELECT * FROM booking;

Result on 26.10.0.1:
  @doc: {"_id": 1, "slots": [{"room": "A", "start": "2026-11-02 09:00:00.000000"},
         {"room": "B", "start": "2026-11-02 14:00:00.000000"}], "title": "Dentist (Dr. Lee)",
         "_metadata": {"etag": "e74bf516a6ca9713ec3b6e4606bbd550"}}
  UPDATE: Query OK, 5 rows affected, no warnings
  schedule: 1  Dentist (Dr. Lee)
  slot:
    +---------------------+-------------+------+------------+----------+
    | start_at            | schedule_id | room | created_by | capacity |
    +---------------------+-------------+------+------------+----------+
    | 2026-11-02 09:00:00 |           1 | A    | unknown    |        0 |
    | 2026-11-02 14:00:00 |           1 | B    | unknown    |        0 |
    +---------------------+-------------+------+------------+----------+
  booking: Empty set (both bookings deleted by the cascade)
DATE, TIME and TIMESTAMP(3) keys give the same result.

Control: the same schema with
  UPDATE schedule_dv SET data = JSON_SET(data, '$.title', 'Dentist (Dr. Lee)') WHERE data->'$._id' = 1;
gives "1 row affected", slot keeps (alice, 3) and (bob, 5), and both bookings
stay. An INT, DECIMAL(6,2) or ENUM key for slot with the @doc round trip
also gives "1 row affected" and no change to the children.

Expected: only schedule.title is updated, as with the JSON_SET() form and
with non-temporal keys.

Suggested fix:
Compare the primary keys of the existing and incoming children as values of
the key column's type, e.g. convert the incoming JSON value to the column's
type (as the generated SQL does) before compare_doms(), instead of comparing
JSON values type-strictly. The same type-strict comparison decides whether
the root's primary key changed (make_update()), which makes every JSON-text
update of a document with a temporal root key fail with error 6496 ("Cannot
update primary key column").

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

Page generated in 0.038 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.