From 9e43b63a4e2dfda22ba39d3141bdcdd562dcf803 Mon Sep 17 00:00:00 2001 From: D3Hunter Date: Wed, 22 Dec 2021 15:43:07 +0800 Subject: [PATCH 1/2] store json as string --- replication/row_event.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/replication/row_event.go b/replication/row_event.go index 06a201d99..866a12036 100644 --- a/replication/row_event.go +++ b/replication/row_event.go @@ -9,11 +9,12 @@ import ( "strconv" "time" - . "github.com/go-mysql-org/go-mysql/mysql" "github.com/pingcap/errors" "github.com/shopspring/decimal" "github.com/siddontang/go-log/log" "github.com/siddontang/go/hack" + + . "github.com/go-mysql-org/go-mysql/mysql" ) var errMissingTableMapEvent = errors.New("invalid table id, no corresponding table map event") @@ -1138,7 +1139,11 @@ func (e *RowsEvent) decodeValue(data []byte, tp byte, meta uint16) (v interface{ // Refer: https://github.com/shyiko/mysql-binlog-connector-java/blob/master/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/AbstractRowsEventDataDeserializer.java#L404 length = int(FixedLengthInt(data[0:meta])) n = length + int(meta) - v, err = e.decodeJsonBinary(data[meta:n]) + var d []byte + d, err = e.decodeJsonBinary(data[meta:n]) + if err == nil { + v = hack.String(d) + } case MYSQL_TYPE_GEOMETRY: // MySQL saves Geometry as Blob in binlog // Seem that the binary format is SRID (4 bytes) + WKB, outer can use From 7fb6db345895b7f50546c66e4b610a693588a675 Mon Sep 17 00:00:00 2001 From: D3Hunter Date: Wed, 22 Dec 2021 16:48:35 +0800 Subject: [PATCH 2/2] fix ut --- replication/row_event_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/replication/row_event_test.go b/replication/row_event_test.go index d81277ff6..025daa3f7 100644 --- a/replication/row_event_test.go +++ b/replication/row_event_test.go @@ -699,22 +699,22 @@ func (_ *testDecodeSuite) TestJsonCompatibility(c *C) { rows.Rows = nil err = rows.Decode(data) c.Assert(err, IsNil) - c.Assert(rows.Rows[0][2], DeepEquals, []uint8("{}")) + c.Assert(rows.Rows[0][2], DeepEquals, "{}") // after MySQL 5.7.22 data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x00\x00\x04\x00\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x01\x00\x0c\x00\x0b\x00\x01\x00\x05\xd2\x04a") rows.Rows = nil err = rows.Decode(data) c.Assert(err, IsNil) - c.Assert(rows.Rows[1][2], DeepEquals, []uint8("{}")) - c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{\"a\":1234}")) + c.Assert(rows.Rows[1][2], DeepEquals, "{}") + c.Assert(rows.Rows[2][2], DeepEquals, "{\"a\":1234}") data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x01\x00\x0c\x00\x0b\x00\x01\x00\x05\xd2\x04a\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x00\x00\x04\x00") rows.Rows = nil err = rows.Decode(data) c.Assert(err, IsNil) - c.Assert(rows.Rows[1][2], DeepEquals, []uint8("{\"a\":1234}")) - c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{}")) + c.Assert(rows.Rows[1][2], DeepEquals, "{\"a\":1234}") + c.Assert(rows.Rows[2][2], DeepEquals, "{}") // before MySQL 5.7.22 rows.ignoreJSONDecodeErr = true @@ -722,8 +722,8 @@ func (_ *testDecodeSuite) TestJsonCompatibility(c *C) { rows.Rows = nil err = rows.Decode(data) c.Assert(err, IsNil) - c.Assert(rows.Rows[1][2], DeepEquals, []uint8("null")) - c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{\"a\":1234}")) + c.Assert(rows.Rows[1][2], DeepEquals, "null") + c.Assert(rows.Rows[2][2], DeepEquals, "{\"a\":1234}") rows.ignoreJSONDecodeErr = false data = []byte("l\x00\x00\x00\x00\x00\x01\x00\x02\x00\x04\xff\xff\xf8\x01\x00\x00\x00\n{\"a\":1234}\r\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x05\xd2\x04a\xf8\x01\x00\x00\x00\x02{}\x05\x00\x00\x00\x00\x00\x00\x04\x00") @@ -731,8 +731,8 @@ func (_ *testDecodeSuite) TestJsonCompatibility(c *C) { err = rows.Decode(data) c.Assert(err, IsNil) // this value is wrong in binlog, but can be parsed without error - c.Assert(rows.Rows[1][2], DeepEquals, []uint8("{}")) - c.Assert(rows.Rows[2][2], DeepEquals, []uint8("{}")) + c.Assert(rows.Rows[1][2], DeepEquals, "{}") + c.Assert(rows.Rows[2][2], DeepEquals, "{}") } func (_ *testDecodeSuite) TestDecodeDatetime2(c *C) {