Bug #121869 JSON time copied into a TIME(n) column by INSERT ... SELECT, UPDATE, triggers or generated columns is not rounded

Submitted: 22 Nov 22:51
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Data Types Severity:S2 (Serious)
Version:26.10.0 (trunk 06bcb0eb0ad) OS:Any
Assigned to: CPU Architecture:Any

[22 Nov 22:51] Ke Han
Description:
A JSON value that holds a time (a JSON TIME scalar, as produced by
CAST(time_col AS JSON) or JSON_OBJECT('t', time_col), or a JSON string in time
format) is stored in a TIME(n) column without rounding to the column's n
fractional digits when it is assigned field to field: INSERT ... SELECT doc,
UPDATE ... SET tm = doc, a stored procedure parameter or variable, a stored
function that RETURNS TIME(n), a trigger (SET NEW.tm = NEW.doc) and a
generated column (tm TIME(n) AS (doc)).

- TIME (n = 0) truncates positive values and floors negative ones:
  12:00:00.7 is stored as 12:00:00 (expected 12:00:01), -00:00:00.4 as
  -00:00:01 (expected 00:00:00), 10:00:00.999999 as 10:00:00.
- TIME(1) to TIME(5) keep digits they cannot hold: a TIME(3) column stores
  09:30:00.1234, which every client sees as 09:30:00.123 but which compares
  as 09:30:00.1234.
- Negative values can lose a whole second: -10:00:00.000001 is stored in a
  TIME(3) column as -10:00:01.000 (expected -10:00:00.000).

