-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[debezium] Return apd.Decimal
from DecodeDecimal
#765
Changes from 20 commits
e133873
64f6bca
97d4da3
f6bbb16
b9baad6
19a13f6
14e5e04
77f2451
1d5d48f
9162e2f
c1bd606
55c4ace
db1a91b
47489e7
e02f40f
aee754d
f6f1fee
48d8648
a487eba
e9413f4
7b9ef83
8ba46d9
fab0b1b
4b3327c
8ab7bf0
8ca4bcc
b41c61e
f23b0a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"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 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,24 +50,26 @@ | |
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)) | ||
actual := DecodeDecimal(bytes, nil, int(scale)).String() | ||
assert.Equal(t, value, actual, value) | ||
bytes, scale := EncodeDecimal(numbers.MustParseDecimal(value)) | ||
assert.Equal(t, expectedScale, scale, value) | ||
|
||
actual := DecodeDecimal(bytes, scale) | ||
assert.Equal(t, value, actual.Text('f'), value) | ||
assert.Equal(t, expectedScale, -actual.Exponent, value) | ||
} | ||
|
||
testEncodeDecimal("0", 0) | ||
|
@@ -91,8 +86,8 @@ | |
|
||
func TestEncodeDecimalWithScale(t *testing.T) { | ||
mustEncodeAndDecodeDecimal := func(value string, scale int32) string { | ||
bytes := EncodeDecimalWithScale(mustParseDecimal(value), scale) | ||
return DecodeDecimal(bytes, nil, int(scale)).String() | ||
return DecodeDecimal(bytes, scale).String() | ||
} | ||
|
||
// Whole numbers: | ||
|
@@ -132,6 +127,7 @@ | |
assert.Equal(t, "-145.1830000000000090", mustEncodeAndDecodeDecimal("-145.183000000000009", 16)) | ||
|
||
assert.Equal(t, "-9063701308.217222135", mustEncodeAndDecodeDecimal("-9063701308.217222135", 9)) | ||
assert.Equal(t, "-74961544796695.89960242", mustEncodeAndDecodeDecimal("-74961544796695.89960242", 8)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test fails on master. |
||
|
||
testCases := []struct { | ||
name string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ func BenchmarkDecodeDecimal_P64_S10(b *testing.B) { | |
assert.NoError(b, err) | ||
dec, err := field.DecodeDecimal(bytes) | ||
assert.NoError(b, err) | ||
assert.Equal(b, "123456789012345678901234567890123456789012345678901234.1234567889", dec.Value()) | ||
assert.Equal(b, "123456789012345678901234567890123456789012345678901234.1234567890", dec.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was decoding incorrectly, the unscaled value is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which makes sense since this number is just |
||
require.NoError(b, err) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now rely on the exponent of the decimal instead of passing in a scale, so the decimal places here were trimmed to 5 to match the previous scale.