Description:
With row-based replication and replica_type_conversions = 'ALL_NON_LOSSY',
a replica accepts a DECIMAL(M,D) -> DECIMAL(M',D') conversion as non-lossy
whenever M' > M, even when the replica column has fewer fraction digits
(D' < D) or fewer integer digits (M' - D' < M - D). The replicated values are
then rounded or clipped to the replica's column, with no error and no warning,
and replication continues:
DECIMAL(10,4) -> DECIMAL(12,2): 19.9950 -> 20.00, 0.0049 -> 0.00
DECIMAL(10,2) -> DECIMAL(11,0): 12345678.99 -> 12345679, 0.50 -> 1
DECIMAL(10,0) -> DECIMAL(11,5): 9999999999 -> 999999.99999, 1234567 -> 999999.99999
A truly wider column (DECIMAL(10,2) -> DECIMAL(12,4)) keeps the values, and a
smaller precision (DECIMAL(10,2) -> DECIMAL(8,2)) stops the applier with
error 13146 under the same setting, as documented. The Change Stream applier
(CHANGE REPLICATION SOURCE TO ... APPLIER_VERSION=2) stores the same values.
The manual (Replication with Differing Table Definitions on Source and
Replica, https://dev.mysql.com/doc/refman/26.10/en/replication-features-differing-tables.html):
"A conversion from DECIMAL(M,D) to DECIMAL(M',D') where D' >= D and
(M'-D') >= (M-D) is non-lossy; for any case where M' < M, D' < D, or both, only
a lossy conversion can be made." and, for ALL_NON_LOSSY: "Setting this mode has
no bearing on whether lossy conversions are permitted; this is controlled with
the ALL_LOSSY mode. If only ALL_NON_LOSSY is set, but not ALL_LOSSY, then
attempting a conversion that would result in the loss of data (such as INT to
TINYINT, or CHAR(25) to VARCHAR(20)) causes the replica to stop with an error."
So the three conversions above should stop the replica with error 13146, as
DECIMAL(10,2) -> DECIMAL(8,2) does. (The manual's next sentence, "if a value
to be stored cannot be fit in the target type, the value is rounded down
according to the rounding rules defined for the server", describes what a
permitted lossy conversion does under ALL_LOSSY. With ALL_NON_LOSSY alone,
these conversions should not be permitted at all.)
ALL_NON_LOSSY is the setting for replicas that get wider columns before the
source (a schema change rolled out to replicas first); with it, a replica
that may later be promoted silently holds different amounts.
The server test mysql-test/common/rpl/type_conversions.test expects
DECIMAL(10,5) -> DECIMAL(10,6) (one integer digit less) to replicate under
ALL_NON_LOSSY, but checks it only with 3.14159, which fits, and has no case
with a larger precision and a smaller scale:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/mysql-...
How to repeat:
Setup: a source and a replica with GTIDs and row-based replication (the
default binlog_format), for example on one Docker network:
docker network create rnet
docker run -d --name src --network rnet -e MYSQL_ROOT_PASSWORD=pw mysql:26.10.0 --server-id=1 --gtid-mode=ON --enforce-gtid-consistency=ON
docker run -d --name rep --network rnet -e MYSQL_ROOT_PASSWORD=pw mysql:26.10.0 --server-id=2 --gtid-mode=ON --enforce-gtid-consistency=ON
Then run each block with the mysql client on the server it names.
-- [source]
CREATE USER 'repl'@'%' IDENTIFIED BY 'replpw';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
-- [replica]
CHANGE REPLICATION SOURCE TO SOURCE_HOST='src', SOURCE_PORT=3306, SOURCE_USER='repl',
SOURCE_PASSWORD='replpw', GET_SOURCE_PUBLIC_KEY=1, SOURCE_AUTO_POSITION=1;
START REPLICA;
-- [source]
CREATE DATABASE shop;
CREATE TABLE shop.items (id INT PRIMARY KEY, price DECIMAL(10,4), total DECIMAL(10,2),
qty DECIMAL(10,0), ok DECIMAL(10,2));
CREATE TABLE shop.control (id INT PRIMARY KEY, amount DECIMAL(10,2));
-- [replica] once the tables have arrived, give the replica wider columns
SET SESSION sql_log_bin = 0;
ALTER TABLE shop.items MODIFY price DECIMAL(12,2), MODIFY total DECIMAL(11,0),
MODIFY qty DECIMAL(11,5), MODIFY ok DECIMAL(12,4);
ALTER TABLE shop.control MODIFY amount DECIMAL(8,2);
STOP REPLICA SQL_THREAD;
SET GLOBAL replica_type_conversions = 'ALL_NON_LOSSY';
START REPLICA SQL_THREAD;
-- [source]
INSERT INTO shop.items VALUES (1, 19.9950, 12345678.99, 9999999999, 12345678.99),
(2, 0.0049, 0.50, 1234567, -0.01);
SELECT * FROM shop.items ORDER BY id;
-- [replica]
SELECT * FROM shop.items ORDER BY id;
SELECT LAST_ERROR_NUMBER, LAST_ERROR_MESSAGE FROM performance_schema.replication_applier_status_by_worker
WHERE LAST_ERROR_NUMBER <> 0;
-- [source] control: a smaller precision stops the applier, as documented
INSERT INTO shop.control VALUES (1, 1.25);
-- [replica]
SELECT LAST_ERROR_NUMBER, LAST_ERROR_MESSAGE FROM performance_schema.replication_applier_status_by_worker
WHERE LAST_ERROR_NUMBER <> 0;
Result on 26.10.0 (8.4.13 gives the same values):
source:
+----+---------+-------------+------------+-------------+
| id | price | total | qty | ok |
+----+---------+-------------+------------+-------------+
| 1 | 19.9950 | 12345678.99 | 9999999999 | 12345678.99 |
| 2 | 0.0049 | 0.50 | 1234567 | -0.01 |
+----+---------+-------------+------------+-------------+
replica (price DECIMAL(12,2), total DECIMAL(11,0), qty DECIMAL(11,5), ok DECIMAL(12,4)):
+----+-------+----------+--------------+---------------+
| id | price | total | qty | ok |
+----+-------+----------+--------------+---------------+
| 1 | 20.00 | 12345679 | 999999.99999 | 12345678.9900 |
| 2 | 0.00 | 1 | 999999.99999 | -0.0100 |
+----+-------+----------+--------------+---------------+
applier errors after the items insert: none (empty set); no warning in the
replica's error log.
after the control insert:
13146 | Worker 1 failed executing transaction '...:12' at source log binlog.000002,
end_log_pos ...; Column 2 (counting the first column as number 1) of table
'shop.control' cannot be converted from type 'decimal(10,2)' to type 'decimal(8,2)'
Expected: the applier stops with error 13146 for shop.items too (price,
total and qty), as it does for shop.control; only the ok column
(DECIMAL(10,2) -> DECIMAL(12,4)) is a non-lossy conversion.
Suggested fix:
can_convert_field_to() passes columns of the same type to
field->compatible_field_size() and then to is_conversion_ok(order), which
accepts order < 0 (non-lossy) under ALL_NON_LOSSY and order > 0 (lossy) only
under ALL_LOSSY:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/rp...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/rp...
Field_new_decimal::compatible_field_size() orders the conversion by precision
first and looks at the scale only when the precisions are equal:
uint const source_precision = (field_metadata >> 8U) & 0x00ff;
uint const source_decimal = field_metadata & 0x00ff;
const int order = compare(source_precision, precision);
*order_var = order != 0 ? order : compare(source_decimal, dec);
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
so any larger target precision gives -1 (non-lossy), whatever happens to the
scale and the integer digits, and equal precisions compare only the scales.
The row is then copied through the conversion table and stored by
Field_new_decimal::store_decimal(), which rounds to the target scale and clips
to the target range.
Following the manual's rule would fix it: return 0 when M' = M and D' = D,
-1 only when D' >= D and M' - D' >= M - D, and 1 (lossy) otherwise.