Skip to content
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

row_event.go: fix the set bytes value in revert order #319

Merged
merged 2 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (s *clientTestSuite) TestConn_TLS(c *C) {

expected := "either ServerName or InsecureSkipVerify must be specified in the tls.Config"
if !strings.Contains(err.Error(), expected) {
c.Fatal("expected '%s' to contain '%s'", err.Error(), expected)
c.Fatalf("expected '%s' to contain '%s'", err.Error(), expected)
}
}

Expand Down
2 changes: 1 addition & 1 deletion replication/binlogsyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (b *BinlogSyncer) registerSlave() error {
if b.cfg.HeartbeatPeriod > 0 {
_, err = b.c.Execute(fmt.Sprintf("SET @master_heartbeat_period=%d;", b.cfg.HeartbeatPeriod))
if err != nil {
log.Error("failed to set @master_heartbeat_period=%d", b.cfg.HeartbeatPeriod, err)
log.Errorf("failed to set @master_heartbeat_period=%d, err: %v", b.cfg.HeartbeatPeriod, err)
return errors.Trace(err)
}
}
Expand Down
44 changes: 38 additions & 6 deletions replication/row_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ func (e *RowsEvent) decodeValue(data []byte, tp byte, meta uint16) (v interface{
v = formatZeroTime(0, 0)
} else {
v = e.parseFracTime(fracTime{
Time: time.Unix(int64(t), 0),
Dec: 0,
Time: time.Unix(int64(t), 0),
Dec: 0,
timestampStringLocation: e.timestampStringLocation,
})
}
Expand Down Expand Up @@ -490,7 +490,7 @@ func (e *RowsEvent) decodeValue(data []byte, tp byte, meta uint16) (v interface{
v = int64(data[0])
n = 1
case 2:
v = int64(binary.BigEndian.Uint16(data))
v = int64(binary.LittleEndian.Uint16(data))
n = 2
default:
err = fmt.Errorf("Unknown ENUM packlen=%d", l)
Expand All @@ -499,7 +499,7 @@ func (e *RowsEvent) decodeValue(data []byte, tp byte, meta uint16) (v interface{
n = int(meta & 0xFF)
nbits := n * 8

v, err = decodeBit(data, nbits, n)
v, err = littleDecodeBit(data, nbits, n)
case MYSQL_TYPE_BLOB:
v, n, err = decodeBlob(data, meta)
case MYSQL_TYPE_VARCHAR,
Expand Down Expand Up @@ -650,6 +650,38 @@ func decodeBit(data []byte, nbits int, length int) (value int64, err error) {
return
}

func littleDecodeBit(data []byte, nbits int, length int) (value int64, err error) {
if nbits > 1 {
switch length {
case 1:
value = int64(data[0])
case 2:
value = int64(binary.LittleEndian.Uint16(data))
case 3:
value = int64(FixedLengthInt(data[0:3]))
case 4:
value = int64(binary.LittleEndian.Uint32(data))
case 5:
value = int64(FixedLengthInt(data[0:5]))
case 6:
value = int64(FixedLengthInt(data[0:6]))
case 7:
value = int64(FixedLengthInt(data[0:7]))
case 8:
value = int64(binary.LittleEndian.Uint64(data))
default:
err = fmt.Errorf("invalid bit length %d", length)
}
} else {
if length != 1 {
err = fmt.Errorf("invalid bit length %d", length)
} else {
value = int64(data[0])
}
}
return
}

func decodeTimestamp2(data []byte, dec uint16, timestampStringLocation *time.Location) (interface{}, int, error) {
//get timestamp binary length
n := int(4 + (dec+1)/2)
Expand All @@ -669,8 +701,8 @@ func decodeTimestamp2(data []byte, dec uint16, timestampStringLocation *time.Loc
}

return fracTime{
Time: time.Unix(sec, usec*1000),
Dec: int(dec),
Time: time.Unix(sec, usec*1000),
Dec: int(dec),
timestampStringLocation: timestampStringLocation,
}, n, nil
}
Expand Down
65 changes: 65 additions & 0 deletions replication/row_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,71 @@ func (_ *testDecodeSuite) TestParseJsonDecimal(c *C) {
}
}

func (_ *testDecodeSuite) TestEnum(c *C) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add test that covers case length=2 in row_event.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😯 i make a mistake, should add more than 256 enum num but not more than 8 to test case length=2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a new test later @july2993

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddontang add here #320

// mysql> desc aenum;
// +-------+-------------------------------------------+------+-----+---------+-------+
// | Field | Type | Null | Key | Default | Extra |
// +-------+-------------------------------------------+------+-----+---------+-------+
// | id | int(11) | YES | | NULL | |
// | aset | enum('0','1','2','3','4','5','6','7','8') | YES | | NULL | |
// +-------+-------------------------------------------+------+-----+---------+-------+
// 2 rows in set (0.00 sec)
//
// insert into aenum(id, aset) values(1, '0');
tableMapEventData := []byte("\x42\x0f\x00\x00\x00\x00\x01\x00\x05\x74\x74\x65\x73\x74\x00\x05")
tableMapEventData = append(tableMapEventData, []byte("\x61\x65\x6e\x75\x6d\x00\x02\x03\xfe\x02\xf7\x01\x03")...)
tableMapEvent := new(TableMapEvent)
tableMapEvent.tableIDSize = 6
err := tableMapEvent.Decode(tableMapEventData)
c.Assert(err, IsNil)

rows := new(RowsEvent)
rows.tableIDSize = 6
rows.tables = make(map[uint64]*TableMapEvent)
rows.tables[tableMapEvent.TableID] = tableMapEvent
rows.Version = 2

data := []byte("\x42\x0f\x00\x00\x00\x00\x01\x00\x02\x00\x02\xff\xfc\x01\x00\x00\x00\x01")

rows.Rows = nil
err = rows.Decode(data)
c.Assert(err, IsNil)
c.Assert(rows.Rows[0][1], Equals, int64(1))
}

func (_ *testDecodeSuite) TestSet(c *C) {
// mysql> desc aset;
// +--------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
// | Field | Type | Null | Key | Default | Extra |
// +--------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
// | id | int(11) | YES | | NULL | |
// | region | set('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18') | YES | | NULL | |
// +--------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
// 2 rows in set (0.00 sec)
//
// insert into aset(id, region) values(1, '1,3');

tableMapEventData := []byte("\xe7\x0e\x00\x00\x00\x00\x01\x00\x05\x74\x74\x65\x73\x74\x00\x04")
tableMapEventData = append(tableMapEventData, []byte("\x61\x73\x65\x74\x00\x02\x03\xfe\x02\xf8\x03\x03")...)
tableMapEvent := new(TableMapEvent)
tableMapEvent.tableIDSize = 6
err := tableMapEvent.Decode(tableMapEventData)
c.Assert(err, IsNil)

rows := new(RowsEvent)
rows.tableIDSize = 6
rows.tables = make(map[uint64]*TableMapEvent)
rows.tables[tableMapEvent.TableID] = tableMapEvent
rows.Version = 2

data := []byte("\xe7\x0e\x00\x00\x00\x00\x01\x00\x02\x00\x02\xff\xfc\x01\x00\x00\x00\x05\x00\x00")

rows.Rows = nil
err = rows.Decode(data)
c.Assert(err, IsNil)
c.Assert(rows.Rows[0][1], Equals, int64(5))
}

func (_ *testDecodeSuite) TestJsonNull(c *C) {
// Table:
// desc hj_order_preview
Expand Down