Skip to content

Commit

Permalink
[dbnode] Fix m3tsz encoding precision issue (#3872)
Browse files Browse the repository at this point in the history
  • Loading branch information
linasm authored Oct 26, 2021
1 parent 40bc7ff commit fe1a38a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/dbnode/encoding/m3tsz/m3tsz.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ func convertToIntFloat(v float64, curMaxMult uint8) (float64, uint8, bool, error
return 0.0, 0, false, errInvalidMultiplier
}

val := v * multipliers[int(curMaxMult)]
sign := 1.0
if v < 0 {
sign = -1.0
val = val * -1.0
}

for mult := curMaxMult; mult <= maxMult && val < maxOptInt; mult++ {
for mult := curMaxMult; mult <= maxMult; mult++ {
val := v * multipliers[int(mult)] * sign
if val >= maxOptInt {
break
}
i, r := math.Modf(val)
if r == 0 {
return sign * i, mult, false, nil
Expand All @@ -111,7 +113,6 @@ func convertToIntFloat(v float64, curMaxMult uint8) (float64, uint8, bool, error
return sign * next, mult, false, nil
}
}
val = val * 10.0
}

return v, 0, true, nil
Expand Down
23 changes: 19 additions & 4 deletions src/dbnode/encoding/m3tsz/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ func TestMixedRoundTrip(t *testing.T) {
}
}

func TestPrecision(t *testing.T) {
var (
num = 100
input = make([]ts.Datapoint, 0, num)
timestamp = xtime.Now()
)

for i := 0; i < num; i++ {
input = append(input, ts.Datapoint{TimestampNanos: timestamp, Value: 187.80131100000006})
timestamp = timestamp.Add(time.Minute)
}

testRoundTrip(t, input)
}

func TestIntOverflow(t *testing.T) {
testRoundTrip(t, generateOverflowDatapoints())
}
Expand Down Expand Up @@ -159,10 +174,10 @@ func validateRoundTrip(t *testing.T, input []ts.Datapoint, intOpt bool) {
expectedAnnotation = nil
}

require.Equal(t, input[i].TimestampNanos, v.TimestampNanos)
require.Equal(t, input[i].Value, v.Value)
require.Equal(t, timeUnits[i], u)
require.Equal(t, expectedAnnotation, a)
require.Equal(t, input[i].TimestampNanos, v.TimestampNanos, "datapoint #%d", i)
require.Equal(t, input[i].Value, v.Value, "datapoint #%d", i)
require.Equal(t, timeUnits[i], u, "datapoint #%d", i)
require.Equal(t, expectedAnnotation, a, "datapoint #%d", i)

i++
}
Expand Down

0 comments on commit fe1a38a

Please sign in to comment.