From 197fd5b1df55d29a6cd9330c89ce78f3d7b2f7e6 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 00:01:09 -0700 Subject: [PATCH 1/7] Move `adp.Decimal` functions --- lib/debezium/decimal_test.go | 34 +++---------------------------- lib/numbers/decimal.go | 39 ++++++++++++++++++++++++++++++++++++ lib/numbers/decimal_test.go | 28 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 lib/numbers/decimal.go create mode 100644 lib/numbers/decimal_test.go diff --git a/lib/debezium/decimal_test.go b/lib/debezium/decimal_test.go index e3642dbed..a0b54d824 100644 --- a/lib/debezium/decimal_test.go +++ b/lib/debezium/decimal_test.go @@ -6,7 +6,7 @@ import ( "math/big" "testing" - "github.com/cockroachdb/apd/v3" + "github.com/artie-labs/transfer/lib/numbers" "github.com/stretchr/testify/assert" ) @@ -41,37 +41,9 @@ func TestDecodeBigInt(t *testing.T) { } } -func mustParseDecimal(value string) *apd.Decimal { - decimal, _, err := apd.NewFromString(value) - if err != nil { - panic(err) - } - return decimal -} - -func TestDecimalWithNewExponent(t *testing.T) { - assert.Equal(t, "0", decimalWithNewExponent(apd.New(0, 0), 0).Text('f')) - assert.Equal(t, "00", decimalWithNewExponent(apd.New(0, 1), 1).Text('f')) - assert.Equal(t, "0", decimalWithNewExponent(apd.New(0, 100), 0).Text('f')) - assert.Equal(t, "00", decimalWithNewExponent(apd.New(0, 0), 1).Text('f')) - assert.Equal(t, "0.0", decimalWithNewExponent(apd.New(0, 0), -1).Text('f')) - - // Same exponent: - assert.Equal(t, "12.349", decimalWithNewExponent(mustParseDecimal("12.349"), -3).Text('f')) - // More precise exponent: - assert.Equal(t, "12.3490", decimalWithNewExponent(mustParseDecimal("12.349"), -4).Text('f')) - assert.Equal(t, "12.34900", decimalWithNewExponent(mustParseDecimal("12.349"), -5).Text('f')) - // Lest precise exponent: - // Extra digits should be truncated rather than rounded. - assert.Equal(t, "12.34", decimalWithNewExponent(mustParseDecimal("12.349"), -2).Text('f')) - assert.Equal(t, "12.3", decimalWithNewExponent(mustParseDecimal("12.349"), -1).Text('f')) - assert.Equal(t, "12", decimalWithNewExponent(mustParseDecimal("12.349"), 0).Text('f')) - assert.Equal(t, "10", decimalWithNewExponent(mustParseDecimal("12.349"), 1).Text('f')) -} - func TestEncodeDecimal(t *testing.T) { testEncodeDecimal := func(value string, expectedScale int32) { - bytes, scale := EncodeDecimal(mustParseDecimal(value)) + bytes, scale := EncodeDecimal(numbers.MustParseDecimal(value)) actual := DecodeDecimal(bytes, nil, int(scale)).String() assert.Equal(t, value, actual, value) assert.Equal(t, expectedScale, scale, value) @@ -91,7 +63,7 @@ func TestEncodeDecimal(t *testing.T) { func TestEncodeDecimalWithScale(t *testing.T) { mustEncodeAndDecodeDecimal := func(value string, scale int32) string { - bytes := EncodeDecimalWithScale(mustParseDecimal(value), scale) + bytes := EncodeDecimalWithScale(numbers.MustParseDecimal(value), scale) return DecodeDecimal(bytes, nil, int(scale)).String() } diff --git a/lib/numbers/decimal.go b/lib/numbers/decimal.go new file mode 100644 index 000000000..e042fe447 --- /dev/null +++ b/lib/numbers/decimal.go @@ -0,0 +1,39 @@ +package numbers + +import "github.com/cockroachdb/apd/v3" + +// MustParseDecimal parses a string to an [apd.Decimal] or panics -- used for tests. +func MustParseDecimal(value string) *apd.Decimal { + decimal, _, err := apd.NewFromString(value) + if err != nil { + panic(err) + } + return decimal +} + +// DecimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. +// If the new exponent is less precise then the extra digits will be truncated. +func DecimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { + exponentDelta := newExponent - decimal.Exponent // Exponent is negative. + + if exponentDelta == 0 { + return new(apd.Decimal).Set(decimal) + } + + coefficient := new(apd.BigInt).Set(&decimal.Coeff) + + if exponentDelta < 0 { + multiplier := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(-exponentDelta)), nil) + coefficient.Mul(coefficient, multiplier) + } else if exponentDelta > 0 { + divisor := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(exponentDelta)), nil) + coefficient.Div(coefficient, divisor) + } + + return &apd.Decimal{ + Form: decimal.Form, + Negative: decimal.Negative, + Exponent: newExponent, + Coeff: *coefficient, + } +} diff --git a/lib/numbers/decimal_test.go b/lib/numbers/decimal_test.go new file mode 100644 index 000000000..a7ec21868 --- /dev/null +++ b/lib/numbers/decimal_test.go @@ -0,0 +1,28 @@ +package numbers + +import ( + "testing" + + "github.com/cockroachdb/apd/v3" + "github.com/stretchr/testify/assert" +) + +func TestDecimalWithNewExponent(t *testing.T) { + assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 0), 0).Text('f')) + assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 1), 1).Text('f')) + assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 100), 0).Text('f')) + assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 0), 1).Text('f')) + assert.Equal(t, "0.0", DecimalWithNewExponent(apd.New(0, 0), -1).Text('f')) + + // Same exponent: + assert.Equal(t, "12.349", DecimalWithNewExponent(MustParseDecimal("12.349"), -3).Text('f')) + // More precise exponent: + assert.Equal(t, "12.3490", DecimalWithNewExponent(MustParseDecimal("12.349"), -4).Text('f')) + assert.Equal(t, "12.34900", DecimalWithNewExponent(MustParseDecimal("12.349"), -5).Text('f')) + // Lest precise exponent: + // Extra digits should be truncated rather than rounded. + assert.Equal(t, "12.34", DecimalWithNewExponent(MustParseDecimal("12.349"), -2).Text('f')) + assert.Equal(t, "12.3", DecimalWithNewExponent(MustParseDecimal("12.349"), -1).Text('f')) + assert.Equal(t, "12", DecimalWithNewExponent(MustParseDecimal("12.349"), 0).Text('f')) + assert.Equal(t, "10", DecimalWithNewExponent(MustParseDecimal("12.349"), 1).Text('f')) +} From 9b7815db1e939e6c9ed2f58ea95f367567864115 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 00:03:13 -0700 Subject: [PATCH 2/7] Move --- lib/debezium/decimal.go | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/lib/debezium/decimal.go b/lib/debezium/decimal.go index b7d531518..e90089a99 100644 --- a/lib/debezium/decimal.go +++ b/lib/debezium/decimal.go @@ -4,6 +4,7 @@ import ( "math/big" "slices" + "github.com/artie-labs/transfer/lib/numbers" "github.com/artie-labs/transfer/lib/typing/decimal" "github.com/cockroachdb/apd/v3" ) @@ -62,33 +63,6 @@ func decodeBigInt(data []byte) *big.Int { return bigInt } -// decimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. -// If the new exponent is less precise then the extra digits will be truncated. -func decimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { - exponentDelta := newExponent - decimal.Exponent // Exponent is negative. - - if exponentDelta == 0 { - return new(apd.Decimal).Set(decimal) - } - - coefficient := new(apd.BigInt).Set(&decimal.Coeff) - - if exponentDelta < 0 { - multiplier := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(-exponentDelta)), nil) - coefficient.Mul(coefficient, multiplier) - } else if exponentDelta > 0 { - divisor := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(exponentDelta)), nil) - coefficient.Div(coefficient, divisor) - } - - return &apd.Decimal{ - Form: decimal.Form, - Negative: decimal.Negative, - Exponent: newExponent, - Coeff: *coefficient, - } -} - // EncodeDecimal is used to encode a [apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. // The scale of the value (which is the negated exponent of the decimal) is returned as the second argument. func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { @@ -105,7 +79,7 @@ func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { func EncodeDecimalWithScale(decimal *apd.Decimal, scale int32) []byte { targetExponent := -scale // Negate scale since [Decimal.Exponent] is negative. if decimal.Exponent != targetExponent { - decimal = decimalWithNewExponent(decimal, targetExponent) + decimal = numbers.DecimalWithNewExponent(decimal, targetExponent) } bytes, _ := EncodeDecimal(decimal) return bytes From bf15472f4042519ccae77e2c2e0063802627626e Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:24:19 -0700 Subject: [PATCH 3/7] Revert --- lib/debezium/decimal.go | 30 +++++++++++++++++++++++++-- lib/debezium/decimal_test.go | 34 ++++++++++++++++++++++++++++--- lib/numbers/decimal.go | 39 ------------------------------------ lib/numbers/decimal_test.go | 28 -------------------------- lib/numbers/numbers.go | 11 ++++++++++ 5 files changed, 70 insertions(+), 72 deletions(-) delete mode 100644 lib/numbers/decimal.go delete mode 100644 lib/numbers/decimal_test.go diff --git a/lib/debezium/decimal.go b/lib/debezium/decimal.go index e90089a99..b7d531518 100644 --- a/lib/debezium/decimal.go +++ b/lib/debezium/decimal.go @@ -4,7 +4,6 @@ import ( "math/big" "slices" - "github.com/artie-labs/transfer/lib/numbers" "github.com/artie-labs/transfer/lib/typing/decimal" "github.com/cockroachdb/apd/v3" ) @@ -63,6 +62,33 @@ func decodeBigInt(data []byte) *big.Int { return bigInt } +// decimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. +// If the new exponent is less precise then the extra digits will be truncated. +func decimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { + exponentDelta := newExponent - decimal.Exponent // Exponent is negative. + + if exponentDelta == 0 { + return new(apd.Decimal).Set(decimal) + } + + coefficient := new(apd.BigInt).Set(&decimal.Coeff) + + if exponentDelta < 0 { + multiplier := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(-exponentDelta)), nil) + coefficient.Mul(coefficient, multiplier) + } else if exponentDelta > 0 { + divisor := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(exponentDelta)), nil) + coefficient.Div(coefficient, divisor) + } + + return &apd.Decimal{ + Form: decimal.Form, + Negative: decimal.Negative, + Exponent: newExponent, + Coeff: *coefficient, + } +} + // EncodeDecimal is used to encode a [apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. // The scale of the value (which is the negated exponent of the decimal) is returned as the second argument. func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { @@ -79,7 +105,7 @@ func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { func EncodeDecimalWithScale(decimal *apd.Decimal, scale int32) []byte { targetExponent := -scale // Negate scale since [Decimal.Exponent] is negative. if decimal.Exponent != targetExponent { - decimal = numbers.DecimalWithNewExponent(decimal, targetExponent) + decimal = decimalWithNewExponent(decimal, targetExponent) } bytes, _ := EncodeDecimal(decimal) return bytes diff --git a/lib/debezium/decimal_test.go b/lib/debezium/decimal_test.go index a0b54d824..e3642dbed 100644 --- a/lib/debezium/decimal_test.go +++ b/lib/debezium/decimal_test.go @@ -6,7 +6,7 @@ import ( "math/big" "testing" - "github.com/artie-labs/transfer/lib/numbers" + "github.com/cockroachdb/apd/v3" "github.com/stretchr/testify/assert" ) @@ -41,9 +41,37 @@ func TestDecodeBigInt(t *testing.T) { } } +func mustParseDecimal(value string) *apd.Decimal { + decimal, _, err := apd.NewFromString(value) + if err != nil { + panic(err) + } + return decimal +} + +func TestDecimalWithNewExponent(t *testing.T) { + assert.Equal(t, "0", decimalWithNewExponent(apd.New(0, 0), 0).Text('f')) + assert.Equal(t, "00", decimalWithNewExponent(apd.New(0, 1), 1).Text('f')) + assert.Equal(t, "0", decimalWithNewExponent(apd.New(0, 100), 0).Text('f')) + assert.Equal(t, "00", decimalWithNewExponent(apd.New(0, 0), 1).Text('f')) + assert.Equal(t, "0.0", decimalWithNewExponent(apd.New(0, 0), -1).Text('f')) + + // Same exponent: + assert.Equal(t, "12.349", decimalWithNewExponent(mustParseDecimal("12.349"), -3).Text('f')) + // More precise exponent: + assert.Equal(t, "12.3490", decimalWithNewExponent(mustParseDecimal("12.349"), -4).Text('f')) + assert.Equal(t, "12.34900", decimalWithNewExponent(mustParseDecimal("12.349"), -5).Text('f')) + // Lest precise exponent: + // Extra digits should be truncated rather than rounded. + assert.Equal(t, "12.34", decimalWithNewExponent(mustParseDecimal("12.349"), -2).Text('f')) + assert.Equal(t, "12.3", decimalWithNewExponent(mustParseDecimal("12.349"), -1).Text('f')) + assert.Equal(t, "12", decimalWithNewExponent(mustParseDecimal("12.349"), 0).Text('f')) + assert.Equal(t, "10", decimalWithNewExponent(mustParseDecimal("12.349"), 1).Text('f')) +} + func TestEncodeDecimal(t *testing.T) { testEncodeDecimal := func(value string, expectedScale int32) { - bytes, scale := EncodeDecimal(numbers.MustParseDecimal(value)) + bytes, scale := EncodeDecimal(mustParseDecimal(value)) actual := DecodeDecimal(bytes, nil, int(scale)).String() assert.Equal(t, value, actual, value) assert.Equal(t, expectedScale, scale, value) @@ -63,7 +91,7 @@ func TestEncodeDecimal(t *testing.T) { func TestEncodeDecimalWithScale(t *testing.T) { mustEncodeAndDecodeDecimal := func(value string, scale int32) string { - bytes := EncodeDecimalWithScale(numbers.MustParseDecimal(value), scale) + bytes := EncodeDecimalWithScale(mustParseDecimal(value), scale) return DecodeDecimal(bytes, nil, int(scale)).String() } diff --git a/lib/numbers/decimal.go b/lib/numbers/decimal.go deleted file mode 100644 index e042fe447..000000000 --- a/lib/numbers/decimal.go +++ /dev/null @@ -1,39 +0,0 @@ -package numbers - -import "github.com/cockroachdb/apd/v3" - -// MustParseDecimal parses a string to an [apd.Decimal] or panics -- used for tests. -func MustParseDecimal(value string) *apd.Decimal { - decimal, _, err := apd.NewFromString(value) - if err != nil { - panic(err) - } - return decimal -} - -// DecimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. -// If the new exponent is less precise then the extra digits will be truncated. -func DecimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { - exponentDelta := newExponent - decimal.Exponent // Exponent is negative. - - if exponentDelta == 0 { - return new(apd.Decimal).Set(decimal) - } - - coefficient := new(apd.BigInt).Set(&decimal.Coeff) - - if exponentDelta < 0 { - multiplier := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(-exponentDelta)), nil) - coefficient.Mul(coefficient, multiplier) - } else if exponentDelta > 0 { - divisor := new(apd.BigInt).Exp(apd.NewBigInt(10), apd.NewBigInt(int64(exponentDelta)), nil) - coefficient.Div(coefficient, divisor) - } - - return &apd.Decimal{ - Form: decimal.Form, - Negative: decimal.Negative, - Exponent: newExponent, - Coeff: *coefficient, - } -} diff --git a/lib/numbers/decimal_test.go b/lib/numbers/decimal_test.go deleted file mode 100644 index a7ec21868..000000000 --- a/lib/numbers/decimal_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package numbers - -import ( - "testing" - - "github.com/cockroachdb/apd/v3" - "github.com/stretchr/testify/assert" -) - -func TestDecimalWithNewExponent(t *testing.T) { - assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 0), 0).Text('f')) - assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 1), 1).Text('f')) - assert.Equal(t, "0", DecimalWithNewExponent(apd.New(0, 100), 0).Text('f')) - assert.Equal(t, "00", DecimalWithNewExponent(apd.New(0, 0), 1).Text('f')) - assert.Equal(t, "0.0", DecimalWithNewExponent(apd.New(0, 0), -1).Text('f')) - - // Same exponent: - assert.Equal(t, "12.349", DecimalWithNewExponent(MustParseDecimal("12.349"), -3).Text('f')) - // More precise exponent: - assert.Equal(t, "12.3490", DecimalWithNewExponent(MustParseDecimal("12.349"), -4).Text('f')) - assert.Equal(t, "12.34900", DecimalWithNewExponent(MustParseDecimal("12.349"), -5).Text('f')) - // Lest precise exponent: - // Extra digits should be truncated rather than rounded. - assert.Equal(t, "12.34", DecimalWithNewExponent(MustParseDecimal("12.349"), -2).Text('f')) - assert.Equal(t, "12.3", DecimalWithNewExponent(MustParseDecimal("12.349"), -1).Text('f')) - assert.Equal(t, "12", DecimalWithNewExponent(MustParseDecimal("12.349"), 0).Text('f')) - assert.Equal(t, "10", DecimalWithNewExponent(MustParseDecimal("12.349"), 1).Text('f')) -} diff --git a/lib/numbers/numbers.go b/lib/numbers/numbers.go index f414bbd83..8954fc1c3 100644 --- a/lib/numbers/numbers.go +++ b/lib/numbers/numbers.go @@ -1,6 +1,17 @@ package numbers +import "github.com/cockroachdb/apd/v3" + // BetweenEq - Looks something like this. start <= number <= end func BetweenEq[T int | int32 | int64](start, end, number T) bool { return number >= start && number <= end } + +// MustParseDecimal parses a string to an [apd.Decimal] or panics -- used for tests. +func MustParseDecimal(value string) *apd.Decimal { + decimal, _, err := apd.NewFromString(value) + if err != nil { + panic(err) + } + return decimal +} From 56e686ec96663e064c0ba855ea52797582bd0275 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:24:58 -0700 Subject: [PATCH 4/7] Use --- lib/debezium/decimal_test.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/debezium/decimal_test.go b/lib/debezium/decimal_test.go index e3642dbed..e74bd7789 100644 --- a/lib/debezium/decimal_test.go +++ b/lib/debezium/decimal_test.go @@ -6,6 +6,7 @@ import ( "math/big" "testing" + "github.com/artie-labs/transfer/lib/numbers" "github.com/cockroachdb/apd/v3" "github.com/stretchr/testify/assert" ) @@ -41,14 +42,6 @@ func TestDecodeBigInt(t *testing.T) { } } -func mustParseDecimal(value string) *apd.Decimal { - decimal, _, err := apd.NewFromString(value) - if err != nil { - panic(err) - } - return decimal -} - func TestDecimalWithNewExponent(t *testing.T) { assert.Equal(t, "0", decimalWithNewExponent(apd.New(0, 0), 0).Text('f')) assert.Equal(t, "00", decimalWithNewExponent(apd.New(0, 1), 1).Text('f')) @@ -57,21 +50,21 @@ func TestDecimalWithNewExponent(t *testing.T) { assert.Equal(t, "0.0", decimalWithNewExponent(apd.New(0, 0), -1).Text('f')) // Same exponent: - assert.Equal(t, "12.349", decimalWithNewExponent(mustParseDecimal("12.349"), -3).Text('f')) + assert.Equal(t, "12.349", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), -3).Text('f')) // More precise exponent: - assert.Equal(t, "12.3490", decimalWithNewExponent(mustParseDecimal("12.349"), -4).Text('f')) - assert.Equal(t, "12.34900", decimalWithNewExponent(mustParseDecimal("12.349"), -5).Text('f')) + assert.Equal(t, "12.3490", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), -4).Text('f')) + assert.Equal(t, "12.34900", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), -5).Text('f')) // Lest precise exponent: // Extra digits should be truncated rather than rounded. - assert.Equal(t, "12.34", decimalWithNewExponent(mustParseDecimal("12.349"), -2).Text('f')) - assert.Equal(t, "12.3", decimalWithNewExponent(mustParseDecimal("12.349"), -1).Text('f')) - assert.Equal(t, "12", decimalWithNewExponent(mustParseDecimal("12.349"), 0).Text('f')) - assert.Equal(t, "10", decimalWithNewExponent(mustParseDecimal("12.349"), 1).Text('f')) + assert.Equal(t, "12.34", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), -2).Text('f')) + assert.Equal(t, "12.3", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), -1).Text('f')) + assert.Equal(t, "12", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), 0).Text('f')) + assert.Equal(t, "10", decimalWithNewExponent(numbers.MustParseDecimal("12.349"), 1).Text('f')) } func TestEncodeDecimal(t *testing.T) { testEncodeDecimal := func(value string, expectedScale int32) { - bytes, scale := EncodeDecimal(mustParseDecimal(value)) + bytes, scale := EncodeDecimal(numbers.MustParseDecimal(value)) actual := DecodeDecimal(bytes, nil, int(scale)).String() assert.Equal(t, value, actual, value) assert.Equal(t, expectedScale, scale, value) @@ -91,7 +84,7 @@ func TestEncodeDecimal(t *testing.T) { func TestEncodeDecimalWithScale(t *testing.T) { mustEncodeAndDecodeDecimal := func(value string, scale int32) string { - bytes := EncodeDecimalWithScale(mustParseDecimal(value), scale) + bytes := EncodeDecimalWithScale(numbers.MustParseDecimal(value), scale) return DecodeDecimal(bytes, nil, int(scale)).String() } From 8d0d3476402a7a89ccaf0fe2b3e2579f95861959 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:25:38 -0700 Subject: [PATCH 5/7] Change --- lib/numbers/numbers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/numbers/numbers.go b/lib/numbers/numbers.go index 8954fc1c3..e1386bb3f 100644 --- a/lib/numbers/numbers.go +++ b/lib/numbers/numbers.go @@ -7,7 +7,7 @@ func BetweenEq[T int | int32 | int64](start, end, number T) bool { return number >= start && number <= end } -// MustParseDecimal parses a string to an [apd.Decimal] or panics -- used for tests. +// MustParseDecimal parses a string to a [apd.Decimal] or panics -- used for tests. func MustParseDecimal(value string) *apd.Decimal { decimal, _, err := apd.NewFromString(value) if err != nil { From 791c2118fef10573fe4667e346cd7a5663a3371d Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:26:02 -0700 Subject: [PATCH 6/7] Period --- lib/debezium/decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/debezium/decimal.go b/lib/debezium/decimal.go index b7d531518..5ab6350a9 100644 --- a/lib/debezium/decimal.go +++ b/lib/debezium/decimal.go @@ -100,7 +100,7 @@ func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { return encodeBigInt(bigIntValue), -decimal.Exponent } -// EncodeDecimalWithScale is used to encode a [apd.Decimal] to [org.apache.kafka.connect.data.Decimal] +// EncodeDecimalWithScale is used to encode a [apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. // using a specific scale. func EncodeDecimalWithScale(decimal *apd.Decimal, scale int32) []byte { targetExponent := -scale // Negate scale since [Decimal.Exponent] is negative. From c0f035af601529d7672531a5058a2bab35aa04f8 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:27:01 -0700 Subject: [PATCH 7/7] Update comments --- lib/debezium/decimal.go | 6 +++--- lib/numbers/numbers.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/debezium/decimal.go b/lib/debezium/decimal.go index 5ab6350a9..2e745e62d 100644 --- a/lib/debezium/decimal.go +++ b/lib/debezium/decimal.go @@ -62,7 +62,7 @@ func decodeBigInt(data []byte) *big.Int { return bigInt } -// decimalWithNewExponent takes a [apd.Decimal] and returns a new [apd.Decimal] with a the given exponent. +// decimalWithNewExponent takes a [*apd.Decimal] and returns a new [*apd.Decimal] with a the given exponent. // If the new exponent is less precise then the extra digits will be truncated. func decimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decimal { exponentDelta := newExponent - decimal.Exponent // Exponent is negative. @@ -89,7 +89,7 @@ func decimalWithNewExponent(decimal *apd.Decimal, newExponent int32) *apd.Decima } } -// EncodeDecimal is used to encode a [apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. +// EncodeDecimal is used to encode a [*apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. // The scale of the value (which is the negated exponent of the decimal) is returned as the second argument. func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { bigIntValue := decimal.Coeff.MathBigInt() @@ -100,7 +100,7 @@ func EncodeDecimal(decimal *apd.Decimal) ([]byte, int32) { return encodeBigInt(bigIntValue), -decimal.Exponent } -// EncodeDecimalWithScale is used to encode a [apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. +// EncodeDecimalWithScale is used to encode a [*apd.Decimal] to [org.apache.kafka.connect.data.Decimal]. // using a specific scale. func EncodeDecimalWithScale(decimal *apd.Decimal, scale int32) []byte { targetExponent := -scale // Negate scale since [Decimal.Exponent] is negative. diff --git a/lib/numbers/numbers.go b/lib/numbers/numbers.go index e1386bb3f..d4ea8f4fd 100644 --- a/lib/numbers/numbers.go +++ b/lib/numbers/numbers.go @@ -7,7 +7,7 @@ func BetweenEq[T int | int32 | int64](start, end, number T) bool { return number >= start && number <= end } -// MustParseDecimal parses a string to a [apd.Decimal] or panics -- used for tests. +// MustParseDecimal parses a string to a [*apd.Decimal] or panics -- used for tests. func MustParseDecimal(value string) *apd.Decimal { decimal, _, err := apd.NewFromString(value) if err != nil {