From 15271494b1124a1eabc182b6d609b47a5494cec4 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Mon, 24 Jun 2024 21:10:42 -0700 Subject: [PATCH] Fix `debezium.EncodeDecimal` --- lib/debezium/decimal.go | 8 +++++--- lib/debezium/decimal_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/debezium/decimal.go b/lib/debezium/decimal.go index e9561e01b..440ac3ba8 100644 --- a/lib/debezium/decimal.go +++ b/lib/debezium/decimal.go @@ -69,12 +69,14 @@ func EncodeDecimal(value string, scale uint16) ([]byte, error) { return nil, fmt.Errorf("unable to use %q as a floating-point number", value) } - scaledValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil) - bigFloatValue.Mul(bigFloatValue, new(big.Float).SetInt(scaledValue)) + if scale > 0 { + scaledValue := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil) + bigFloatValue.Mul(bigFloatValue, new(big.Float).SetInt(scaledValue)) + } // Extract the scaled integer value. bigIntValue := new(big.Int) - if _, success := bigIntValue.SetString(bigFloatValue.String(), 10); !success { + if _, success := bigIntValue.SetString(bigFloatValue.Text('f', 0), 10); !success { return nil, fmt.Errorf("unable to use %q as a floating-point number", value) } diff --git a/lib/debezium/decimal_test.go b/lib/debezium/decimal_test.go index 80b6c4588..2a341a13f 100644 --- a/lib/debezium/decimal_test.go +++ b/lib/debezium/decimal_test.go @@ -139,6 +139,16 @@ func TestEncodeDecimal(t *testing.T) { value: "-65535", scale: 0, }, + { + name: "number with a scale of 15", + value: "0.000022998904125", + scale: 15, + }, + { + name: "number with a scale of 15", + value: "145.183000000000000", + scale: 15, + }, { name: "malformed - empty string", value: "",