diff --git a/go/mysql/binlog_event_rbr.go b/go/mysql/binlog_event_rbr.go index 913cdc2100e..7c2341a322a 100644 --- a/go/mysql/binlog_event_rbr.go +++ b/go/mysql/binlog_event_rbr.go @@ -22,6 +22,7 @@ import ( "fmt" "math" "strconv" + "strings" "time" "vitess.io/vitess/go/sqltypes" @@ -764,8 +765,21 @@ func CellValue(data []byte, pos int, typ byte, metadata uint16, styp querypb.Typ } } - return sqltypes.MakeTrusted(querypb.Type_DECIMAL, - txt.Bytes()), l, nil + // remove preceding 0s from the integral part, otherwise we get "000000000001.23" instead of "1.23" + trimPrecedingZeroes := func(b []byte) []byte { + s := string(b) + isNegative := false + if s[0] == '-' { + isNegative = true + s = s[1:] + } + s = strings.TrimLeft(s, "0") + if isNegative { + s = fmt.Sprintf("-%s", s) + } + return []byte(s) + } + return sqltypes.MakeTrusted(querypb.Type_DECIMAL, trimPrecedingZeroes(txt.Bytes())), l, nil case TypeEnum: switch metadata & 0xff { diff --git a/go/mysql/binlog_event_rbr_test.go b/go/mysql/binlog_event_rbr_test.go index 314eaf21b07..d225b2b555f 100644 --- a/go/mysql/binlog_event_rbr_test.go +++ b/go/mysql/binlog_event_rbr_test.go @@ -473,7 +473,7 @@ func TestCellLengthAndData(t *testing.T) { metadata: 20<<8 | 2, // DECIMAL(20,2) data: []byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x01, 0x0a}, out: sqltypes.MakeTrusted(querypb.Type_DECIMAL, - []byte("000000000000000001.10")), + []byte("1.10")), }, { typ: TypeBlob, metadata: 1, diff --git a/go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go b/go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go index 38ac3392cfd..4851b685b2f 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go +++ b/go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go @@ -1512,6 +1512,7 @@ func TestTypes(t *testing.T) { "create table vitess_strings(vb varbinary(16), c char(16), vc varchar(16), b binary(4), tb tinyblob, bl blob, ttx tinytext, tx text, en enum('a','b'), s set('a','b'), primary key(vb))", "create table vitess_misc(id int, b bit(8), d date, dt datetime, t time, g geometry, primary key(id))", "create table vitess_null(id int, val varbinary(128), primary key(id))", + "create table vitess_decimal(id int, dec1 decimal(12,4), dec2 decimal(13,4), primary key(id))", }) defer execStatements(t, []string{ "drop table vitess_ints", @@ -1519,6 +1520,7 @@ func TestTypes(t *testing.T) { "drop table vitess_strings", "drop table vitess_misc", "drop table vitess_null", + "drop table vitess_decimal", }) engine.se.Reload(context.Background()) @@ -1592,6 +1594,35 @@ func TestTypes(t *testing.T) { `gtid`, `commit`, }}, + }, { + input: []string{ + "insert into vitess_decimal values(1, 1.23, 1.23)", + "insert into vitess_decimal values(2, -1.23, -1.23)", + "insert into vitess_decimal values(3, 0000000001.23, 0000000001.23)", + "insert into vitess_decimal values(4, -0000000001.23, -0000000001.23)", + }, + output: [][]string{{ + `begin`, + `type:FIELD field_event: fields: fields: > `, + `type:ROW row_event: > > `, + `gtid`, + `commit`, + }, { + `begin`, + `type:ROW row_event: > > `, + `gtid`, + `commit`, + }, { + `begin`, + `type:ROW row_event: > > `, + `gtid`, + `commit`, + }, { + `begin`, + `type:ROW row_event: > > `, + `gtid`, + `commit`, + }}, }} runCases(t, nil, testcases, "", nil) }