Skip to content

store json as string in rows events #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion replication/row_event.go
Original file line number Diff line number Diff line change
@@ -1151,7 +1151,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
18 changes: 9 additions & 9 deletions replication/row_event_test.go
Original file line number Diff line number Diff line change
@@ -699,40 +699,40 @@ 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
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\x01\x00\x0c\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("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")
rows.Rows = nil
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) {