Skip to content

Commit b358328

Browse files
committed
batch float with trailing zero and add a useFloatWithTrailingZero flag to enable
1 parent 639eccd commit b358328

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22
55
toolchain go1.23.1
66

77
require (
8+
filippo.io/edwards25519 v1.1.0
89
github.com/BurntSushi/toml v1.3.2
910
github.com/Masterminds/semver v1.5.0
1011
github.com/go-sql-driver/mysql v1.7.1
@@ -19,7 +20,6 @@ require (
1920
)
2021

2122
require (
22-
filippo.io/edwards25519 v1.1.0 // indirect
2323
github.com/davecgh/go-spew v1.1.1 // indirect
2424
github.com/kr/pretty v0.3.1 // indirect
2525
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86 // indirect

replication/binlogsyncer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ type BinlogSyncerConfig struct {
7676
// Use decimal.Decimal structure for decimals.
7777
UseDecimal bool
7878

79+
// Use FloatWithTrailingZero structure for floats.
80+
UseFloatWithTrailingZero bool
81+
7982
// RecvBufferSize sets the size in bytes of the operating system's receive buffer associated with the connection.
8083
RecvBufferSize int
8184

@@ -197,6 +200,7 @@ func NewBinlogSyncer(cfg BinlogSyncerConfig) *BinlogSyncer {
197200
b.parser.SetParseTime(b.cfg.ParseTime)
198201
b.parser.SetTimestampStringLocation(b.cfg.TimestampStringLocation)
199202
b.parser.SetUseDecimal(b.cfg.UseDecimal)
203+
b.parser.SetUseFloatWithTrailingZero(b.cfg.UseFloatWithTrailingZero)
200204
b.parser.SetVerifyChecksum(b.cfg.VerifyChecksum)
201205
b.parser.SetRowsEventDecodeFunc(b.cfg.RowsEventDecodeFunc)
202206
b.parser.SetTableMapOptionalMetaDecodeFunc(b.cfg.TableMapOptionalMetaDecodeFunc)

replication/json_binary.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package replication
33
import (
44
"fmt"
55
"math"
6+
"strconv"
67

78
"github.com/go-mysql-org/go-mysql/mysql"
89
"github.com/go-mysql-org/go-mysql/utils"
@@ -52,6 +53,8 @@ type (
5253
JsonDiffOperation byte
5354
)
5455

56+
type FloatWithTrailingZero float64
57+
5558
const (
5659
// The JSON value in the given path is replaced with a new value.
5760
//
@@ -96,6 +99,14 @@ func (jd *JsonDiff) String() string {
9699
return fmt.Sprintf("json_diff(op:%s path:%s value:%s)", jd.Op, jd.Path, jd.Value)
97100
}
98101

102+
func (f FloatWithTrailingZero) MarshalJSON() ([]byte, error) {
103+
if float64(f) == float64(int(f)) {
104+
return []byte(strconv.FormatFloat(float64(f), 'f', 1, 64)), nil
105+
}
106+
107+
return []byte(strconv.FormatFloat(float64(f), 'f', -1, 64)), nil
108+
}
109+
99110
func jsonbGetOffsetSize(isSmall bool) int {
100111
if isSmall {
101112
return jsonbSmallOffsetSize
@@ -125,6 +136,7 @@ func jsonbGetValueEntrySize(isSmall bool) int {
125136
func (e *RowsEvent) decodeJsonBinary(data []byte) ([]byte, error) {
126137
d := jsonBinaryDecoder{
127138
useDecimal: e.useDecimal,
139+
useFloatWithTrailingZero: e.useFloatWithTrailingZero,
128140
ignoreDecodeErr: e.ignoreJSONDecodeErr,
129141
}
130142

@@ -141,9 +153,10 @@ func (e *RowsEvent) decodeJsonBinary(data []byte) ([]byte, error) {
141153
}
142154

143155
type jsonBinaryDecoder struct {
144-
useDecimal bool
145-
ignoreDecodeErr bool
146-
err error
156+
useDecimal bool
157+
useFloatWithTrailingZero bool
158+
ignoreDecodeErr bool
159+
err error
147160
}
148161

149162
func (d *jsonBinaryDecoder) decodeValue(tp byte, data []byte) interface{} {
@@ -175,6 +188,9 @@ func (d *jsonBinaryDecoder) decodeValue(tp byte, data []byte) interface{} {
175188
case JSONB_UINT64:
176189
return d.decodeUint64(data)
177190
case JSONB_DOUBLE:
191+
if d.useFloatWithTrailingZero {
192+
return d.decodeDoubleWithTrailingZero(data)
193+
}
178194
return d.decodeDouble(data)
179195
case JSONB_STRING:
180196
return d.decodeString(data)
@@ -395,6 +411,11 @@ func (d *jsonBinaryDecoder) decodeDouble(data []byte) float64 {
395411
return v
396412
}
397413

414+
func (d *jsonBinaryDecoder) decodeDoubleWithTrailingZero(data []byte) FloatWithTrailingZero {
415+
v := d.decodeDouble(data)
416+
return FloatWithTrailingZero(v)
417+
}
418+
398419
func (d *jsonBinaryDecoder) decodeString(data []byte) string {
399420
if d.err != nil {
400421
return ""

replication/parser.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type BinlogParser struct {
3636
stopProcessing uint32
3737

3838
useDecimal bool
39+
useFloatWithTrailingZero bool
3940
ignoreJSONDecodeErr bool
4041
verifyChecksum bool
4142

@@ -202,6 +203,10 @@ func (p *BinlogParser) SetUseDecimal(useDecimal bool) {
202203
p.useDecimal = useDecimal
203204
}
204205

206+
func (p *BinlogParser) SetUseFloatWithTrailingZero(useFloatWithTrailingZero bool) {
207+
p.useFloatWithTrailingZero = useFloatWithTrailingZero
208+
}
209+
205210
func (p *BinlogParser) SetIgnoreJSONDecodeError(ignoreJSONDecodeErr bool) {
206211
p.ignoreJSONDecodeErr = ignoreJSONDecodeErr
207212
}
@@ -410,6 +415,7 @@ func (p *BinlogParser) newRowsEvent(h *EventHeader) *RowsEvent {
410415
e.parseTime = p.parseTime
411416
e.timestampStringLocation = p.timestampStringLocation
412417
e.useDecimal = p.useDecimal
418+
e.useFloatWithTrailingZero = p.useFloatWithTrailingZero
413419
e.ignoreJSONDecodeErr = p.ignoreJSONDecodeErr
414420

415421
switch h.EventType {

replication/row_event.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,11 @@ type RowsEvent struct {
945945
Rows [][]interface{}
946946
SkippedColumns [][]int
947947

948-
parseTime bool
949-
timestampStringLocation *time.Location
950-
useDecimal bool
951-
ignoreJSONDecodeErr bool
948+
parseTime bool
949+
timestampStringLocation *time.Location
950+
useDecimal bool
951+
useFloatWithTrailingZero bool
952+
ignoreJSONDecodeErr bool
952953
}
953954

954955
// EnumRowsEventType is an abridged type describing the operation which triggered the given RowsEvent.

0 commit comments

Comments
 (0)