I have been getting wrong data in Clickhouse when working with MaterializeMysql database engine. It affects one particular field only that is decimal (32,8) and contains a financial amount (like money, crypto, etc.). I know it is experimental but 

The replication works perfectly when I drop the database on Clickhouse and recreated it from scratch (initial replication). The only time the values go haywire is when I start inserting, deleting and updating the source table in MySQL. 

**Version:** 21.4.6.55

**Table definition from "show create table"**
CREATE TABLE clickH.actions
(
    `id` Int64,
    `wallet_id` Int64,
    `tx_id` Int64,
    `tx_timestamp` Int64,
    `counterparty_wallet_id` Int64,
    `amount` Decimal(32, 8),
    `tx_hash` String,
    `_sign` Int8 MATERIALIZED 1,
    `_version` UInt64 MATERIALIZED 1,
    INDEX _version _version TYPE minmax GRANULARITY 1
)
ENGINE = ReplacingMergeTree(_version)
PARTITION BY intDiv(id, 18446744073709551)
ORDER BY (wallet_id, counterparty_wallet_id, id)
SETTINGS index_granularity = 8192



**example values mysql ARE CORRECT**
**# amount, id**
'-0.00010000', '745620408'
'267.01256666', '784811048'
'-0.00010000', '1056421051'
'-0.09516232', '1057113705'
'-0.00010000', '1084950022'

**same rows clickhouse ARE WRONG**
**amount/id**
8388611294967299294967337.94957295	1056421051
8388611294967299294967337.94957295	745620408
8388611294967299294967337.94957295	1084950022
8388611294967299294967337.85451063	1057113705
8388608000000000000000267.01256666	784811048

Important is to note that those values were correct in the initial sync and got wrong while further updates have been done, NOT ON THOSE ROWS! Ids are loaded incremental.


On the MySql side, i have binlog buffer time on 10000s. Could this have an impact? Otherwise, I don't know what else can.

#include "MySQLReplication.h"
#include <DataTypes/DataTypeString.h>
#include <IO/ReadBufferFromString.h>
#include <IO/MySQLBinlogEventReadBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/Operators.h>
#include <base/DateLUT.h>
#include <Common/FieldVisitorToString.h>
#include <Core/MySQL/PacketsGeneric.h>
#include <Core/MySQL/PacketsProtocolText.h>
namespace DB
{
namespace ErrorCodes
{
    extern const int UNKNOWN_EXCEPTION;
    extern const int LOGICAL_ERROR;
    extern const int ATTEMPT_TO_READ_AFTER_EOF;
    extern const int CANNOT_READ_ALL_DATA;
}
namespace MySQLReplication
{
    using namespace MySQLProtocol;
    using namespace MySQLProtocol::Generic;
    using namespace MySQLProtocol::ProtocolText;
    
    void EventHeader::parse(ReadBuffer & payload)