The same values through an expression (CAST(doc AS TIME(n)), doc->'$',
JSON_VALUE(... RETURNING TIME(6)), VALUES((SELECT doc ...)), JSON_TABLE) are
rounded, and 8.4.13 rounds on every route. The manual: "Inserting a TIME,
DATE, or TIMESTAMP value with a fractional seconds part into a column of the
same type but having fewer fractional digits results in rounding."
(https://dev.mysql.com/doc/refman/26.10/en/fractional-seconds.html); with
TIME_TRUNCATE_FRACTIONAL the value would be truncated, never floored. The
default (strict) sql_mode is used, and there is no error or warning.

Consequences: a UNIQUE key on a TIME(3) column holds two rows that both read
09:30:00.123 (the second INSERT fails with ERROR 1062 on 8.4.13),
DELETE ... WHERE starts = '09:30:00.123' deletes only one of them, CHECK TABLE
reports OK, and mysqldump writes both rows as '09:30:00.123', so loading the
dump fails with ERROR 1062.

How to repeat:
CREATE DATABASE m217; USE m217;
CREATE TABLE src (id INT PRIMARY KEY, doc JSON);
INSERT INTO src VALUES (1, CAST(TIME'12:00:00.7' AS JSON)), (2, CAST(TIME'-00:00:00.4' AS JSON)),
  (3, CAST(TIME'-10:00:00.000001' AS JSON)), (4, CAST(TIME'10:00:00.999999' AS JSON)),
  (5, '"09:30:00.1234"'), (6, '"12:00:00.123456"');
CREATE TABLE t (id INT PRIMARY KEY, t0 TIME, t3 TIME(3), t5 TIME(5));

-- 1. INSERT ... SELECT from the JSON column (CAST(... AS TIME(6)) shows the stored digits)
INSERT INTO t SELECT id, doc, doc, doc FROM src;
SELECT t.id, src.doc, t.t0, CAST(t.t3 AS TIME(6)) AS t3_stored, CAST(t.t5 AS TIME(6)) AS t5_stored
FROM t JOIN src USING (id) ORDER BY id;

-- 2. the same values through an expression are rounded
DELETE FROM t;
INSERT INTO t SELECT id, CAST(doc AS TIME(6)), CAST(doc AS TIME(6)), CAST(doc AS TIME(6)) FROM src;
SELECT id, t0, CAST(t3 AS TIME(6)) AS t3_stored, CAST(t5 AS TIME(6)) AS t5_stored FROM t ORDER BY id;

-- 3. other field-to-field assignments of the JSON value
DELETE FROM t;
INSERT INTO t (id) SELECT id FROM src;
UPDATE t JOIN src USING (id) SET t.t0 = src.doc, t.t3 = src.doc, t.t5 = src.doc;
SELECT id, t0, CAST(t3 AS TIME(6)) AS t3_stored FROM t WHERE id IN (1, 5) ORDER BY id;
CREATE TABLE tg (id INT PRIMARY KEY, doc JSON, t3 TIME(3) AS (doc) STORED);
INSERT INTO tg (id, doc) SELECT id, doc FROM src WHERE id = 5;
SELECT id, t3, CAST(t3 AS TIME(6)) AS t3_stored FROM tg;
CREATE TABLE tt (id INT PRIMARY KEY, doc JSON, t3 TIME(3));
CREATE TRIGGER tt_bi BEFORE INSERT ON tt FOR EACH ROW SET NEW.t3 = NEW.doc;
INSERT INTO tt (id, doc) SELECT id, doc FROM src WHERE id = 5;
SELECT id, t3, CAST(t3 AS TIME(6)) AS t3_stored FROM tt;
CREATE PROCEDURE p_ins(k INT, v JSON) INSERT INTO t VALUES (k, v, v, v);
CALL p_ins(7, '"09:30:00.1234"');
SELECT id, t0, CAST(t3 AS TIME(6)) AS t3_stored FROM t WHERE id = 7;

-- 4. a UNIQUE TIME(3) key then holds two rows that both read 09:30:00.123
CREATE TABLE shifts (id INT PRIMARY KEY, starts TIME(3), UNIQUE KEY u (starts));
INSERT INTO shifts VALUES (1, '09:30:00.123');
INSERT INTO shifts SELECT 2, doc FROM src WHERE id = 5;     -- doc is "09:30:00.1234": expected ERROR 1062
SELECT id, starts, CAST(starts AS TIME(6)) AS starts_stored FROM shifts ORDER BY id;
DELETE FROM shifts WHERE starts = '09:30:00.123';
SELECT id, starts FROM shifts;
CHECK TABLE shifts EXTENDED;

Result on 26.10.0:
1. INSERT ... SELECT doc
+----+--------------------+-----------+------------------+------------------+
| id | doc                | t0        | t3_stored        | t5_stored        |
+----+--------------------+-----------+------------------+------------------+
|  1 | "12:00:00.700000"  | 12:00:00  | 12:00:00.700000  | 12:00:00.700000  |
|  2 | "-00:00:00.400000" | -00:00:01 | -00:00:00.400000 | -00:00:00.400000 |
|  3 | "-10:00:00.000001" | -10:00:01 | -10:00:01.000000 | -10:00:00.000001 |
|  4 | "10:00:00.999999"  | 10:00:00  | 10:00:00.999900  | 10:00:00.999999  |
|  5 | "09:30:00.1234"    | 09:30:00  | 09:30:00.123400  | 09:30:00.123400  |
|  6 | "12:00:00.123456"  | 12:00:00  | 12:00:00.123400  | 12:00:00.123456  |
+----+--------------------+-----------+------------------+------------------+
2. CAST(doc AS TIME(6)) (correct; 8.4.13 returns this table for step 1 too)
+----+-----------+------------------+------------------+
| id | t0        | t3_stored        | t5_stored        |
+----+-----------+------------------+------------------+
|  1 | 12:00:01  | 12:00:00.700000  | 12:00:00.700000  |
|  2 | 00:00:00  | -00:00:00.400000 | -00:00:00.400000 |
|  3 | -10:00:00 | -10:00:00.000000 | -10:00:00.000000 |
|  4 | 10:00:01  | 10:00:01.000000  | 10:00:01.000000  |
|  5 | 09:30:00  | 09:30:00.123000  | 09:30:00.123400  |
|  6 | 12:00:00  | 12:00:00.123000  | 12:00:00.123460  |
+----+-----------+------------------+------------------+
3. UPDATE:            (1, 12:00:00, 12:00:00.700000), (5, 09:30:00, 09:30:00.123400)
   generated column:  (5, 09:30:00.123, 09:30:00.123400)
   trigger:           (5, 09:30:00.123, 09:30:00.123400)
   procedure:         (7, 09:30:00, 09:30:00.123400)
   (8.4.13: 12:00:01 and 09:30:00.123000 on every route)
4. the second INSERT succeeds; shifts holds
   (1, 09:30:00.123, 09:30:00.123000) and (2, 09:30:00.123, 09:30:00.123400);
   the DELETE removes only row 1, row 2 (09:30:00.123) is left;
   CHECK TABLE: status OK.
   On 8.4.13 the second INSERT fails:
   ERROR 1062 (23000): Duplicate entry '09:30:00.123' for key 'shifts.u'
mysqldump of shifts on 26.10.0 writes
  INSERT INTO `shifts` VALUES (1,'09:30:00.123'),(2,'09:30:00.123');
and loading it fails with ERROR 1062 (23000): Duplicate entry '09:30:00.123'
for key 'shifts.u'.

Suggested fix:
field_conv_slow() converts a JSON field to a TIME field with
  Time_val time;
  res = from->val_time(&time);
  store_res = to->store_time(time, 0);
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
i.e. it tells the field that the value has no fractional digits, although
Field_json::val_time() returns the JSON time with all six:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
Field_time::store_time(Time_val, uint8 dec_arg) adjusts the fraction only when
dec_arg > dec:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
so the unadjusted value reaches Time_val::store_time(), which expects a value
that is already rounded (an assert only) and packs the raw bits:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/mysys/...
That gives truncation (floor for negative values) for TIME and keeps extra
digits for TIME(1..5).

The other callers pass the real precision: copy_temporal() passes
from->decimals(), JSON_TABLE passes DATETIME_MAX_DECIMALS, and
Item::save_in_field_inner() passes the item's decimals:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
In 9.6.0 the same branch converted the value to a MYSQL_TIME and called
store_time(&mtime), which rounds:
https://github.com/mysql/mysql-server/blob/mysql-9.6.0/sql/field_conv.cc#L859-L884
and store_time(time, 0) is there from 9.7.0 on.

Passing DATETIME_MAX_DECIMALS (or the value's actual number of digits) instead
of 0 should fix it.

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

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