Skip to content

fix(canal): fix "Index of range error" for unsigned columns #405

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 6 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
28 changes: 12 additions & 16 deletions canal/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package canal

import (
"fmt"

"github.com/siddontang/go-mysql/replication"
"github.com/siddontang/go-mysql/schema"
)
Expand Down Expand Up @@ -51,29 +52,24 @@ func (r *RowsEvent) handleUnsigned() {
}

for i := 0; i < len(r.Rows); i++ {
for _, index := range r.Table.UnsignedColumns {
switch t := r.Rows[i][index].(type) {
for _, columnIdx := range r.Table.UnsignedColumns {
switch value := r.Rows[i][columnIdx].(type) {
case int8:
r.Rows[i][index] = uint8(t)
r.Rows[i][columnIdx] = uint8(value)
case int16:
r.Rows[i][index] = uint16(t)
r.Rows[i][columnIdx] = uint16(value)
case int32:
if r.Table.Columns[i].Type == schema.TYPE_MEDIUM_INT {
// problem with mediumint is that it's a 3-byte type. There is no compatible golang type to match that.
// So to convert from negative to positive we'd need to convert the value manually
if i >= 0 {
r.Rows[i][index] = uint32(t)
} else {
r.Rows[i][index] = uint32(maxMediumintUnsigned + t + 1)
}
return
// problem with mediumint is that it's a 3-byte type. There is no compatible golang type to match that.
// So to convert from negative to positive we'd need to convert the value manually
if value < 0 && r.Table.Columns[columnIdx].Type == schema.TYPE_MEDIUM_INT {
r.Rows[i][columnIdx] = uint32(maxMediumintUnsigned + value + 1)
} else {
r.Rows[i][index] = uint32(t)
r.Rows[i][columnIdx] = uint32(value)
}
case int64:
r.Rows[i][index] = uint64(t)
r.Rows[i][columnIdx] = uint64(value)
case int:
r.Rows[i][index] = uint(t)
r.Rows[i][columnIdx] = uint(value)
default:
// nothing to do
}
Expand Down
6 changes: 1 addition & 5 deletions canal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ func (c *Canal) runSyncBinlog() error {

// Update the delay between the Canal and the Master before the handler hooks are called
c.updateReplicationDelay(ev)
// if log pos equal zero ,it is a fake rotate event,ignore it.
// see https://github.com/mysql/mysql-server/blob/8cc757da3d87bf4a1f07dcfb2d3c96fed3806870/sql/rpl_binlog_sender.cc#L899
if ev.Header.LogPos == 0 {
continue
}

savePos = false
force = false
pos := c.master.Position()
Expand Down