Skip to content

Commit

Permalink
Merge pull request #7340 from planetscale/rn-backport-7297
Browse files Browse the repository at this point in the history
Backport 7297: VStreamer Events: remove preceding zeroes from decimals in Row Events
  • Loading branch information
deepthi authored Jan 23, 2021
2 parents f3ed336 + 312b3db commit 332d5f2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
18 changes: 16 additions & 2 deletions go/mysql/binlog_event_rbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math"
"strconv"
"strings"
"time"

"vitess.io/vitess/go/sqltypes"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go/mysql/binlog_event_rbr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,13 +1512,15 @@ 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",
"drop table vitess_fracts",
"drop table vitess_strings",
"drop table vitess_misc",
"drop table vitess_null",
"drop table vitess_decimal",
})
engine.se.Reload(context.Background())

Expand Down Expand Up @@ -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:<table_name:"vitess_decimal" fields:<name:"id" type:INT32 table:"vitess_decimal" org_table:"vitess_decimal" database:"vttest" org_name:"id" column_length:11 charset:63 > fields:<name:"dec1" type:DECIMAL table:"vitess_decimal" org_table:"vitess_decimal" database:"vttest" org_name:"dec1" column_length:14 charset:63 decimals:4 > fields:<name:"dec2" type:DECIMAL table:"vitess_decimal" org_table:"vitess_decimal" database:"vttest" org_name:"dec2" column_length:15 charset:63 decimals:4 > > `,
`type:ROW row_event:<table_name:"vitess_decimal" row_changes:<after:<lengths:1 lengths:6 lengths:6 values:"11.23001.2300" > > > `,
`gtid`,
`commit`,
}, {
`begin`,
`type:ROW row_event:<table_name:"vitess_decimal" row_changes:<after:<lengths:1 lengths:7 lengths:7 values:"2-1.2300-1.2300" > > > `,
`gtid`,
`commit`,
}, {
`begin`,
`type:ROW row_event:<table_name:"vitess_decimal" row_changes:<after:<lengths:1 lengths:6 lengths:6 values:"31.23001.2300" > > > `,
`gtid`,
`commit`,
}, {
`begin`,
`type:ROW row_event:<table_name:"vitess_decimal" row_changes:<after:<lengths:1 lengths:7 lengths:7 values:"4-1.2300-1.2300" > > > `,
`gtid`,
`commit`,
}},
}}
runCases(t, nil, testcases, "", nil)
}
Expand Down

0 comments on commit 332d5f2

Please sign in to comment.