Description:
CREATE JSON RELATIONAL DUALITY VIEW accepts tables whose primary key is a
binary column: BINARY(16) filled with UUID_TO_BIN() (the layout the manual
recommends for UUID keys), VARBINARY, BIT, or any binary string. SELECT
shows such a key as "base64:type254:...", and INSERT through the view
decodes it correctly. The statements that the server generates for DELETE
and UPDATE do not decode it. They compare the key with the text of its
base64 form, e.g.
DELETE FROM item WHERE id = _utf8mb4 'base64:type254:bM14DLq6ECaVZFuMZWAk2w=='
which matches no row. There is no error or warning:
- DELETE of a document whose nested children (with the DELETE annotation)
have binary keys: "1 row affected". The root row is deleted, but every
child row stays.
- UPDATE that changes a field of such a child, or removes children from
the document: "0 rows affected". Nothing is changed, and the document
still shows the old children.
- A document inserted later with the same root key (a reused order number)
shows the deleted order's items.
- UPDATE or DELETE of a document whose root key is binary: "0 rows
affected". The document stays as it was.
The manual (JSON Duality View requirements) says the opposite: "If
sub-object has a DELETE annotation, then all elements of the nested
sub-object must be deleted" and, for updates, "Any missing sub-objects or
elements in sub-objects are deleted". It states no restriction on
primary-key types. With CHAR(36) keys the same statements work (2 rows
affected by the UPDATE, 3 by the DELETE, children deleted). Without a
foreign key the damage is silent; ON DELETE CASCADE hides the DELETE case,
and RESTRICT makes it fail with error 1451.
When the client sends the document back as JSON text (the read/modify/write
round trip) instead of using JSON_SET() on the server, some of these updates
fail loudly instead: an update that keeps some of the children fails with
error 1062 (the unchanged child is "deleted" by its base64 text, i.e. not at
all, and inserted again by its decoded value), and an update of a document
whose root key is binary fails with error 6496 ("Cannot update primary key
column").
Cause, in sql/json_duality_view/dml.cc at trunk 06bcb0eb0ad:
append_json_dom(sbuf, dom, b64) decodes the "base64:typeNN:" text of a
binary column into a _binary literal only when b64 is true
(col_expects_b64(), append_b64_dom()). The three places that put the primary
key into a WHERE clause call it without that argument and only assert that
the key is not such a column:
- make_delete(), line 2362: assert(!col_expects_b64(
ct_node.primary_key_column())); append_json_dom(&sbuf, pk_val). It is used
for document deletes and for the deleted children of an update;
- make_update(), line 2316, and select_diff_vector(), line 1789: the same for
UPDATE ... WHERE and for the existence check of child rows.
Release builds compile the asserts out, and the DDL checks
(sql/json_duality_view/ddl.cc) have no rule against binary keys.
How to repeat:
CREATE DATABASE m230;
USE m230;
CREATE TABLE orders (id INT PRIMARY KEY, customer VARCHAR(20));
CREATE TABLE item (id BINARY(16) PRIMARY KEY, order_id INT, sku VARCHAR(20), qty INT);
CREATE JSON RELATIONAL DUALITY VIEW order_dv AS
SELECT JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE)
'_id': id, 'customer': customer,
'items': (SELECT JSON_ARRAYAGG(JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE)
'id': id, 'sku': sku, 'qty': qty))
FROM item WHERE item.order_id = orders.id))
FROM orders;
INSERT INTO orders VALUES (1, 'ann');
INSERT INTO item VALUES (UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'), 1, 'pen', 2),
(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024dc'), 1, 'ink', 1);
SELECT data FROM order_dv;
-- 1. change the quantity of the first item
UPDATE order_dv SET data = JSON_SET(data, '$.items[0].qty', 7) WHERE data->'$._id' = 1;
SELECT BIN_TO_UUID(id), sku, qty FROM item ORDER BY sku;
-- 2. remove both items from the document
UPDATE order_dv SET data = JSON_SET(data, '$.items', JSON_ARRAY()) WHERE data->'$._id' = 1;
SELECT BIN_TO_UUID(id), sku, qty FROM item ORDER BY sku;
-- 3. delete the document
DELETE FROM order_dv WHERE data->'$._id' = 1;
SELECT * FROM orders;
SELECT BIN_TO_UUID(id), order_id, sku FROM item ORDER BY sku;
-- 4. a new order that reuses the key
INSERT INTO order_dv VALUES ('{"_id": 1, "customer": "zoe"}');
SELECT data FROM order_dv;
-- 5. a document whose root key is binary
CREATE TABLE doc (id BINARY(16) PRIMARY KEY, title VARCHAR(20));
CREATE JSON RELATIONAL DUALITY VIEW doc_dv AS
SELECT JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE) '_id': id, 'title': title) FROM doc;
INSERT INTO doc VALUES (UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'), 'contract');
UPDATE doc_dv SET data = JSON_SET(data, '$.title', 'contract v2') WHERE data->>'$.title' = 'contract';
DELETE FROM doc_dv WHERE data->>'$.title' = 'contract';
SELECT BIN_TO_UUID(id), title FROM doc;
Result on 26.10.0.1:
SELECT data FROM order_dv:
{"_id": 1, "items": [{"id": "base64:type254:bM14DLq6ECaVZFuMZWAk2w==", "qty": 2, "sku": "pen"},
{"id": "base64:type254:bM14DLq6ECaVZFuMZWAk3A==", "qty": 1, "sku": "ink"}], "customer": "ann", ...}
1. UPDATE: Query OK, 0 rows affected, no warning; pen still has qty 2.
2. UPDATE: Query OK, 0 rows affected, no warning; both items are still in
the table and in the document.
3. DELETE: Query OK, 1 row affected; orders is empty, but item still holds
6ccd780c-baba-1026-9564-5b8c656024dc 1 ink
6ccd780c-baba-1026-9564-5b8c656024db 1 pen
4. the new document for "zoe" (inserted without items):
{"_id": 1, "items": [{"id": "base64:type254:bM14DLq6ECaVZFuMZWAk2w==", "qty": 2, "sku": "pen"},
{"id": "base64:type254:bM14DLq6ECaVZFuMZWAk3A==", "qty": 1, "sku": "ink"}], "customer": "zoe", ...}
5. UPDATE: Query OK, 0 rows affected; DELETE: Query OK, 0 rows affected;
the row is unchanged:
6ccd780c-baba-1026-9564-5b8c656024db contract
VARBINARY(16) and BIT(64) item keys give the same results for steps 2 to 4.
Control: the same statements with item.id CHAR(36) (and the UUID text as
values) report 2 rows affected for step 2 and 3 for step 3, and the item
table is empty after each.
Expected: the UPDATE changes or deletes the children as the new document
says; the DELETE removes the document's rows, including all nested children
with the DELETE annotation; or, if binary keys are not supported, CREATE
JSON RELATIONAL DUALITY VIEW rejects them.
Suggested fix:
Render the primary key with append_json_dom(..., col_expects_b64(kci)) in
make_delete(), make_update() and select_diff_vector(), as the other value
renderings in dml.cc already do (make_insert(), the SET list of
make_update()), and replace the asserts by that call. Alternatively, reject
binary primary keys in the DDL checks of ddl.cc.
Description: CREATE JSON RELATIONAL DUALITY VIEW accepts tables whose primary key is a binary column: BINARY(16) filled with UUID_TO_BIN() (the layout the manual recommends for UUID keys), VARBINARY, BIT, or any binary string. SELECT shows such a key as "base64:type254:...", and INSERT through the view decodes it correctly. The statements that the server generates for DELETE and UPDATE do not decode it. They compare the key with the text of its base64 form, e.g. DELETE FROM item WHERE id = _utf8mb4 'base64:type254:bM14DLq6ECaVZFuMZWAk2w==' which matches no row. There is no error or warning: - DELETE of a document whose nested children (with the DELETE annotation) have binary keys: "1 row affected". The root row is deleted, but every child row stays. - UPDATE that changes a field of such a child, or removes children from the document: "0 rows affected". Nothing is changed, and the document still shows the old children. - A document inserted later with the same root key (a reused order number) shows the deleted order's items. - UPDATE or DELETE of a document whose root key is binary: "0 rows affected". The document stays as it was. The manual (JSON Duality View requirements) says the opposite: "If sub-object has a DELETE annotation, then all elements of the nested sub-object must be deleted" and, for updates, "Any missing sub-objects or elements in sub-objects are deleted". It states no restriction on primary-key types. With CHAR(36) keys the same statements work (2 rows affected by the UPDATE, 3 by the DELETE, children deleted). Without a foreign key the damage is silent; ON DELETE CASCADE hides the DELETE case, and RESTRICT makes it fail with error 1451. When the client sends the document back as JSON text (the read/modify/write round trip) instead of using JSON_SET() on the server, some of these updates fail loudly instead: an update that keeps some of the children fails with error 1062 (the unchanged child is "deleted" by its base64 text, i.e. not at all, and inserted again by its decoded value), and an update of a document whose root key is binary fails with error 6496 ("Cannot update primary key column"). Cause, in sql/json_duality_view/dml.cc at trunk 06bcb0eb0ad: append_json_dom(sbuf, dom, b64) decodes the "base64:typeNN:" text of a binary column into a _binary literal only when b64 is true (col_expects_b64(), append_b64_dom()). The three places that put the primary key into a WHERE clause call it without that argument and only assert that the key is not such a column: - make_delete(), line 2362: assert(!col_expects_b64( ct_node.primary_key_column())); append_json_dom(&sbuf, pk_val). It is used for document deletes and for the deleted children of an update; - make_update(), line 2316, and select_diff_vector(), line 1789: the same for UPDATE ... WHERE and for the existence check of child rows. Release builds compile the asserts out, and the DDL checks (sql/json_duality_view/ddl.cc) have no rule against binary keys. How to repeat: CREATE DATABASE m230; USE m230; CREATE TABLE orders (id INT PRIMARY KEY, customer VARCHAR(20)); CREATE TABLE item (id BINARY(16) PRIMARY KEY, order_id INT, sku VARCHAR(20), qty INT); CREATE JSON RELATIONAL DUALITY VIEW order_dv AS SELECT JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE) '_id': id, 'customer': customer, 'items': (SELECT JSON_ARRAYAGG(JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE) 'id': id, 'sku': sku, 'qty': qty)) FROM item WHERE item.order_id = orders.id)) FROM orders; INSERT INTO orders VALUES (1, 'ann'); INSERT INTO item VALUES (UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'), 1, 'pen', 2), (UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024dc'), 1, 'ink', 1); SELECT data FROM order_dv; -- 1. change the quantity of the first item UPDATE order_dv SET data = JSON_SET(data, '$.items[0].qty', 7) WHERE data->'$._id' = 1; SELECT BIN_TO_UUID(id), sku, qty FROM item ORDER BY sku; -- 2. remove both items from the document UPDATE order_dv SET data = JSON_SET(data, '$.items', JSON_ARRAY()) WHERE data->'$._id' = 1; SELECT BIN_TO_UUID(id), sku, qty FROM item ORDER BY sku; -- 3. delete the document DELETE FROM order_dv WHERE data->'$._id' = 1; SELECT * FROM orders; SELECT BIN_TO_UUID(id), order_id, sku FROM item ORDER BY sku; -- 4. a new order that reuses the key INSERT INTO order_dv VALUES ('{"_id": 1, "customer": "zoe"}'); SELECT data FROM order_dv; -- 5. a document whose root key is binary CREATE TABLE doc (id BINARY(16) PRIMARY KEY, title VARCHAR(20)); CREATE JSON RELATIONAL DUALITY VIEW doc_dv AS SELECT JSON_DUALITY_OBJECT(WITH (INSERT, UPDATE, DELETE) '_id': id, 'title': title) FROM doc; INSERT INTO doc VALUES (UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'), 'contract'); UPDATE doc_dv SET data = JSON_SET(data, '$.title', 'contract v2') WHERE data->>'$.title' = 'contract'; DELETE FROM doc_dv WHERE data->>'$.title' = 'contract'; SELECT BIN_TO_UUID(id), title FROM doc; Result on 26.10.0.1: SELECT data FROM order_dv: {"_id": 1, "items": [{"id": "base64:type254:bM14DLq6ECaVZFuMZWAk2w==", "qty": 2, "sku": "pen"}, {"id": "base64:type254:bM14DLq6ECaVZFuMZWAk3A==", "qty": 1, "sku": "ink"}], "customer": "ann", ...} 1. UPDATE: Query OK, 0 rows affected, no warning; pen still has qty 2. 2. UPDATE: Query OK, 0 rows affected, no warning; both items are still in the table and in the document. 3. DELETE: Query OK, 1 row affected; orders is empty, but item still holds 6ccd780c-baba-1026-9564-5b8c656024dc 1 ink 6ccd780c-baba-1026-9564-5b8c656024db 1 pen 4. the new document for "zoe" (inserted without items): {"_id": 1, "items": [{"id": "base64:type254:bM14DLq6ECaVZFuMZWAk2w==", "qty": 2, "sku": "pen"}, {"id": "base64:type254:bM14DLq6ECaVZFuMZWAk3A==", "qty": 1, "sku": "ink"}], "customer": "zoe", ...} 5. UPDATE: Query OK, 0 rows affected; DELETE: Query OK, 0 rows affected; the row is unchanged: 6ccd780c-baba-1026-9564-5b8c656024db contract VARBINARY(16) and BIT(64) item keys give the same results for steps 2 to 4. Control: the same statements with item.id CHAR(36) (and the UUID text as values) report 2 rows affected for step 2 and 3 for step 3, and the item table is empty after each. Expected: the UPDATE changes or deletes the children as the new document says; the DELETE removes the document's rows, including all nested children with the DELETE annotation; or, if binary keys are not supported, CREATE JSON RELATIONAL DUALITY VIEW rejects them. Suggested fix: Render the primary key with append_json_dom(..., col_expects_b64(kci)) in make_delete(), make_update() and select_diff_vector(), as the other value renderings in dml.cc already do (make_insert(), the SET list of make_update()), and replace the asserts by that call. Alternatively, reject binary primary keys in the DDL checks of ddl.cc.